-
Notifications
You must be signed in to change notification settings - Fork 6
EnvironmentChanges
Thomas Schwotzer edited this page Aug 16, 2021
·
5 revisions
We already know the ASAPMessageReceivedListener.
There are other listeners:
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
} // classThis 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.