-
Notifications
You must be signed in to change notification settings - Fork 6
ConnectionHandler
There is a lot talking about our ASAP encounter. It is quite prosaic on developer level. There is an interface ASAPConnectionHandler that defines variants that defines variants of a single method. Here is the full version. Variants just use defaults.
public interface ASAPConnectionHandler {
...
ASAPConnection handleConnection(
InputStream is, OutputStream os, // streams for our point-to-point connection
boolean encrypt, boolean sign, // security setting point-to-point connection
EncounterConnectionType connectionType, // what kind of point-to-point is it?
Set<CharSequence> appsWhiteList,
Set<CharSequence> appsBlackList // define black/white list of peers
) throws IOException, ASAPException;This interface does not explain how to create a point-to-point connection. It can be used if such a connection was created.
There other libs, like [ASAPAndroid] how deal with establishing a point-to-point connection. In the end, a pair of streams is created (input and output stream). Anybody who created such a connection is very aware of the kind of connection. Take one from our list, defined in EncounterConnectionType.
Point-to-point security can be switched on or off with two flags (encrypt, sign). encrypt == true would encrypt any message send over this connection. Note, encryption requires a public key of message receiver on senders’ side. Nothing is sent if there is no such key. That is a good thing. If security is important you should take it seriously.
Signing only requires access to a local ASAPKeyStore that provides local peers’ private key.
What class implements that interface? ASAPPeerFS does. We use it in our tests, e.g. here. Actually, we use a subclass ASAPTestPeerFS that makes testing easier.
It provides two additional methods: startEncounter and stopEncounter. Check out the code. It has less than 100 lines. Finally, there is the connection establishment:
private void startSession() {
new Thread(new Runnable() {
@Override
public void run() {
try {
ASAPTestPeerFS.this.handleConnection(
ASAPTestPeerFS.this.socket.getInputStream(),
ASAPTestPeerFS.this.socket.getOutputStream(),
EncounterConnectionType.INTERNET);
} catch (IOException | ASAPException e) {
ASAPTestPeerFS.this.log("fatal while connecting: " + e.getLocalizedMessage());
}
}
}).start();
}We put it into a thread because this code is called within a test environment. We do not want to make the test thread stop because it was captured by our handleConnection algorithm. In more complex environments, we do not call this method directly but use the EncounterManager instead.