Commit bceb2bbf authored by Lily Tsai's avatar Lily Tsai
Browse files

add linking logic to server side of simulation

parent 487ae99f
......@@ -5,8 +5,6 @@ import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;
import org.mpisws.helpers.Identifier;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
......@@ -16,12 +14,8 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.List;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mDHNonces;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mDHPubKeys;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.mSharedSecrets;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHFullKeys;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHNonces;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.otherDHPubKeys;
......@@ -39,7 +33,10 @@ public class SimulationClient {
private Socket sock = null;
SimulationClient(Context context) {
mNsdManager = (NsdManager)context.getSystemService(Context.NSD_SERVICE);
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
public void sendDataToSimulationServer() {
initializeDiscoveryListener();
initializeResolveListener();
mNsdManager.discoverServices(mServiceType, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
......@@ -116,41 +113,58 @@ public class SimulationClient {
mHost = mService.getHost();
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
setupSocket();
sendDHKeysOverSocket();
}
};
}
public void setupSocket() {
try {
sock = new Socket(mHost, mPort);
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendDHKeysOverSocket() {
new Thread(() -> {
try {
sock = new Socket(mHost, mPort);
if (sock == null) return;
String serializedSecrets;
String serializedNonces;
String serializedPubKeys;
// serialize
OutputStream os = sock.getOutputStream();
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(mSharedSecrets);
so.flush();
serializedSecrets = bo.toString();
so.writeObject(otherDHNonces);
so.flush();
serializedNonces = bo.toString();
so.writeObject(otherDHPubKeys);
so.flush();
serializedPubKeys = bo.toString();
} catch (Exception e) {
System.out.println(e);
return;
}
// write over socket
PrintWriter pw = new PrintWriter(os, true);
pw.println(serializedSecrets);
pw.println(serializedNonces);
pw.println(serializedPubKeys);
// wait for server to ack
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
Log.d(TAG, line);
}
public void sendDHKeysOverSocket() throws IOException {
if (sock == null) return;
OutputStream os = sock.getOutputStream();
String serializedSecrets;
String serializedNonces;
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(mSharedSecrets);
so.flush();
serializedSecrets = bo.toString();
so.writeObject(otherDHNonces);
so.flush();
serializedNonces = bo.toString();
} catch (Exception e) {
System.out.println(e);
return;
}
PrintWriter pw = new PrintWriter(os, true);
pw.println(serializedSecrets);
pw.println(serializedNonces);
sock.close();
in.close();
pw.close();
sock.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
......@@ -34,7 +34,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;
public static final int NUM_SIMULATED_DEVICES = 10;
private static final int NUM_SIMULATED_EPOCHS = 20;
protected static List<Identifier> mDHPubKeys = new ArrayList<>(NUM_SIMULATED_EPOCHS);
protected static List<Identifier> mDHNonces = new ArrayList<>(NUM_SIMULATED_EPOCHS);
......
......@@ -3,63 +3,126 @@ package org.mpisws.testapp;
import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.os.Handler;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.mpisws.embeddedsocial.ESClient;
import org.mpisws.embeddedsocial.ESMessage;
import org.mpisws.helpers.Identifier;
import org.mpisws.helpers.Utils;
import org.mpisws.messaging.EpochLinkMessage;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import static org.mpisws.encounters.EncounterBasedCommunication.CHANGE_EPOCH_TIME;
import static org.mpisws.encounters.encounterformation.simulator.SimulatorEncounterFormationCore.NUM_SIMULATED_DEVICES;
import static org.mpisws.helpers.Utils.SHA1;
public class SimulationServer {
static private int port;
static private String mServiceName = "EbCSimulator";
static private String mServiceType = "_ebcsimulator._tcp";
private int currentEpoch = 0;
private ServerSocket mServerSocket;
private NsdManager.RegistrationListener mRegistrationListener;
private NsdManager mNsdManager;
private List<Identifier> nonces;
private List<Identifier> sharedSecrets;
private List<Identifier> pubKeys;
SimulationServer(Context context) {
mNsdManager = (NsdManager) context.getSystemService(Context.NSD_SERVICE);
}
// Initialize a server socket on the next available port.
List<Identifier> nonces = null;
List<Identifier> sharedSecrets = null;
try {
mServerSocket = new ServerSocket(0);
// Store the chosen port.
port = mServerSocket.getLocalPort();
registerService(port);
Socket sock = mServerSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
List<String> lines = new ArrayList<>();
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
public void startServer() {
new Thread(() -> {
// Initialize a server socket on the next available port.
try {
mServerSocket = new ServerSocket(0);
// Store the chosen port.
port = mServerSocket.getLocalPort();
registerService(port);
Socket sock = mServerSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
List<String> lines = new ArrayList<>();
String line;
while ((line = in.readLine()) != null) {
lines.add(line);
}
PrintStream output = new PrintStream(sock.getOutputStream());
output.println("Finished");
Utils.myAssert(lines.size() == 3);
sharedSecrets = deserializeIDList(lines.get(0));
nonces = deserializeIDList(lines.get(1));
pubKeys = deserializeIDList(lines.get(2));
if (sharedSecrets == null || nonces == null) {
return;
}
output.close();
in.close();
sock.close();
processData();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
Utils.myAssert(lines.size() == 2);
sharedSecrets = deserializeIDList(lines.get(0));
nonces = deserializeIDList(lines.get(1));
sock.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}).start();
}
private void processData() {
// Create all topics you'll ever have to create
List<Pair<Identifier, Identifier>> topicsToCreate = new ArrayList<>();
for (int i=0; i < nonces.size(); i++) {
topicsToCreate.add(new ImmutablePair<>(nonces.get(i), pubKeys.get(i)));
}
for (Identifier ss : sharedSecrets) {
topicsToCreate.add(new ImmutablePair<>(ss, ss));
}
ESClient.getInstance().createTopics(topicsToCreate);
// Every "epoch" or so try to post link messages to the prior "epoch" ss for each "device"
// Let's try and post to the prior 3 epochs
new Handler().postDelayed(() -> {
int lowEpoch = currentEpoch > 3 ? currentEpoch - 3 : 0;
List<Identifier> sses = sharedSecrets.subList(lowEpoch*NUM_SIMULATED_DEVICES, currentEpoch*NUM_SIMULATED_DEVICES);
List<String> topicHandles = ESClient.getInstance().getTopicHandles(sses);
List<ESMessage> msgsToSend = new ArrayList<>();
for (int i = 0; i < NUM_SIMULATED_DEVICES; i++) {
for (int j = currentEpoch+1; j > lowEpoch+1; j--) {
int oldIndex = (j-1-lowEpoch)*NUM_SIMULATED_DEVICES + i;
int newIndex = (j-lowEpoch)*NUM_SIMULATED_DEVICES + i;
if (topicHandles.get(oldIndex) == null || topicHandles.get(oldIndex).compareTo("") == 0) {
continue;
}
EpochLinkMessage epochLinkMessage = new EpochLinkMessage.EpochLinkMessageBuilder()
.addOldNonce(nonces.get((lowEpoch*NUM_SIMULATED_DEVICES) + oldIndex).toString())
.addNewNonce(nonces.get((lowEpoch*NUM_SIMULATED_DEVICES) + newIndex).toString())
.build();
msgsToSend.add(new ESMessage(epochLinkMessage.toSendMessageText(sses.get(oldIndex).getBytes()),
new Identifier(SHA1(sharedSecrets.get(i).getBytes())).toString(),
topicHandles.get(oldIndex),
true, null, true, -1));
}
}
ESClient.getInstance().sendMsgs(msgsToSend);
currentEpoch++;
}, CHANGE_EPOCH_TIME);
}
private List<Identifier> deserializeIDList(String serializedObject) throws IOException, ClassNotFoundException {
......
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