Skip to content

EnvironmentChanges

Thomas Schwotzer edited this page Jan 18, 2021 · 5 revisions

Environment

Environment can be a very different thing. It depends in what hardware an app runs and in what context it is used. There is just one thing we can sum up under environment in this most general ASAP project: Is there a peer encounter or not.

We choose to use a slightly different terminology and call it online peers. From that perspective, there is an online peer list which changes whenever a connection can be established or an existing one gets lost.

There is a listener for this.

ASAPEnvironmentChangesListener

The ASAPEnvironmentChangesListener interface has got a single method.

void onlinePeersChanged(Set<CharSequence> peerList);

Applications interested in those changes have to implement and register a listener.

DiscoverPeerAndStartEGChat implements this usage in an example.

private class YourApplication implements ASAPEnvironmentChangesListener /*, .. */ {
private Set<CharSequence> knowPeers = new HashSet<>();

@Override
public void onlinePeersChanged(Set<CharSequence> peerList) {
    // peer list has changed - maybe there is a new peer around
    for(CharSequence maybeNewPeerName : peerList) {
        CharSequence newPeerName = maybeNewPeerName;
        for (CharSequence peerName : this.knowPeers) {
            if(maybeNewPeerName.toString().equalsIgnoreCase(peerName.toString())) {
                newPeerName = null; // not new
                break; // found in my known peers list, try next in peerList
             }
         }
         if(newPeerName != null) {
             // found one - enough for this example
             this.doSomethingWith(newPeerName); // example
             break;
          }
    }
} // method
} // class

This class keeps a list of peers as member (knowPeers ). The listener method is called whenever a change of connections occurs. The algorithm looks for a peer that is not known yet. A method is performed for the first not yet known peer (doSomething()). A message is sent in this example.

Let the complete example run.

A listener is registered with a ASAPPeer.

private class YourApplication implements ASAPEnvironmentChangesListener /*, .. */ {
...
    ASAPPeer peer = ..;
    // object itself implements this listener interface.
    peer.addASAPEnvironmentChangesListener(this);

The object implements the interface itself in this case.

Clone this wiki locally