-
Notifications
You must be signed in to change notification settings - Fork 10
/
SessionRequestHandler.java
165 lines (155 loc) · 4.58 KB
/
SessionRequestHandler.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*******************************************************************************
* Copyright (c) 2009, 2019 Xored Software Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Xored Software Inc - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.rcptt.ecl.server.tcp;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.internal.core.CorePlugin;
import org.eclipse.rcptt.ecl.runtime.CoreUtils;
import org.eclipse.rcptt.ecl.runtime.EclRuntime;
import org.eclipse.rcptt.ecl.runtime.IPipe;
import org.eclipse.rcptt.ecl.runtime.IProcess;
import org.eclipse.rcptt.ecl.runtime.ISession;
import org.osgi.framework.FrameworkUtil;
final class SessionRequestHandler implements Runnable {
private final Socket socket;
private final ISession session;
private final BufferedInputStream inputStream;
private final int defaultTimeout;
private final static ILog LOG = Platform.getLog(FrameworkUtil.getBundle(SessionRequestHandler.class));
SessionRequestHandler(Socket socket, boolean useJobs) throws IOException {
// super("ECL tcp session:" + socket.getPort());
this.socket = socket;
try {
this.socket.setTcpNoDelay(true);
} catch (SocketException e) {
CorePlugin.log(e);
}
this.session = EclRuntime.createSession(useJobs);
this.inputStream = new BufferedInputStream(socket.getInputStream());
this.defaultTimeout = socket.getSoTimeout();
}
public void run() {
try {
IPipe pipe = CoreUtils.createEMFPipe(inputStream,
socket.getOutputStream());
while (!Thread.currentThread().isInterrupted()
&& !socket.isClosed()) {
try {
pipe.reinit();
Object object = pipe.take(Long.MAX_VALUE);
if (!(object instanceof Command))
break;
Command command = (Command) object;
IStatus status;
try {
IPipe input = readInput(pipe);
IProcess process = session.execute(command, input, null);
status = writeOutput(pipe, process);
} catch (CoreException e) {
status = e.getStatus();
}
try {
pipe.write(status);
} catch (ClassCastException e) { // Serialization errors
CorePlugin.log(e);
status = CorePlugin.err(e);
pipe.write(status);
}
pipe.close(status);
} catch (Exception e) {
Throwable te = e;
if (e instanceof CoreException) {
if (e.getCause() instanceof SocketException) {
te = e.getCause();
}
}
if (te instanceof SocketException) {
try {
socket.close();
} catch (IOException e2) {
}
break;
}
CorePlugin.log(e);
}
}
} catch (Exception e) {
CorePlugin.log(e);
} finally {
try {
socket.close();
} catch (Exception e) {
CorePlugin.log(e);
}
try {
session.close();
} catch (Exception e) {
CorePlugin.log(e);
}
}
}
private IPipe readInput(IPipe pipe) throws CoreException {
Object object;
IPipe input = session.createPipe();
while (true) {
object = pipe.take(Long.MAX_VALUE);
if (object instanceof IStatus) {
input.close((IStatus) object);
break;
}
input.write(object);
}
return input;
}
private IStatus writeOutput(IPipe pipe, IProcess process)
throws CoreException, IOException {
Object object;
do {
object = process.getOutput().take(100);
if (object == null) {
if (!isConnected()) {
throw new CoreException(Status.CANCEL_STATUS);
}
} else if (object instanceof IStatus) {
try {
return process.waitFor();
} catch (InterruptedException e) {
return Status.CANCEL_STATUS;
}
} else {
pipe.write(object);
}
} while (true);
}
private boolean isConnected() throws IOException {
inputStream.mark(10);
socket.setSoTimeout(1);
try {
return inputStream.read() >= 0 && inputStream.read() >= 0;
} catch (SocketTimeoutException e) {
return true;
} finally {
socket.setSoTimeout(defaultTimeout);
inputStream.reset();
}
}
public void recover(Socket client) {
}
}