11package net .sharksystem .asap ;
22
33import net .sharksystem .SharkException ;
4+ import net .sharksystem .asap .utils .DateTimeHelper ;
45import net .sharksystem .fs .ExtraDataFS ;
56import net .sharksystem .asap .protocol .ASAPConnection ;
67import net .sharksystem .asap .protocol .ASAPConnectionListener ;
1415
1516public class ASAPEncounterManagerImpl implements ASAPEncounterManager , ASAPEncounterManagerAdmin ,
1617 ASAPConnectionListener {
17- public static final long DEFAULT_WAIT_BEFORE_RECONNECT_TIME = 1000 ; // a second - debugging
18+ public static final long DEFAULT_WAIT_BEFORE_RECONNECT_TIME = 60000 ; // 60 seconds == 1 minute
1819 public static final long DEFAULT_WAIT_TO_AVOID_RACE_CONDITION = 500 ; // milliseconds - worked fine with BT.
1920 public static final String DATASTORAGE_FILE_EXTENSION = "em" ;
2021 private static final CharSequence ENCOUNTER_MANAGER_DENY_LIST_KEY = "denylist" ;
@@ -75,28 +76,41 @@ private boolean coolDownOver(CharSequence id, ASAPEncounterConnectionType connec
7576 Date lastEncounter = this .encounterDate .get (id );
7677
7778 if (lastEncounter == null ) {
78- Log .writeLog (this , this .toString (), "device/peer not in encounteredDevices" );
79- this .encounterDate .put (id , now );
79+ Log .writeLog (this , this .toString (),
80+ "device/peer not in encounteredDevices - add "
81+ + DateTimeHelper .long2ExactTimeString (now .getTime ()));
82+ // this.encounterDate.put(id, now); do not enter it into that list before (!!) a connection is established
8083 return true ;
8184 }
8285
8386 // calculate reconnection time
87+ Log .writeLog (this , this .toString (), "this.waitBeforeReconnect == " + this .waitBeforeReconnect );
8488
8589 // get current time, in its incarnation as date
86- long nowInMillis = System . currentTimeMillis ();
87- long reconnectedBeforeInMillis = nowInMillis - this .waitBeforeReconnect ;
88- Date reconnectBefore = new Date (reconnectedBeforeInMillis );
90+ long nowInMillis = now . getTime ();
91+ long shouldReconnectIfBeforeInMillis = nowInMillis - this .waitBeforeReconnect ;
92+ Date shouldReconnectIfBefore = new Date (shouldReconnectIfBeforeInMillis );
8993
90- Log .writeLog (this , this .toString (), "now: " + now .toString ());
91- Log .writeLog (this , this .toString (), "connectBefore: " + reconnectBefore .toString ());
94+ StringBuilder sb = new StringBuilder ();
95+ sb .append ("\n " );
96+ sb .append (DateTimeHelper .long2ExactTimeString (nowInMillis ));
97+ sb .append (" == now\n " );
98+ sb .append (DateTimeHelper .long2ExactTimeString (shouldReconnectIfBeforeInMillis ));
99+ sb .append (" == should reconnect if met before that moment\n " );
100+ sb .append (DateTimeHelper .long2ExactTimeString (lastEncounter .getTime ()));
101+ sb .append (" == last encounter" );
102+ Log .writeLog (this , this .toString (), sb .toString ());
92103
93104 // known peer
94105 Log .writeLog (this , this .toString (), "device/peer (" + id + ") in encounteredDevices list?" );
95106 // it was in the list
96- if (lastEncounter .before (reconnectBefore )) {
107+ if (lastEncounter .before (shouldReconnectIfBefore )) {
97108 Log .writeLog (this , this .toString (), "yes - should connect: " + id );
98109 // remember that and overwrite previous entry
99- this .encounterDate .put (id , now );
110+ /* that was a nasty bug since this method is called before establishing a connection
111+ * AND after connection establishment and before lauching an ASAP session
112+ */
113+ // this.encounterDate.put(id, now);
100114 return true ;
101115 }
102116
@@ -116,9 +130,11 @@ public boolean shouldCreateConnectionToPeer(CharSequence remoteAdressOrPeerID,
116130 StreamPair streamPair = this .openStreamPairs .get (remoteAdressOrPeerID );
117131 if (streamPair != null ) {
118132 return false ;
133+ } else {
134+ Log .writeLog (this , this .toString (), remoteAdressOrPeerID + " no parallel open connection" );
119135 }
120136
121- // we know this peer and we are still in cool down period
137+ // we know this peer and it is still in cool down period
122138 if (!this .coolDownOver (remoteAdressOrPeerID , connectionType )) return false ;
123139
124140 // is this id a remote adress?
@@ -141,6 +157,11 @@ public boolean shouldCreateConnectionToPeer(CharSequence remoteAdressOrPeerID,
141157 return true ;
142158 }
143159
160+ @ Override
161+ public void forgetPreviousEncounter () {
162+ this .encounterDate = new HashMap <>();
163+ }
164+
144165 @ Override
145166 public void handleEncounter (StreamPair streamPair , ASAPEncounterConnectionType connectionType ) throws IOException {
146167 this .handleEncounter (streamPair , connectionType , false , false );
@@ -194,9 +215,11 @@ private void handleEncounter(StreamPair streamPair, ASAPEncounterConnectionType
194215 }
195216 }
196217
197- // we a through with it - remember that new stream pair
218+ // we are through with it - remember that new stream pair
198219 Log .writeLog (this , this .toString (), "remember streamPair: " + streamPair );
199220 this .openStreamPairs .put (connectionID , streamPair );
221+ Log .writeLog (this , this .toString (), "remember encounter: " + streamPair .getEndpointID ());
222+ this .encounterDate .put (streamPair .getEndpointID (), new Date ());
200223
201224 Log .writeLog (this , this .toString (), "going to launch a new asap connection" );
202225
0 commit comments