chiark / gitweb /
reorg TrasnmitUpdate
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 11 Jul 2020 23:28:31 +0000 (00:28 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 11 Jul 2020 23:28:31 +0000 (00:28 +0100)
src/sse.rs
src/updates.rs
templates/script.ts

index 4b57ba57edbf8aa3d60e5eef2c540d7b42618c21..bc96fda742992d8e29d30e755f709dc5cdb28d08 100644 (file)
@@ -25,20 +25,6 @@ struct UpdateReader {
   ami : Arc<Mutex<Instance>>,
 }
 
-#[derive(Debug,Serialize)]
-enum TransmitUpdate<'u> {
-  Recorded {
-    piece : VisiblePieceId,
-    cseq : ClientSequence,
-    zg : Option<Generation>,
-  },
-  Piece {
-    piece : VisiblePieceId,
-    op : &'u PieceUpdateOp<PreparedPieceState>,
-  },
-  Log (&'u LogEntry),
-}
-
 #[derive(Error,Debug)]
 #[error("WouldBlock error misreported!")]
 struct FlushWouldBlockError{}
@@ -70,27 +56,10 @@ impl Read for UpdateReader {
       let next_len = UPDATE_MAX_FRAMING_SIZE + next.json_len();
       if next_len > buf.len() { break }
 
-      write!(buf, "data: [")?;
-      for u in &next.us {
-        let tu = match u {
-          &PreparedUpdateEntry::Piece
-          { piece, client, sameclient_cseq : cseq, ref op }
-          if client== self.client => {
-            let zg = op.new_z_generation();
-            TransmitUpdate::Recorded { piece, cseq, zg }
-          },
-          &PreparedUpdateEntry::Piece { piece, ref op, .. } => {
-            TransmitUpdate::Piece { piece, op }
-          },
-          PreparedUpdateEntry::Log(logent) => {
-            TransmitUpdate::Log(&*logent)
-          },
-        };
-        serde_json::to_writer(&mut buf, &tu)?;
-        write!(buf,",")?;
-      }
-      serde_json::to_writer(&mut buf, &next.gen)?;
-      write!(buf, "]\n\
+      let tu = next.for_transmit(self.client);
+      write!(buf, "data: ")?;
+      serde_json::to_writer(&mut buf, &tu)?;
+      write!(buf, "\n\
                    id: {}\n\n",
              &self.to_send)?;
       self.to_send.try_increment().unwrap();
index efe5b886de36e185b55dc8417907bd83a0fe9a59..6baabec5bea432e6783cfc76f8717f10c205eace 100644 (file)
@@ -55,6 +55,28 @@ pub enum PieceUpdateOp<NS> {
   SetZLevel(ZLevel),
 }
 
+// ---------- for traansmission ----------
+
+#[derive(Debug,Serialize)]
+pub struct TransmitUpdate<'u> (
+  Generation,
+  Vec<TransmitUpdateEntry<'u>>,
+);
+
+#[derive(Debug,Serialize)]
+enum TransmitUpdateEntry<'u> {
+  Recorded {
+    piece : VisiblePieceId,
+    cseq : ClientSequence,
+    zg : Option<Generation>,
+  },
+  Piece {
+    piece : VisiblePieceId,
+    op : &'u PieceUpdateOp<PreparedPieceState>,
+  },
+  Log (&'u LogEntry),
+}
+
 // ========== implementation ==========
 
 // ---------- prepared updates, queued in memory ----------
@@ -87,7 +109,7 @@ impl PreparedUpdateEntry {
   }
 }
 
-// ---------- piece updates ----------
+// ---------- PieceUpdatesOp ----------
 
 impl<NS> PieceUpdateOp<NS> {
   pub fn new_state(&self) -> Option<&NS> {
@@ -122,3 +144,29 @@ impl<NS> PieceUpdateOp<NS> {
     }
   }
 }
+
+// ---------- for traansmission ----------
+
+impl PreparedUpdate {
+  pub fn for_transmit(&self, dest : ClientId) -> TransmitUpdate {
+    let mut ents = vec![];
+    for u in &self.us {
+      let ue = match u {
+        &PreparedUpdateEntry::Piece
+        { piece, client, sameclient_cseq : cseq, ref op }
+        if client == dest => {
+          let zg = op.new_z_generation();
+          TransmitUpdateEntry::Recorded { piece, cseq, zg }
+        },
+        &PreparedUpdateEntry::Piece { piece, ref op, .. } => {
+          TransmitUpdateEntry::Piece { piece, op }
+        },
+        PreparedUpdateEntry::Log(logent) => {
+          TransmitUpdateEntry::Log(&*logent)
+        },
+      };
+      ents.push(ue);
+    };
+    TransmitUpdate(self.gen, ents)
+  }
+}
index 48b23578742e734cf147201e7a0e298cb29322cf..031d68265ddb2f11129bcd6668e3d31acbe2e5bc 100644 (file)
@@ -463,9 +463,8 @@ function startup() {
   var es = new EventSource("/_/updates/"+ctoken+'/'+gen);
   es.onmessage = function(event) {
     console.log('GOTEVE', event)
-    var j = JSON.parse(event.data);
-    var tgen = j.pop();
-    for (var m of j) {
+    var [tgen, ms] = JSON.parse(event.data);
+    for (var m of ms) {
       var k = Object.keys(m)[0];
       messages[k](m[k]);
     }