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