summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/src/org/yocto/remote/utils/RemoteShellExec.java
blob: 711088c8d34e434b61d0c97ee32ff818390d30fd (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.remote.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.rse.core.model.IHost;

public class RemoteShellExec {

	public static final int
    STATE_NULL = 0,
    STATE_RUNNING = 1,
    STATE_EXITED = 2;

	private final String command;
	private final IHost host;

	private InputStream fInStream;
    private OutputStream fOutStream;
    private InputStream fErrStream;
    private Process remoteShellProcess;

	private int exitCode = 0;
	private int status = STATE_NULL;

	private final String RETURN_VALUE_TAG = "org.yocto.sdk.remotetools.RVTAG";
	private final String RETURN_VALUE_CMD = ";echo \"" + RETURN_VALUE_TAG + "$?\"";

	public RemoteShellExec(IHost host, String command) {
		assert(host != null);
		this.host = host;
		this.command = command;
	}

	public int getStatus() {
		return status;
	}

	public int getExitCode() {
		return exitCode;
	}

	private void reset() {
		fInStream = null;
		fOutStream = null;
		fErrStream = null;

		remoteShellProcess = null;
		exitCode = 0;
		status = STATE_NULL;
	}

	public InputStream getInputStream() {
        return fInStream;
    }

    public OutputStream getOutputStream() {
        return fOutStream;
    }

    public InputStream getErrStream() {
        return fErrStream;
    }

	public synchronized void start(String prelaunchCmd, String argument, IProgressMonitor monitor) throws Exception {
		if(status == STATE_RUNNING)
				return;

		reset();
		argument = (argument == null ? RETURN_VALUE_CMD : argument + RETURN_VALUE_CMD);
		remoteShellProcess = RemoteHelper.remoteShellExec(this.host, prelaunchCmd, this.command, argument, monitor);
		fInStream = remoteShellProcess.getInputStream();
		fOutStream = remoteShellProcess.getOutputStream();
		fErrStream = remoteShellProcess.getErrorStream();
		status = STATE_RUNNING;
	}

	 public synchronized void terminate() throws Exception {
		 if(status != STATE_RUNNING || remoteShellProcess != null)
			 return;

		 remoteShellProcess.destroy();
		 reset();
	 }

	 public int waitFor(IProgressMonitor monitor) throws InterruptedException {
		 while(status == STATE_RUNNING) {
			 if(monitor != null) {
	    			if(monitor.isCanceled()) {
	    				throw new InterruptedException("User Cancelled");
	    			}
 			 }

			 try {
				 remoteShellProcess.waitFor();
			 }catch(InterruptedException e){
				 //get the return value
				 try {
					 if(fInStream.available() != 0) {
						 BufferedReader in = new BufferedReader(new InputStreamReader(fInStream));
						 String thisline;
						 int idx;
						 while((thisline = in.readLine()) != null) {
							    if(thisline.indexOf(RETURN_VALUE_CMD) == -1) {
									idx = thisline.indexOf(RETURN_VALUE_TAG);
									if(idx != -1) {
										try {
											exitCode=(new Integer(thisline.substring(idx+RETURN_VALUE_TAG.length()))).intValue();
										}catch(NumberFormatException e2) {
										}
										break;
									}
							    }
						 }
					 }
				 }catch(IOException e1) {
					 //do nothing
				 }
			 }finally {
				 status=STATE_EXITED;
			 }
		 }
		 return exitCode;
	 }
}