Issue #4: Improved sync process. Only unsynced operations are sent now

This commit is contained in:
Kevin Jahns
2014-08-22 20:50:39 +02:00
parent ad5898a77a
commit 77739deda3
48 changed files with 3403 additions and 6930 deletions

View File

@@ -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]

View File

@@ -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.

View File

@@ -44,7 +44,7 @@ class HistoryBuffer
#
# Encode this operation in such a way that it can be parsed by remote peers.
#
# TODO: Make this more efficient!
_encode: (state_vector={})->
json = []
unknown = (user, o_number)->
@@ -55,15 +55,18 @@ class HistoryBuffer
for u_name,user of @buffer
for o_number,o of user
if (not isNaN(parseInt(o_number))) and unknown(u_name, o_number)
# its necessary to send it, and not known in state_vector
o_json = o._encode()
if o.next_cl?
# search for the next _known_ operation. (When state_vector is {} then this is the Delimiter)
o_next = o.next_cl
while o_next.next_cl? and unknown(o_next.creator, o_next.op_number)
o_next = o_next.next_cl
o_json.next = o_next.getUid()
else if o.prev_cl?
# same as the above with prev.
o_prev = o.prev_cl
while o_prev.prev_cl? and unknown(o_next.creator, o_next.op_number)
while o_prev.prev_cl? and unknown(o_prev.creator, o_prev.op_number)
o_prev = o_prev.prev_cl
o_json.prev = o_prev.getUid()
json.push o_json
@@ -114,7 +117,14 @@ class HistoryBuffer
if not @operation_counter[o.creator]?
@operation_counter[o.creator] = 0
if typeof o.op_number is 'number' and o.creator isnt @getUserId()
@operation_counter[o.creator]++
# TODO: fix this issue better.
# Operations should income in order
# Then you don't have to do this..
if o.op_number is @operation_counter[o.creator]
@operation_counter[o.creator]++
while @getOperation({creator:o.creator, op_number: @operation_counter[o.creator]})?
@operation_counter[o.creator]++
#if @operation_counter[o.creator] isnt (o.op_number + 1)
#console.log (@operation_counter[o.creator] - (o.op_number + 1))
#console.log o