summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-09-23 01:36:25 -0500
committerDavid Barksdale <amatus.amongus@gmail.com>2010-09-23 01:36:25 -0500
commit3331c94437279578d0f2451e1009cb8d15cbd323 (patch)
treea4d7b55ff07f8cdda9a744f6326ab2522454df71
parent0b522b0d7f3b9542c72380c865699a92179d0a22 (diff)
Moved some functions around and added more doc strings.
-rw-r--r--src/org/gnu/clojure/gnunet/hostlist.clj41
-rw-r--r--src/org/gnu/clojure/gnunet/inet.clj19
-rw-r--r--src/org/gnu/clojure/gnunet/peer.clj43
-rw-r--r--src/org/gnu/clojure/gnunet/tcp.clj19
4 files changed, 65 insertions, 57 deletions
diff --git a/src/org/gnu/clojure/gnunet/hostlist.clj b/src/org/gnu/clojure/gnunet/hostlist.clj
index 97b026c..2edf064 100644
--- a/src/org/gnu/clojure/gnunet/hostlist.clj
+++ b/src/org/gnu/clojure/gnunet/hostlist.clj
@@ -1,11 +1,42 @@
(ns org.gnu.clojure.gnunet.hostlist
- (:use (org.gnu.clojure.gnunet parser message hello iostream)))
+ (:use (org.gnu.clojure.gnunet parser message hello iostream peer identity))
+ (:import (java.util Date TimerTask)))
-(defn download-hostlist
- "Calls the hello-processor on all parsed hello messages at the given URL."
- [hello-processor url]
+(defn download-hostlist!
+ "Calls hello-processor! on all parsed hello messages at the given URL."
+ [hello-processor! url]
(doseq [{hello :message} (first
((none-or-more (parse-message-types
{message-type-hello parse-hello}))
(read-url url)))]
- (hello-processor hello)))
+ (hello-processor! hello)))
+
+;; Event - Peer receives a HELLO message
+(defn admit-hello!
+ "Updates the remote-peers map with new information contained in a hello."
+ [peer hello]
+ (letfn [(update-transports
+ [transports new-transports]
+ (merge-transports (Date.)
+ transports
+ (list-transports new-transports)))
+ (update-remote-peers
+ [remote-peers hello]
+ (let [id (vec (generate-id (:public-key hello)))
+ remote-peer (remote-peers id)]
+ (if remote-peer
+ (do
+ (send
+ (:transports-agent remote-peer)
+ update-transports
+ (:transports hello))
+ remote-peers)
+ (assoc remote-peers id (new-remote-peer-from-hello hello)))))]
+ (send (:remote-peers-agent peer) update-remote-peers hello)))
+
+(defn create-hostlist-timer-task
+ "Creates a TimerTask that when run fetches the hostlist at url and updates
+ peer."
+ [peer url]
+ (proxy [java.util.TimerTask] []
+ (run [] (download-hostlist (partial admit-hello! peer) url)))) \ No newline at end of file
diff --git a/src/org/gnu/clojure/gnunet/inet.clj b/src/org/gnu/clojure/gnunet/inet.clj
new file mode 100644
index 0000000..8d34a23
--- /dev/null
+++ b/src/org/gnu/clojure/gnunet/inet.clj
@@ -0,0 +1,19 @@
+(ns org.gnu.clojure.gnunet.inet
+ (:use (org.gnu.clojure.gnunet parser message)
+ clojure.contrib.monads)
+ (:import (java.net InetAddress InetSocketAddress)))
+
+(defn encode-address
+ [inet-socket-address]
+ (concat
+ (.getAddress (.getAddress inet-socket-address))
+ (encode-int16 (.getPort inet-socket-address))))
+
+(def parse-address
+ (match-one
+ (domonad parser-m [addr (items 16)
+ port parse-uint16]
+ (InetSocketAddress. (InetAddress/getByAddress (byte-array addr)) port))
+ (domonad parser-m [addr (items 4)
+ port parse-uint16]
+ (InetSocketAddress. (InetAddress/getByAddress (byte-array addr)) port))))
diff --git a/src/org/gnu/clojure/gnunet/peer.clj b/src/org/gnu/clojure/gnunet/peer.clj
index b7f508d..ed67a8d 100644
--- a/src/org/gnu/clojure/gnunet/peer.clj
+++ b/src/org/gnu/clojure/gnunet/peer.clj
@@ -1,11 +1,13 @@
(ns org.gnu.clojure.gnunet.peer
- (:use (org.gnu.clojure.gnunet identity hello hostlist))
- (:import (java.util Date TimerTask)))
+ (:use (org.gnu.clojure.gnunet identity)))
(defstruct remote-peer
- :public-key
- :id
- :transports-agent)
+ :public-key ;; java.security.PublicKey
+ :id ;; 64 byte (512-bit) sequence
+ :transports-agent) ;; agent of a map associating transport names (strings) to
+ ;; maps associating transport addresses (usually
+ ;; java.net.InetSocketAddress) to expiration times
+ ;; (java.util.Date)
(defn new-remote-peer-from-hello
[hello]
@@ -17,8 +19,8 @@
(def peer (apply create-struct (concat
(keys (struct-map remote-peer))
(list
- :private-key
- :remote-peers-agent))))
+ :private-key ;; java.security.PrivateKey
+ :remote-peers-agent)))) ;; agent of a map of peer IDs to struct remote-peer
(defstruct peer-options
:keypair)
@@ -30,30 +32,3 @@
:transports-agent (agent {})
:private-key (.getPrivate (:keypair options))
:remote-peers-agent (agent {})))
-
-;; Event - Peer receives a HELLO message
-(defn admit-hello!
- [peer hello]
- (letfn [(update-transports
- [transports new-transports]
- (merge-transports (Date.)
- transports
- (list-transports new-transports)))
- (update-remote-peers
- [remote-peers hello]
- (let [id (vec (generate-id (:public-key hello)))
- remote-peer (remote-peers id)]
- (if remote-peer
- (do
- (send
- (:transports-agent remote-peer)
- update-transports
- (:transports hello))
- remote-peers)
- (assoc remote-peers id (new-remote-peer-from-hello hello)))))]
- (send (:remote-peers-agent peer) update-remote-peers hello)))
-
-(defn create-hostlist-timer-task
- [peer url]
- (proxy [java.util.TimerTask] []
- (run [] (download-hostlist (partial admit-hello! peer) url)))) \ No newline at end of file
diff --git a/src/org/gnu/clojure/gnunet/tcp.clj b/src/org/gnu/clojure/gnunet/tcp.clj
index 98bc03b..0533a4d 100644
--- a/src/org/gnu/clojure/gnunet/tcp.clj
+++ b/src/org/gnu/clojure/gnunet/tcp.clj
@@ -1,7 +1,5 @@
(ns org.gnu.clojure.gnunet.tcp
- (:use (org.gnu.clojure.gnunet parser message identity crypto)
- clojure.contrib.monads)
- (:import (java.net InetAddress InetSocketAddress)))
+ (:use (org.gnu.clojure.gnunet parser) clojure.contrib.monads))
(def message-type-tcp-welcome 60)
@@ -11,18 +9,3 @@
(def parse-welcome
(domonad parser-m [my-id (items id-size)] my-id))
-
-(defn encode-address
- [inet-socket-address]
- (concat
- (.getAddress (.getAddress inet-socket-address))
- (encode-int16 (.getPort inet-socket-address))))
-
-(def parse-address
- (match-one
- (domonad parser-m [addr (items 16)
- port parse-uint16]
- (InetSocketAddress. (InetAddress/getByAddress (byte-array addr)) port))
- (domonad parser-m [addr (items 4)
- port parse-uint16]
- (InetSocketAddress. (InetAddress/getByAddress (byte-array addr)) port))))