diff options
Diffstat (limited to 'plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java')
-rw-r--r-- | plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java new file mode 100644 index 0000000..401a782 --- /dev/null +++ b/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java | |||
@@ -0,0 +1,112 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2013 Intel Corporation. | ||
3 | * All rights reserved. This program and the accompanying materials | ||
4 | * are made available under the terms of the Eclipse Public License v1.0 | ||
5 | * which accompanies this distribution, and is available at | ||
6 | * http://www.eclipse.org/legal/epl-v10.html | ||
7 | * | ||
8 | * Contributors: | ||
9 | * Ioana Grigoropol(Intel) - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | package org.yocto.remote.utils; | ||
12 | |||
13 | import java.io.BufferedReader; | ||
14 | import java.io.IOException; | ||
15 | import java.io.InputStreamReader; | ||
16 | import java.util.concurrent.locks.Lock; | ||
17 | |||
18 | import org.eclipse.core.runtime.IProgressMonitor; | ||
19 | import org.eclipse.rse.internal.services.local.shells.LocalHostShell; | ||
20 | import org.eclipse.rse.internal.services.shells.TerminalServiceHostShell; | ||
21 | import org.eclipse.rse.services.shells.HostShellProcessAdapter; | ||
22 | import org.eclipse.rse.services.shells.IHostShell; | ||
23 | |||
24 | public abstract class OutputProcessor{ | ||
25 | private static final int ERROR_BUFFER = 1; | ||
26 | private static final int OUTPUT_BUFFER = 2; | ||
27 | protected String task; | ||
28 | protected ProcessStreamBuffer processBuffer; | ||
29 | protected IHostShell hostShell; | ||
30 | protected CommandResponseHandler cmdHandler; | ||
31 | protected IProgressMonitor monitor; | ||
32 | |||
33 | public OutputProcessor(IProgressMonitor monitor, IHostShell hostShell, CommandResponseHandler cmdHandler, String task){ | ||
34 | this.monitor = monitor; | ||
35 | this.hostShell = hostShell; | ||
36 | this.processBuffer = new ProcessStreamBuffer(hostShell instanceof TerminalServiceHostShell); | ||
37 | this.cmdHandler = cmdHandler; | ||
38 | this.task = task; | ||
39 | } | ||
40 | public ProcessStreamBuffer processOutput() throws Exception{ | ||
41 | if (hostShell == null) | ||
42 | throw new Exception("An error has occured while trying to run remote command!"); | ||
43 | monitor.beginTask(this.task, RemoteHelper.TOTALWORKLOAD); | ||
44 | Lock lock = null; | ||
45 | if (hostShell instanceof LocalHostShell) { | ||
46 | lock = ((LocalHostShell)hostShell).getLock(); | ||
47 | lock.lock(); | ||
48 | } | ||
49 | BufferedReader inbr = null; | ||
50 | BufferedReader errbr = null; | ||
51 | |||
52 | if (hostShell instanceof LocalHostShell) { | ||
53 | inbr = ((LocalHostShell)hostShell).getReader(false); | ||
54 | errbr = ((LocalHostShell)hostShell).getReader(true); | ||
55 | } else { | ||
56 | Process p = new HostShellProcessAdapter(hostShell); | ||
57 | inbr = new BufferedReader(new InputStreamReader(p.getInputStream())); | ||
58 | errbr = new BufferedReader(new InputStreamReader(p.getErrorStream())); | ||
59 | } | ||
60 | boolean cancel = false; | ||
61 | while (!cancel) { | ||
62 | if(monitor.isCanceled()) { | ||
63 | cancel = true; | ||
64 | if (lock != null) | ||
65 | lock.unlock(); | ||
66 | throw new InterruptedException("User Cancelled"); | ||
67 | } | ||
68 | processBuffer(errbr, ERROR_BUFFER); | ||
69 | processBuffer(inbr, OUTPUT_BUFFER); | ||
70 | cancel = true; | ||
71 | } | ||
72 | if (lock != null) | ||
73 | lock.unlock(); | ||
74 | return processBuffer; | ||
75 | } | ||
76 | protected abstract boolean isErrChStop(char ch); | ||
77 | protected abstract boolean isOutChStop(char ch); | ||
78 | protected boolean isChStop(char ch, int type){ | ||
79 | if (type == ERROR_BUFFER) | ||
80 | return isErrChStop(ch); | ||
81 | else if(type == OUTPUT_BUFFER) | ||
82 | return isOutChStop(ch); | ||
83 | return false; | ||
84 | } | ||
85 | protected abstract void processOutputBufferLine(char ch, String str); | ||
86 | protected abstract void processErrorBufferLine(char ch, String str); | ||
87 | protected void processBufferLine(String str, char ch, int type){ | ||
88 | if (type == ERROR_BUFFER) | ||
89 | processErrorBufferLine(ch, str); | ||
90 | else if(type == OUTPUT_BUFFER) | ||
91 | processOutputBufferLine(ch, str); | ||
92 | } | ||
93 | protected void processBuffer(BufferedReader br, int type) throws IOException{ | ||
94 | StringBuffer buffer = new StringBuffer(); | ||
95 | int c; | ||
96 | if (br != null) | ||
97 | while ((c = br.read()) != -1) { | ||
98 | char ch = (char) c; | ||
99 | buffer.append(ch); | ||
100 | if (isChStop(ch, type)){ | ||
101 | String str = buffer.toString(); | ||
102 | processBufferLine(str, ch, type); | ||
103 | System.out.println(str); | ||
104 | if (str.trim().equals(RemoteHelper.TERMINATOR)) { | ||
105 | break; | ||
106 | } | ||
107 | cmdHandler.response(str, false); | ||
108 | buffer.delete(0, buffer.length()); | ||
109 | } | ||
110 | } | ||
111 | } | ||
112 | } \ No newline at end of file | ||