77import android .content .Intent ;
88import android .content .pm .PackageManager ;
99import android .os .IBinder ;
10+ import android .os .Message ;
1011import android .os .Messenger ;
1112import androidx .core .content .ContextCompat ;
1213import android .util .Log ;
2829import net .sharksystem .asap .android .wifidirect .WifiP2PEngine ;
2930import net .sharksystem .asap .engine .ASAPChunkReceivedListener ;
3031import net .sharksystem .asap .utils .Helper ;
32+ import net .sharksystem .asap .utils .PeerIDHelper ;
33+ import net .sharksystem .hub .ASAPHubException ;
3134import net .sharksystem .hub .peerside .ASAPHubManager ;
3235import net .sharksystem .hub .peerside .ASAPHubManagerImpl ;
36+ import net .sharksystem .hub .peerside .HubConnector ;
37+ import net .sharksystem .hub .peerside .HubConnectorFactory ;
38+ import net .sharksystem .hub .peerside .HubConnectorStatusListener ;
39+ import net .sharksystem .hub .peerside .NewConnectionListener ;
40+ import net .sharksystem .utils .Utils ;
3341
3442import java .io .File ;
3543import java .io .IOException ;
3644import java .util .ArrayList ;
45+ import java .util .Collection ;
46+ import java .util .HashMap ;
3747import java .util .List ;
48+ import java .util .Map ;
3849import java .util .Set ;
3950
4051/**
@@ -57,7 +68,7 @@ public class ASAPService extends Service
5768 private boolean onlineExchange ;
5869 private long maxExecutionTime ;
5970 private ArrayList <CharSequence > supportedFormats ;
60- private ASAPHubManager asapASAPHubManager ;
71+ private ASAPHubManagerImpl asapASAPHubManager ;
6172
6273 String getASAPRootFolderName () {
6374 return this .asapEngineRootFolderName ;
@@ -138,6 +149,7 @@ public ASAPEncounterManager getASAPEncounterManager() {
138149 public ASAPHubManager getASAPHubManager () {
139150 if (this .asapASAPHubManager == null ) {
140151 this .asapASAPHubManager = new ASAPHubManagerImpl (this .getASAPEncounterManager ());
152+ new Thread (this .asapASAPHubManager ).start ();
141153 }
142154
143155 return this .asapASAPHubManager ;
@@ -342,6 +354,51 @@ void stopLoRa() {
342354 }
343355 }
344356
357+ //////////////////////////////////////////////////////////////////////////////////////
358+ // ASAP hub management //
359+ //////////////////////////////////////////////////////////////////////////////////////
360+
361+ private Map <byte [], HubConnector > connectedHubs = new HashMap <>();
362+
363+ private byte [] getHubDescriptionObject (byte [] sameButNotIdentical ) {
364+ for (byte [] key : this .connectedHubs .keySet ()) {
365+ if (Utils .compareArrays (key , sameButNotIdentical )) return key ;
366+ }
367+
368+ return null ;
369+ }
370+
371+ void connectHub (byte [] connectorDescription ) {
372+ // create thread (network activity) - add to hub encounter manager.
373+ new Thread (new Runnable () {
374+ @ Override
375+ public void run () {
376+ try {
377+ HubConnector hubConnector =
378+ HubConnectorFactory .createHubConnectorByDescription (connectorDescription );
379+ ASAPService .this .connectedHubs .put (connectorDescription , hubConnector );
380+ ASAPService .this .getASAPHubManager ().addHub (hubConnector );
381+
382+ } catch (IOException | ASAPException e ) {
383+ Log .e (ASAPService .this .getLogStart (), e .getLocalizedMessage ());
384+ }
385+
386+ }
387+ }).start ();
388+ }
389+
390+ void disconnectHub (byte [] connectorDescription ) {
391+ connectorDescription = this .getHubDescriptionObject (connectorDescription );
392+ if (connectorDescription != null ) {
393+ HubConnector connector2Remove = this .connectedHubs .remove (connectorDescription );
394+ if (connector2Remove == null ) {
395+ Log .d (this .getLogStart (), "cannot disconnect from hub - entry not found" );
396+ } else {
397+ this .getASAPHubManager ().removeHub (connector2Remove );
398+ }
399+ }
400+ }
401+
345402 //////////////////////////////////////////////////////////////////////////////////////
346403 // status management //
347404 //////////////////////////////////////////////////////////////////////////////////////
0 commit comments