3030import java .io .IOException ;
3131import java .util .ArrayList ;
3232import java .util .List ;
33+ import java .util .Set ;
3334
3435/**
3536 * Service that searches for and creates layer 2 point-to-point connections
@@ -46,7 +47,7 @@ public class ASAPService extends Service implements ASAPChunkReceivedListener,
4647 private String asapEngineRootFolderName ;
4748
4849 //private asapMultiEngine asapMultiEngine = null;
49- private ASAPPeer asapMultiEngine ;
50+ private ASAPPeer asapPeer ;
5051 private ASAPOnlineMessageSender asapOnlineMessageSender ;
5152 private CharSequence owner ;
5253 private CharSequence rootFolder ;
@@ -60,7 +61,7 @@ String getASAPRootFolderName() {
6061
6162 public ASAPPeer getASAPPeer () {
6263 Log .d (this .getLogStart (), "asap peer is a singleton." );
63- if (this .asapMultiEngine == null ) {
64+ if (this .asapPeer == null ) {
6465 Log .d (this .getLogStart (), "going to set up asapPeer" );
6566
6667 // check write permissions
@@ -84,18 +85,18 @@ public ASAPPeer getASAPPeer() {
8485 Log .d (this .getLogStart (),"done creating root folder" );
8586 }
8687
87- this .asapMultiEngine = ASAPPeerFS .createASAPPeer (
88+ this .asapPeer = ASAPPeerFS .createASAPPeer (
8889 this .owner , this .asapEngineRootFolderName ,
8990 this .maxExecutionTime , this .supportedFormats , this );
9091
9192 Log .d (this .getLogStart (),"engines created" );
9293
9394 // listener for radar app
94- this .asapMultiEngine .addOnlinePeersChangedListener (this );
95+ this .asapPeer .addOnlinePeersChangedListener (this );
9596 Log .d (this .getLogStart (),"added online peer changed listener" );
9697
9798 // add online feature to each engine
98- this .asapMultiEngine .activateOnlineMessages ();
99+ this .asapPeer .activateOnlineMessages ();
99100 Log .d (this .getLogStart (),"online messages activated for ALL asap engines" );
100101
101102 } catch (IOException e ) {
@@ -111,7 +112,7 @@ public ASAPPeer getASAPPeer() {
111112 Log .d (this .getLogStart (), "peer was already created" );
112113 }
113114
114- return this .asapMultiEngine ;
115+ return this .asapPeer ;
115116 }
116117
117118 //////////////////////////////////////////////////////////////////////////////////////
@@ -396,12 +397,12 @@ public void pauseBroadcasts() {
396397
397398 public ASAPOnlineMessageSender getASAPOnlineMessageSender () throws ASAPException {
398399 if (this .asapOnlineMessageSender == null ) {
399- if (this .asapMultiEngine == null ) {
400+ if (this .asapPeer == null ) {
400401 throw new ASAPException ("asap engine not initialized" );
401402 }
402403
403404 this .asapOnlineMessageSender =
404- new ASAPOnlineMessageSenderEngineSide (this .asapMultiEngine );
405+ new ASAPOnlineMessageSenderEngineSide (this .asapPeer );
405406 }
406407
407408 return this .asapOnlineMessageSender ;
@@ -411,6 +412,8 @@ public ASAPOnlineMessageSender getASAPOnlineMessageSender() throws ASAPException
411412 // ASAPOnlinePeersChangedListener //
412413 //////////////////////////////////////////////////////////////////////////////////
413414
415+ private int numberOnlinePeers = 0 ;
416+
414417 @ Override
415418 public void onlinePeersChanged (ASAPPeer asapPeer ) {
416419 Log .d (this .getLogStart (), "onlinePeersChanged" );
@@ -428,6 +431,20 @@ public void onlinePeersChanged(ASAPPeer asapPeer) {
428431 this .sendBroadcast (intent );
429432
430433 this .checkLayer2ConnectionStatus ();
434+
435+ // force reconnection - to re-establish a accidentally broken connection
436+ int onlinePeerPreviously = this .numberOnlinePeers ;
437+ Log .d (this .getLogStart (), "formed number of online peers: " + onlinePeerPreviously );
438+ Set <CharSequence > onlinePeers = this .asapPeer .getOnlinePeers ();
439+ if (onlinePeers != null ) {
440+ this .numberOnlinePeers = onlinePeers .size ();
441+ Log .d (this .getLogStart (), "current number of online peers: "
442+ + this .numberOnlinePeers );
443+
444+ if (this .numberOnlinePeers < onlinePeerPreviously ) {
445+ this .startReconnectPairedDevices ();
446+ }
447+ }
431448 }
432449
433450 private String getLogStart () {
0 commit comments