Sync complete state on checksum failure

This commit is contained in:
Nicholas Kariniemi 2014-08-30 07:35:43 +03:00
parent bd08ea5ec5
commit 763e6f2fc8
2 changed files with 27 additions and 39 deletions

View file

@ -18,30 +18,24 @@
(let [client-id (java.util.UUID/randomUUID)
server-shadow (atom cs/empty-state)]
(add-watch state client-id (fn [_ _ _ new-state]
(when-let [msg (cs/diff-states new-state server-shadow)]
(when-let [msg (cs/diff-states new-state @server-shadow)]
(a/put! to msg)
(reset! server-shadow new-state))))
(a/go-loop []
(when-let [{:keys [type diff hash shadow-hash] :as msg} (<! from)]
(condp = type
:diff (do
(println "Received client diff:" shadow-hash "->" hash)
(println "Before shadow:" (hasch/uuid @server-shadow) @server-shadow)
(if (= (hasch/uuid @server-shadow) shadow-hash)
(println "Before hash check: good")
(println "Before hash check: FAIL"))
(let [new-shadow (swap! server-shadow #(sync/patch-state % diff))
new-state (swap! state #(sync/patch-state % diff))]
;; TODO: check if hashes match
(println "After shadow:" (hasch/uuid new-shadow) new-shadow)
(if-let [{:keys [type diff hash shadow-hash] :as msg} (<! from)]
(do (condp = type
:diff (let [new-shadow (swap! server-shadow sync/patch-state diff)]
(if (= (hasch/uuid new-shadow) hash)
(println "After hash check: good")
(println "After hash check: FAIL"))
(>! @to-db diff)))
(let [new-state (swap! state sync/patch-state diff)]
(>! @to-db diff))
(do (println "Hash check failed --> complete sync")
(let [sync-state @state]
(reset! server-shadow sync-state)
(a/put! to (cs/complete-sync-response sync-state))))))
:complete (let [new-state (reset! server-shadow @state)]
(a/put! to (cs/complete-sync-response new-state)))
(println "Invalid msg:" msg))
(recur)
(recur))
(remove-watch state client-id)))))
(defn init [_to-db grubs recipes]

View file

@ -16,22 +16,16 @@
;; TODO: reset shadow only if send succeeds
(reset! client-shadow new))))
(go-loop []
(when-let [{:keys [type diff hash shadow-hash] :as msg} (<! to)]
(condp = type
:diff (do
;(logs "Received server diff:" shadow-hash "->" hash)
;(logs "Before shadow:" (hasch/uuid @client-shadow) @client-shadow)
(if (= (hasch/uuid @client-shadow) shadow-hash)
(log "Before hash check: good")
(log "Before hash check: FAIL"))
(let [new-shadow (swap! client-shadow #(sync/patch-state % diff))
new-state (swap! app-state #(sync/patch-state % diff))]
;(logs "After shadow:" (hasch/uuid @client-shadow) @client-shadow)
(if-let [{:keys [type diff hash shadow-hash] :as msg} (<! to)]
(do (condp = type
:diff (let [new-shadow (swap! client-shadow #(sync/patch-state % diff))]
(if (= (hasch/uuid new-shadow) hash)
(log "After hash check: good")
(log "After hash check: FAIL"))))
(swap! app-state #(sync/patch-state % diff))
(do (log "Hash check failed --> complete sync")
(a/put! from cs/complete-sync-request))))
:complete (do (reset! client-shadow (:state msg))
(reset! app-state (:state msg)))
(logs "Invalid msg:" msg))
(recur)))
(recur))
(remove-watch app-state :app-state)))
(a/put! from cs/complete-sync-request)))