Skip to content

Commit 4dba8a0

Browse files
committed
better ui support by encounter manager
1 parent 0a8e1d6 commit 4dba8a0

File tree

6 files changed

+70
-7
lines changed

6 files changed

+70
-7
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package net.sharksystem.asap;
22

33
public enum ASAPEncounterConnectionType {
4-
UNKNOWN, AD_HOC_LAYER_2_NETWORK, ASAP_HUB, INTERNET, ONION_NETWORK
4+
UNKNOWN (0), AD_HOC_LAYER_2_NETWORK (1), ASAP_HUB (2), INTERNET (3), ONION_NETWORK (4);
5+
6+
private final int type;
7+
8+
ASAPEncounterConnectionType(int type) {
9+
this.type = type;
10+
}
11+
public String toString() {
12+
switch(this.type) {
13+
case 0: return "unknown";
14+
case 1: return "ad-hoc";
15+
case 2: return "hub";
16+
case 3: return "internet";
17+
case 4: return "onion";
18+
default: return "<there is no type " + this.type;
19+
}
20+
}
521
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public interface ASAPEncounterManagerAdmin {
1919
*/
2020
Set<CharSequence> getConnectedPeerIDs();
2121

22+
/**
23+
*
24+
* @param peerID peerID
25+
* @return connection type of existing connection with given peer
26+
* @throws ASAPException if no connection to that peer exists
27+
*/
28+
ASAPEncounterConnectionType getConnectionType(CharSequence peerID) throws ASAPException;
29+
2230
/**
2331
* Add a peerID to what no connection should be established.
2432
* Note: Adding a peer to the deny list does not necessarily terminate an

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import java.io.*;
1414
import java.util.*;
1515

16-
public class ASAPEncounterManagerImpl implements ASAPEncounterManager, ASAPEncounterManagerAdmin,
17-
ASAPConnectionListener {
16+
public class ASAPEncounterManagerImpl implements
17+
ASAPEncounterManager, ASAPEncounterManagerAdmin, ASAPConnectionListener {
1818
public static final long DEFAULT_WAIT_BEFORE_RECONNECT_TIME = 60000; // 60 seconds == 1 minute
1919
public static final long DEFAULT_WAIT_TO_AVOID_RACE_CONDITION = 500; // milliseconds - worked fine with BT.
2020
public static final String DATASTORAGE_FILE_EXTENSION = "em";
@@ -221,9 +221,6 @@ private void handleEncounter(StreamPair streamPair, ASAPEncounterConnectionType
221221
this.openStreamPairs.put(connectionID, streamPair);
222222
Log.writeLog(this, this.toString(), "remember encounter: " + streamPair.getEndpointID());
223223
this.encounterDate.put(streamPair.getEndpointID(), new Date());
224-
225-
Log.writeLog(this, this.toString(), "going to launch a new asap connection");
226-
227224
try {
228225
Log.writeLog(this, this.toString(), "call asap peer to handle connection");
229226
ASAPConnection asapConnection =
@@ -232,6 +229,8 @@ private void handleEncounter(StreamPair streamPair, ASAPEncounterConnectionType
232229

233230
asapConnection.addASAPConnectionListener(this);
234231

232+
Log.writeLog(this, this.toString(),
233+
"asap peers is handling session: " + asapConnection.toString());
235234
this.openASAPConnections.put(asapConnection, connectionID);
236235

237236
} catch (IOException | ASAPException e) {
@@ -315,7 +314,7 @@ private boolean solveRaceCondition(StreamPair streamPair, boolean connectionInit
315314
@Override
316315
public synchronized void asapConnectionStarted(String remotePeerName, ASAPConnection connection) {
317316
CharSequence peerID = connection.getEncounteredPeer();
318-
Log.writeLog(this, this.toString(), "new ASAP session started with peerID " + peerID);
317+
Log.writeLog(this, this.toString(), "new ASAP session: " + connection);
319318

320319
CharSequence streamPairID = this.openASAPConnections.get(connection);
321320
if(PeerIDHelper.sameID(streamPairID, peerID)) {
@@ -336,6 +335,7 @@ public synchronized void asapConnectionStarted(String remotePeerName, ASAPConnec
336335

337336
@Override
338337
public synchronized void asapConnectionTerminated(Exception terminatingException, ASAPConnection connection) {
338+
Log.writeLog(this, this.toString(), "terminated: " + connection);
339339
CharSequence peerID = connection.getEncounteredPeer();
340340

341341
CharSequence peerIDOrAddress = this.openASAPConnections.get(connection);
@@ -434,6 +434,17 @@ public Set<CharSequence> getDenyList() {
434434
public Set<CharSequence> getConnectedPeerIDs() {
435435
return this.openStreamPairs.keySet();
436436
}
437+
438+
public ASAPEncounterConnectionType getConnectionType(CharSequence peerID) throws ASAPException {
439+
for(ASAPConnection connection : this.openASAPConnections.keySet()) {
440+
if(PeerIDHelper.sameID(connection.getEncounteredPeer(), peerID)) {
441+
// found our connection
442+
return connection.getASAPEncounterConnectionType();
443+
}
444+
}
445+
throw new ASAPException("there is no connection to peer " + peerID);
446+
}
447+
437448
@Override
438449
public void cancelConnection(CharSequence peerID) {
439450
StreamPair stream2Close = this.openStreamPairs.get(peerID);

src/main/java/net/sharksystem/asap/apps/testsupport/TestASAPConnectionHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ public boolean isSigned() {
6767
return false;
6868
}
6969

70+
@Override
71+
public ASAPEncounterConnectionType getASAPEncounterConnectionType() {
72+
return ASAPEncounterConnectionType.UNKNOWN;
73+
}
74+
7075
@Override
7176
public void kill() {
7277

src/main/java/net/sharksystem/asap/protocol/ASAPConnection.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.sharksystem.asap.protocol;
22

3+
import net.sharksystem.asap.ASAPEncounterConnectionType;
4+
35
public interface ASAPConnection {
46
CharSequence getEncounteredPeer();
57

@@ -12,6 +14,8 @@ public interface ASAPConnection {
1214

1315
boolean isSigned();
1416

17+
ASAPEncounterConnectionType getASAPEncounterConnectionType();
18+
1519
// terminate that connection - does not effect the underlying connections established e.g. with Bluetooth
1620
void kill();
1721
}

src/main/java/net/sharksystem/asap/protocol/ASAPSessionImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ public boolean isSigned() {
9898
return false;
9999
}
100100

101+
@Override
102+
public ASAPEncounterConnectionType getASAPEncounterConnectionType() {
103+
return this.connectionType;
104+
}
105+
101106
@Override
102107
public void kill() {
103108
this.kill(new ASAPException("kill called from outside asap connection"));
@@ -533,4 +538,18 @@ public void run() {
533538
}
534539
}
535540
}
541+
public String toString() {
542+
StringBuilder sb = new StringBuilder();
543+
sb.append(this.asapInternalPeer.getOwner());
544+
sb.append(" <--> ");
545+
if(this.encounteredPeer == null) sb.append("<unkown yet>");
546+
else sb.append(this.encounteredPeer);
547+
sb.append(" | type: ");
548+
sb.append(this.connectionType);
549+
sb.append(" | encrypted: ");
550+
sb.append(this.encrypt);
551+
sb.append(" | sign: ");
552+
sb.append(this.sign);
553+
return sb.toString();
554+
}
536555
}

0 commit comments

Comments
 (0)