summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/OutputProcessor.java
blob: 401a782637f5037f459ab11ac7149d576a03deef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*******************************************************************************
 * Copyright (c) 2013 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Ioana Grigoropol(Intel) - initial API and implementation
 *******************************************************************************/
package org.yocto.remote.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.locks.Lock;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.internal.services.local.shells.LocalHostShell;
import org.eclipse.rse.internal.services.shells.TerminalServiceHostShell;
import org.eclipse.rse.services.shells.HostShellProcessAdapter;
import org.eclipse.rse.services.shells.IHostShell;

public abstract class OutputProcessor{
	private static final int ERROR_BUFFER = 1;
	private static final int OUTPUT_BUFFER = 2;
	protected String task;
	protected ProcessStreamBuffer processBuffer;
	protected IHostShell hostShell;
	protected CommandResponseHandler cmdHandler;
	protected IProgressMonitor monitor;

	public OutputProcessor(IProgressMonitor monitor, IHostShell hostShell, CommandResponseHandler cmdHandler, String task){
		this.monitor = monitor;
		this.hostShell = hostShell;
		this.processBuffer = new ProcessStreamBuffer(hostShell instanceof TerminalServiceHostShell);
		this.cmdHandler = cmdHandler;
		this.task = task;
	}
	public ProcessStreamBuffer processOutput() throws Exception{
		if (hostShell == null)
			throw new Exception("An error has occured while trying to run remote command!");
		monitor.beginTask(this.task, RemoteHelper.TOTALWORKLOAD);
		Lock lock = null;
		if (hostShell instanceof LocalHostShell) {
			lock = ((LocalHostShell)hostShell).getLock();
			lock.lock();
		}
		BufferedReader inbr = null;
		BufferedReader errbr = null;

		if (hostShell instanceof LocalHostShell) {
			inbr = ((LocalHostShell)hostShell).getReader(false);
			errbr = ((LocalHostShell)hostShell).getReader(true);
		} else {
			Process p = new HostShellProcessAdapter(hostShell);
			inbr = new BufferedReader(new InputStreamReader(p.getInputStream()));
			errbr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		}
		boolean cancel = false;
		while (!cancel) {
			if(monitor.isCanceled()) {
				cancel = true;
				if (lock != null)
					lock.unlock();
				throw new InterruptedException("User Cancelled");
			}
			processBuffer(errbr, ERROR_BUFFER);
			processBuffer(inbr, OUTPUT_BUFFER);
			cancel = true;
		}
		if (lock != null)
			lock.unlock();
		return processBuffer;
	}
	protected abstract boolean isErrChStop(char ch);
	protected abstract boolean isOutChStop(char ch);
	protected boolean isChStop(char ch, int type){
		if (type == ERROR_BUFFER)
			return isErrChStop(ch);
		else if(type == OUTPUT_BUFFER)
			return isOutChStop(ch);
		return false;
	}
	protected abstract void processOutputBufferLine(char ch, String str);
	protected abstract void processErrorBufferLine(char ch, String str);
	protected void processBufferLine(String str, char ch, int type){
		if (type == ERROR_BUFFER)
			processErrorBufferLine(ch, str);
		else if(type == OUTPUT_BUFFER)
			processOutputBufferLine(ch, str);
	}
	protected void processBuffer(BufferedReader br, int type) throws IOException{
		StringBuffer buffer = new StringBuffer();
		int c;
		if (br != null)
		while ((c = br.read()) != -1) {
			char ch = (char) c;
			buffer.append(ch);
			if (isChStop(ch, type)){
				String str = buffer.toString();
				processBufferLine(str, ch, type);
				System.out.println(str);
				if (str.trim().equals(RemoteHelper.TERMINATOR)) {
					break;
				}
				cmdHandler.response(str, false);
				buffer.delete(0, buffer.length());
			}
		}
	}
}