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{}
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();
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 ----------
}
}
-// ---------- piece updates ----------
+// ---------- PieceUpdatesOp ----------
impl<NS> PieceUpdateOp<NS> {
pub fn new_state(&self) -> Option<&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)
+ }
+}
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]);
}