|
Lines 28-33
Link Here
|
| 28 |
import java.util.Hashtable; |
28 |
import java.util.Hashtable; |
| 29 |
|
29 |
|
| 30 |
import org.eclipse.tptp.platform.execution.client.core.*; |
30 |
import org.eclipse.tptp.platform.execution.client.core.*; |
|
|
31 |
import org.eclipse.tptp.platform.execution.client.core.internal.commands.AuthenticateCommand; |
| 32 |
import org.eclipse.tptp.platform.execution.security.User; |
| 31 |
import org.eclipse.tptp.platform.execution.util.*; |
33 |
import org.eclipse.tptp.platform.execution.util.*; |
| 32 |
import org.eclipse.tptp.platform.execution.util.internal.CommandFragment; |
34 |
import org.eclipse.tptp.platform.execution.util.internal.CommandFragment; |
| 33 |
import org.eclipse.tptp.platform.execution.util.internal.Constants; |
35 |
import org.eclipse.tptp.platform.execution.util.internal.Constants; |
|
Lines 37-46
Link Here
|
| 37 |
import org.eclipse.tptp.platform.execution.exceptions.*; |
39 |
import org.eclipse.tptp.platform.execution.exceptions.*; |
| 38 |
|
40 |
|
| 39 |
public class ConnectionImpl implements IConnection { |
41 |
public class ConnectionImpl implements IConnection { |
| 40 |
|
|
|
| 41 |
protected Socket _socket; |
42 |
protected Socket _socket; |
| 42 |
protected INode _node; |
43 |
protected INode _node; |
| 43 |
protected int _port; |
44 |
protected int _port; |
|
|
45 |
protected InetAddress inetAddress = null; |
| 44 |
|
46 |
|
| 45 |
private int _connectionId = 0; |
47 |
private int _connectionId = 0; |
| 46 |
|
48 |
|
|
Lines 51-254
Link Here
|
| 51 |
private boolean _isComplete = false; |
53 |
private boolean _isComplete = false; |
| 52 |
|
54 |
|
| 53 |
private final Vector _listeners = new Vector(); |
55 |
private final Vector _listeners = new Vector(); |
| 54 |
private final Object _connectionLock = new Object(); |
|
|
| 55 |
private final Object _loginLock = new Object(); |
| 56 |
|
56 |
|
|
|
57 |
private static final Object writeLock = new Object(); |
| 58 |
private static final Object writeDataLock = new Object(); |
| 57 |
private static final Object contextLock = new Object(); |
59 |
private static final Object contextLock = new Object(); |
|
|
60 |
private static final Object authLock = new Object(); |
| 58 |
private boolean _isInitialized = false; |
61 |
private boolean _isInitialized = false; |
| 59 |
private boolean _loginPending = false; |
62 |
private boolean isAuthenticated = false; |
| 60 |
private ReconnectRequestedException _reconnectRequestedException=null; |
|
|
| 61 |
private SecureConnectionRequiredException _secureConnectionRequiredException=null; |
| 62 |
private LoginFailedException _loginFailed=null; |
| 63 |
|
63 |
|
| 64 |
private int _soTimeout = Constants.TIMEOUT_PERIOD; |
64 |
private int _soTimeout = Constants.TIMEOUT_PERIOD; |
| 65 |
|
65 |
|
| 66 |
class DataConnection |
66 |
class DataConnection { // it is better to move all processing of data connections to AC or Node |
| 67 |
{ |
|
|
| 68 |
private Socket _dataSocket; |
67 |
private Socket _dataSocket; |
| 69 |
private ACTCPDataServer _dataServer; |
68 |
private ACTCPDataServer _dataServer; |
|
|
69 |
private int dataConnectionId; |
| 70 |
|
70 |
|
| 71 |
public void setDataSocket(Socket dataSocket){_dataSocket = dataSocket;} |
71 |
public void setDataSocket(Socket dataSocket) { _dataSocket = dataSocket; } |
| 72 |
public void setDataServer(ACTCPDataServer dataServer){_dataServer = dataServer;} |
72 |
public void setDataServer(ACTCPDataServer dataServer) { _dataServer = dataServer; } |
| 73 |
public Socket getDataSocket(){return _dataSocket;} |
73 |
public Socket getDataSocket() { return _dataSocket; } |
| 74 |
public ACTCPDataServer getDataServer(){return _dataServer;} |
74 |
public ACTCPDataServer getDataServer() { return _dataServer; } |
|
|
75 |
public int getDataConnectionId() { return dataConnectionId; } |
| 76 |
public void setDataconnectionId(int dataConnectionId) { this.dataConnectionId = dataConnectionId; } |
| 75 |
} |
77 |
} |
| 76 |
|
78 |
|
| 77 |
public ConnectionImpl() { |
79 |
public ConnectionImpl() { |
| 78 |
super(); |
|
|
| 79 |
} |
80 |
} |
| 80 |
|
81 |
|
| 81 |
public int getConnectionId() |
82 |
public int getConnectionId() { |
| 82 |
{ |
83 |
return _connectionId; |
| 83 |
return this._connectionId; |
|
|
| 84 |
} |
84 |
} |
| 85 |
|
85 |
|
| 86 |
public boolean isNewAC() |
86 |
public boolean isNewAC() { |
| 87 |
{ |
|
|
| 88 |
return true; |
87 |
return true; |
| 89 |
} |
88 |
} |
| 90 |
|
89 |
|
| 91 |
public long getNextContextId() |
90 |
public long getNextContextId() { |
| 92 |
{ |
|
|
| 93 |
synchronized(contextLock){ |
91 |
synchronized(contextLock){ |
| 94 |
return _context++; |
92 |
return _context++; |
| 95 |
} |
93 |
} |
| 96 |
|
|
|
| 97 |
} |
94 |
} |
| 98 |
|
95 |
|
| 99 |
public void addContext(long contextID, ICommandHandler handler) |
96 |
public void addContext(long contextID, ICommandHandler handler) { |
| 100 |
{ |
|
|
| 101 |
synchronized(contextLock){ |
97 |
synchronized(contextLock){ |
| 102 |
this._contextMapper.addContext(contextID, handler); |
98 |
this._contextMapper.addContext(contextID, handler); |
| 103 |
} |
99 |
} |
| 104 |
} |
100 |
} |
| 105 |
|
101 |
|
| 106 |
public void connect(INode node, ConnectionInfo connInfo) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException |
102 |
protected Socket connectSocket () throws IOException { |
| 107 |
{ |
103 |
if (_node == null) return null; |
| 108 |
_port=connInfo.getPort(); |
104 |
return connectSocket(_node.getAllInetAddresses(), _port); |
|
|
105 |
} |
| 106 |
|
| 107 |
protected Socket connectSocket (InetAddress addrs[], int port) throws IOException { |
| 108 |
if (addrs == null || addrs.length <= 0) return null; |
| 109 |
|
| 110 |
Socket socket = null; |
| 111 |
|
| 112 |
for (int i=0; i<addrs.length; i++) { |
| 113 |
if(Constants.TPTP_DEBUG) System.out.println("Connecting to " + addrs[i] + " at port " + _port); |
| 114 |
socket = new Socket(addrs[i], port); |
| 115 |
socket.setSoTimeout(_soTimeout); |
| 116 |
socket.setTcpNoDelay(true); |
| 117 |
inetAddress = addrs[i]; |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
return socket; |
| 122 |
} |
| 123 |
|
| 124 |
public void connect(INode node, ConnectionInfo connInfo) throws IOException, SecureConnectionRequiredException, |
| 125 |
LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException { |
| 126 |
|
| 109 |
_soTimeout = connInfo.getSoTimeout(); |
127 |
_soTimeout = connInfo.getSoTimeout(); |
| 110 |
int offset=0; |
128 |
if (_soTimeout > 0) _soTimeout *= Constants.CONNECT_TIMEOUT_TRY_COUNT; |
| 111 |
InetAddress[] addrs=node.getAllInetAddresses(); |
129 |
|
|
|
130 |
_node = node; |
| 131 |
_port = connInfo.getPort(); |
| 112 |
|
132 |
|
| 113 |
do { |
133 |
_socket = connectSocket(node.getAllInetAddresses(), _port); |
| 114 |
/* Connect to the remote machine */ |
134 |
if (_socket == null) throw new IOException(); |
| 115 |
try { |
|
|
| 116 |
if(Constants.TPTP_DEBUG)System.out.println("Connecting to " + addrs[offset] + " at port " + _port); |
| 117 |
_socket=new Socket(addrs[offset], _port); |
| 118 |
|
| 119 |
sendConnectCommand(); |
| 120 |
break; |
| 121 |
} |
| 122 |
catch(IOException e) { |
| 123 |
offset++; |
| 124 |
if (Constants.TPTP_DEBUG)System.out.println(e.getMessage()); |
| 125 |
if(offset==addrs.length) { |
| 126 |
if (Constants.TPTP_DEBUG)System.out.println("Error while connecting to the Agent Controller on " + addrs[offset] + " running at port " + _port); |
| 127 |
throw e; |
| 128 |
} |
| 129 |
} |
| 130 |
}while(offset<addrs.length); |
| 131 |
_node=node; |
| 132 |
|
135 |
|
| 133 |
this.init(); |
136 |
init(); |
| 134 |
|
137 |
|
| 135 |
// We expected to have a connectionID by now. If we don't, it's because the wait above timed out |
138 |
// We expected to have a connectionID by now. If we don't, it's because the wait above timed out |
| 136 |
if ( _connectionId == 0 ) { |
139 |
if (_connectionId == 0) { |
| 137 |
throw new LoginFailedException("Timeout while waiting for connection to complete"); |
140 |
throw new LoginFailedException("Timeout while waiting for connection to complete"); |
| 138 |
} |
141 |
} |
| 139 |
} |
142 |
} |
| 140 |
public void connect(INode node, int port) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException |
143 |
|
| 141 |
{ |
144 |
public void connect(INode node, int port) throws IOException, SecureConnectionRequiredException, LoginFailedException, UntrustedAgentControllerException, ReconnectRequestedException { |
| 142 |
ConnectionInfo connInfo = new ConnectionInfo(); |
145 |
ConnectionInfo connInfo = new ConnectionInfo(); |
| 143 |
connInfo.setPort(port); |
146 |
connInfo.setPort(port); |
| 144 |
connect( node, connInfo ); |
147 |
connect(node, connInfo); |
| 145 |
} |
|
|
| 146 |
|
| 147 |
protected void sendConnectCommand() throws IOException |
| 148 |
{ |
| 149 |
if(Constants.TPTP_DEBUG)System.out.println("The AC is New"); |
| 150 |
sendControlCommand(Constants.CONNECT); |
| 151 |
} |
148 |
} |
| 152 |
|
149 |
|
| 153 |
|
150 |
protected void init() throws SecureConnectionRequiredException, LoginFailedException, IOException, ReconnectRequestedException { |
| 154 |
/* Init */ |
151 |
_contextMapper = new ContextMapper(); |
| 155 |
protected void init() throws SecureConnectionRequiredException, LoginFailedException, IOException, ReconnectRequestedException |
|
|
| 156 |
{ |
| 157 |
try |
| 158 |
{ |
| 159 |
_socket.setSoTimeout(_soTimeout); |
| 160 |
_socket.setTcpNoDelay(true); |
| 161 |
} |
| 162 |
catch(SocketException e) { |
| 163 |
/* We can ignore this */ |
| 164 |
} |
| 165 |
|
| 166 |
_contextMapper=new ContextMapper(); |
| 167 |
_dataConnectionMap = new Hashtable(); |
152 |
_dataConnectionMap = new Hashtable(); |
| 168 |
_cmdHandler=new ICommandHandler() |
153 |
|
| 169 |
{ |
154 |
_cmdHandler = new ICommandHandler() { |
| 170 |
public void incomingCommand(INode node, ICommandElement command) |
155 |
public void incomingCommand(INode node, ICommandElement command) { |
| 171 |
{ |
|
|
| 172 |
handleACCommands(command); |
156 |
handleACCommands(command); |
| 173 |
} |
157 |
} |
| 174 |
}; |
158 |
}; |
| 175 |
|
159 |
|
| 176 |
/* RKD: With the V5 RAC it first accepts the socket connection and then determines is the connection |
160 |
sendControlCommand(Constants.CONNECT); |
| 177 |
* is allowed. If the connection is not allowed the the connection is cut. We need to first connect |
161 |
|
| 178 |
* and then determine if we have been cut off. The connection has already occurred above us in the |
162 |
byte rbuf[] = new byte[512]; |
| 179 |
* stack. We then will wait until either a read timeout has occured on the reader thread or the |
163 |
BufferedInputStream inStream = new BufferedInputStream(_socket.getInputStream()); |
| 180 |
* connection gets cut by the other side. |
164 |
int offset = 0; |
| 181 |
*/ |
165 |
|
| 182 |
|
166 |
while (true) { |
| 183 |
synchronized(_connectionLock) { |
167 |
int bytesRead = inStream.read(rbuf, offset, rbuf.length-offset); |
| 184 |
SocketReaderThread reader=new SocketReaderThread(); |
168 |
|
| 185 |
reader.setName(_node.getName()+"_connection"); |
169 |
if(bytesRead < 0) break; |
| 186 |
reader.setDaemon(true); |
170 |
|
| 187 |
reader.start(); |
171 |
if (offset > 0) { |
| 188 |
try |
172 |
bytesRead += offset; |
| 189 |
{ |
173 |
offset = 0; |
| 190 |
_connectionLock.wait(); |
174 |
} |
| 191 |
} |
|
|
| 192 |
catch(InterruptedException e){e.printStackTrace();} |
| 193 |
|
| 194 |
_isInitialized=true; |
| 195 |
|
| 196 |
if (_reconnectRequestedException!=null) { |
| 197 |
ReconnectRequestedException temp=_reconnectRequestedException; |
| 198 |
_reconnectRequestedException = null; |
| 199 |
throw temp; |
| 200 |
} |
| 201 |
|
175 |
|
| 202 |
/* If this connection has a pending exception because the server is secure we need to throw the |
176 |
// If we've tried to connect to a backward compatibility |
| 203 |
* exception so that the caller can create a new connection with the proper security settings. |
177 |
// layer of the AC, we'll immediately get a response in |
| 204 |
*/ |
178 |
// RAC format. If we ignore this, the BC layer will |
| 205 |
if(_secureConnectionRequiredException!=null) { |
179 |
// respond to our first message by giving us the port |
| 206 |
SecureConnectionRequiredException temp=_secureConnectionRequiredException; |
180 |
// to connect to the regular socket TL. |
| 207 |
_secureConnectionRequiredException=null; |
181 |
// |
| 208 |
_loginPending=false; |
182 |
int v = checkForRACMessage(rbuf, offset); |
| 209 |
throw temp; |
183 |
if(v >= 0) { |
|
|
184 |
// We read the header, the next four bytes will be the length |
| 185 |
// (including the header) of the RAC message |
| 186 |
int len = (int) TPTPMessageUtil.readTPTPLongFromBuffer(rbuf, v); |
| 187 |
if (len == bytesRead) { |
| 188 |
offset = 0; |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
if (len > bytesRead) { |
| 193 |
inStream.skip(len - bytesRead); |
| 194 |
offset = 0; |
| 195 |
continue; |
| 196 |
} |
| 197 |
|
| 198 |
offset = len; |
| 199 |
} |
| 200 |
|
| 201 |
// First we make sure we have at least enough read for the message header. |
| 202 |
// If not, compress and re-read. |
| 203 |
// |
| 204 |
if (bytesRead-offset < Constants.AC_MESSAGE_HEADER_SIZE) { |
| 205 |
System.arraycopy(rbuf, offset, rbuf, 0, bytesRead-offset); |
| 206 |
offset = bytesRead - offset; |
| 207 |
continue; |
| 210 |
} |
208 |
} |
|
|
209 |
|
| 210 |
// We have the header information, now lets try and get the entire message. Once |
| 211 |
// again the same error conditions can occur. |
| 212 |
// |
| 213 |
//System.out.println("Offset xx::"+new String(buffer)); |
| 211 |
|
214 |
|
| 212 |
//System.out.println("init - 7"); |
215 |
ControlMessage msg=new ControlMessage(); |
| 213 |
synchronized(_loginLock) { |
216 |
msg.setMessageType(Constants.TPTP_AC_MESSAGE); |
| 214 |
if(_loginPending) { |
217 |
int current; |
| 215 |
try { |
218 |
|
| 216 |
_loginLock.wait(); |
219 |
try { |
|
|
220 |
if (rbuf.length == bytesRead + offset) { |
| 221 |
current = msg.readFromBuffer(rbuf, offset); |
| 222 |
} |
| 223 |
else { |
| 224 |
// length is not passed to readFromBuffer so we should ensure |
| 225 |
// that readFromBuffer will not read trash from the end of the buffer |
| 226 |
// and no exception will be thrown. |
| 227 |
// ControlMessage.readFromBuffer(buffer, offset, length) method |
| 228 |
// is needed in order to prevent extra memory allocations in this case. |
| 229 |
byte[] buffer2 = new byte[bytesRead]; |
| 230 |
System.arraycopy(rbuf, offset, buffer2, 0, bytesRead); |
| 231 |
current = msg.readFromBuffer(buffer2, 0); |
| 232 |
} |
| 233 |
} catch (Exception e) { |
| 234 |
throw new IOException(); |
| 235 |
} |
| 236 |
|
| 237 |
long flags = msg.getFlags(); |
| 238 |
if ((flags & Constants.CONNECTION_COMPLETE) != 0) { |
| 239 |
isAuthenticated = (flags & Constants.AUTHENTICATION_FAILED) == 0; |
| 240 |
_connectionId = extractConnectionId(rbuf, current); |
| 241 |
break; |
| 242 |
} // equals - since CONNECTION_RECONNECT_REQUEST has two bits set |
| 243 |
else if ((flags & Constants.CONNECTION_REFUSED) != 0) { |
| 244 |
if ((flags & Constants.SECURITY_REQUIRED) != 0) { |
| 245 |
throw new SecureConnectionRequiredException(); |
| 246 |
} |
| 247 |
else if((flags & Constants.CONNECTION_RECONNECT_REQUEST) == Constants.CONNECTION_RECONNECT_REQUEST) { |
| 248 |
int portreq = -1; |
| 249 |
|
| 250 |
try { |
| 251 |
close(); |
| 252 |
CommandFragment cmd = (CommandFragment)msg.getCommand(0); |
| 253 |
String strToParse = cmd.getCommandData(); |
| 254 |
TPTPXMLParse ParseObj = new TPTPXMLParse(); |
| 255 |
ParseObj.setParser(strToParse.trim()); |
| 256 |
Hashtable CommandHash = ParseObj.getHashTable(); |
| 257 |
if(CommandHash.containsKey("port")) { |
| 258 |
portreq = Integer.parseInt((String)ParseObj.getHashTable().get("port")); |
| 259 |
} |
| 217 |
} |
260 |
} |
| 218 |
catch(InterruptedException e) { |
261 |
catch(Exception e){} |
| 219 |
/* We should ignore this */ |
262 |
|
|
|
263 |
if(portreq == -1) { |
| 264 |
// This is a bad situation. The AC should have given us a port |
| 265 |
// Since it didn't (unexpectedly), let's try a default |
| 266 |
portreq = 10006; |
| 220 |
} |
267 |
} |
|
|
268 |
|
| 269 |
// Tell the caller how to reconnect |
| 270 |
throw new ReconnectRequestedException(portreq); |
| 221 |
} |
271 |
} |
|
|
272 |
|
| 273 |
throw new IOException("Connection Refused"); |
| 222 |
} |
274 |
} |
| 223 |
|
275 |
|
| 224 |
if(_loginFailed!=null) { |
276 |
throw new IOException("Unknown command"); |
| 225 |
LoginFailedException temp=_loginFailed; |
|
|
| 226 |
_loginFailed=null; |
| 227 |
_loginPending=false; |
| 228 |
throw temp; |
| 229 |
} |
| 230 |
if(_isComplete) { |
| 231 |
throw new IOException(); |
| 232 |
} |
| 233 |
} |
277 |
} |
|
|
278 |
|
| 279 |
(new SocketReaderThread()).start(); |
| 234 |
} |
280 |
} |
| 235 |
|
281 |
|
| 236 |
private void handleACCommands(ICommandElement command) |
282 |
private void handleACCommands(ICommandElement command) { |
| 237 |
{ |
|
|
| 238 |
long contextId=command.getContext(); |
283 |
long contextId=command.getContext(); |
| 239 |
if(Constants.TPTP_DEBUG)System.out.println("The context of the returned command:" + contextId); |
284 |
if(Constants.TPTP_DEBUG)System.out.println("The context of the returned command:" + contextId); |
| 240 |
|
285 |
|
| 241 |
//Find the command handler associated with this contextId and |
286 |
//Find the command handler associated with this contextId and |
| 242 |
// forward the message appropriately. |
287 |
// forward the message appropriately. |
| 243 |
ICommandHandler ch=_contextMapper.getHandler(contextId); |
288 |
ICommandHandler ch=_contextMapper.getHandler(contextId); |
| 244 |
if(ch != null) |
289 |
if(ch != null) { |
| 245 |
{ |
|
|
| 246 |
if(Constants.TPTP_DEBUG)System.out.println("Forwarding to command handler"); |
290 |
if(Constants.TPTP_DEBUG)System.out.println("Forwarding to command handler"); |
| 247 |
ch.incomingCommand(_node, command); |
291 |
ch.incomingCommand(_node, command); |
| 248 |
} |
292 |
} |
| 249 |
else { |
|
|
| 250 |
if(Constants.TPTP_DEBUG)System.out.println("Could not find command handler"); |
| 251 |
} |
| 252 |
} |
293 |
} |
| 253 |
|
294 |
|
| 254 |
private int extractConnectionId(byte[] buffer, int offset) |
295 |
private int extractConnectionId(byte[] buffer, int offset) |
|
Lines 273-280
Link Here
|
| 273 |
* nonzero if error |
314 |
* nonzero if error |
| 274 |
* |
315 |
* |
| 275 |
*********************************************************/ |
316 |
*********************************************************/ |
| 276 |
private void sendCONNECT_DATACommand(Socket datasock, int direction) throws IOException |
317 |
private void sendCONNECT_DATACommand(Socket datasock, int direction) throws IOException { |
| 277 |
{ |
|
|
| 278 |
long flags = 0 ; |
318 |
long flags = 0 ; |
| 279 |
|
319 |
|
| 280 |
/* build the CONNECT command string */ |
320 |
/* build the CONNECT command string */ |
|
Lines 291-297
Link Here
|
| 291 |
long dataPathType = direction; |
331 |
long dataPathType = direction; |
| 292 |
offset = TPTPMessageUtil.writeTPTPLongToBuffer(buffer, offset, dataPathType); |
332 |
offset = TPTPMessageUtil.writeTPTPLongToBuffer(buffer, offset, dataPathType); |
| 293 |
|
333 |
|
| 294 |
OutputStream stream=datasock.getOutputStream(); |
334 |
OutputStream stream = datasock.getOutputStream(); |
| 295 |
stream.write(buffer); |
335 |
stream.write(buffer); |
| 296 |
stream.flush(); |
336 |
stream.flush(); |
| 297 |
} |
337 |
} |
|
Lines 308-315
Link Here
|
| 308 |
* nonzero if error |
348 |
* nonzero if error |
| 309 |
* |
349 |
* |
| 310 |
*********************************************************/ |
350 |
*********************************************************/ |
| 311 |
private void sendControlCommand(long commandFlags) throws IOException |
351 |
private void sendControlCommand(long commandFlags) throws IOException { |
| 312 |
{ |
|
|
| 313 |
long flags = 0 ; |
352 |
long flags = 0 ; |
| 314 |
|
353 |
|
| 315 |
/* build the CONNECT command string */ |
354 |
/* build the CONNECT command string */ |
|
Lines 324-427
Link Here
|
| 324 |
sendMessage(connectMessage, _cmdHandler); |
363 |
sendMessage(connectMessage, _cmdHandler); |
| 325 |
} |
364 |
} |
| 326 |
|
365 |
|
|
|
366 |
int createDataConnection(int direction) throws IOException, SecureConnectionRequiredException { |
| 367 |
Socket datasock = connectSocket(); |
| 368 |
if (datasock == null) throw new IOException(); |
| 327 |
|
369 |
|
| 328 |
int createDataConnection(int direction) throws IOException |
370 |
return initDataConnection(datasock, direction); |
| 329 |
{ |
371 |
} |
| 330 |
Socket datasock = null; |
372 |
|
| 331 |
int dataConnectionId = -1; |
373 |
protected int initDataConnection(Socket datasock, int direction) throws IOException, SecureConnectionRequiredException { |
| 332 |
byte[] buffer=new byte[Constants.MAX_MESSAGE_LENGTH]; |
374 |
if (datasock == null || !datasock.isConnected()) return -1; |
| 333 |
int masterOffset=0; |
|
|
| 334 |
|
375 |
|
| 335 |
int offset=0; |
376 |
// CONNECT_DATA command |
| 336 |
InetAddress[] addrs=_node.getAllInetAddresses(); |
|
|
| 337 |
|
| 338 |
do { |
| 339 |
/* Connect to the remote machine */ |
| 340 |
try { |
| 341 |
datasock=new Socket(addrs[offset], _port); |
| 342 |
datasock.setTcpNoDelay(true); |
| 343 |
datasock.setSoTimeout(_soTimeout); |
| 344 |
break; |
| 345 |
} |
| 346 |
catch(IOException exp) |
| 347 |
{ |
| 348 |
offset++; |
| 349 |
//System.out.println(e.getMessage()); |
| 350 |
if(offset==addrs.length) { |
| 351 |
throw exp; |
| 352 |
} |
| 353 |
} |
| 354 |
}while(offset<addrs.length); |
| 355 |
|
| 356 |
/* CONNECT_DATA command */ |
| 357 |
sendCONNECT_DATACommand(datasock, direction); |
377 |
sendCONNECT_DATACommand(datasock, direction); |
| 358 |
|
378 |
|
| 359 |
/* Get the InputStream for this socket so we can read some data */ |
379 |
// Get the InputStream for this socket so we can read some data |
| 360 |
InputStream inStream=datasock.getInputStream(); |
380 |
byte[] buffer = new byte[256]; |
| 361 |
int bytesRead=inStream.read(buffer, masterOffset, buffer.length-masterOffset); |
381 |
InputStream inStream = datasock.getInputStream(); |
|
|
382 |
int d = inStream.read(buffer, 0, buffer.length); |
| 362 |
|
383 |
|
| 363 |
ControlMessage DataConnectionResponseMessage = new ControlMessage(); |
384 |
ControlMessage DataConnectionResponseMessage = new ControlMessage(); |
| 364 |
DataConnectionResponseMessage.setMessageType(Constants.TPTP_AC_MESSAGE); |
385 |
DataConnectionResponseMessage.setMessageType(Constants.TPTP_AC_MESSAGE); |
| 365 |
masterOffset = DataConnectionResponseMessage.readFromBuffer(buffer, masterOffset); |
386 |
int masterOffset = DataConnectionResponseMessage.readFromBuffer(buffer, 0); |
| 366 |
long flags = DataConnectionResponseMessage.getFlags(); |
387 |
long flags = DataConnectionResponseMessage.getFlags(); |
| 367 |
if ((flags & Constants.DATA_CONNECTION_COMPLETE) != 0) |
388 |
if ((flags & Constants.CONNECTION_REFUSED) != 0) { |
| 368 |
{ |
389 |
if ((flags & Constants.SECURITY_REQUIRED) != 0) { |
| 369 |
dataConnectionId = extractConnectionId(buffer, masterOffset); |
390 |
throw new SecureConnectionRequiredException(); |
| 370 |
} |
391 |
} |
|
|
392 |
|
| 393 |
throw new IOException("Connection Refused"); |
| 394 |
} |
| 395 |
|
| 396 |
if ((flags & Constants.DATA_CONNECTION_COMPLETE) <= 0) return -1; |
| 397 |
|
| 398 |
int dataConnectionId = extractConnectionId(buffer, masterOffset); |
| 399 |
if (dataConnectionId <= 0) return -1; |
| 371 |
|
400 |
|
| 372 |
DataConnection dataConnectionInfo = new DataConnection(); |
401 |
DataConnection dataConnectionInfo = new DataConnection(); |
| 373 |
ACTCPDataServer dataServer = new ACTCPDataServer(); |
402 |
ACTCPDataServer dataServer = new ACTCPDataServer(); |
| 374 |
if (dataConnectionId > 0) |
|
|
| 375 |
{ |
| 376 |
//dataConnectionId = ::getConnectionId(pMsg) ; |
403 |
//dataConnectionId = ::getConnectionId(pMsg) ; |
| 377 |
if(Constants.TPTP_DEBUG)System.out.println("The Data Channel ConnectionID: " + dataConnectionId); |
404 |
if(Constants.TPTP_DEBUG)System.out.println("The Data Channel ConnectionID: " + dataConnectionId); |
| 378 |
dataConnectionInfo.setDataSocket(datasock); |
405 |
dataConnectionInfo.setDataSocket(datasock); |
| 379 |
dataConnectionInfo.setDataServer(dataServer); |
406 |
dataConnectionInfo.setDataServer(dataServer); |
| 380 |
_dataConnectionMap.put(new Integer(dataConnectionId), dataConnectionInfo); |
407 |
dataConnectionInfo.setDataconnectionId(dataConnectionId); |
| 381 |
|
408 |
_dataConnectionMap.put(new Integer(dataConnectionId), dataConnectionInfo); |
| 382 |
try |
409 |
|
| 383 |
{ |
410 |
try { |
| 384 |
dataServer.startServer(null, datasock); |
411 |
dataServer.startServer(null, datasock); |
| 385 |
} |
412 |
} |
| 386 |
catch(SocketException sockExp) |
413 |
catch(SocketException sockExp) { |
| 387 |
{ |
414 |
} |
| 388 |
System.out.println("Error starting the TCPDataServer"); |
415 |
catch(IOException Exp) { |
| 389 |
} |
|
|
| 390 |
catch(IOException Exp) |
| 391 |
{ |
| 392 |
System.out.println("Error starting the TCPDataServer"); |
| 393 |
} |
| 394 |
} |
416 |
} |
| 395 |
|
417 |
|
| 396 |
return dataConnectionId; |
418 |
return dataConnectionId; |
| 397 |
} |
419 |
} |
| 398 |
|
420 |
|
| 399 |
|
421 |
public int addDataListener(int dataConnectionId, IDataProcessor dataProcessor) { |
| 400 |
public int addDataListener(int dataConnectionId, IDataProcessor dataProcessor) |
422 |
if(dataConnectionId == -1) return -1; |
| 401 |
{ |
423 |
|
| 402 |
if(dataConnectionId == -1){ return -1;} |
|
|
| 403 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
424 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
| 404 |
ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); |
425 |
ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); |
| 405 |
if(dataServer != null) |
426 |
if(dataServer != null) { |
| 406 |
{ |
|
|
| 407 |
dataServer.addDataprocessor(dataProcessor); |
427 |
dataServer.addDataprocessor(dataProcessor); |
| 408 |
return 0; |
428 |
return 0; |
| 409 |
} |
429 |
} |
|
|
430 |
|
| 410 |
return -1; |
431 |
return -1; |
| 411 |
} |
432 |
} |
| 412 |
|
433 |
|
| 413 |
public int removeDataListener(int dataConnectionId, IDataProcessor dataProcessor) |
434 |
public int removeDataListener(int dataConnectionId, IDataProcessor dataProcessor) { |
| 414 |
{ |
435 |
try { |
| 415 |
try |
|
|
| 416 |
{ |
| 417 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
436 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
| 418 |
ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); |
437 |
ACTCPDataServer dataServer = dataConnectionInfo.getDataServer(); |
| 419 |
dataServer.removeDataprocessor(dataProcessor); |
438 |
dataServer.removeDataprocessor(dataProcessor); |
| 420 |
dataServer.shutdownServer(); |
439 |
dataServer.shutdownServer(); |
| 421 |
}catch(Exception e) |
440 |
} catch(Exception e) {} |
| 422 |
{ |
441 |
|
| 423 |
///e.printStackTrace(); |
|
|
| 424 |
} |
| 425 |
return 0; |
442 |
return 0; |
| 426 |
} |
443 |
} |
| 427 |
|
444 |
|
|
Lines 435-448
Link Here
|
| 435 |
_dataConnectionMap.remove(new Integer(dataConnID)); |
452 |
_dataConnectionMap.remove(new Integer(dataConnID)); |
| 436 |
} |
453 |
} |
| 437 |
|
454 |
|
| 438 |
private void closeDataConnection(int dataConnID) |
455 |
private void closeDataConnection(int dataConnID) { |
| 439 |
{ |
|
|
| 440 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnID)); |
456 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnID)); |
| 441 |
|
457 |
|
| 442 |
try |
458 |
try { |
| 443 |
{ |
459 |
if (dataConnectionInfo != null) { |
| 444 |
if (dataConnectionInfo != null) |
|
|
| 445 |
{ |
| 446 |
// Send DISCONNECT command for data channel |
460 |
// Send DISCONNECT command for data channel |
| 447 |
sendControlCommand(Constants.DISCONNECT); |
461 |
sendControlCommand(Constants.DISCONNECT); |
| 448 |
|
462 |
|
|
Lines 452-522
Link Here
|
| 452 |
dataConnectionInfo.getDataSocket().close(); |
466 |
dataConnectionInfo.getDataSocket().close(); |
| 453 |
} |
467 |
} |
| 454 |
} |
468 |
} |
| 455 |
catch(Exception exp) |
469 |
catch(Exception exp) { |
| 456 |
{ |
|
|
| 457 |
if (Constants.TPTP_DEBUG) System.out.println("Error destroying the data channel - " + exp.getMessage()); |
470 |
if (Constants.TPTP_DEBUG) System.out.println("Error destroying the data channel - " + exp.getMessage()); |
| 458 |
} |
471 |
} |
| 459 |
} |
472 |
} |
| 460 |
|
473 |
|
| 461 |
public void sendData(int dataConnectionId, byte[] buffer, int bufferLength) |
474 |
public void sendData(int dataConnectionId, byte[] buffer, int bufferLength) { |
| 462 |
{ |
475 |
try { |
| 463 |
try |
|
|
| 464 |
{ |
| 465 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
476 |
DataConnection dataConnectionInfo = (DataConnection)_dataConnectionMap.get(new Integer(dataConnectionId)); |
| 466 |
OutputStream stream=dataConnectionInfo._dataSocket.getOutputStream(); |
477 |
|
| 467 |
stream.write(buffer); |
478 |
synchronized (writeDataLock) { |
| 468 |
stream.flush(); |
479 |
OutputStream stream=dataConnectionInfo._dataSocket.getOutputStream(); |
| 469 |
} |
480 |
stream.write(buffer); |
| 470 |
catch(IOException exp) |
481 |
stream.flush(); |
| 471 |
{ |
482 |
} |
| 472 |
System.out.println("Error writing the data to the Socket - " + exp); |
483 |
} |
| 473 |
} |
484 |
catch(IOException exp) { |
| 474 |
catch(Exception e) |
485 |
} |
| 475 |
{ |
486 |
catch(Exception e) { |
| 476 |
System.out.println("Error sending the data - " + e); |
|
|
| 477 |
} |
487 |
} |
| 478 |
} |
488 |
} |
| 479 |
|
489 |
|
| 480 |
public void sendMessage(IControlMessage msg, ICommandHandler handler) throws IOException |
490 |
public void sendMessage(IControlMessage msg, ICommandHandler handler) throws IOException { |
| 481 |
{ |
491 |
try { |
| 482 |
int commandCount = msg.getCommandCount(); |
492 |
if (handler != null) { |
| 483 |
|
493 |
int commandCount = msg.getCommandCount(); |
| 484 |
try |
494 |
for (int i=0; i < commandCount; i++) { |
| 485 |
{ |
495 |
_contextMapper.addContext(msg.getCommand(i).getContext(), handler); |
| 486 |
for (int i=0; i < commandCount; i++) { |
496 |
} |
| 487 |
//System.out.println("Adding the handler to the context ..."); |
|
|
| 488 |
//System.out.println("Context:" + msg.getCommand(i).getContext()); |
| 489 |
//System.out.println("Handler:" + handler); |
| 490 |
this._contextMapper.addContext(msg.getCommand(i).getContext(), handler); |
| 491 |
} |
497 |
} |
| 492 |
|
498 |
|
| 493 |
int count=msg.getSize(); |
499 |
byte[] buffer=new byte[msg.getSize()]; |
| 494 |
//System.out.println("message size " + count); |
|
|
| 495 |
byte[] buffer=new byte[count]; |
| 496 |
msg.writeToBuffer(buffer, 0); |
500 |
msg.writeToBuffer(buffer, 0); |
| 497 |
|
501 |
|
| 498 |
OutputStream stream=_socket.getOutputStream(); |
502 |
synchronized (writeLock) { |
| 499 |
stream.write(buffer); |
503 |
OutputStream stream=_socket.getOutputStream(); |
| 500 |
stream.flush(); |
504 |
stream.write(buffer); |
|
|
505 |
stream.flush(); |
| 506 |
} |
| 501 |
} |
507 |
} |
| 502 |
catch(IOException exp) |
508 |
catch(IOException exp) { |
| 503 |
{ |
|
|
| 504 |
if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); |
509 |
if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); |
| 505 |
throw exp; |
510 |
throw exp; |
| 506 |
} |
511 |
} |
| 507 |
catch(Exception exp) |
512 |
catch(Exception exp) { |
| 508 |
{ |
|
|
| 509 |
if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); |
513 |
if (Constants.TPTP_DEBUG)System.out.println("SendMessage error - " + exp); |
| 510 |
//throw exp; |
|
|
| 511 |
} |
514 |
} |
| 512 |
|
|
|
| 513 |
} |
515 |
} |
| 514 |
|
516 |
|
| 515 |
public void disconnect() { |
517 |
public void disconnect() { |
| 516 |
synchronized(_connectionLock) { |
|
|
| 517 |
if(!_isComplete) { |
518 |
if(!_isComplete) { |
| 518 |
_isComplete=true; |
519 |
_isComplete=true; |
| 519 |
_connectionLock.notifyAll(); |
520 |
|
| 520 |
try |
521 |
try |
| 521 |
{ |
522 |
{ |
| 522 |
// Destroy Data Connections |
523 |
// Destroy Data Connections |
|
Lines 533-543
Link Here
|
| 533 |
// Send DISCONNECT command |
534 |
// Send DISCONNECT command |
| 534 |
sendControlCommand(Constants.DISCONNECT); |
535 |
sendControlCommand(Constants.DISCONNECT); |
| 535 |
|
536 |
|
| 536 |
// Close the SocketReaderThread |
|
|
| 537 |
_isComplete = true; |
| 538 |
|
| 539 |
// Close the Socket |
| 540 |
_socket.close(); |
| 541 |
if (Constants.TPTP_DEBUG) System.out.println("Successfully disconnected."); |
537 |
if (Constants.TPTP_DEBUG) System.out.println("Successfully disconnected."); |
| 542 |
} |
538 |
} |
| 543 |
catch(IOException e) { |
539 |
catch(IOException e) { |
|
Lines 545-550
Link Here
|
| 545 |
if (Constants.TPTP_DEBUG) System.out.println("Exception while disconnecting ... " + e); |
541 |
if (Constants.TPTP_DEBUG) System.out.println("Exception while disconnecting ... " + e); |
| 546 |
} |
542 |
} |
| 547 |
|
543 |
|
|
|
544 |
close(); |
| 545 |
|
| 548 |
/* If this is a nodeimpl clear the keystore spec so it tries an insecure connect next time */ |
546 |
/* If this is a nodeimpl clear the keystore spec so it tries an insecure connect next time */ |
| 549 |
//Commented by GN since security not supported yet in the new |
547 |
//Commented by GN since security not supported yet in the new |
| 550 |
// if(_node instanceof INode) { |
548 |
// if(_node instanceof INode) { |
|
Lines 562-570
Link Here
|
| 562 |
} |
560 |
} |
| 563 |
*/ |
561 |
*/ |
| 564 |
} |
562 |
} |
| 565 |
} |
|
|
| 566 |
} |
563 |
} |
| 567 |
|
564 |
|
|
|
565 |
public void close () { |
| 566 |
if (_socket != null) { |
| 567 |
try { |
| 568 |
_socket.close(); |
| 569 |
} catch (Exception e) {} |
| 570 |
} |
| 571 |
|
| 572 |
_isComplete = true; |
| 573 |
} |
| 574 |
|
| 568 |
public INode getNode() { |
575 |
public INode getNode() { |
| 569 |
return _node; |
576 |
return _node; |
| 570 |
} |
577 |
} |
|
Lines 575-586
Link Here
|
| 575 |
} |
582 |
} |
| 576 |
return false; |
583 |
return false; |
| 577 |
} |
584 |
} |
| 578 |
public static int checkForRACMessage(byte[] buffer, int offset) |
585 |
|
| 579 |
{ |
586 |
public static int checkForRACMessage(byte[] buffer, int offset) { |
| 580 |
Message tempMessage=new Message(); |
587 |
Message tempMessage=new Message(); |
| 581 |
tempMessage.readFromBuffer(buffer, offset); |
588 |
tempMessage.readFromBuffer(buffer, offset); |
| 582 |
if(tempMessage.getMagicNumber() == Constants.RA_MAGIC) |
589 |
if(tempMessage.getMagicNumber() == Constants.RA_MAGIC) { |
| 583 |
{ |
|
|
| 584 |
return Constants.RAC_MESSAGE_HEADER_SIZE; |
590 |
return Constants.RAC_MESSAGE_HEADER_SIZE; |
| 585 |
} |
591 |
} |
| 586 |
return -1; |
592 |
return -1; |
|
Lines 589-705
Link Here
|
| 589 |
public int getPort() { |
595 |
public int getPort() { |
| 590 |
return _port; |
596 |
return _port; |
| 591 |
} |
597 |
} |
|
|
598 |
|
| 599 |
public InetAddress getInetAddress() { |
| 600 |
return inetAddress; |
| 601 |
} |
| 602 |
|
| 592 |
protected ContextMapper getContextMapper() |
603 |
protected ContextMapper getContextMapper() |
| 593 |
{ |
604 |
{ |
| 594 |
return this._contextMapper; |
605 |
return this._contextMapper; |
| 595 |
} |
606 |
} |
| 596 |
|
607 |
|
| 597 |
protected int processControlMessage(byte[] buffer, int offset, int length) { |
608 |
protected int processControlMessage(byte[] buffer, int offset, int length) { |
| 598 |
if(_cmdHandler != null) { |
609 |
if(_cmdHandler == null) return -1; |
| 599 |
ControlMessage msg=new ControlMessage(); |
610 |
|
| 600 |
msg.setMessageType(Constants.TPTP_AC_MESSAGE); |
611 |
ControlMessage msg=new ControlMessage(); |
|
|
612 |
msg.setMessageType(Constants.TPTP_AC_MESSAGE); |
| 601 |
|
613 |
|
| 602 |
int current=-1; |
614 |
int current=-1; |
| 603 |
try { |
615 |
try { |
| 604 |
if(Constants.TPTP_DEBUG)System.out.println("Processing the Message."); |
616 |
if(Constants.TPTP_DEBUG)System.out.println("Processing the Message."); |
| 605 |
if (buffer.length == length + offset) { |
617 |
if (buffer.length == length + offset) { |
| 606 |
current = msg.readFromBuffer(buffer, offset); |
618 |
current = msg.readFromBuffer(buffer, offset); |
| 607 |
} |
619 |
} |
| 608 |
else { |
620 |
else { |
| 609 |
// length is not passed to readFromBuffer so we should ensure |
621 |
// length is not passed to readFromBuffer so we should ensure |
| 610 |
// that readFromBuffer will not read trash from the end of the buffer |
622 |
// that readFromBuffer will not read trash from the end of the buffer |
| 611 |
// and no exception will be thrown. |
623 |
// and no exception will be thrown. |
| 612 |
// ControlMessage.readFromBuffer(buffer, offset, length) method |
624 |
// ControlMessage.readFromBuffer(buffer, offset, length) method |
| 613 |
// is needed in order to prevent extra memory allocations in this case. |
625 |
// is needed in order to prevent extra memory allocations in this case. |
| 614 |
byte[] buffer2 = new byte[length]; |
626 |
byte[] buffer2 = new byte[length]; |
| 615 |
System.arraycopy(buffer, offset, buffer2, 0, length); |
627 |
System.arraycopy(buffer, offset, buffer2, 0, length); |
| 616 |
current = msg.readFromBuffer(buffer2, 0); |
628 |
current = msg.readFromBuffer(buffer2, 0); |
| 617 |
if (current >= 0) |
629 |
if (current >= 0) |
| 618 |
current += offset; |
630 |
current += offset; |
| 619 |
} |
|
|
| 620 |
/* If we read more bytes then were valid, return -1 */ |
| 621 |
if(current>offset+length) |
| 622 |
{ |
| 623 |
return -1; |
| 624 |
} |
| 625 |
} |
631 |
} |
| 626 |
catch(IndexOutOfBoundsException e) |
632 |
/* If we read more bytes then were valid, return -1 */ |
| 627 |
{ |
633 |
if(current>offset+length) { |
| 628 |
return -1; |
634 |
return -1; |
| 629 |
} |
635 |
} |
| 630 |
catch(Exception e) |
636 |
} |
| 631 |
{ |
637 |
catch(IndexOutOfBoundsException e) { |
| 632 |
System.out.println("Exception while processing the message" + e); |
638 |
return -1; |
| 633 |
return -1; |
639 |
} |
| 634 |
} |
640 |
catch(Exception e) { |
|
|
641 |
System.out.println("Exception while processing the message" + e); |
| 642 |
return -1; |
| 643 |
} |
| 635 |
|
644 |
|
| 636 |
/* If we have parsed the message successfully Then process it */ |
645 |
/* If we have parsed the message successfully Then process it */ |
| 637 |
int len = msg.getSize(); |
646 |
int len = msg.getSize(); |
| 638 |
|
647 |
|
| 639 |
if (current == len+offset) { |
648 |
if (current == len+offset) { |
| 640 |
/* Valid pass on each command */ |
649 |
/* Valid pass on each command */ |
| 641 |
if(Constants.TPTP_DEBUG)System.out.println("Checking for CONNECTION_COMPLETE response"); |
650 |
// if(Constants.TPTP_DEBUG)System.out.println("Checking for CONNECTION_COMPLETE response"); |
| 642 |
long flags = msg.getFlags(); |
651 |
long flags = msg.getFlags(); |
| 643 |
if ((flags & Constants.CONNECTION_COMPLETE) != 0) |
652 |
|
| 644 |
{ |
653 |
if ((flags & Constants.AUTHENTICATION_FAILED) != 0) { |
| 645 |
this._connectionId = extractConnectionId(buffer, current); |
654 |
isAuthenticated = false; |
| 646 |
current += msg.getLength(); |
655 |
synchronized (authLock) { |
| 647 |
if(Constants.TPTP_DEBUG)System.out.println("Connection complete response - " + _connectionId); |
656 |
authLock.notifyAll(); |
| 648 |
synchronized (_connectionLock) { |
657 |
} |
| 649 |
_connectionLock.notifyAll(); |
658 |
} |
| 650 |
} |
659 |
else if ((flags & Constants.AUTHENTICATION_SUCCESSFUL) != 0) { |
| 651 |
} |
660 |
isAuthenticated = true; |
| 652 |
if((flags & Constants.CONNECTION_RECONNECT_REQUEST) != 0) |
661 |
synchronized (authLock) { |
| 653 |
{ |
662 |
authLock.notifyAll(); |
| 654 |
int portreq = -1; |
663 |
} |
| 655 |
synchronized(_socket) |
664 |
} |
| 656 |
{ |
665 |
else { |
| 657 |
try |
666 |
int count=msg.getCommandCount(); |
| 658 |
{ |
667 |
for(int i=0; i<count; i++) { |
| 659 |
_isComplete=true; |
668 |
_cmdHandler.incomingCommand(_node, msg.getCommand(i)); |
| 660 |
_dataConnectionMap.clear(); |
669 |
} |
| 661 |
_socket.close(); |
670 |
} |
| 662 |
CommandFragment cmd=(CommandFragment)msg.getCommand(0); |
671 |
} |
| 663 |
String strToParse = cmd.getCommandData();//new String(buffer,Constants.ACK_HDR_LEN,buffer.length-12); |
|
|
| 664 |
TPTPXMLParse ParseObj = new TPTPXMLParse(); |
| 665 |
ParseObj.setParser(strToParse.trim()); |
| 666 |
Hashtable CommandHash = ParseObj.getHashTable(); |
| 667 |
if(CommandHash.containsKey("port")) |
| 668 |
{ |
| 669 |
portreq = Integer.parseInt((String)ParseObj.getHashTable().get("port")); |
| 670 |
} |
| 671 |
} |
| 672 |
catch(Exception e){} |
| 673 |
} |
| 674 |
|
| 675 |
if(portreq == -1) { |
| 676 |
// This is a bad situation. The AC should have given us a port |
| 677 |
// Since it didn't (unexpectedly), let's try a default |
| 678 |
portreq = 10006; |
| 679 |
} |
| 680 |
|
| 681 |
// Tell the caller how to reconnect |
| 682 |
_reconnectRequestedException = new ReconnectRequestedException( portreq ); |
| 683 |
synchronized (_connectionLock) { |
| 684 |
// This notify will cause init to throw the above request |
| 685 |
// and cause the read thread to bail |
| 686 |
_connectionLock.notifyAll(); |
| 687 |
} |
| 688 |
} |
| 689 |
int count=msg.getCommandCount(); |
| 690 |
|
672 |
|
| 691 |
for(int i=0; i<count; i++) |
673 |
return current; |
| 692 |
{ |
|
|
| 693 |
_cmdHandler.incomingCommand(_node, msg.getCommand(i)); |
| 694 |
} |
| 695 |
} |
| 696 |
return current; |
| 697 |
} |
| 698 |
return -1; |
| 699 |
} |
674 |
} |
| 700 |
|
675 |
|
| 701 |
|
|
|
| 702 |
|
| 703 |
/** |
676 |
/** |
| 704 |
* @see Connection#addConnectionListener(ConnectionListener) |
677 |
* @see Connection#addConnectionListener(ConnectionListener) |
| 705 |
*/ |
678 |
*/ |
|
Lines 710-716
Link Here
|
| 710 |
} |
683 |
} |
| 711 |
} |
684 |
} |
| 712 |
} |
685 |
} |
| 713 |
|
|
|
| 714 |
|
686 |
|
| 715 |
/** |
687 |
/** |
| 716 |
* @see Connection#removeConnectionListener(ConnectionListener) |
688 |
* @see Connection#removeConnectionListener(ConnectionListener) |
|
Lines 726-744
Link Here
|
| 726 |
public void setSoTimeout(int timeout) { |
698 |
public void setSoTimeout(int timeout) { |
| 727 |
_soTimeout = timeout; |
699 |
_soTimeout = timeout; |
| 728 |
} |
700 |
} |
|
|
701 |
|
| 702 |
public Socket getSocket() { |
| 703 |
return _socket; |
| 704 |
} |
| 705 |
|
| 706 |
public void setSocket(Socket socket) { |
| 707 |
_socket = socket; |
| 708 |
_isComplete = true; |
| 709 |
} |
| 729 |
|
710 |
|
| 730 |
class SocketReaderThread extends Thread implements Constants |
711 |
class SocketReaderThread extends Thread implements Constants { |
| 731 |
{ |
712 |
public void run() { |
| 732 |
public void run() |
713 |
byte[] buffer = new byte[MAX_MESSAGE_LENGTH]; |
| 733 |
{ |
|
|
| 734 |
/* Run forever */ |
| 735 |
byte[] buffer=new byte[MAX_MESSAGE_LENGTH]; |
| 736 |
int masterOffset=0; |
714 |
int masterOffset=0; |
| 737 |
boolean incompleteMsg; |
715 |
boolean incompleteMsg; |
| 738 |
int timeoutCount=0; |
716 |
|
| 739 |
_isComplete = false; |
717 |
_isComplete = false; |
| 740 |
|
718 |
|
| 741 |
/* Get the InputStream for this socket so we can read some data */ |
719 |
// Get the InputStream for this socket so we can read some data |
| 742 |
BufferedInputStream inStream; |
720 |
BufferedInputStream inStream; |
| 743 |
try { |
721 |
try { |
| 744 |
inStream = new BufferedInputStream(_socket.getInputStream()); |
722 |
inStream = new BufferedInputStream(_socket.getInputStream()); |
|
Lines 749-819
Link Here
|
| 749 |
return; |
727 |
return; |
| 750 |
} |
728 |
} |
| 751 |
|
729 |
|
| 752 |
while(!_isComplete) |
730 |
while(!_isComplete) { |
| 753 |
{ |
731 |
incompleteMsg = false; // 185463 |
| 754 |
incompleteMsg = false; /* 185463 */ |
732 |
try { |
| 755 |
try |
733 |
int bytesRead = inStream.read(buffer, masterOffset, buffer.length-masterOffset); |
| 756 |
{ |
734 |
if(bytesRead == -1) break; |
| 757 |
int bytesRead=inStream.read(buffer, masterOffset, buffer.length-masterOffset); |
|
|
| 758 |
|
735 |
|
| 759 |
if(bytesRead==-1) {break;} |
736 |
if (masterOffset > 0) { |
| 760 |
|
|
|
| 761 |
if (masterOffset > 0) |
| 762 |
{ |
| 763 |
bytesRead += masterOffset; |
737 |
bytesRead += masterOffset; |
| 764 |
masterOffset = 0; |
738 |
masterOffset = 0; |
| 765 |
} |
739 |
} |
| 766 |
|
740 |
|
| 767 |
/* |
741 |
// |
| 768 |
* If we've tried to connect to a backward compatibility |
742 |
// If we've tried to connect to a backward compatibility |
| 769 |
* layer of the AC, we'll immediately get a response in |
743 |
// layer of the AC, we'll immediately get a response in |
| 770 |
* RAC format. If we ignore this, the BC layer will |
744 |
// RAC format. If we ignore this, the BC layer will |
| 771 |
* respond to our first message by giving us the port |
745 |
// respond to our first message by giving us the port |
| 772 |
* to connect to the regular socket TL. |
746 |
// to connect to the regular socket TL. |
| 773 |
*/ |
747 |
// |
| 774 |
int vret = checkForRACMessage(buffer, masterOffset); |
748 |
int vret = checkForRACMessage(buffer, masterOffset); |
| 775 |
if(vret != -1) |
749 |
if (vret != -1) { |
| 776 |
{ |
|
|
| 777 |
// We read the header, the next four bytes will be the length |
750 |
// We read the header, the next four bytes will be the length |
| 778 |
// (including the header) of the RAC message |
751 |
// (including the header) of the RAC message |
| 779 |
long length=TPTPMessageUtil.readTPTPLongFromBuffer(buffer, vret); |
752 |
long length = TPTPMessageUtil.readTPTPLongFromBuffer(buffer, vret); |
| 780 |
masterOffset = (int)length; |
753 |
masterOffset = (int)length; |
| 781 |
} |
754 |
} |
| 782 |
|
755 |
|
| 783 |
while(masterOffset<bytesRead) |
756 |
while(masterOffset<bytesRead) { |
| 784 |
{ |
|
|
| 785 |
int newOffset=0; |
757 |
int newOffset=0; |
| 786 |
/* First we make sure we have at least enough read for the message header. |
758 |
// First we make sure we have at least enough read for the message header. |
| 787 |
If not, compress and re-read. |
759 |
// If not, compress and re-read. |
| 788 |
*/ |
760 |
// |
| 789 |
if ( bytesRead-masterOffset < AC_MESSAGE_HEADER_SIZE ) { |
761 |
if ( bytesRead-masterOffset < AC_MESSAGE_HEADER_SIZE ) { |
| 790 |
System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); |
762 |
System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); |
| 791 |
masterOffset=bytesRead-masterOffset; |
763 |
masterOffset=bytesRead-masterOffset; |
| 792 |
incompleteMsg = true; |
764 |
incompleteMsg = true; |
| 793 |
break; |
765 |
break; |
| 794 |
} |
766 |
} |
| 795 |
/* We have the header information, now lets try and get the entire message. Once |
767 |
|
| 796 |
again the same error conditions can occur. |
768 |
// We have the header information, now lets try and get the entire message. Once |
| 797 |
*/ |
769 |
// again the same error conditions can occur. |
| 798 |
if(Constants.TPTP_DEBUG)System.out.println("Received a non-acknowledgement message"); |
770 |
// |
| 799 |
//System.out.println("Offset xx::"+new String(buffer)); |
771 |
newOffset = processControlMessage(buffer, masterOffset, bytesRead); |
| 800 |
newOffset=processControlMessage(buffer, masterOffset, bytesRead); |
772 |
|
| 801 |
if(Constants.TPTP_DEBUG)System.out.println("Recvd Message : " + buffer.toString()); |
773 |
if(Constants.TPTP_DEBUG)System.out.println("Recvd Message: " + buffer.toString()); |
| 802 |
if ( newOffset == -1 ) |
774 |
|
| 803 |
{ |
775 |
if (newOffset == -1) { |
| 804 |
// newOffset of -1 indicates the message is bigger than what's left in the buffer |
776 |
// newOffset of -1 indicates the message is bigger than what's left in the buffer |
| 805 |
// If the masterOffset is zero, this means the message is bigger than the buffer |
777 |
// If the masterOffset is zero, this means the message is bigger than the buffer |
| 806 |
// itself, so we need to grow the buffer. Otherwise, we'll slide the message |
778 |
// itself, so we need to grow the buffer. Otherwise, we'll slide the message |
| 807 |
// to the beginning of the buffer and try to read some more |
779 |
// to the beginning of the buffer and try to read some more |
| 808 |
if(masterOffset>0) |
780 |
if(masterOffset>0) { |
| 809 |
{ |
|
|
| 810 |
System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); |
781 |
System.arraycopy( buffer, masterOffset, buffer, 0, bytesRead-masterOffset ); |
| 811 |
} |
782 |
} |
| 812 |
else if (bytesRead == buffer.length) |
783 |
else if (bytesRead == buffer.length) |
| 813 |
{ |
784 |
{ |
| 814 |
int len = buffer.length * 2; |
785 |
int len = buffer.length * 2; |
| 815 |
byte[] tmpbuffer=new byte[len]; |
786 |
byte[] tmpbuffer=new byte[len]; |
| 816 |
/* Copy necessary data over to new buffer */ |
787 |
// Copy necessary data over to new buffer |
| 817 |
System.arraycopy(buffer, masterOffset, tmpbuffer, 0, bytesRead-masterOffset); |
788 |
System.arraycopy(buffer, masterOffset, tmpbuffer, 0, bytesRead-masterOffset); |
| 818 |
buffer = tmpbuffer; |
789 |
buffer = tmpbuffer; |
| 819 |
} |
790 |
} |
|
Lines 822-843
Link Here
|
| 822 |
break; |
793 |
break; |
| 823 |
} |
794 |
} |
| 824 |
masterOffset=newOffset; |
795 |
masterOffset=newOffset; |
| 825 |
} /* end of inner while */ |
796 |
} // end of inner while |
| 826 |
|
797 |
|
| 827 |
if (!incompleteMsg) |
798 |
if (!incompleteMsg) |
| 828 |
{ |
799 |
{ |
| 829 |
masterOffset=0; |
800 |
masterOffset=0; |
| 830 |
} |
801 |
} |
| 831 |
} |
802 |
} |
| 832 |
catch(InterruptedIOException e) |
803 |
catch(InterruptedIOException e) { |
| 833 |
{ |
|
|
| 834 |
if(timeoutCount > Constants.CONNECT_TIMEOUT_TRY_COUNT && !_isInitialized) |
| 835 |
{ |
| 836 |
synchronized(_connectionLock) { |
| 837 |
_connectionLock.notifyAll(); |
| 838 |
} |
| 839 |
} |
| 840 |
timeoutCount++; |
| 841 |
} |
804 |
} |
| 842 |
catch(SocketException e) |
805 |
catch(SocketException e) |
| 843 |
{ |
806 |
{ |
|
Lines 849-857
Link Here
|
| 849 |
//e.printStackTrace(); |
812 |
//e.printStackTrace(); |
| 850 |
break; |
813 |
break; |
| 851 |
} |
814 |
} |
| 852 |
} /* end of outer while */ |
815 |
} // end of outer while |
| 853 |
/* The server is now stopping */ |
816 |
// The server is now stopping |
|
|
817 |
|
| 854 |
disconnect(); |
818 |
disconnect(); |
| 855 |
} |
819 |
} |
| 856 |
} |
820 |
} |
|
|
821 |
|
| 822 |
public boolean isAuthenticated() { |
| 823 |
return isAuthenticated; |
| 824 |
} |
| 825 |
|
| 826 |
protected boolean authenticateUser(User user) { |
| 827 |
if (user == null || user.getName() == null || user.getPassword() == null) return false; |
| 828 |
|
| 829 |
ControlMessage authMsg = new ControlMessage(); |
| 830 |
authMsg.setMessageType(Constants.TPTP_AC_MESSAGE); |
| 831 |
authMsg.setMagicNumber(Constants.AC_MAGIC_NUMBER); |
| 832 |
authMsg.setFlags(Constants.AUTHENTICATE); |
| 833 |
|
| 834 |
AuthenticateCommand authCmd = new AuthenticateCommand(user.getName(), user.getPassword()); |
| 835 |
authMsg.appendCommand(authCmd); |
| 836 |
|
| 837 |
try { |
| 838 |
sendMessage(authMsg, new ICommandHandler() { |
| 839 |
public void incomingCommand(INode node, ICommandElement command) {} |
| 840 |
}); |
| 841 |
} catch (Exception e) { |
| 842 |
return false; |
| 843 |
} |
| 844 |
|
| 845 |
synchronized (authLock) { |
| 846 |
try { authLock.wait(Constants.WAIT_RESPONCE_TIMEOUT); } catch (Exception e) {} |
| 847 |
} |
| 848 |
|
| 849 |
return isAuthenticated; |
| 850 |
} |
| 857 |
} |
851 |
} |