]> hydra-www.ietfng.org Git - acmetensortoys-ctfws-android/commitdiff
Messages now take timestamps...
authorNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Fri, 10 Feb 2017 07:14:01 +0000 (02:14 -0500)
committerNathaniel Wesley Filardo <nwf@cs.jhu.edu>
Fri, 10 Feb 2017 07:14:01 +0000 (02:14 -0500)
To suppress out-of-order delivery; see parallel commit
f3997b3067a8a9026a6a42c085bb77108cf825a8 in the IoT source

mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSCallbacksMQTT.java
mobile/src/main/java/com/acmetensortoys/ctfwstimer/CtFwSDisplay.java

index c0b55401300d94f5a3d35a327d0997f8f79c511e..e2f9d99fcc07c4db417211a0286d1110316adbf9 100644 (file)
@@ -81,19 +81,41 @@ class CtFwSCallbacksMQTT {
         }
     };
 
+    private long lastMsgTimestamp = 0;
+    private void onMessageCommon(String str) {
+        Scanner s = new Scanner(str);
+        try {
+            long t = s.nextLong();
+            // If there is no configuration, assume the message is new enough
+            // If there *is* a configuration, check the time.
+            if ((!mCgs.configured  || t >= mCgs.startT) && (lastMsgTimestamp <= t)) {
+                s.useDelimiter("\\z");
+                lastMsgTimestamp = t;
+                mCdl.notifyMessage(t, s.next().trim());
+            }
+        } catch (NoSuchElementException nse) {
+            // Maybe they forgot a time stamp.  That's not ideal, but... fake it?
+            lastMsgTimestamp = System.currentTimeMillis()/1000;
+            mCdl.notifyMessage(lastMsgTimestamp, str);
+            lastMsgTimestamp -= 30; // XXX Back off a bit, for time sync reasons
+        }
+    }
+
     IMqttMessageListener onMessage = new IMqttMessageListener() {
         @Override
         public void messageArrived(String topic, MqttMessage message) throws Exception {
-            Log.d("CtFwS", "Message(Broadcast): " + message);
-            mCdl.notifyMessage(message.toString());
+            String str = message.toString();
+            Log.d("CtFwS", "Message(Broadcast): " + str);
+            onMessageCommon(str);
         }
     };
 
     IMqttMessageListener onPlayerMessage = new IMqttMessageListener() {
         @Override
         public void messageArrived(String topic, MqttMessage message) throws Exception {
-            Log.d("CtFwS", "Message(Players): " + message);
-            mCdl.notifyMessage(message.toString());
+            String str = message.toString();
+            Log.d("CtFwS", "Message(Players): " + str);
+            onMessageCommon(str);
         }
     };
 }
index cc66d914da0582a712d8a7c03a2b3782d970c879..c0ceba68a6e8fe24e620983c7a16e559c557c753 100644 (file)
@@ -250,13 +250,10 @@ class CtFwSDisplay {
         });
     }
 
-    void notifyMessage(String m) {
-        lastMsgTimeMS = System.currentTimeMillis();
-
+    void notifyMessage(long ts, String m) {
         final StringBuffer sb = new StringBuffer();
-        long ts = lastMsgTimeMS/1000 - mCgs.startT;
-        if (!mCgs.configured || ts < 0) { ts = 0; }
-        sb.append(DateUtils.formatElapsedTime(ts));
+        long td = (ts == 0) ? 0 : (mCgs.configured) ? ts - mCgs.startT : 0;
+        sb.append(DateUtils.formatElapsedTime(td));
         sb.append(": ");
         sb.append(m);
         sb.append("\n");