Working hash checks a la hasch

This commit is contained in:
Nicholas Kariniemi 2014-08-13 21:37:17 +03:00
parent a672155a92
commit 4ac42b0cf3
4 changed files with 22 additions and 16 deletions

View file

@ -17,7 +17,8 @@
[clj-webdriver "0.6.1" :exclusions [org.clojure/core.cache]]
[om "0.7.0"]
[sablono "0.2.17"]
[cljs-uuid "0.0.4"]]
[cljs-uuid "0.0.4"]
[net.polyc0l0r/hasch "0.2.3"]]
:profiles {:uberjar {:aot :all}}
:min-lein-version "2.1.2"
:plugins [[lein-cljsbuild "1.0.3"]

View file

@ -1,7 +1,8 @@
(ns grub.state
(:require [grub.sync :as sync]
[grub.util :as util]
[clojure.core.async :as a :refer [<! >! chan go]]))
[clojure.core.async :as a :refer [<! >! chan go]]
[hasch.core :as hasch]))
(def empty-state
{:grubs {}
@ -21,13 +22,13 @@
(when (not= state* server-shadow*)
(let [diff (sync/diff-states server-shadow* state*)
msg {:diff diff
:hash (hash state*)
:shadow-hash (hash server-shadow*)}]
:hash (hasch/uuid state*)
:shadow-hash (hasch/uuid server-shadow*)}]
(println "Sync because:")
(println "Server = " state*)
(println "Client = " server-shadow*)
(println "Diff:" diff)
(println "Send" (hash server-shadow*) "->" (hash state*))
(println "Send" (hasch/uuid server-shadow*) "->" (hasch/uuid state*))
(a/put! to-client msg)
;; TODO: only reset server shadow if send succeeds
(reset! server-shadow state*)))))
@ -41,15 +42,15 @@
(if-let [{:keys [diff hash shadow-hash]} (<! from)]
(do
(println "Received client diff:" shadow-hash "->" hash)
(println "Before shadow:" (clojure.core/hash @server-shadow) @server-shadow)
(if (= (clojure.core/hash @server-shadow) shadow-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:" (clojure.core/hash new-shadow) new-shadow)
(if (= (clojure.core/hash new-shadow) hash)
(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)

View file

@ -1,6 +1,7 @@
(ns grub.state
(:require [grub.sync :as sync]
[cljs.core.async :as a :refer [<! >! chan]])
[cljs.core.async :as a :refer [<! >! chan]]
[hasch.core :as hasch])
(:require-macros [grub.macros :refer [log logs]]
[cljs.core.async.macros :refer [go go-loop]]))
@ -13,12 +14,14 @@
(go-loop []
(when-let [{:keys [diff hash shadow-hash]} (<! in)]
(logs "Received server diff:" shadow-hash "->" hash)
(if (= (cljs.core/hash @client-shadow) shadow-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))]
(if (= (cljs.core/hash new-shadow) hash)
(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"))
(recur))))

View file

@ -5,7 +5,8 @@
[cljs.reader]
goog.net.WebSocket
goog.events.EventHandler
goog.events.EventTarget)
goog.events.EventTarget
[hasch.core :as hasch])
(:require-macros [cljs.core.async.macros :refer [go go-loop]]
[grub.macros :refer [log logs]]))
@ -18,13 +19,13 @@
client-shadow @state/client-shadow
diff (sync/diff-states client-shadow app-state)
msg {:diff diff
:hash (hash app-state)
:shadow-hash (hash client-shadow)}]
:hash (hasch/uuid app-state)
:shadow-hash (hasch/uuid client-shadow)}]
(logs "Sync because:")
(logs "Server = " client-shadow)
(logs "Client = " app-state)
(logs "Diff:" diff)
(logs "Send" (hash client-shadow) "->" (hash app-state))
(logs "Send" (hasch/uuid client-shadow) "->" (hasch/uuid app-state))
;; TODO: reset client shadow only if send succeeds
(.send @websocket* msg)
(reset! state/client-shadow app-state))))