aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus@amatus.name>2014-08-20 21:29:09 -0500
committerDavid Barksdale <amatus@amatus.name>2014-08-20 21:29:09 -0500
commitcac12cceb58c3d71e0f33027558812a82ce5b58a (patch)
tree14dadc3c6b2e2bfe866a16dc2deac8533d499e04
parent7229075045972147d11f23dc02c68e0553bc4094 (diff)
Forgot to add core.cljs
-rw-r--r--src/cljs/gnunet_web/core.cljs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/cljs/gnunet_web/core.cljs b/src/cljs/gnunet_web/core.cljs
new file mode 100644
index 0000000..2c34c06
--- /dev/null
+++ b/src/cljs/gnunet_web/core.cljs
@@ -0,0 +1,70 @@
+;; core.cljs - core service for gnunet-web website
+;; Copyright (C) 2014 David Barksdale <amatus@amatus.name>
+;;
+;; This program is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+;;
+;; This program is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+;;
+;; You should have received a copy of the GNU General Public License
+;; along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+(ns gnunet-web.core
+ (:use [gnunet-web.hello :only (parse-hello)]
+ [gnunet-web.message :only (encode-message parse-message-types
+ parse-peer-identity)]
+ [gnunet-web.parser :only (parser parse-absolute-time parse-uint32)]
+ [gnunet-web.service :only (client-connect)])
+ (:require [goog.crypt])
+ (:require-macros [monads.macros :as monadic]))
+
+(def message-type-monitor-peers 78)
+(def message-type-monitor-notify 79)
+
+(def monitor-peers-message
+ (encode-message
+ {:message-type message-type-monitor-peers
+ :message []}))
+
+(def parse-monitor-notify-message
+ (with-meta
+ (monadic/do parser
+ [state parse-uint32
+ peer parse-peer-identity
+ timeout parse-absolute-time]
+ {:state state
+ :peer peer
+ :timeout timeout})
+ {:message-type message-type-monitor-notify}))
+
+(defn monitor-peers
+ [callback]
+ (let [message-channel (js/MessageChannel.)]
+ (set! (.-onmessage (.-port1 message-channel))
+ (fn [event]
+ (let [message (.-v ((parse-message-types
+ #{parse-monitor-notify-message})
+ (.-data event)))]
+ (if (coll? message)
+ (callback (:message (first message)))))))
+ (client-connect "core" "web app (monitor-peers)"
+ (.-port2 message-channel))
+ (.postMessage (.-port1 message-channel)
+ (into-array monitor-peers-message))))
+
+(def state-strings
+ {0 "Down"
+ 1 "Key Sent"
+ 2 "Key Received"
+ 3 "Connected"
+ 4 "Re-keying"
+ 5 "Disconnected"})
+
+(defn state->string
+ [state]
+ (get state-strings state))