Commit 182ef779 authored by Lily Tsai's avatar Lily Tsai

bluetooth transfer of data working

parent d510f0a3
......@@ -186,11 +186,11 @@ public final class EmbeddedSocialBatchedClientImpl {
this.batchSuccessful = true;
}
System.out.println("BATCH OF " + batchSize + " COMPLETED: \n"
/*System.out.println("BATCH OF " + batchSize + " COMPLETED: \n"
// + "\t Success: " + this.batchSuccessful
// + "\t Number Responses: " + batchResps.size()
+ "\n Batch Request was: \n" + batchBody.toString()
+ "\n Batch Response was: \n" + batchResponseBody);
+ "\n Batch Response was: \n" + batchResponseBody);*/
// Notify the individual interceptors to resume
synchronized (this.syncObject) {
......
......@@ -124,24 +124,12 @@ public class SimulationClientBT {
// serialize
OutputStream os = sock.getOutputStream();
try {
serializedSecrets = serializeList(mSharedSecrets);
serializedNonces = serializeList(otherDHNonces);
serializedPubKeys = serializeList(otherDHPubKeys);
} catch (Exception e) {
System.out.println(e);
return;
}
DataOutputStream out = new DataOutputStream(os);
out.writeInt(serializedSecrets.length());
out.writeInt(serializedNonces.length());
out.writeInt(serializedPubKeys.length());
String toWrite = serializedSecrets + serializedNonces + serializedPubKeys;
out.writeChars(toWrite);
ObjectOutputStream out = new ObjectOutputStream(os);
out.writeObject(mSharedSecrets);
out.writeObject(otherDHNonces);
out.writeObject(otherDHPubKeys);
out.flush();
Log.d(TAG, "Lengths: " + toWrite.length() + ", "+ serializedSecrets.length() + ", " + serializedNonces.length() + "," + serializedPubKeys.length());
Log.d(TAG, "Sent data over socket! " + toWrite);
Log.d(TAG, "Sent data over socket!");
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = in.readLine();
......@@ -149,6 +137,7 @@ public class SimulationClientBT {
in.close();
os.close();
out.close();
sock.close();
} catch (IOException e) {
e.printStackTrace();
......@@ -157,13 +146,4 @@ public class SimulationClientBT {
Log.d(TAG, "Running simulator core!");
new Thread(core).start();
}
private String serializeList(List<Identifier> list) throws IOException {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(list);
so.flush();
Log.d(TAG, "Serialized string to " + bo.toString());
return bo.toString();
}
}
......@@ -6,6 +6,7 @@ import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import org.apache.commons.lang3.tuple.ImmutablePair;
......@@ -76,30 +77,11 @@ public class SimulationServerBT {
serversocket.close();
// If a connection was accepted
if (socket != null) {
DataInputStream in = new DataInputStream(socket.getInputStream());
byte[] messageByte = new byte[3000];
String dataString = "";
int ssLen = in.readInt();
int nonceLen = in.readInt();
int pubKeyLen = in.readInt();
Log.d(TAG, ssLen + ", " + nonceLen + ", " + pubKeyLen);
while(true)
{
int bytesRead = in.read(messageByte);
dataString += new String(messageByte, 0, bytesRead);
Log.d(TAG, dataString);
if (dataString.length() >= ssLen+nonceLen+pubKeyLen) {
break;
}
Log.d(TAG, "dataString length: " + dataString.length());
}
Log.d(TAG, "dataString length: " + dataString.length());
sharedSecrets = deserializeIDList(dataString.substring(0, ssLen));
nonces = deserializeIDList(dataString.substring(ssLen, ssLen+nonceLen));
pubKeys = deserializeIDList(dataString.substring(ssLen+nonceLen, ssLen+nonceLen+pubKeyLen));
if (sharedSecrets == null || nonces == null || pubKeys == null) {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
sharedSecrets = (List<Identifier>)in.readObject();
nonces = (List<Identifier>)in.readObject();
pubKeys = (List<Identifier>)in.readObject();
if (sharedSecrets.size() != nonces.size() || nonces.size() != pubKeys.size()) {
return;
}
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
......@@ -117,15 +99,6 @@ public class SimulationServerBT {
}
}).start();
}
private List<Identifier> deserializeIDList(String serializedObject) throws IOException, ClassNotFoundException {
byte b[] = serializedObject.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
List<Identifier> obj = (List<Identifier>) si.readObject();
Log.d(TAG, "Deserialized! " + obj.size());
return obj;
}
private void processData() {
// Create all topics you'll ever have to create
List<Pair<Identifier, Identifier>> topicsToCreate = new ArrayList<>();
......@@ -135,38 +108,41 @@ public class SimulationServerBT {
for (Identifier ss : sharedSecrets) {
topicsToCreate.add(new ImmutablePair<>(ss, ss));
}
Log.d(TAG, "Creating topics: " + topicsToCreate.size());
Log.d(TAG, "creating nonce and shared secret topics: " + topicsToCreate.size());
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);
Looper.prepare();
for (int loop = 0; loop < NUM_SIMULATED_EPOCHS; loop++) {
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;
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));
}
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++;
// heh hack
if (currentEpoch == NUM_SIMULATED_EPOCHS) currentEpoch--;
}, CHANGE_EPOCH_TIME);
ESClient.getInstance().sendMsgs(msgsToSend);
currentEpoch++;
// heh hack
if (currentEpoch == NUM_SIMULATED_EPOCHS) currentEpoch--;
}, CHANGE_EPOCH_TIME);
}
}
}
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