Issue #4: Improved sync process. Only unsynced operations are sent now
This commit is contained in:
@@ -53,7 +53,7 @@ createIwcConnector = (callback, options)->
|
||||
@initialized[him] = true
|
||||
iwcHandler["Yatta_push_HB_element"] = [receiveHB]
|
||||
|
||||
@sendIwcIntent "Yatta_get_HB_element", {}
|
||||
@sendIwcIntent "Yatta_get_HB_element", @HB.getOperationCounter()
|
||||
|
||||
receive_ = (intent)=>
|
||||
o = intent.extras
|
||||
@@ -65,9 +65,11 @@ createIwcConnector = (callback, options)->
|
||||
if received_HB?
|
||||
@engine.applyOpsCheckDouble received_HB
|
||||
|
||||
sendHistoryBuffer = ()=>
|
||||
sendHistoryBuffer = (intent)=>
|
||||
state_vector = intent.extras
|
||||
console.log state_vector
|
||||
json =
|
||||
HB : @yatta.getHistoryBuffer()._encode()
|
||||
HB : @yatta.getHistoryBuffer()._encode(state_vector)
|
||||
user : @yatta.getUserId()
|
||||
@sendIwcIntent "Yatta_push_HB_element", json
|
||||
@iwcHandler["Yatta_get_HB_element"] = [sendHistoryBuffer]
|
||||
|
||||
@@ -35,7 +35,6 @@ createPeerJsConnector = ()->
|
||||
@connections = {}
|
||||
|
||||
@peer.on 'connection', (conn)=>
|
||||
conn.send "hey" # is never send. But without it it won't work either..
|
||||
@addConnection conn
|
||||
|
||||
send_ = (o)=>
|
||||
@@ -50,29 +49,47 @@ createPeerJsConnector = ()->
|
||||
for conn_id of @connections
|
||||
conn_id
|
||||
|
||||
#
|
||||
# What this method does:
|
||||
# * Send state vector
|
||||
# * Receive HB -> apply them
|
||||
# * Send connections
|
||||
# * Receive Connections -> Connect to unknow connections
|
||||
#
|
||||
addConnection: (conn)->
|
||||
@connections[conn.peer] = conn
|
||||
|
||||
initialized_me = false
|
||||
initialized_him = false
|
||||
conn.on 'data', (data)=>
|
||||
if data is "hey"
|
||||
console.log "Yatta: Connection received with init message (debug)" # I can remove this hey stuff when this happens.
|
||||
if data is "empty_message"
|
||||
# nop
|
||||
else if data.HB?
|
||||
initialized_me = true
|
||||
@engine.applyOpsCheckDouble data.HB
|
||||
conn.send
|
||||
conns: @getAllConnectionIds()
|
||||
else if data.op?
|
||||
@engine.applyOp data.op
|
||||
else if data.conns?
|
||||
for conn_id in data.conns
|
||||
@connectToPeer conn_id
|
||||
else if data.state_vector?
|
||||
if not initialized_him
|
||||
# make sure, that it is sent only once
|
||||
conn.send
|
||||
HB: @yatta.getHistoryBuffer()._encode(data.state_vector)
|
||||
initialized_him = true
|
||||
else
|
||||
throw new Error "Can't parse this operation"
|
||||
|
||||
sendHB = ()=>
|
||||
sendStateVector = ()=>
|
||||
conn.send
|
||||
HB: @yatta.getHistoryBuffer()._encode()
|
||||
conn.send
|
||||
conns: @getAllConnectionIds()
|
||||
|
||||
setTimeout sendHB, 1000
|
||||
state_vector: @HB.getOperationCounter()
|
||||
if not initialized_me
|
||||
# Because of a bug in PeerJs,
|
||||
# we never know if state vector was actually sent
|
||||
setTimeout sendStateVector, 100
|
||||
sendStateVector()
|
||||
|
||||
#
|
||||
# This function is called whenever an operation was executed.
|
||||
|
||||
Reference in New Issue
Block a user