@@ -69,7 +69,7 @@ public void createNewConnection(NewConnectionCreatorListener listener,
69
69
}
70
70
71
71
//////////////////////////////////////////////////////////////////////////////////////////////////////////
72
- // connection establisher interface to hub and connector peer side //
72
+ // interface to establish connection to hub and connector peer side //
73
73
//////////////////////////////////////////////////////////////////////////////////////////////////////////
74
74
75
75
/**
@@ -98,20 +98,55 @@ public void connectionRequest(CharSequence sourcePeerID, CharSequence targetPeer
98
98
this .connectionRequest (targetPeerID );
99
99
} else {
100
100
// remember call
101
- this .externalConnectionRequestList .add (
102
- new ConnectionRequest (
103
- sourcePeerID , targetPeerID ,System .currentTimeMillis () + timeout ,
104
- this .canEstablishTCPConnections ()));
101
+ ConnectionRequest newConnectionRequest = new ConnectionRequest (
102
+ sourcePeerID , targetPeerID ,System .currentTimeMillis () + timeout ,
103
+ this .canEstablishTCPConnections ());
104
+
105
+ // check for duplicates
106
+ ConnectionRequest duplicate = this .connectionRequestExists (newConnectionRequest );
107
+ if (duplicate != null ) {
108
+ StringBuilder sb = new StringBuilder ();
109
+ sb .append ("ignore new connection request: " );
110
+ sb .append ("new: " + newConnectionRequest );
111
+ sb .append ("pending: " + duplicate );
112
+ } else {
113
+ this .pendingConnectionRequests .add (newConnectionRequest );
114
+ this .processPendingConnectionRequestList ();
115
+ }
116
+ }
117
+ }
105
118
106
- this .handleExternalConnectionRequestList ();
119
+ private ConnectionRequest connectionRequestExists (ConnectionRequest newConnectionRequest ) {
120
+ ConnectionRequest duplicate = null ;
121
+ for (ConnectionRequest pendingRequest : this .pendingConnectionRequests ) {
122
+ if (this .sameConnectionRequest (pendingRequest , newConnectionRequest )) {
123
+ duplicate = pendingRequest ; break ;
124
+ }
107
125
}
126
+ return duplicate ;
127
+ }
128
+
129
+ private boolean sameConnectionRequest (ConnectionRequest requestA , ConnectionRequest requestB ) {
130
+ return (
131
+ (
132
+ PeerIDHelper .sameID (requestA .sourcePeerID , requestB .sourcePeerID )
133
+ &&
134
+ PeerIDHelper .sameID (requestA .targetPeerID , requestB .targetPeerID )
135
+ )
136
+ ||
137
+ (
138
+ PeerIDHelper .sameID (requestA .sourcePeerID , requestB .targetPeerID )
139
+ &&
140
+ PeerIDHelper .sameID (requestA .targetPeerID , requestB .sourcePeerID )
141
+ )
142
+ );
108
143
}
109
144
110
145
void connectionRequest (CharSequence targetPeerID ) throws ASAPHubException , IOException {
111
146
this .hub .connectionRequest (this .getPeerID (), targetPeerID , this .getTimeOutConnectionRequest ());
112
147
}
113
148
114
- private List <ConnectionRequest > externalConnectionRequestList = new ArrayList <>();
149
+ private List <ConnectionRequest > pendingConnectionRequests = new ArrayList <>();
115
150
116
151
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
117
152
// reaction on status changes //
@@ -120,7 +155,7 @@ void connectionRequest(CharSequence targetPeerID) throws ASAPHubException, IOExc
120
155
@ Override
121
156
protected void silenceStarted () {
122
157
try {
123
- this .handleExternalConnectionRequestList ();
158
+ this .processPendingConnectionRequestList ();
124
159
} catch (ASAPHubException | IOException e ) {
125
160
e .printStackTrace ();
126
161
}
@@ -166,28 +201,42 @@ protected void shutdown() {
166
201
this .hub .unregister (this .getPeerID ());
167
202
}
168
203
169
- synchronized private boolean handleExternalConnectionRequestList () throws ASAPHubException , IOException {
170
- // lets see if we can start another connection
171
- Log .writeLog (this , this .toString (), "#entries connection request list: "
172
- + this .externalConnectionRequestList .size ());
204
+ synchronized private boolean processPendingConnectionRequestList () throws ASAPHubException , IOException {
205
+ // let's see if we can start another connection
206
+ Log .writeLog (this , this .toString (), "process pending connection request; #entries in list: "
207
+ + this .pendingConnectionRequests .size ());
208
+
209
+ if (this .pendingConnectionRequests .size () > 0 ) {
210
+ boolean first = true ;
211
+ StringBuilder sb = new StringBuilder ();
212
+ for (ConnectionRequest request : this .pendingConnectionRequests ) {
213
+ if (first ) first = false ;
214
+ else sb .append ("\n " );
215
+ sb .append (request .toString ());
216
+ }
217
+ Log .writeLog (this , this .toString (), "list: \n " + sb .toString ());
218
+ }
173
219
174
- if (this .externalConnectionRequestList .isEmpty ()) return false ; // empty nothing to do
220
+ if (this .pendingConnectionRequests .isEmpty ()) {
221
+ Log .writeLog (this , this .toString (), "no other requests - nothing to do" );
222
+ return false ; // empty nothing to do
223
+ }
175
224
176
225
// remove outdated requests
177
- ConnectionRequest connectionRequest = null ;
178
- while (connectionRequest == null && !this .externalConnectionRequestList .isEmpty ()) {
179
- connectionRequest = this .externalConnectionRequestList .remove (0 );
180
- if (connectionRequest .until < System .currentTimeMillis ()) {
226
+ ConnectionRequest nextRequestToProcess = null ;
227
+ while (nextRequestToProcess == null && !this .pendingConnectionRequests .isEmpty ()) {
228
+ nextRequestToProcess = this .pendingConnectionRequests .remove (0 );
229
+ if (nextRequestToProcess .until < System .currentTimeMillis ()) {
181
230
Log .writeLog (this , this .toString (), "discard connection request - timed out" );
182
- connectionRequest = null ;
231
+ nextRequestToProcess = null ;
183
232
}
184
233
}
185
234
186
- if (connectionRequest == null ) return false ; // list empty
235
+ if (nextRequestToProcess == null ) return false ; // list empty
187
236
188
- if (this .canEstablishTCPConnections () && connectionRequest .newConnection ) {
237
+ if (this .canEstablishTCPConnections () && nextRequestToProcess .newConnection ) {
189
238
try {
190
- return this .initDataSessionOnNewConnection (connectionRequest ,
239
+ return this .initDataSessionOnNewConnection (nextRequestToProcess ,
191
240
this .getTimeOutConnectionRequest (), this .getTimeOutDataConnection ());
192
241
} catch (RuntimeException e ) {
193
242
Log .writeLog (this , "not yet implemented? Go ahead and try shared channel: "
@@ -201,31 +250,42 @@ synchronized private boolean handleExternalConnectionRequestList() throws ASAPHu
201
250
// we are in the right status - take the oldest request
202
251
203
252
// handle connection request
204
- Log .writeLog (this , this .toString (), "launch data session by request: " + connectionRequest );
253
+ Log .writeLog (this , this .toString (), "launch data session by request: " + nextRequestToProcess );
205
254
206
255
// init data session - this can fail if we are not in silence mode - that's ok, though
207
256
StreamPair streamPair = null ;
208
257
try {
209
- streamPair = this .initDataSession (connectionRequest , this .getTimeOutDataConnection ());
258
+ streamPair = this .initDataSession (nextRequestToProcess , this .getTimeOutDataConnection ());
210
259
}
211
260
catch (ASAPHubException e ) {
212
261
Log .writeLog (this , this .toString (), "cannot init data session yet - we can wait" );
213
262
return false ;
214
263
}
215
264
216
265
// tell hub
217
- Log .writeLog (this , this .toString (), "tell hub about newly created data session: " + connectionRequest );
218
- this .hub .startDataSession (this .getPeerID (), connectionRequest .sourcePeerID ,
266
+ Log .writeLog (this , this .toString (), "tell hub about newly created data session: " + nextRequestToProcess );
267
+ this .hub .startDataSession (this .getPeerID (), nextRequestToProcess .sourcePeerID ,
219
268
streamPair , this .getTimeOutDataConnection ());
220
269
} else {
221
270
Log .writeLog (this , this .toString (), "not in silence mode - ask for silence" );
222
271
// not in silence - should we asked for silence
223
272
if (this .statusHubConnectorProtocol ()) { // we are in protocol status - change it
224
273
// put request back
225
- this .externalConnectionRequestList .add (connectionRequest );
226
- this .askForSilence (this .getTimeOutSilenceChannel ());
274
+ Log .writeLog (this , this .toString (), "put request back in pending list: "
275
+ + nextRequestToProcess );
276
+ this .pendingConnectionRequests .add (nextRequestToProcess );
277
+ try {
278
+ this .askForSilence (this .getTimeOutSilenceChannel ());
279
+ }
280
+ catch (ASAPHubException ahe ) {
281
+ Log .writeLog (this , this .toString (),
282
+ "cannot ask for silence; connection request remains in pending list: "
283
+ + ahe .getLocalizedMessage ());
284
+ }
227
285
} else {
228
- Log .writeLog (this , this .toString (), "cannot ask for silence .. not in connector mode" );
286
+ Log .writeLog (this , this .toString (),
287
+ "cannot ask for silence .. not in connector mode - discard connection request "
288
+ + nextRequestToProcess );
229
289
}
230
290
}
231
291
return true ;
@@ -239,7 +299,7 @@ protected boolean initDataSessionOnNewConnection(
239
299
240
300
protected void actionWhenBackFromDataSession () {
241
301
try {
242
- if (this .handleExternalConnectionRequestList ()) return ; // there are pending request
302
+ if (this .processPendingConnectionRequestList ()) return ; // there are pending request
243
303
// relaunch Connector thread
244
304
} catch (ASAPHubException | IOException e ) {
245
305
e .printStackTrace ();
@@ -280,7 +340,7 @@ private boolean localCall(CharSequence sourcePeerID, CharSequence targetPeerID)
280
340
public void disconnect (CharSequence sourcePeerID , CharSequence targetPeerID ) throws ASAPHubException {
281
341
Log .writeLog (this , "disconnect called" );
282
342
ConnectionRequest removeRequest = null ;
283
- for (ConnectionRequest request : this .externalConnectionRequestList ) {
343
+ for (ConnectionRequest request : this .pendingConnectionRequests ) {
284
344
if ( PeerIDHelper .sameID (sourcePeerID , request .sourcePeerID )
285
345
&& PeerIDHelper .sameID (targetPeerID , request .targetPeerID )) {
286
346
@@ -290,7 +350,7 @@ public void disconnect(CharSequence sourcePeerID, CharSequence targetPeerID) thr
290
350
}
291
351
}
292
352
293
- if (removeRequest != null ) this .externalConnectionRequestList .remove (removeRequest );
353
+ if (removeRequest != null ) this .pendingConnectionRequests .remove (removeRequest );
294
354
}
295
355
296
356
/**
@@ -310,12 +370,21 @@ public void startDataSession(CharSequence sourcePeerID, CharSequence targetPeerI
310
370
if (this .localCall (sourcePeerID , targetPeerID ))
311
371
throw new ASAPHubException ("a connection started notification cannot come from local peer" );
312
372
313
- StreamPair stream2Peer = this .initDataSession (sourcePeerID , targetPeerID , timeout );
314
- Log .writeLog (this , this .toString (), "got connection to peer side" );
373
+ try {
374
+ StreamPair stream2Peer = this .initDataSession (sourcePeerID , targetPeerID , timeout );
375
+ Log .writeLog (this , this .toString (), "got connection to peer side" );
315
376
316
- // link stream pair from hub with stream pair to peer
317
- new StreamPairLink (stream2Peer , sourcePeerID , stream2Hub , targetPeerID );
318
- Log .writeLog (this , this .toString (), "created and started stream pair link" );
377
+ // link stream pair from hub with stream pair to peer
378
+ new StreamPairLink (stream2Peer , sourcePeerID , stream2Hub , targetPeerID );
379
+ Log .writeLog (this , this .toString (), "created and started stream pair link" );
380
+ }
381
+ catch (ASAPHubException ahe ) {
382
+ Log .writeLog (this , this .toString (), "could not establish data session: "
383
+ + ahe .getLocalizedMessage ());
384
+ Log .writeLog (this , this .toString (), "could not establish data session: " );
385
+ ConnectionRequest pendingRequest = ConnectionRequest .createNewConnectRequest (sourcePeerID , targetPeerID );
386
+ Log .writeLog (this , this .toString (), "REMEMBER REQUEST ?: " + pendingRequest );
387
+ }
319
388
}
320
389
321
390
/**
0 commit comments