Skip to content

Commit 6b43834

Browse files
committed
Hub integrated
1 parent c5009d8 commit 6b43834

File tree

12 files changed

+265
-12
lines changed

12 files changed

+265
-12
lines changed

app/build.gradle

Lines changed: 3 additions & 3 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

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</activity>
2020
<activity android:name="net.sharksystem.asap.android.example.ASAPExampleActivity"/>
2121
<activity android:name="net.sharksystem.asap.android.example.ASAPExampleMessagingActivity"/>
22+
<activity android:name="net.sharksystem.asap.android.example.ASAPExampleHubManagementActivity"/>
2223

2324
<service
2425
android:name="net.sharksystem.asap.android.service.ASAPService"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
public class ASAPServiceMessage {
1818
private static final CharSequence DEFAULT_CLOSED_MAKAN_NAME = "closed makan";
19+
public static final String HUB_CONNECTOR_DESCRIPTION_TAG = "hubDescription";
20+
1921
private int messageNumber;
2022
private String format = null;
2123
private String uri = null;

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ASAPServiceMethods {
1111
/** mandatory: format, uri, recipients */
1212
public static final int CREATE_CLOSED_CHANNEL = 1;
1313

14-
///////////// layer 2 protocol management
14+
///////////// layer 2 / point-to-point protocol management
1515
public static final int START_WIFI_DIRECT = 100;
1616
public static final int STOP_WIFI_DIRECT = 101;
1717

@@ -32,6 +32,10 @@ public class ASAPServiceMethods {
3232
public static final int STOP_BROADCASTS = 201;
3333
public static final int ASK_PROTOCOL_STATUS = 202;
3434

35+
///////////// hub management
36+
public static final int CONNECT_HUB = 300;
37+
public static final int DISCONNECT_HUB = 301;
38+
3539
// tags for putting extra data to messages or broadcasts
3640
public static final String URI_TAG = "ASAP_MESSAGE_URI";
3741
public static final String ASAP_MESSAGE_TAG = "ASAP_MESSAGE";
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.sharksystem.asap.android.app2serviceMessaging;
2+
3+
import android.os.Bundle;
4+
import android.os.Message;
5+
6+
import net.sharksystem.asap.android.ASAPServiceMessage;
7+
import net.sharksystem.asap.android.ASAPServiceMethods;
8+
9+
public class MessageFactory {
10+
11+
/////////////////// ASAP hub management
12+
public static Message createConnectHubMessage(byte[] hubDescription) {
13+
Message msg = Message.obtain(null, ASAPServiceMethods.CONNECT_HUB, 0, 0);
14+
return addHubDescription(msg, hubDescription);
15+
}
16+
17+
public static Message createDisconnectHubMessage(byte[] hubDescription) {
18+
Message msg = Message.obtain(null, ASAPServiceMethods.DISCONNECT_HUB, 0, 0);
19+
return addHubDescription(msg, hubDescription);
20+
}
21+
22+
private static Message addHubDescription(Message msg, byte[] hubDescription) {
23+
Bundle bundle = new Bundle();
24+
bundle.putByteArray(ASAPServiceMessage.HUB_CONNECTOR_DESCRIPTION_TAG, hubDescription);
25+
msg.setData(bundle);
26+
return msg;
27+
}
28+
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
import net.sharksystem.asap.android.ASAPAndroid;
2222
import net.sharksystem.asap.android.ASAPServiceMessage;
2323
import net.sharksystem.asap.android.ASAPServiceMethods;
24+
import net.sharksystem.asap.android.app2serviceMessaging.MessageFactory;
2425
import net.sharksystem.asap.android.service.ASAPService;
2526
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceNotificationListener;
2627
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestListener;
2728
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyBroadcastReceiver;
2829
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
30+
import net.sharksystem.hub.peerside.HubConnectorFactory;
2931

32+
import java.io.IOException;
3033
import java.util.ArrayList;
3134
import java.util.Collection;
3235
import java.util.List;
@@ -217,6 +220,48 @@ protected String getLogStart() {
217220
return this.getClass().getSimpleName();
218221
}
219222

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+
220265
/////////////////////////////////////////////////////////////////////////////////////
221266
// mac protocol stuff //
222267
/////////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ private void setupCleanASAPStorage() throws IOException, ASAPException {
195195
ExampleAppDefinitions.ASAP_EXAMPLE_APPNAME);
196196
}
197197

198-
public void onAddOnlineSenderClick(View view) {
199-
Log.d(this.getLogStart(), "onAddOnlineSenderClick reached");
200-
Log.d(this.getLogStart(), "TODO: implement");
198+
public void onASAPHub(View view) {
199+
Log.d(this.getLogStart(), "onASAPHub reached");
200+
this.startActivity(new Intent(this, ASAPExampleHubManagementActivity.class));
201+
201202
/*
202203
try {
203204
this.checkStorage();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.sharksystem.asap.android.example;
2+
3+
import android.os.Bundle;
4+
import android.view.View;
5+
import android.widget.EditText;
6+
7+
import net.sharksystem.asap.android.R;
8+
import net.sharksystem.asap.android.apps.ASAPActivity;
9+
10+
public class ASAPExampleHubManagementActivity extends ASAPActivity {
11+
@Override
12+
protected void onCreate(Bundle savedInstanceState) {
13+
super.onCreate(savedInstanceState);
14+
setContentView(R.layout.example_hub_management_layout);
15+
}
16+
17+
private void hubAction(boolean on) {
18+
EditText hostnameET = findViewById(R.id.hostname);
19+
String hostName = hostnameET.getText().toString();
20+
21+
EditText portET = findViewById(R.id.port);
22+
String portString = portET.getText().toString();
23+
24+
int port = Integer.parseInt(portString);
25+
26+
if(on) {
27+
this.connectTCPHub(hostName, port);
28+
} else {
29+
this.disconnectTCPHub(hostName, port);
30+
}
31+
}
32+
33+
public void onConnectHubButtonClick(View view) {
34+
this.hubAction(true);
35+
}
36+
37+
public void onDisconnectHubButtonClick(View view) {
38+
this.hubAction(false);
39+
}
40+
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package net.sharksystem.asap.android.service;
22

3+
import static net.sharksystem.asap.android.ASAPServiceMessage.HUB_CONNECTOR_DESCRIPTION_TAG;
4+
5+
import android.os.Bundle;
36
import android.os.Handler;
47
import android.os.Message;
58
import android.util.Log;
@@ -88,16 +91,37 @@ public void handleMessage(Message msg) {
8891
this.asapService.stopLoRa();
8992
break;
9093

94+
case ASAPServiceMethods.CONNECT_HUB:
95+
this.asapService.connectHub(this.getHubDescription(msg));
96+
break;
97+
98+
case ASAPServiceMethods.DISCONNECT_HUB:
99+
this.asapService.disconnectHub(this.getHubDescription(msg));
100+
break;
101+
91102
default:
92103
super.handleMessage(msg);
93104
}
94105
}
95106
// catch(ASAPException e) {
96-
catch(Throwable e) {
107+
catch (Throwable e) {
97108
Log.d(this.getLogStart(), e.getLocalizedMessage());
98109
}
99110
}
100111

112+
113+
private byte[] getHubDescription(Message msg) throws ASAPException {
114+
Bundle msgData = msg.getData();
115+
if(msgData ==null) {
116+
Log.e(this.getLogStart(), "send message must contain parameters");
117+
throw new ASAPException("send message must contain parameters");
118+
}
119+
byte[] hubConnectorDescription = msgData.getByteArray(HUB_CONNECTOR_DESCRIPTION_TAG);
120+
if(hubConnectorDescription == null) throw new ASAPException("hub connector data not set");
121+
122+
return hubConnectorDescription;
123+
}
124+
101125
private void handleSendMessage(Message msg) throws ASAPException, IOException {
102126
ASAPServiceMessage asapMessage = ASAPServiceMessage.createASAPServiceMessage(msg);
103127

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.content.Intent;
88
import android.content.pm.PackageManager;
99
import android.os.IBinder;
10+
import android.os.Message;
1011
import android.os.Messenger;
1112
import androidx.core.content.ContextCompat;
1213
import android.util.Log;
@@ -28,13 +29,23 @@
2829
import net.sharksystem.asap.android.wifidirect.WifiP2PEngine;
2930
import net.sharksystem.asap.engine.ASAPChunkReceivedListener;
3031
import net.sharksystem.asap.utils.Helper;
32+
import net.sharksystem.asap.utils.PeerIDHelper;
33+
import net.sharksystem.hub.ASAPHubException;
3134
import net.sharksystem.hub.peerside.ASAPHubManager;
3235
import 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

3442
import java.io.File;
3543
import java.io.IOException;
3644
import java.util.ArrayList;
45+
import java.util.Collection;
46+
import java.util.HashMap;
3747
import java.util.List;
48+
import java.util.Map;
3849
import 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

Comments
 (0)