@@ -69,7 +69,7 @@ public void createNewConnection(NewConnectionCreatorListener listener,
6969 }
7070
7171 //////////////////////////////////////////////////////////////////////////////////////////////////////////
72- // connection establisher interface to hub and connector peer side //
72+ // interface to establish connection to hub and connector peer side //
7373 //////////////////////////////////////////////////////////////////////////////////////////////////////////
7474
7575 /**
@@ -98,20 +98,55 @@ public void connectionRequest(CharSequence sourcePeerID, CharSequence targetPeer
9898 this .connectionRequest (targetPeerID );
9999 } else {
100100 // 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+ }
105118
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+ }
107125 }
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+ );
108143 }
109144
110145 void connectionRequest (CharSequence targetPeerID ) throws ASAPHubException , IOException {
111146 this .hub .connectionRequest (this .getPeerID (), targetPeerID , this .getTimeOutConnectionRequest ());
112147 }
113148
114- private List <ConnectionRequest > externalConnectionRequestList = new ArrayList <>();
149+ private List <ConnectionRequest > pendingConnectionRequests = new ArrayList <>();
115150
116151 /////////////////////////////////////////////////////////////////////////////////////////////////////////////
117152 // reaction on status changes //
@@ -120,7 +155,7 @@ void connectionRequest(CharSequence targetPeerID) throws ASAPHubException, IOExc
120155 @ Override
121156 protected void silenceStarted () {
122157 try {
123- this .handleExternalConnectionRequestList ();
158+ this .processPendingConnectionRequestList ();
124159 } catch (ASAPHubException | IOException e ) {
125160 e .printStackTrace ();
126161 }
@@ -166,28 +201,42 @@ protected void shutdown() {
166201 this .hub .unregister (this .getPeerID ());
167202 }
168203
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+ }
173219
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+ }
175224
176225 // 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 ()) {
181230 Log .writeLog (this , this .toString (), "discard connection request - timed out" );
182- connectionRequest = null ;
231+ nextRequestToProcess = null ;
183232 }
184233 }
185234
186- if (connectionRequest == null ) return false ; // list empty
235+ if (nextRequestToProcess == null ) return false ; // list empty
187236
188- if (this .canEstablishTCPConnections () && connectionRequest .newConnection ) {
237+ if (this .canEstablishTCPConnections () && nextRequestToProcess .newConnection ) {
189238 try {
190- return this .initDataSessionOnNewConnection (connectionRequest ,
239+ return this .initDataSessionOnNewConnection (nextRequestToProcess ,
191240 this .getTimeOutConnectionRequest (), this .getTimeOutDataConnection ());
192241 } catch (RuntimeException e ) {
193242 Log .writeLog (this , "not yet implemented? Go ahead and try shared channel: "
@@ -201,31 +250,42 @@ synchronized private boolean handleExternalConnectionRequestList() throws ASAPHu
201250 // we are in the right status - take the oldest request
202251
203252 // 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 );
205254
206255 // init data session - this can fail if we are not in silence mode - that's ok, though
207256 StreamPair streamPair = null ;
208257 try {
209- streamPair = this .initDataSession (connectionRequest , this .getTimeOutDataConnection ());
258+ streamPair = this .initDataSession (nextRequestToProcess , this .getTimeOutDataConnection ());
210259 }
211260 catch (ASAPHubException e ) {
212261 Log .writeLog (this , this .toString (), "cannot init data session yet - we can wait" );
213262 return false ;
214263 }
215264
216265 // 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 ,
219268 streamPair , this .getTimeOutDataConnection ());
220269 } else {
221270 Log .writeLog (this , this .toString (), "not in silence mode - ask for silence" );
222271 // not in silence - should we asked for silence
223272 if (this .statusHubConnectorProtocol ()) { // we are in protocol status - change it
224273 // 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+ }
227285 } 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 );
229289 }
230290 }
231291 return true ;
@@ -239,7 +299,7 @@ protected boolean initDataSessionOnNewConnection(
239299
240300 protected void actionWhenBackFromDataSession () {
241301 try {
242- if (this .handleExternalConnectionRequestList ()) return ; // there are pending request
302+ if (this .processPendingConnectionRequestList ()) return ; // there are pending request
243303 // relaunch Connector thread
244304 } catch (ASAPHubException | IOException e ) {
245305 e .printStackTrace ();
@@ -280,7 +340,7 @@ private boolean localCall(CharSequence sourcePeerID, CharSequence targetPeerID)
280340 public void disconnect (CharSequence sourcePeerID , CharSequence targetPeerID ) throws ASAPHubException {
281341 Log .writeLog (this , "disconnect called" );
282342 ConnectionRequest removeRequest = null ;
283- for (ConnectionRequest request : this .externalConnectionRequestList ) {
343+ for (ConnectionRequest request : this .pendingConnectionRequests ) {
284344 if ( PeerIDHelper .sameID (sourcePeerID , request .sourcePeerID )
285345 && PeerIDHelper .sameID (targetPeerID , request .targetPeerID )) {
286346
@@ -290,7 +350,7 @@ public void disconnect(CharSequence sourcePeerID, CharSequence targetPeerID) thr
290350 }
291351 }
292352
293- if (removeRequest != null ) this .externalConnectionRequestList .remove (removeRequest );
353+ if (removeRequest != null ) this .pendingConnectionRequests .remove (removeRequest );
294354 }
295355
296356 /**
@@ -310,12 +370,21 @@ public void startDataSession(CharSequence sourcePeerID, CharSequence targetPeerI
310370 if (this .localCall (sourcePeerID , targetPeerID ))
311371 throw new ASAPHubException ("a connection started notification cannot come from local peer" );
312372
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" );
315376
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+ }
319388 }
320389
321390 /**
0 commit comments