Skip to content

Commit 668a8f8

Browse files
committed
changed something when working on hub management features in CLI in SharkMessenger. fixed something in encounter manager.
1 parent e401aba commit 668a8f8

File tree

6 files changed

+127
-12
lines changed

6 files changed

+127
-12
lines changed

src/main/java/net/sharksystem/asap/ASAPEncounterManager.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public interface ASAPEncounterManager {
5252
*/
5353
boolean shouldCreateConnectionToPeer(CharSequence addressOrPeerID, ASAPEncounterConnectionType connectionType);
5454

55+
/**
56+
* Encounter manager forgets about its previous encounter. It will re-connect whenever possible.
57+
* Gives peers a chance enforce reconnection if something changed o their side.
58+
*/
59+
void forgetPreviousEncounter();
60+
5561
/**
5662
* A connection to another device was established. This method can be called to handle this new connection.
5763
* It can lead to an immediate shutdown, e.g. the other device already made a successful attempt to connect e.g.

src/main/java/net/sharksystem/asap/ASAPEncounterManagerImpl.java

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap;
22

33
import net.sharksystem.SharkException;
4+
import net.sharksystem.asap.utils.DateTimeHelper;
45
import net.sharksystem.fs.ExtraDataFS;
56
import net.sharksystem.asap.protocol.ASAPConnection;
67
import net.sharksystem.asap.protocol.ASAPConnectionListener;
@@ -14,7 +15,7 @@
1415

1516
public 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

src/main/java/net/sharksystem/asap/engine/ASAPInternalPeerFS.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,10 +708,40 @@ public void putExtra(CharSequence key, byte[] value) throws IOException, SharkEx
708708
this.getExtraData().putExtra(key, value);
709709
}
710710

711+
@Override
712+
public void putExtra(CharSequence key, Integer value) throws IOException, SharkException {
713+
this.getExtraData().putExtra(key, value);
714+
}
715+
716+
@Override
717+
public void putExtra(CharSequence key, String value) throws IOException, SharkException {
718+
this.getExtraData().putExtra(key, value);
719+
}
720+
721+
@Override
722+
public void putExtra(CharSequence key, Set<CharSequence> value) throws IOException, SharkException {
723+
this.getExtraData().putExtra(key, value);
724+
}
725+
711726
public byte[] getExtra(CharSequence key) throws IOException, SharkException {
712727
return this.getExtraData().getExtra(key);
713728
}
714729

730+
@Override
731+
public int getExtraInteger(CharSequence key) throws IOException, SharkException {
732+
return this.getExtraData().getExtraInteger(key);
733+
}
734+
735+
@Override
736+
public Set<CharSequence> getExtraCharSequenceSetParameter(CharSequence key) throws IOException, SharkException {
737+
return this.getExtraData().getExtraCharSequenceSetParameter(key);
738+
}
739+
740+
@Override
741+
public String getExtraString(CharSequence key) throws IOException, SharkException {
742+
return this.getExtraData().getExtraString(key);
743+
}
744+
715745
@Override
716746
public void removeAll() throws IOException, SharkException {
717747
this.getExtraData().removeAll();

src/main/java/net/sharksystem/asap/utils/DateTimeHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,8 @@ public static String long2ExactTimeString(long longtime) {
2929

3030
return df.format(date);
3131
}
32+
33+
public static String nowAsStringMostSpecific() {
34+
return long2ExactTimeString(System.currentTimeMillis());
35+
}
3236
}

src/main/java/net/sharksystem/fs/ExtraData.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@
33
import net.sharksystem.SharkException;
44

55
import java.io.IOException;
6+
import java.util.Set;
67

78
public interface ExtraData {
89
// remember a parameter with a key
910
void putExtra(CharSequence key, byte[] value) throws IOException, SharkException;
11+
12+
void putExtra(CharSequence key, Integer value) throws IOException, SharkException;
13+
14+
void putExtra(CharSequence key, String value) throws IOException, SharkException;
15+
16+
void putExtra(CharSequence key, Set<CharSequence> value) throws IOException, SharkException;
17+
1018
// get a parameter by a key
1119
byte[] getExtra(CharSequence key) throws IOException, SharkException;
20+
21+
int getExtraInteger(CharSequence key) throws IOException, SharkException;
22+
23+
Set<CharSequence> getExtraCharSequenceSetParameter(CharSequence key) throws IOException, SharkException;
24+
25+
String getExtraString(CharSequence key) throws IOException, SharkException;
1226
// remove all data
1327
void removeAll() throws IOException, SharkException;
1428
}

src/main/java/net/sharksystem/fs/ExtraDataFS.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.io.*;
88
import java.util.HashMap;
99
import java.util.Map;
10+
import java.util.Set;
1011

1112
public class ExtraDataFS implements ExtraData {
1213
public static final String EXTRA_FILE_EXTENSION = ".extraData";
@@ -137,6 +138,26 @@ public void putExtra(CharSequence key, byte[] value) throws IOException, SharkEx
137138
this.saveExtraData();
138139
}
139140

141+
@Override
142+
public void putExtra(CharSequence key, Integer value) throws IOException, SharkException {
143+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
144+
ASAPSerialization.writeIntegerParameter(value, baos);
145+
this.putExtra(key, baos.toByteArray());
146+
}
147+
148+
@Override
149+
public void putExtra(CharSequence key, String value) throws IOException, SharkException {
150+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
151+
ASAPSerialization.writeCharSequenceParameter(value, baos);
152+
this.putExtra(key, baos.toByteArray());
153+
}
154+
155+
public void putExtra(CharSequence key, Set<CharSequence> value) throws IOException, SharkException {
156+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
157+
ASAPSerialization.writeCharSequenceSetParameter(value, baos);
158+
this.putExtra(key, baos.toByteArray());
159+
}
160+
140161
/**
141162
* Return a value - can be null if null was set as value.
142163
* @param key
@@ -147,6 +168,23 @@ public byte[] getExtra(CharSequence key) throws IOException, SharkException {
147168
return this.extraData.get(key);
148169
}
149170

171+
@Override
172+
public int getExtraInteger(CharSequence key) throws IOException, SharkException {
173+
ByteArrayInputStream bais = new ByteArrayInputStream(this.getExtra(key));
174+
return ASAPSerialization.readIntegerParameter(bais);
175+
}
176+
177+
@Override
178+
public String getExtraString(CharSequence key) throws IOException, SharkException {
179+
ByteArrayInputStream bais = new ByteArrayInputStream(this.getExtra(key));
180+
return ASAPSerialization.readCharSequenceParameter(bais);
181+
}
182+
183+
public Set<CharSequence> getExtraCharSequenceSetParameter(CharSequence key) throws IOException, SharkException {
184+
ByteArrayInputStream bais = new ByteArrayInputStream(this.getExtra(key));
185+
return ASAPSerialization.readCharSequenceSetParameter(bais);
186+
}
187+
150188
@Override
151189
public void removeAll() {
152190
this.getExtraFile().delete();

0 commit comments

Comments
 (0)