From 6a3d64e18456fb394fe393044062f0d1334c8d1c Mon Sep 17 00:00:00 2001 From: michaelr Date: Sun, 6 Mar 2016 21:31:24 +0200 Subject: [PATCH] simple chat app used to test protocol lib --- cljs-tests/src/cljs_tests/protocol/api.cljs | 59 -------- .../src/cljs_tests/protocol/delivery.cljs | 55 -------- .../src/cljs_tests/protocol/handler.cljs | 5 - .../cljs_tests/protocol/state/delivery.cljs | 36 ----- .../src/cljs_tests/protocol/state/state.cljs | 30 ----- .../src/cljs_tests/protocol/whisper.cljs | 127 ------------------ cljs-tests/src/cljs_tests/utils/logging.cljs | 23 ---- simple-whisper-chat/.gitignore | 13 ++ .../project.clj | 46 +++---- .../resources/public/css/style.css | 0 .../resources/public/index.html | 2 +- .../resources/public/vendor/js/web3-light.js | 0 .../public/vendor/js/web3-light.min.js | 0 .../resources/public/vendor/js/web3.js | 0 .../resources/public/vendor/js/web3.js.map | 0 .../resources/public/vendor/js/web3.min.js | 0 .../src/cljs/syng_im}/core.cljs | 8 +- 17 files changed, 40 insertions(+), 364 deletions(-) delete mode 100644 cljs-tests/src/cljs_tests/protocol/api.cljs delete mode 100644 cljs-tests/src/cljs_tests/protocol/delivery.cljs delete mode 100644 cljs-tests/src/cljs_tests/protocol/handler.cljs delete mode 100644 cljs-tests/src/cljs_tests/protocol/state/delivery.cljs delete mode 100644 cljs-tests/src/cljs_tests/protocol/state/state.cljs delete mode 100644 cljs-tests/src/cljs_tests/protocol/whisper.cljs delete mode 100644 cljs-tests/src/cljs_tests/utils/logging.cljs create mode 100644 simple-whisper-chat/.gitignore rename {cljs-tests => simple-whisper-chat}/project.clj (60%) rename {cljs-tests => simple-whisper-chat}/resources/public/css/style.css (100%) rename {cljs-tests => simple-whisper-chat}/resources/public/index.html (94%) rename {cljs-tests => simple-whisper-chat}/resources/public/vendor/js/web3-light.js (100%) rename {cljs-tests => simple-whisper-chat}/resources/public/vendor/js/web3-light.min.js (100%) rename {cljs-tests => simple-whisper-chat}/resources/public/vendor/js/web3.js (100%) rename {cljs-tests => simple-whisper-chat}/resources/public/vendor/js/web3.js.map (100%) rename {cljs-tests => simple-whisper-chat}/resources/public/vendor/js/web3.min.js (100%) rename {cljs-tests/src/cljs_tests => simple-whisper-chat/src/cljs/syng_im}/core.cljs (96%) diff --git a/cljs-tests/src/cljs_tests/protocol/api.cljs b/cljs-tests/src/cljs_tests/protocol/api.cljs deleted file mode 100644 index d8003a4..0000000 --- a/cljs-tests/src/cljs_tests/protocol/api.cljs +++ /dev/null @@ -1,59 +0,0 @@ -(ns cljs-tests.protocol.api - (:require [cljs.core.async :refer [> (state/delivery-queue) - (peek))] - (when timestamp - (if (expired? timestamp) - (do (state/pop-delivery-queue) - (cons item (delivery-expired-seq))) - nil))))) - -(defn start-delivery-loop [] - (go (loop [_ ( (assoc-in state [:pending-messages msg-id] {:msg msg - :retry-count 0}) - (push-msg-to-delivery-queue msg-id))))) - -(defn pop-delivery-queue [] - (swap! state update-in [:delivery-queue] pop)) - -(defn push-delivery-queue [msg-id] - (swap! state push-msg-to-delivery-queue msg-id)) - -(defn remove-pending-message [msg-id] - (log/info "Removing message" msg-id "from pending") - (swap! state update-in [:pending-messages] dissoc msg-id)) - -(defn delivery-queue [] - (:delivery-queue @state)) diff --git a/cljs-tests/src/cljs_tests/protocol/state/state.cljs b/cljs-tests/src/cljs_tests/protocol/state/state.cljs deleted file mode 100644 index 98e1417..0000000 --- a/cljs-tests/src/cljs_tests/protocol/state/state.cljs +++ /dev/null @@ -1,30 +0,0 @@ -(ns cljs-tests.protocol.state.state - (:require [cljs-time.core :as t])) - -(def state (atom {:pending-messages {} - :filters {} - :delivery-queue #queue [] - :handler nil - :identity nil - :connection nil})) - -(defn add-filter [topics filter] - (swap! state assoc-in [:filters topics] filter)) - -(defn set-handler [handler] - (swap! state assoc :handler handler)) - -(defn set-identity [identity] - (swap! state assoc :identity identity)) - -(defn set-connection [connection] - (swap! state assoc :connection connection)) - -(defn connection [] - (:connection @state)) - -(defn my-identity [] - (:identity @state)) - -(defn handler [] - (:handler @state)) diff --git a/cljs-tests/src/cljs_tests/protocol/whisper.cljs b/cljs-tests/src/cljs_tests/protocol/whisper.cljs deleted file mode 100644 index e3543b7..0000000 --- a/cljs-tests/src/cljs_tests/protocol/whisper.cljs +++ /dev/null @@ -1,127 +0,0 @@ -(ns cljs-tests.protocol.whisper - (:require [cljs.core.async :refer [chan put! close! > {:topics (mapv from-ascii topics)} - (clj->js))) - -(defn make-web3 [rpc-url] - (->> (js/Web3.providers.HttpProvider. rpc-url) - (js/Web3.))) - -(defn make-callback [{:keys [error-msg result-channel]}] - (fn [error result] - (if error - (do - (log/error (str error-msg ":") error) - (handler/invoke-handler :error {:error-msg error-msg - :details error})) - (put! result-channel result)) - (close! result-channel))) - -(defn new-identity [web3] - (let [result-channel (chan) - callback (make-callback {:error-msg "Call to newIdentity failed" - :result-channel result-channel})] - (.newIdentity (.-shh web3) callback) - result-channel)) - -(defn handle-ack [{:keys [ack-msg-id]}] - (log/info "Got ack for message:" ack-msg-id) - (delivery/remove-pending-message ack-msg-id) - (handler/invoke-handler :msg-acked {:msg-id ack-msg-id})) - -(defn post-msg [web3 msg] - (let [js-msg (clj->js msg)] - (log/info "Sending whisper message:" js-msg) - (-> (whisper web3) - (.post js-msg (fn [error result] - (when error - (let [error-msg "Call to shh.post() failed"] - (log/error (str error-msg ":") error) - (handler/invoke-handler :error {:error-msg error-msg - :details error})))))))) - -(defn make-msg - "Returns [msg-id msg], `msg` is formed for Web3.shh.post()" - [{:keys [from to ttl topics payload] - :or {ttl syng-msg-ttl - topics []}}] - (let [msg-id (.guid js/chance)] - {:msg-id msg-id - :msg (cond-> {:ttl ttl - :topics (->> (conj topics syng-app-topic) - (mapv from-ascii)) - :payload (->> (merge payload {:msg-id msg-id}) - (str) - (from-ascii))} - from (assoc :from from) - to (assoc :to to))})) - -(defn send-ack [web3 to msg-id] - (log/info "Acking message:" msg-id "To:" to) - (let [{:keys [msg]} (make-msg {:from (state/my-identity) - :to to - :payload {:type :ack - :ack-msg-id msg-id}})] - (post-msg web3 msg))) - -(defn handle-user-msg [web3 from {:keys [msg-id] :as payload}] - (send-ack web3 from msg-id) - (handler/invoke-handler :new-msg {:from from - :payload payload})) - -(defn handle-arriving-whisper-msg [web3 msg] - (log/info "Got whisper message:" msg) - (let [{from :from - to :to - topics :topics ;; always empty (bug in go-ethereum?) - payload :payload - :as msg} (js->clj msg :keywordize-keys true)] - (if (= to (state/my-identity)) - (let [{msg-type :type - msg-id :msg-id - :as payload} (->> (to-ascii payload) - (read-string))] - (case msg-type - :ack (handle-ack payload) - :user-msg (handle-user-msg web3 from payload))) - (log/warn "My identity:" (state/my-identity) "Message To:" to "Message is encrypted for someone else, ignoring")))) - -(defn listen - "Returns a filter which can be stopped with (stop-whisper-listener)" - [web3] - (let [topics [syng-app-topic] - shh (whisper web3) - filter (.filter shh (make-topics topics) (fn [error msg] - (if error - (handler/invoke-handler :error {:error-msg error}) - (handle-arriving-whisper-msg web3 msg))))] - (state/add-filter topics filter))) - -(defn stop-listener [filter] - (.stopWatching filter)) - - - - diff --git a/cljs-tests/src/cljs_tests/utils/logging.cljs b/cljs-tests/src/cljs_tests/utils/logging.cljs deleted file mode 100644 index 533f03c..0000000 --- a/cljs-tests/src/cljs_tests/utils/logging.cljs +++ /dev/null @@ -1,23 +0,0 @@ -(ns cljs-tests.utils.logging - (:require [cljs-time.core :as t] - [cljs-time.format :as tf])) - -(defn timestamp [] - (tf/unparse (:hour-minute-second-fraction tf/formatters) (t/now))) - -(defn info [& args] - (let [args (cons (timestamp) args)] - (.apply (.-log js/console) js/console (into-array args)))) - -(defn warn [& args] - (let [args (cons (timestamp) args)] - (.apply (.-warn js/console) js/console (into-array args)))) - -(defn error [& args] - (let [args (cons (timestamp) args)] - (.apply (.-error js/console) js/console (into-array args)))) - - -(comment - - ) \ No newline at end of file diff --git a/simple-whisper-chat/.gitignore b/simple-whisper-chat/.gitignore new file mode 100644 index 0000000..cdd1de3 --- /dev/null +++ b/simple-whisper-chat/.gitignore @@ -0,0 +1,13 @@ +/resources/public/js/compiled/** +figwheel_server.log +pom.xml +*jar +/lib/ +/classes/ +/out/ +/target/ +.lein-deps-sum +.lein-repl-history +.lein-plugins/ +.repl +.nrepl-port diff --git a/cljs-tests/project.clj b/simple-whisper-chat/project.clj similarity index 60% rename from cljs-tests/project.clj rename to simple-whisper-chat/project.clj index 352d98a..f3b5033 100644 --- a/cljs-tests/project.clj +++ b/simple-whisper-chat/project.clj @@ -1,8 +1,8 @@ -(defproject cljs-tests "0.1.0-SNAPSHOT" +(defproject syng-im/simple-whisper-chat "0.1.0-SNAPSHOT" :description "FIXME: write this!" :url "http://example.com/FIXME" :license {:name "Eclipse Public License" - :url "http://www.eclipse.org/legal/epl-v10.html"} + :url "http://www.eclipse.org/legal/epl-v10.html"} :min-lein-version "2.5.3" @@ -14,7 +14,7 @@ ;; cljs deps [cljsjs/chance "0.7.3-0"] [com.andrewmcveigh/cljs-time "0.4.0"] - ] + [syng-im/protocol "0.1.0"]] :plugins [[lein-figwheel "0.5.0-6"] [lein-cljsbuild "1.1.2" :exclusions [[org.clojure/clojure]]]] @@ -24,40 +24,38 @@ :clean-targets ^{:protect false} ["resources/public/js/compiled" "target"] :cljsbuild {:builds - [{:id "dev" - :source-paths ["src"] + [{:id "dev" + :source-paths ["src/cljs"] ;; If no code is to be run, set :figwheel true for continued automagical reloading - :figwheel {:on-jsload "cljs-tests.core/on-js-reload"} + :figwheel {:on-jsload "syng-im.core/on-js-reload"} - :compiler {:main cljs-tests.core - :asset-path "js/compiled/out" - :output-to "resources/public/js/compiled/cljs_tests.js" - :output-dir "resources/public/js/compiled/out" - :source-map-timestamp true - :foreign-libs [{:file "resources/public/vendor/js/web3.js" - :file-min "resources/public/vendor/js/web3.min.js" - :provides ["cljsjs.web3"] - ;:requires ["cljsjs.jquery"] - }]}} + :compiler {:main syng-im.core + :asset-path "js/compiled/out" + :output-to "resources/public/js/compiled/syng_im.js" + :output-dir "resources/public/js/compiled/out" + :source-map-timestamp true + :foreign-libs [{:file "resources/public/vendor/js/web3.js" + :file-min "resources/public/vendor/js/web3.min.js" + :provides ["cljsjs.web3"]}]}} ;; This next build is an compressed minified build for ;; production. You can build this with: ;; lein cljsbuild once min - {:id "min" - :source-paths ["src"] - :compiler {:output-to "resources/public/js/compiled/cljs_tests.js" - :main cljs-tests.core - :optimizations :advanced - :pretty-print false}}]} + {:id "min" + :source-paths ["src/cljs"] + :compiler {:output-to "resources/public/js/compiled/syng_im.js" + :main syng-im.core + :optimizations :advanced + :pretty-print false}}]} :figwheel {;; :http-server-root "public" ;; default and assumes "resources" ;; :server-port 3449 ;; default ;; :server-ip "127.0.0.1" - :css-dirs ["resources/public/css"] ;; watch and update CSS + :css-dirs ["resources/public/css"] ;; watch and update CSS ;; Start an nREPL server into the running figwheel process - :nrepl-port 7888 + :nrepl-port 7888 ;; Server Ring Handler (optional) ;; if you want to embed a ring handler into the figwheel http-kit diff --git a/cljs-tests/resources/public/css/style.css b/simple-whisper-chat/resources/public/css/style.css similarity index 100% rename from cljs-tests/resources/public/css/style.css rename to simple-whisper-chat/resources/public/css/style.css diff --git a/cljs-tests/resources/public/index.html b/simple-whisper-chat/resources/public/index.html similarity index 94% rename from cljs-tests/resources/public/index.html rename to simple-whisper-chat/resources/public/index.html index 4de63c2..a11d50d 100644 --- a/cljs-tests/resources/public/index.html +++ b/simple-whisper-chat/resources/public/index.html @@ -43,6 +43,6 @@ - + diff --git a/cljs-tests/resources/public/vendor/js/web3-light.js b/simple-whisper-chat/resources/public/vendor/js/web3-light.js similarity index 100% rename from cljs-tests/resources/public/vendor/js/web3-light.js rename to simple-whisper-chat/resources/public/vendor/js/web3-light.js diff --git a/cljs-tests/resources/public/vendor/js/web3-light.min.js b/simple-whisper-chat/resources/public/vendor/js/web3-light.min.js similarity index 100% rename from cljs-tests/resources/public/vendor/js/web3-light.min.js rename to simple-whisper-chat/resources/public/vendor/js/web3-light.min.js diff --git a/cljs-tests/resources/public/vendor/js/web3.js b/simple-whisper-chat/resources/public/vendor/js/web3.js similarity index 100% rename from cljs-tests/resources/public/vendor/js/web3.js rename to simple-whisper-chat/resources/public/vendor/js/web3.js diff --git a/cljs-tests/resources/public/vendor/js/web3.js.map b/simple-whisper-chat/resources/public/vendor/js/web3.js.map similarity index 100% rename from cljs-tests/resources/public/vendor/js/web3.js.map rename to simple-whisper-chat/resources/public/vendor/js/web3.js.map diff --git a/cljs-tests/resources/public/vendor/js/web3.min.js b/simple-whisper-chat/resources/public/vendor/js/web3.min.js similarity index 100% rename from cljs-tests/resources/public/vendor/js/web3.min.js rename to simple-whisper-chat/resources/public/vendor/js/web3.min.js diff --git a/cljs-tests/src/cljs_tests/core.cljs b/simple-whisper-chat/src/cljs/syng_im/core.cljs similarity index 96% rename from cljs-tests/src/cljs_tests/core.cljs rename to simple-whisper-chat/src/cljs/syng_im/core.cljs index d4b5161..1be1338 100644 --- a/cljs-tests/src/cljs_tests/core.cljs +++ b/simple-whisper-chat/src/cljs/syng_im/core.cljs @@ -1,6 +1,6 @@ -(ns cljs-tests.core - (:require [cljs-tests.protocol.api :as p] - [cljs-tests.utils.logging :as log] +(ns syng-im.core + (:require [syng-im.protocol.api :as p] + [syng-im.utils.logging :as log] [goog.dom :as g] [goog.dom.forms :as f] [goog.events :as e] @@ -95,7 +95,7 @@ ;(p/make-whisper-msg web3-2 user2-ident user1-ident "Hello World!") - (require '[cljs-tests.protocol.whisper :as w]) + (require '[syng-im.protocol.whisper :as w]) (def web3 (w/make-web3 "http://localhost:4546")) (.newIdentity (w/whisper web3) (fn [error result] (println error result)))