Commit 9bb8f0d7 authored by LIly's avatar LIly
Browse files

hm

parent 342b1b92
......@@ -47,7 +47,6 @@ public class EncounterBasedCommunication {
public static final long SCAN_BATCH_INTERVAL = (long) (2 * 60000);
public static final int REQUEST_ENABLE_BT = 1;
public static final int REQUEST_ACCESS_FINE_LOCATION = 2;
public static final boolean USE_SIMULATOR = true;
private static final String TAG = EncounterBasedCommunication.class.getSimpleName();
private UserAccountClient userAccountClient;
......@@ -96,6 +95,11 @@ public class EncounterBasedCommunication {
}
public interface ISDDRClient {
/**
* Begin the encounter formation service simulating encounters
*/
void startSimulatingEncounters();
/**
* Begin the encounter formation service, which emits BLE adverts and occasional BLE scans to
* detect and form encounters with nearby EbC devices
......
......@@ -34,6 +34,14 @@ public class SDDRClient implements EncounterBasedCommunication.ISDDRClient {
this.context = context;
}
@Override
public void startSimulatingEncounters() {
Intent serviceIntent = new Intent(context, EncounterFormationService.class);
serviceIntent.putExtra("start_simulated_sddr_service", 0);
context.startService(serviceIntent);
Log.d(TAG, "Started simulated encounters service");
}
@Override
public void startFormingEncounters() {
Intent serviceIntent = new Intent(context, EncounterFormationService.class);
......
......@@ -14,9 +14,6 @@ import android.widget.Toast;
import org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore;
import static org.mpisws.encounters.EncounterBasedCommunication.USE_SIMULATOR;
/**
* Created by tslilyai on 10/27/17.
*/
......@@ -28,6 +25,7 @@ import static org.mpisws.encounters.EncounterBasedCommunication.USE_SIMULATOR;
*/
public class EncounterFormationService extends Service {
private static final String TAG = EncounterFormationService.class.getSimpleName();
private static boolean SIMULATE = false;
private EncounterFormationCore core;
private SimulatorEncounterFormationCore score;
private Thread thread;
......@@ -52,16 +50,17 @@ public class EncounterFormationService extends Service {
this.stopSelf();
}
Log.v(TAG, "Bluetooth and location permissions enabled");
if (SIMULATE && score==null) {
Log.v(TAG, "Starting SDDR_API Core from thread " + Thread.currentThread().getName());
Log.v(TAG, "Simulated? " + SIMULATE);
score = new SimulatorEncounterFormationCore(this);
thread = new Thread(score);
thread.start();
}
if (core == null) {
Log.v(TAG, "Starting SDDR_API Core from thread " + Thread.currentThread().getName());
Log.v(TAG, "Simulated? " + USE_SIMULATOR);
if (USE_SIMULATOR) {
score = new SimulatorEncounterFormationCore(this);
thread = new Thread(score);
} else {
core = new EncounterFormationCore(this);
thread = new Thread(core);
}
core = new EncounterFormationCore(this);
thread = new Thread(core);
thread.start();
} else {
Log.v(TAG, "SDDR_API already running");
......@@ -71,7 +70,6 @@ public class EncounterFormationService extends Service {
@Override
public void onCreate() {
Log.v(TAG, "creating service");
check_and_start_core();
}
@Override
......@@ -81,8 +79,15 @@ public class EncounterFormationService extends Service {
return START_STICKY;
}
if (intent.getExtras().containsKey("start_simulated_sddr_service"))
{
SIMULATE = true;
check_and_start_core();
}
if (intent.getExtras().containsKey("@string.start_sddr_service"))
{
SIMULATE = false;
check_and_start_core();
return START_STICKY;
}
......@@ -128,7 +133,7 @@ public class EncounterFormationService extends Service {
core.stop();
core = null;
}
if (USE_SIMULATOR && score != null) {
if (score != null) {
score.stop();
score = null;
}
......
......@@ -18,6 +18,12 @@ import org.mpisws.encounters.encounterformation.SDDR_Native;
import org.mpisws.encounters.encounterhistory.bridges.EncounterEntriesBridge;
import org.mpisws.helpers.Identifier;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
......@@ -35,7 +41,7 @@ import static org.mpisws.encounters.EncounterBasedCommunication.CHANGE_EPOCH_TIM
public class SimulatorEncounterFormationCore implements Runnable {
private static final String TAG = SimulatorEncounterFormationCore.class.getSimpleName();
protected static final int NUM_SIMULATED_DEVICES = 10;
private static final int NUM_SIMULATED_EPOCHS = 20;
private static final int NUM_SIMULATED_EPOCHS = 30;
protected static List<Identifier> mDHPubKeys = new ArrayList<>(NUM_SIMULATED_EPOCHS);
protected static List<Identifier> mDHNonces = new ArrayList<>(NUM_SIMULATED_EPOCHS);
protected static List<Identifier> mDHFullKeys = new ArrayList<>(NUM_SIMULATED_EPOCHS);
......@@ -75,10 +81,12 @@ public class SimulatorEncounterFormationCore implements Runnable {
changeEpochTime = System.currentTimeMillis() + CHANGE_EPOCH_TIME;
encounterConfirmationAndEpochLinking = new SimulatorEncounterConfirmationAndEpochLinking(mService);
new EncounterEntriesBridge(mService).finalizeOldEncounters();
initializeSimulatedAdverts();
if (initializeSimulatedAdverts() == -1)
throw new IllegalStateException("No device listening to post link adverts");
}
private void initializeSimulatedAdverts() {
private int initializeSimulatedAdverts() {
Socket clientSocket;
List<Pair<Identifier, Identifier>> topicsToCreate = new ArrayList<>();
for (int i = 0; i < NUM_SIMULATED_EPOCHS*NUM_SIMULATED_DEVICES; i++) {
SDDR_Native.c_changeEpoch();
......@@ -101,6 +109,31 @@ public class SimulatorEncounterFormationCore implements Runnable {
topicsToCreate.add(new ImmutablePair<>(sharedSecret, sharedSecret));
}
}
Thread thread = new Thread(() -> {
try {
//Replace below IP with the IP of that device in which server socket open.
//If you change port then change the port number in the server side code also.
Socket s = new Socket("xxx.xxx.xxx.xxx", 9002);
OutputStream out = s.getOutputStream();
PrintWriter output = new PrintWriter(out);
output.println(msg);
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
final String st = input.readLine();
output.close();
out.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
});
thread.start();
Log.d(TAG, "Creating " + topicsToCreate.size() + " topics");
ESClient.getInstance().createTopics(topicsToCreate);
}
......
......@@ -136,7 +136,12 @@ public class SimulatorScannerProcessor {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Log.v(TAG, "some scan result");
SDDR_Native.c_preDiscovery();
simulateReceivingResults();
SDDR_Native.c_postDiscovery();
processEncounters();
core.postScanProcessing();
}
@Override
......
......@@ -21,11 +21,17 @@ import android.widget.Toast;
import org.mpisws.embeddedsocial.ESClient;
import org.mpisws.encounters.EncounterBasedCommunication;
import org.mpisws.encounters.encounterformation.simulator.SimulatorAdvertiser;
import org.mpisws.messaging.ReceivedMessageWrapper;
import org.mpisws.testapp.googleauth.GoogleNativeAuthenticator;
import org.mpisws.testapp.googleauth.GoogleToken;
import org.mpisws.testapp.tests.ESBatchTester;
import org.mpisws.testapp.tests.SimulatorTestServer;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;
import static org.mpisws.encounters.EncounterBasedCommunication.REQUEST_ACCESS_FINE_LOCATION;
......@@ -91,9 +97,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
/***************************** ES AND SDDR TESTS **********************/
case R.id.testESFunctions:
new Handler().postDelayed(() -> new ESBatchTester(32).simulateConfirmations(), 15000);
case R.id.simulateEncounterFormationAndConfirmationClient:
ebc.getSDDRClient().startSimulatingEncounters();
break;
case R.id.simulateEncounterFormationAndConfirmationServer:
SimulatorTestServer simulator = new SimulatorTestServer(this);
simulator.registerService();
simulator.startServer();
break;
case R.id.testSendMessages:
ebc.getSDDRClient().updateDatabaseOnAgent();
......@@ -129,6 +139,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED) {
......
package org.mpisws.testapp.tests;
import android.content.Context;
import android.content.Intent;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
public class SimulatorTestServer {
private static final String TAG = SimulatorTestServer.class.getSimpleName();
private static final int port = 8080;
public static final String SERVICE_TYPE = "_localdash._tcp";
private NsdManager mNsdManager;
private NsdManager.RegistrationListener mRegistrationListener;
private NsdServiceInfo serviceInfo = new NsdServiceInfo();
public SimulatorTestServer(Context context) {
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
registerService();
}
public void registerService() {
tearDown(); // Cancel any previous registration request
initializeRegistrationListener();
NsdServiceInfo serviceInfo = new NsdServiceInfo();
serviceInfo.setPort(port);
serviceInfo.setServiceName("SimulateEbCService");
serviceInfo.setServiceType(SERVICE_TYPE);
Log.v(TAG, "Registering service: " + port);
mNsdManager.registerService(serviceInfo, NsdManager.PROTOCOL_DNS_SD, mRegistrationListener);
}
public void tearDown() {
if (mRegistrationListener != null) {
try {
mNsdManager.unregisterService(mRegistrationListener);
} finally {
}
mRegistrationListener = null;
}
}
public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
Log.d(TAG, "Service registered: " + NsdServiceInfo);
}
@Override
public void onRegistrationFailed(NsdServiceInfo arg0, int arg1) {
Log.d(TAG, "Service registration failed: " + arg1);
}
@Override
public void onServiceUnregistered(NsdServiceInfo arg0) {
Log.d(TAG, "Service unregistered: " + arg0.getServiceName());
}
@Override
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.d(TAG, "Service unregistration failed: " + errorCode);
}
};
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment