Skip to content

Commit 15922c0

Browse files
committed
hub support integrated
1 parent 0c4d773 commit 15922c0

File tree

12 files changed

+220
-92
lines changed

12 files changed

+220
-92
lines changed

app/build.gradle

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ https://developer.android.com/studio/projects/android-library
33
*/
44

55
// choose one line either for an app or a lib
6-
apply plugin: 'com.android.application'
7-
//apply plugin: 'com.android.library'
6+
//apply plugin: 'com.android.application'
7+
apply plugin: 'com.android.library'
88

99
android {
1010
compileSdkVersion 30
1111
defaultConfig {
1212
// we produce a library by commenting out that line:
13-
applicationId "net.sharksystem.asap.example"
13+
//applicationId "net.sharksystem.asap.example"
1414
minSdkVersion 23
1515
targetSdkVersion 30
1616
versionCode 1
@@ -28,10 +28,11 @@ android {
2828
dependencies {
2929
//implementation fileTree(include: ['*.jar'], dir: 'libs')
3030

31-
implementation 'androidx.appcompat:appcompat:1.2.0'
31+
implementation 'androidx.appcompat:appcompat:1.3.1'
3232
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3333
implementation files('libs\\ASAPJava.jar')
3434
implementation files('libs\\ASAPHub.jar')
35+
implementation files('libs\\SharkPeer.jar')
3536
testImplementation 'junit:junit:4.13.1'
3637
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3738
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

app/src/main/java/net/sharksystem/asap/android/ASAPServiceMethods.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public class ASAPServiceMethods {
3333
public static final int ASK_PROTOCOL_STATUS = 202;
3434

3535
///////////// hub management
36-
public static final int CONNECT_HUB = 300;
37-
public static final int DISCONNECT_HUB = 301;
36+
public static final int CONNECT_ASAP_HUBS = 300;
37+
public static final int DISCONNECT_ASAP_HUBS = 301;
3838

3939
// tags for putting extra data to messages or broadcasts
4040
public static final String URI_TAG = "ASAP_MESSAGE_URI";

app/src/main/java/net/sharksystem/asap/android/app2serviceMessaging/MessageFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class MessageFactory {
1010

1111
/////////////////// ASAP hub management
1212
public static Message createConnectHubMessage(byte[] hubDescription) {
13-
Message msg = Message.obtain(null, ASAPServiceMethods.CONNECT_HUB, 0, 0);
13+
Message msg = Message.obtain(null, ASAPServiceMethods.CONNECT_ASAP_HUBS, 0, 0);
1414
return addHubDescription(msg, hubDescription);
1515
}
1616

1717
public static Message createDisconnectHubMessage(byte[] hubDescription) {
18-
Message msg = Message.obtain(null, ASAPServiceMethods.DISCONNECT_HUB, 0, 0);
18+
Message msg = Message.obtain(null, ASAPServiceMethods.DISCONNECT_ASAP_HUBS, 0, 0);
1919
return addHubDescription(msg, hubDescription);
2020
}
2121

app/src/main/java/net/sharksystem/asap/android/apps/ASAPActivity.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -220,48 +220,6 @@ protected String getLogStart() {
220220
return this.getClass().getSimpleName();
221221
}
222222

223-
/////////////////////////////////////////////////////////////////////////////////////
224-
// ASAP hub management //
225-
/////////////////////////////////////////////////////////////////////////////////////
226-
227-
/**
228-
* Call this message to connect to a hub via tcp
229-
*/
230-
public void connectTCPHub(CharSequence hostName, int port) {
231-
Log.d(this.getLogStart(), "send message to service: connect hub via tcp: "
232-
+ hostName + ":" + port);
233-
234-
try {
235-
Message connectHubMessage = MessageFactory.createConnectHubMessage(
236-
HubConnectorFactory.createTCPConnectorDescription(hostName, port));
237-
238-
this.sendMessage2Service(connectHubMessage);
239-
240-
} catch (IOException e) {
241-
Log.e(this.getLogStart(), "cannot create hub connect message: "
242-
+ e.getLocalizedMessage());
243-
}
244-
}
245-
246-
/**
247-
* Call this message to disconnect to a hub via tcp
248-
*/
249-
public void disconnectTCPHub(CharSequence hostName, int port) {
250-
Log.d(this.getLogStart(), "send message to service: disconnect from hub via tcp: "
251-
+ hostName + ":" + port);
252-
253-
try {
254-
Message connectHubMessage = MessageFactory.createDisconnectHubMessage(
255-
HubConnectorFactory.createTCPConnectorDescription(hostName, port));
256-
257-
this.sendMessage2Service(connectHubMessage);
258-
259-
} catch (IOException e) {
260-
Log.e(this.getLogStart(), "cannot create hub disconnect message: "
261-
+ e.getLocalizedMessage());
262-
}
263-
}
264-
265223
/////////////////////////////////////////////////////////////////////////////////////
266224
// mac protocol stuff //
267225
/////////////////////////////////////////////////////////////////////////////////////
@@ -343,6 +301,31 @@ public void startBluetoothDiscovery() {
343301
this.sendMessage2Service(ASAPServiceMethods.START_BLUETOOTH_DISCOVERY);
344302
}
345303

304+
/////////////////////////////////////////////////////////////////////////////////////
305+
// ASAP hub management //
306+
/////////////////////////////////////////////////////////////////////////////////////
307+
308+
/**
309+
* Call this message to connect all known hubs and run encounter attempts in regular intervals.
310+
*/
311+
public void connectASAPHubs() {
312+
Log.d(this.getLogStart(), "send message to service: start ASAP hubs");
313+
this.sendMessage2Service(ASAPServiceMethods.CONNECT_ASAP_HUBS);
314+
}
315+
316+
/**
317+
* Call this message to connect cancel all hub connections. Your app will be back in
318+
* Ad-hoc communication only mode.
319+
*/
320+
public void disconnectASAPHubs() {
321+
Log.d(this.getLogStart(), "send message to service: stop ASAP hubs");
322+
this.sendMessage2Service(ASAPServiceMethods.DISCONNECT_ASAP_HUBS);
323+
}
324+
325+
/////////////////////////////////////////////////////////////////////////////////////
326+
// manage broadcast with service //
327+
/////////////////////////////////////////////////////////////////////////////////////
328+
346329
/*
347330
There is a race condition:
348331
ASAPActivity a can launch ASAPActivity b. It happens (quite often actually) that a.onStop()
@@ -632,6 +615,16 @@ public void asapNotifyOnlinePeersChanged(Set<CharSequence> peerList) {
632615
this.getASAPAndroidPeer().notifyOnlinePeersChanged(peerList);
633616
}
634617

618+
@Override
619+
public void asapNotifyHubsConnected() {
620+
this.getASAPAndroidPeer().notifyASAPHubsConnected(true);
621+
}
622+
623+
@Override
624+
public void asapNotifyHubsDisconnected() {
625+
this.getASAPAndroidPeer().notifyASAPHubsConnected(false);
626+
}
627+
635628
/**
636629
* Application developers can use this method like life-cycle methods in Android
637630
* (onStart() etc.). Overwrite this method to get informed about changes in environment.
@@ -684,4 +677,6 @@ public boolean isBluetoothDiscoverable() {
684677
public boolean isBluetoothDiscovery() {
685678
return this.getASAPAndroidPeer().getBTDiscovery();
686679
}
680+
681+
public boolean isASAPHubsConnected() { return this.getASAPAndroidPeer().getASAPHubsConnected(); }
687682
}

app/src/main/java/net/sharksystem/asap/android/apps/ASAPAndroidPeer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class ASAPAndroidPeer extends BroadcastReceiver implements ASAPPeer {
6060
private boolean btDisoverableOn = false;
6161
private boolean btDisoveryOn = false;
6262
private boolean btEnvironmentOn = false;
63+
private boolean asapHubsConnected = false;
6364

6465
private Activity activity;
6566

@@ -533,6 +534,14 @@ public boolean getBTDiscovery() {
533534
return this.btDisoveryOn;
534535
}
535536

537+
public boolean getASAPHubsConnected() {
538+
return this.asapHubsConnected;
539+
}
540+
541+
public void notifyASAPHubsConnected(boolean connected) {
542+
this.asapHubsConnected = connected;
543+
}
544+
536545
public void notifyOnlinePeersChanged(Set<CharSequence> newPeerList) {
537546
this.getASAPPeerApplicationSide().notifyOnlinePeersChanged(newPeerList);
538547

app/src/main/java/net/sharksystem/asap/android/example/ASAPExampleHubManagementActivity.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
package net.sharksystem.asap.android.example;
22

33
import android.os.Bundle;
4+
import android.util.Log;
45
import android.view.View;
56
import android.widget.EditText;
7+
import android.widget.Toast;
68

9+
import net.sharksystem.SharkPeerHubSupport;
10+
import net.sharksystem.SharkPeerHubSupportImpl;
711
import net.sharksystem.asap.android.R;
812
import net.sharksystem.asap.android.apps.ASAPActivity;
13+
import net.sharksystem.hub.peerside.TCPHubConnectorDescription;
14+
15+
import java.io.IOException;
916

1017
public class ASAPExampleHubManagementActivity extends ASAPActivity {
1118
@Override
@@ -23,10 +30,19 @@ private void hubAction(boolean on) {
2330

2431
int port = Integer.parseInt(portString);
2532

33+
SharkPeerHubSupport sharkPeerSettings = new SharkPeerHubSupportImpl(this.getASAPPeer());
34+
try {
35+
sharkPeerSettings.addHubDescription(new TCPHubConnectorDescription(hostName, port));
36+
} catch (IOException e) {
37+
String s = "cannot create hub description: " + hostName + ":" + port;
38+
Log.w(this.getLogStart(), s);
39+
Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
40+
}
41+
2642
if(on) {
27-
this.connectTCPHub(hostName, port);
43+
this.connectASAPHubs();
2844
} else {
29-
this.disconnectTCPHub(hostName, port);
45+
this.disconnectASAPHubs();
3046
}
3147
}
3248

app/src/main/java/net/sharksystem/asap/android/service/ASAPMessageHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ public void handleMessage(Message msg) {
9191
this.asapService.stopLoRa();
9292
break;
9393

94-
case ASAPServiceMethods.CONNECT_HUB:
95-
this.asapService.connectHub(this.getHubDescription(msg));
94+
case ASAPServiceMethods.CONNECT_ASAP_HUBS:
95+
this.asapService.connectASAPHubs();
9696
break;
9797

98-
case ASAPServiceMethods.DISCONNECT_HUB:
99-
this.asapService.disconnectHub(this.getHubDescription(msg));
98+
case ASAPServiceMethods.DISCONNECT_ASAP_HUBS:
99+
this.asapService.disconnectASAPHubs();
100100
break;
101101

102102
default:

0 commit comments

Comments
 (0)