Skip to content

EncounterManager

Thomas Schwotzer edited this page Aug 20, 2021 · 27 revisions

We expect to have more than one point-to-point connection type in a real environment. Android phones support Bluetooth, Wifi-direct but also Internet access via the mobile network. We also work with Long Range Wifi (Lora) modules that allows setting up your own urban wide mobile decentralized ASAP P2P system.

Each protocol type is management by its own software module. We have e.g. a BluetoothEngine in our ASAPAndroid lib.

There are at least two interesting tasks to solve in such a multi (ad hoc) protocol environment. Peers can connect over more than one point-to-point connection. It is a wast of resource to have two identical data exchanges over different protocols.

The second problem comes with ad hoc networks. Let’s call it flickering. Ad hoc connection have a certain communication range, like about 10 meters with Bluetooth. The range is highly sensible to the actual environment. That is not a problem for us.

Peers who are at the edge of this communication range could have the tendency to establish and loos the connection. An established connection would be basis of an ASAP encounter. But what, if this connection breaks down and is re-established a second later? We are talking about cellphones with limited battery power. We have to avoid this flickering effect.

We tackled those problems with the ASAPEncounterManager.

Do not use a ConnectionHandler directly in your code but a single instance of EncounterManager.

void handleEncounter(StreamPair streamPair, EncounterConnectionType connectionType) throws IOException;

Call this method once a point-to-point connection is created. Hint about our StreamPair can be found here. Your environment must have only on instance (see also singleton pattern). In that case, it keeps track of each encounter regardless used point-to-point protocol.

A call on handleConnection would never be refused, though. If you want an encounter - you get it. It is better to call another method before.

boolean shouldCreateConnectionToPeer(CharSequence addressOrPeerID, EncounterConnectionType connectionType);

This methods helps to avoid the flickering. Call this method before launching an encounter and if you already have got a peer id. Call handleEncounterif it return true.

There is another very interesting problem. It comes up sporadic but to often to ignore it. It took more than a day to understand this race condition and another one to solve it. We discusses that issue in the java doc in a variant of handleEncounter.

EncounterManager implementation

There is a single implementation and there are no plans to implement another one: ASAPEncounterManagerImpl

It requires an ConnectionHandler. How can you get one? Learn about ConnectionHandler.