From fa5f3f03614db4759b42a61cdf3204df3dc0b978 Mon Sep 17 00:00:00 2001 From: Lily Tsai Date: Fri, 6 Jul 2018 11:57:05 +0200 Subject: [PATCH] handler still not working... --- .../testapp/simulator/SimulationServerBT.java | 67 ++++++++++--------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/testapp/src/main/java/org/mpisws/testapp/simulator/SimulationServerBT.java b/testapp/src/main/java/org/mpisws/testapp/simulator/SimulationServerBT.java index 8489dbc4..42dee889 100644 --- a/testapp/src/main/java/org/mpisws/testapp/simulator/SimulationServerBT.java +++ b/testapp/src/main/java/org/mpisws/testapp/simulator/SimulationServerBT.java @@ -94,8 +94,7 @@ public class SimulationServerBT { }).start(); } private void processData() { - Looper.prepare(); - Handler handler = new Handler(); + final Handler handler = new Handler(Looper.getMainLooper()); // Create all topics you'll ever have to create List> topicsToCreate = new ArrayList<>(); for (int i=0; i < nonces.size(); i++) { @@ -109,37 +108,43 @@ public class SimulationServerBT { // 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 - for (int loop = 1; loop <= NUM_SIMULATED_EPOCHS; loop++) { - Log.d(TAG, "Handler loop " + loop); - handler.postDelayed(() -> { - Log.d(TAG, "Handler loop!"); - int lowEpoch = currentEpoch > 3 ? currentEpoch - 3 : 0; - List sses = sharedSecrets.subList(lowEpoch * NUM_SIMULATED_DEVICES, currentEpoch * NUM_SIMULATED_DEVICES); - List topicHandles = ESClient.getInstance().getTopicHandles(sses); + handler.postDelayed(new Runnable() { + public void run() { + new Thread(() -> { + Log.d(TAG, "Runnable!"); + int lowEpoch = currentEpoch > 3 ? currentEpoch - 3 : 0; + List sses = sharedSecrets.subList(lowEpoch * NUM_SIMULATED_DEVICES, currentEpoch * NUM_SIMULATED_DEVICES); + List eids = new ArrayList<>(); + for (Identifier ss : sses) { + eids.add(Coder.convertSharedSecretToID(ss)); + } + List topicHandles = ESClient.getInstance().getTopicHandles(eids); - List 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 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()), + eids.get(oldIndex).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()), - Coder.convertSharedSecretToID(sharedSecrets.get(i)).toString(), - topicHandles.get(oldIndex), - true, null, true, -1)); } - } - ESClient.getInstance().sendMsgs(msgsToSend); - currentEpoch++; - // heh hack - if (currentEpoch == NUM_SIMULATED_EPOCHS) currentEpoch--; - }, loop*CHANGE_EPOCH_TIME); - } + ESClient.getInstance().sendMsgs(msgsToSend); + currentEpoch++; + // heh hack + if (currentEpoch == NUM_SIMULATED_EPOCHS) currentEpoch--; + handler.postDelayed(this, CHANGE_EPOCH_TIME); + }).start(); + } + }, CHANGE_EPOCH_TIME); } } -- GitLab