Skip to content

Commit c5009d8

Browse files
committed
Compiles with hub integration. Requires testing.
1 parent 585be2f commit c5009d8

File tree

5 files changed

+62
-62
lines changed

5 files changed

+62
-62
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
implementation 'androidx.appcompat:appcompat:1.2.0'
3232
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
3333
implementation files('libs\\ASAPJava.jar')
34+
implementation files('libs\\ASAPHub.jar')
3435
testImplementation 'junit:junit:4.13.1'
3536
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
3637
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,16 @@ public boolean samePeer(CharSequence otherPeerID) {
680680
return PeerIDHelper.sameID(this.getPeerID(), otherPeerID);
681681
}
682682

683+
@Override
684+
public void putExtra(CharSequence key, byte[] bytes) throws IOException, ASAPException {
685+
this.asapPeerApplicationSide.putExtra(key, bytes);
686+
}
687+
688+
@Override
689+
public byte[] getExtra(CharSequence key) throws ASAPException, IOException {
690+
return this.asapPeerApplicationSide.getExtra(key);
691+
}
692+
683693
///////////////////////////////////////////////////////////////////////////////////////////////
684694
// ASAP messages are sent with the service //
685695
///////////////////////////////////////////////////////////////////////////////////////////////

app/src/main/java/net/sharksystem/asap/android/bluetooth/BluetoothEngine.java

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
import android.content.IntentFilter;
1010
import android.util.Log;
1111

12+
import net.sharksystem.asap.ASAPEncounterManager;
1213
import net.sharksystem.asap.ASAPException;
14+
import net.sharksystem.asap.EncounterConnectionType;
1315
import net.sharksystem.asap.android.service.MacLayerEngine;
1416
import net.sharksystem.asap.android.service.ASAPService;
1517
import net.sharksystem.asap.android.Util;
1618
import net.sharksystem.asap.android.service2AppMessaging.ASAPServiceRequestNotifyIntent;
19+
import net.sharksystem.streams.StreamPair;
20+
import net.sharksystem.streams.StreamPairImpl;
1721

18-
import java.io.DataInputStream;
19-
import java.io.DataOutputStream;
2022
import java.io.IOException;
21-
import java.net.Socket;
2223
import java.util.ArrayList;
2324
import java.util.HashMap;
2425
import java.util.List;
@@ -44,10 +45,10 @@ public class BluetoothEngine extends MacLayerEngine {
4445
private boolean btEnvironmentOn = false;
4546
private BluetoothServerSocketThread btServerSocketThread;
4647

47-
public static BluetoothEngine getASAPBluetoothEngine(ASAPService ASAPService,
48+
public static BluetoothEngine getASAPBluetoothEngine(ASAPService asapService,
4849
Context context) {
4950
if(BluetoothEngine.engine == null) {
50-
BluetoothEngine.engine = new BluetoothEngine(ASAPService, context);
51+
BluetoothEngine.engine = new BluetoothEngine(asapService, context);
5152
}
5253

5354
return BluetoothEngine.engine;
@@ -445,43 +446,17 @@ public void checkConnectionStatus() {
445446
/**
446447
* Both client and server sockets
447448
* @param socket
448-
* @param isClient for debugging: who is calling client socket / server socket?
449+
* @param initiator for debugging: who is calling client socket / server socket?
449450
* @throws IOException
450451
*/
451-
void handleBTSocket(BluetoothSocket socket, boolean isClient) throws IOException {
452+
void handleBTSocket(BluetoothSocket socket, boolean initiator) throws IOException {
452453
String remoteMacAddress = socket.getRemoteDevice().getAddress();
453454

454-
String logMessage = isClient ? "Client" : "Server";
455-
logMessage += "socket called: handle new BT connection" + socket;
455+
StreamPair streamPair = StreamPairImpl.getStreamPairWithEndpointAddress(
456+
socket.getInputStream(), socket.getOutputStream(), remoteMacAddress);
456457

457-
Log.d(this.getLogStart(), logMessage + remoteMacAddress);
458-
459-
// already an open connection?
460-
if (this.checkAlreadyConnectedWithDevice(remoteMacAddress)) {
461-
socket.close();
462-
return;
463-
}
464-
465-
// avoid the nasty race condition
466-
boolean waited = this.waitBeforeASAPSessionLaunch(
467-
socket.getInputStream(),
468-
socket.getOutputStream(),
469-
isClient, 500);
470-
471-
// ask again?
472-
if(waited) {
473-
if (this.checkAlreadyConnectedWithDevice(remoteMacAddress)) {
474-
socket.close();
475-
return;
476-
}
477-
}
478-
479-
// remember that new connection
480-
Log.d(this.getLogStart(), "remember socket: " + socket);
481-
this.openSockets.put(remoteMacAddress, socket);
482-
483-
Log.d(this.getLogStart(), "launch asap session");
484-
this.launchASAPConnection(remoteMacAddress, socket.getInputStream(), socket.getOutputStream());
458+
this.getAsapService().getASAPEncounterManager().handleEncounter(
459+
streamPair, EncounterConnectionType.AD_HOC_LAYER_2_NETWORK, initiator);
485460
}
486461

487462
public void propagateStatus(Context ctx) throws ASAPException {

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import androidx.core.content.ContextCompat;
1212
import android.util.Log;
1313

14+
import net.sharksystem.asap.ASAPEncounterManager;
15+
import net.sharksystem.asap.ASAPEncounterManagerImpl;
1416
import net.sharksystem.asap.ASAPEnvironmentChangesListener;
1517
import net.sharksystem.asap.ASAPException;
1618
import net.sharksystem.asap.ASAPHop;
@@ -26,6 +28,8 @@
2628
import net.sharksystem.asap.android.wifidirect.WifiP2PEngine;
2729
import net.sharksystem.asap.engine.ASAPChunkReceivedListener;
2830
import net.sharksystem.asap.utils.Helper;
31+
import net.sharksystem.hub.peerside.ASAPHubManager;
32+
import net.sharksystem.hub.peerside.ASAPHubManagerImpl;
2933

3034
import java.io.File;
3135
import java.io.IOException;
@@ -47,11 +51,13 @@ public class ASAPService extends Service
4751
private String asapEngineRootFolderName;
4852

4953
private ASAPPeerFS asapPeer;
54+
private ASAPEncounterManager asapEncounterManager;
5055
private CharSequence owner;
5156
private CharSequence rootFolder;
5257
private boolean onlineExchange;
5358
private long maxExecutionTime;
5459
private ArrayList<CharSequence> supportedFormats;
60+
private ASAPHubManager asapASAPHubManager;
5561

5662
String getASAPRootFolderName() {
5763
return this.asapEngineRootFolderName;
@@ -116,6 +122,27 @@ public ASAPPeerFS getASAPPeer() {
116122
return this.asapPeer;
117123
}
118124

125+
public ASAPEncounterManager getASAPEncounterManager() {
126+
if(this.asapEncounterManager == null) {
127+
try {
128+
this.asapEncounterManager = new ASAPEncounterManagerImpl(this.getASAPPeer());
129+
} catch (ASAPException e) {
130+
Log.e(this.getLogStart(), "cannot create encounter manager: "
131+
+ e.getLocalizedMessage());
132+
}
133+
}
134+
135+
return this.asapEncounterManager;
136+
}
137+
138+
public ASAPHubManager getASAPHubManager() {
139+
if(this.asapASAPHubManager == null) {
140+
this.asapASAPHubManager = new ASAPHubManagerImpl(this.getASAPEncounterManager());
141+
}
142+
143+
return this.asapASAPHubManager;
144+
}
145+
119146
//////////////////////////////////////////////////////////////////////////////////////
120147
// live cycle methods //
121148
//////////////////////////////////////////////////////////////////////////////////////

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

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ public void restart() {
6666
this.start();
6767
}
6868

69+
private String getLogStart() {
70+
return "ASAPMacLayerEngine";
71+
}
72+
73+
/**
74+
* TODO: do we need this? I doubt it (thsc)
75+
* It can be called to check whether open connection are still running. It's a good idea for
76+
* all connection oriented protocols but useless with connectionless.
77+
*/
78+
public abstract void checkConnectionStatus();
79+
6980
/** keeps info about device we have tried (!!) recently to connect
7081
* <MAC address, connection time>
7182
*/
@@ -114,25 +125,8 @@ public boolean shouldConnectToMACPeer(String macAddress) {
114125
return false;
115126
}
116127

117-
private String getLogStart() {
118-
return "ASAPMacLayerEngine";
119-
}
120-
121128
private Map<String, ASAPConnection> asapConnections = new HashMap<>();
122129

123-
private String localMacAddress = null;
124-
public String getLocalMacAddress() {
125-
if(localMacAddress == null) {
126-
WifiManager wifiManager = (WifiManager)
127-
asapService.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
128-
129-
WifiInfo wInfo = wifiManager.getConnectionInfo();
130-
this.localMacAddress = wInfo.getMacAddress();
131-
}
132-
133-
return this.localMacAddress;
134-
}
135-
136130
/**
137131
* kill connection to address
138132
* @param address
@@ -222,7 +216,7 @@ public boolean waitBeforeASAPSessionLaunch(InputStream is, OutputStream os,
222216
Log.d(this.getLogStart(), sb.toString());
223217

224218
/* Here comes the bias: An initiator with a smaller value waits a moment */
225-
if(connectionInitiator & initiatorValue < nonInitiatorValue) {
219+
if(connectionInitiator && initiatorValue < nonInitiatorValue) {
226220
try {
227221
sb = new StringBuilder();
228222
sb.append("wait ");
@@ -238,11 +232,4 @@ public boolean waitBeforeASAPSessionLaunch(InputStream is, OutputStream os,
238232

239233
return false;
240234
}
241-
242-
/**
243-
* TODO: do we need this? I doubt it (thsc)
244-
* It can be called to check whether open connection are still running. It's a good idea for
245-
* all connection oriented protocols but useless with connectionless.
246-
*/
247-
public abstract void checkConnectionStatus();
248235
}

0 commit comments

Comments
 (0)