-
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 actual defines a single method, see below. All other methods are just variantes using default values for some parameter.
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 is to be used after such a connection was created.
There are other libs, like ASAPAndroid who deal with establishing a point-to-point connection.
In the end, a pair of streams (input and output stream) is created. Anybody who creates a connection is aware of its kind (Bluetooth etc.) Take one of this list from 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.