From 763e6f2fc80868678684127de46d2a64f36dff35 Mon Sep 17 00:00:00 2001 From: Nicholas Kariniemi Date: Sat, 30 Aug 2014 07:35:43 +0300 Subject: [PATCH] Sync complete state on checksum failure --- src/clj/grub/state.clj | 36 +++++++++++++++--------------------- src/cljs/grub/state.cljs | 30 ++++++++++++------------------ 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/src/clj/grub/state.clj b/src/clj/grub/state.clj index 61ebe5a..5621299 100644 --- a/src/clj/grub/state.clj +++ b/src/clj/grub/state.clj @@ -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} (" 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 (= (hasch/uuid new-shadow) hash) - (println "After hash check: good") - (println "After hash check: FAIL")) - (>! @to-db diff))) - :complete (let [new-state (reset! server-shadow @state)] - (a/put! to (cs/complete-sync-response new-state))) - (println "Invalid msg:" msg)) - (recur) + (if-let [{:keys [type diff hash shadow-hash] :as msg} (! @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)) (remove-watch state client-id))))) (defn init [_to-db grubs recipes] diff --git a/src/cljs/grub/state.cljs b/src/cljs/grub/state.cljs index 0deac20..083c2b4 100644 --- a/src/cljs/grub/state.cljs +++ b/src/cljs/grub/state.cljs @@ -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} (" 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 (= (hasch/uuid new-shadow) hash) - (log "After hash check: good") - (log "After hash check: FAIL")))) - :complete (do (reset! client-shadow (:state msg)) - (reset! app-state (:state msg))) - (logs "Invalid msg:" msg)) - (recur))) + (if-let [{:keys [type diff hash shadow-hash] :as msg} ( 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)) + (remove-watch app-state :app-state))) (a/put! from cs/complete-sync-request)))