/
NCCPConnection.java
444 lines (415 loc) · 12.9 KB
/
NCCPConnection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (c) 2005-2013 Jyoti Parwatikar
* and Washington University in St. Louis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* File: NCCPConnection.java
* Author: Jyoti Parwatikar
* Email: jp@arl.wustl.edu
* Organization: Washington University
*
* Derived from: none
*
* Date Created: 10/23/2003
*
* Description:
*
* Modification History:
*
*/
import java.lang.*;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.swing.event.*;
import javax.swing.*;
import java.nio.*;
import java.lang.reflect.Array;
public abstract class NCCPConnection extends NodeLabel
{
public static final String PEER = "peer";
private int state = ConnectionEvent.CONNECTION_CLOSED;
//private boolean connected = false;
protected int count = 0;
private boolean portSet = false;
private boolean testing = false;//temporary for testing
private Vector listeners;
protected Vector pendingMessages = null; //changed 10/1/08 from private
private Proxy proxy = null;
private NonProxyConn nonProxy = null;
private boolean isRecording = false;
private class MessageRunnable implements Runnable
{
private int msgsize = 0;
private int msgtype = 0;
private DataInputStream din = null;
private byte msgbytes[] = null;
public MessageRunnable(int msz, int mtp, DataInputStream di) throws IOException, SocketException
{
msgsize = msz;
msgtype = mtp;
msgbytes = new byte[msgsize-2];
di.readFully(msgbytes);
// di.read(msgbytes);
din = new DataInputStream(new ByteArrayInputStream(msgbytes));
}
public void run()
{
try
{
if (msgtype == NCCP.MessageResponse)
processResponse(msgsize, msgtype, din);
else
processOtherMsg(msgsize, msgtype, din);
}
catch(java.io.IOException e) {}
}
public void print(int dlevel)
{
if (msgtype == NCCP.MessageResponse)
ExpCoordinator.print(new String("NCCPConnection.Message Response size = " + msgsize) , dlevel);
else
ExpCoordinator.print(new String("NCCPConnection.Message type = " + msgtype + " size = " + msgsize) , dlevel);
String tmp_str = "";
int max = msgsize -2;
for (int i = 0 ; i < max; ++i)
{
for (int j = 0; j < 4; ++j)
{
if (i < max)
{
tmp_str = tmp_str.concat(new String(msgbytes[i] + " "));
++i;
}
}
ExpCoordinator.print(new String(" " + tmp_str), dlevel);
tmp_str = "";
}
}
}
public static class ConnectionEvent extends EventObject
{
public static final int CONNECTION_FAILED = 1;
public static final int CONNECTION_CLOSED = 2;
public static final int CONNECTION_OPENED = 3;
public static final int CONNECTION_PENDING = 4;
public static final int ADDRESS_CHANGED = 5;
private int eType = 0;
public ConnectionEvent(NodeLabel id, int tp)
{
super(id);
eType = tp;
}
public int getType() { return eType;}
public int getPort() { return (((NodeLabel)getSource()).getPort());}
public String getHost() { return (((NodeLabel)getSource()).getHost());}
}
public static interface ConnectionListener extends EventListener
{
public void connectionFailed(NCCPConnection.ConnectionEvent e);
public void connectionClosed(NCCPConnection.ConnectionEvent e);
public void connectionOpened(NCCPConnection.ConnectionEvent e);
}
public NCCPConnection(String h, int p, String lbl)
{
super(h,p,lbl);
listeners = new Vector();
pendingMessages = new Vector();
}
public void setProxy(Proxy p)
{
proxy = p;
proxy.addConnection(this);
}
protected void setNonProxy()
{
nonProxy = new NonProxyConn(this);
}
public boolean connect()
{
ExpCoordinator.printer.print("NCCPConnection.connect", 3);
if (host.length() == 0)
{
ExpCoordinator.printer.print("NCCPConnection.connect host is not set", 6);
return false;
}
if (proxy == null && nonProxy == null)
{
ExpCoordinator.printer.print("NCCPConnection.connect proxy null");
return false;
}
if (state == ConnectionEvent.CONNECTION_CLOSED || state == ConnectionEvent.CONNECTION_FAILED)
{
state = ConnectionEvent.CONNECTION_PENDING;
if (proxy != null)
{
proxy.openConnection(this);
}
}
/*
if (nonProxy != null && proxy == null)
{
return (nonProxy.connect());
}
*/
return true;
}
public void connectionFailed()
{
ExpCoordinator.printer.print(new String("ConnectionFailed Closed control socket for " + host + "." + port));
if (state != ConnectionEvent.CONNECTION_CLOSED)
{
state = ConnectionEvent.CONNECTION_FAILED;
fireEvent(new ConnectionEvent(this,ConnectionEvent.CONNECTION_FAILED));
close();
}
}
public void setConnected(boolean b) { setConnected(b, true);}
public void setConnected(boolean b, boolean process_pending)
{
if (b && state != ConnectionEvent.CONNECTION_OPENED)
{
if (host.length() > 0) ExpCoordinator.print("NCCPConnection open connection to ipaddress " + host + " port " + port);
state = ConnectionEvent.CONNECTION_OPENED;
//connected = true;
fireEvent(new ConnectionEvent(this, state));
ExpCoordinator.printer.print("NCCPConnection.setConnected " + b + " event fired", 8);
if (process_pending)
{
int max = pendingMessages.size();
ExpCoordinator.printer.print(new String(" pendingMessages " + max + " elements"), 8);
for (int i = 0; i < max; ++i)
{
sendMessage((NCCP.Message)pendingMessages.elementAt(i));
}
pendingMessages.removeAllElements();
}
}
else
{
if (!b && (state != ConnectionEvent.CONNECTION_CLOSED || state != ConnectionEvent.CONNECTION_FAILED))
{
if (host.length() > 0) ExpCoordinator.print(new String("NCCPConnection.setConnected -- Closed control socket for " + host + "." + port));
state = ConnectionEvent.CONNECTION_CLOSED;
fireEvent(new ConnectionEvent(this, state));
}
}
}
public void sendAck(NCCP.PeriodicMessage msg)
{
//form ack from message switching the rendezvous markers for the return trip back to the sender
NCCP.PeriodicMessage ack = new NCCP.PeriodicMessage(NCCP.Operation_AckPeriodic, msg.getReturnRendMarker(), msg.getRendMarker());
ExpCoordinator.printer.print("NCCPConnection::sendAck", 9);
//ack.print();
sendMessage(ack);
}
public void sendMessage(NCCP.Message msg)
{
if (isClosed() || isFailed()) connect();
if (isConnected())
{
ExpCoordinator.printer.print("NCCPConnection::sendMessage", 5);
if (proxy != null) proxy.sendMessage(msg, this);
else
{
if (nonProxy != null) nonProxy.sendMessage(msg);
}
}
else //still can't connect
{
pendingMessages.add(msg);
}
}
public void close()
{
if (proxy != null && isConnected())
{
proxy.closeConnection(this);
setConnected(false);
}
else
{
if (nonProxy != null && isConnected())
{
try{
Thread.sleep(1000);
}
catch (java.lang.InterruptedException e2)
{}
nonProxy.close();
setConnected(false);
}
}
}
public void flush()
{
if (nonProxy != null) nonProxy.flush();
}
public void processMessage(DataInputStream dataIn) throws IOException
{
count = 0;
int msgsize = dataIn.readInt();
count = 1;
int msgtype = 0;
if (msgsize >= 2) msgtype = dataIn.readUnsignedShort();
else
{
if (msgsize == 1) dataIn.readByte();
ExpCoordinator.printer.print(new String("ERROR:NCCPConnection.run msgsize(" + msgsize + ") < 2 msgtype = " + msgtype));
ExpCoordinator.printer.printHistory();
return;
}
count = 2;
switch (msgtype)
{
case NCCP.MessageResponse:
ExpCoordinator.print(new String("NCCPConnection::run message is NCCP.MessageResponse " + msgsize + " " + msgtype), 3);
MessageRunnable tmp_msg = new MessageRunnable(msgsize, msgtype, dataIn);
tmp_msg.print(5);
SwingUtilities.invokeLater(tmp_msg);
break;
case NCCP.MessagePeriodic:
ExpCoordinator.printer.print(new String("NCCPConnection::run message is NCCP.MessagePeriodic " + msgsize + " " + msgtype), 3);
processPeriodicMsg(msgsize, msgtype, dataIn);
break;
default:
ExpCoordinator.printer.print(new String("NCCPConnection::run message is Other " + msgsize + " " + msgtype));
SwingUtilities.invokeLater(new MessageRunnable(msgsize, msgtype, dataIn));
}
}
public boolean isTesting() { return testing;} //temporary for testing
public boolean isConnected() { return (state == ConnectionEvent.CONNECTION_OPENED);}
public boolean isClosed() { return (state == ConnectionEvent.CONNECTION_CLOSED);}
public boolean isFailed() { return (state == ConnectionEvent.CONNECTION_FAILED);}
public void addConnectionListener(NCCPConnection.ConnectionListener l)
{
if (!listeners.contains(l))
{
ExpCoordinator.printer.print(new String("NCCPConnection.addConnectionListener to " + toString()), 6);
listeners.add(l);
}
}
public void removeConnectionListener(NCCPConnection.ConnectionListener l)
{
if (listeners.contains(l))
{
//ExpCoordinator.printer.print(new String("NCCPConnection.removeConnectionListener to " + toString()), 2);
listeners.remove(l);
}
}
public void fireEvent(int tp)
{
NCCPConnection.ConnectionEvent e = new NCCPConnection.ConnectionEvent(this, tp);
fireEvent(e);
}
public void fireEvent(NCCPConnection.ConnectionEvent e)
{
ConnectionListener l;
int max = listeners.size();
for (int i = 0; i < max; ++i)
{
ExpCoordinator.print(new String("NCCPConnection.fireEvent (" + toString() + ") event: " + e.getType() + " max:" + max + " i:" + i), 2);
l = (ConnectionListener) (listeners.elementAt(i));
switch(e.getType())
{
case ConnectionEvent.CONNECTION_FAILED:
l.connectionFailed(e);
break;
case ConnectionEvent.CONNECTION_CLOSED:
l.connectionClosed(e);
break;
case ConnectionEvent.CONNECTION_OPENED:
ExpCoordinator.printer.print(new String("Opened control connection (" + toString() + ")"));
l.connectionOpened(e);
}
}
}
//descendants define to handle a NCCP response
protected abstract void processResponse(int msgsize, int msgtype, DataInputStream dIn) throws SocketException, IOException, java.lang.NullPointerException ;
//descendants define to handle a NCCP periodic response
protected abstract void processPeriodicMsg(int msgsize, int msgtype, DataInputStream dIn) throws SocketException, IOException, java.lang.NullPointerException ;
public byte[] getRAddr()
{
try
{
InetAddress addr = InetAddress.getByName(host);
return addr.getAddress();
}
catch (UnknownHostException e)
{
System.err.println("NCCPConnection::getRAddr Don't know about host: ");
return null;
}
}
protected void processOtherMsg(int msgsize, int msgtype, DataInputStream dIn) throws SocketException, IOException, java.lang.NullPointerException
{
dIn.skipBytes(msgsize - 2); //need to just skip this message because we don't recognize it
ExpCoordinator.printer.print(new String("NCCPConnection.processOtherMsg skipping message " + msgsize + " " + msgtype));
ExpCoordinator.printer.printHistory();
}
public byte[] getNBOPort() { return (getNBOPort((short)port));}
public static byte[] getNBOPort(short p)
{
ByteBuffer buf = ByteBuffer.allocate(2);
buf.putShort((short)p);
buf.order(ByteOrder.BIG_ENDIAN);
buf.rewind();
byte[] rtn = new byte[2];
buf.get(rtn);
return rtn;
}
public byte[] getNBOHost() { return (getNBOHost(host));}
public static byte[] getNBOHost(String h)
{
if (h.equals(PEER)) return null;
byte hostBytes[] = h.getBytes();
int len = Array.getLength(hostBytes);
ByteBuffer buf = ByteBuffer.allocate(len);
buf.put(hostBytes);
buf.order(ByteOrder.BIG_ENDIAN);
buf.rewind();
return (buf.array());
}
public String toString() { return (new String( host + " " + port));}
protected Proxy getProxy() { return proxy;}
public void write(ONL.Writer wrtr) throws java.io.IOException
{
wrtr.writeString(host);
wrtr.writeInt(port);
short cid = 0;
if (proxy != null) cid = proxy.getConnectionID(this);
wrtr.writeShort(cid);
}
public void read(ONL.Reader rdr) throws java.io.IOException
{
setHost(new String(rdr.readString()));
setPort(rdr.readInt());
short cid = (short)rdr.readShort();
ExpCoordinator.print(new String("NCCPConnection.read host:" + getHost() + " port:" + getPort() + " cid:" + cid), 5);
if (proxy != null) proxy.setConnectionID(this, cid);
}
public void setConnectionID(short cid)
{
ExpCoordinator.print(new String("NCCPConnection.setConnectionID host:" + getHost() + " port:" + getPort() + " cid:" + cid), 5);
if (proxy != null) proxy.setConnectionID(this, cid);
}
public short getConnectionID()
{
if (proxy != null) return ((short)proxy.getConnectionID(this));
else return ((short)0);
}
protected int getState() { return state;}
}