aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus@amatus.name>2014-07-31 20:11:18 -0500
committerDavid Barksdale <amatus@amatus.name>2014-07-31 20:11:18 -0500
commit8a05e19cefb0aa858a8ed322ea09a43350cb6a3a (patch)
tree0fdb2605274c503ea8385b427fae8fd104536622
parent1d3344c9825318127ea71647aa0d3a4e54e149d9 (diff)
Parse peer iterate reply messages
-rw-r--r--src/cljs/gnunet_web/encoder.cljs2
-rw-r--r--src/cljs/gnunet_web/hello.cljs10
-rw-r--r--src/cljs/gnunet_web/parser.cljs2
-rw-r--r--src/cljs/gnunet_web/transport.cljs39
4 files changed, 43 insertions, 10 deletions
diff --git a/src/cljs/gnunet_web/encoder.cljs b/src/cljs/gnunet_web/encoder.cljs
index 1d29b90..de0acad 100644
--- a/src/cljs/gnunet_web/encoder.cljs
+++ b/src/cljs/gnunet_web/encoder.cljs
@@ -45,4 +45,4 @@
(goog.crypt/stringToUtf8ByteArray x)
[0]))
-(def encode-date encode-uint64)
+(def encode-absolute-time encode-uint64)
diff --git a/src/cljs/gnunet_web/hello.cljs b/src/cljs/gnunet_web/hello.cljs
index a5ba291..250d841 100644
--- a/src/cljs/gnunet_web/hello.cljs
+++ b/src/cljs/gnunet_web/hello.cljs
@@ -17,10 +17,10 @@
(ns gnunet-web.hello
(:use [amatus.datastructures :only (flatten-nested-maps nested-group-by)]
[clojure.set :only (difference union)]
- [gnunet-web.encoder :only (encode-date encode-uint16 encode-uint32
- encode-utf8)]
+ [gnunet-web.encoder :only (encode-absolute-time encode-uint16
+ encode-uint32 encode-utf8)]
[gnunet-web.message :only (encode-message parse-peer-identity)]
- [gnunet-web.parser :only (items none-or-more parser parse-date
+ [gnunet-web.parser :only (items none-or-more parser parse-absolute-time
parse-uint16 parse-uint32 parse-utf8)])
(:require-macros [monads.macros :as monadic]))
@@ -30,7 +30,7 @@
(monadic/do parser
[transport parse-utf8
address-length parse-uint16
- expiration parse-date
+ expiration parse-absolute-time
encoded-address (items address-length)]
{:transport transport
:expiration expiration
@@ -43,7 +43,7 @@
(concat
(encode-utf8 transport)
(encode-uint16 (count encoded-address))
- (encode-date expiration)
+ (encode-absolute-time expiration)
encoded-address))
(defn latest-expiration
diff --git a/src/cljs/gnunet_web/parser.cljs b/src/cljs/gnunet_web/parser.cljs
index 4bf1fe1..6f915ac 100644
--- a/src/cljs/gnunet_web/parser.cljs
+++ b/src/cljs/gnunet_web/parser.cljs
@@ -110,4 +110,4 @@
zero (items 1)]
(goog.crypt/utf8ByteArrayToString (to-array xs))))
-(def parse-date parse-uint64)
+(def parse-absolute-time parse-uint64)
diff --git a/src/cljs/gnunet_web/transport.cljs b/src/cljs/gnunet_web/transport.cljs
index b90a5b1..d027bbf 100644
--- a/src/cljs/gnunet_web/transport.cljs
+++ b/src/cljs/gnunet_web/transport.cljs
@@ -16,10 +16,16 @@
(ns gnunet-web.transport
(:use [gnunet-web.encoder :only (encode-uint32)]
- [gnunet-web.message :only (encode-message)]
- [gnunet-web.service :only (client-connect)]))
+ [gnunet-web.message :only (encode-message parse-message-types
+ parse-peer-identity)]
+ [gnunet-web.parser :only (items optional 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-peer-request 380)
+(def message-type-peer-iterate-reply 383)
(defn encode-monitor-peer-request-message
[{:keys [one-shot peer] :or {one-shot false peer (repeat 32 0)}}]
@@ -30,12 +36,39 @@
(encode-uint32 one-shot)
peer)}))
+(def parse-peer-iterate-reply
+ (with-meta
+ (optional
+ (monadic/do parser
+ [reserved parse-uint32
+ peer parse-peer-identity
+ state-timeout parse-absolute-time
+ local-address-info parse-uint32
+ state parse-uint32
+ address-length parse-uint32
+ plugin-length parse-uint32
+ address (items address-length)
+ plugin-bytes (items plugin-length)
+ :let [plugin (goog.crypt/utf8ByteArrayToString
+ (to-array (.apply js/Array (array)
+ plugin-bytes)))]]
+ {:peer peer
+ :state-timeout state-timeout
+ :local-address-info local-address-info
+ :state state
+ :address (vec (.apply js/Array (array) address))
+ :plugin plugin}))
+ {:message-type message-type-peer-iterate-reply}))
+
(defn monitor-peers
[callback]
(let [message-channel (js/MessageChannel.)]
(set! (.-onmessage (.-port1 message-channel))
(fn [event]
- (.log js/console "monitor-peers" event)))
+ (let [message (first (.-v ((parse-message-types
+ #{parse-peer-iterate-reply})
+ (.-data event))))]
+ (callback message))))
(client-connect "transport" "web app" (.-port2 message-channel))
(.postMessage (.-port1 message-channel)
(into-array (encode-monitor-peer-request-message {})))))