summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-10-04 20:38:48 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-10-04 20:38:48 -0700
commit3d4851cb3c3dac0a5c6503c075ac2ef5c3d6c69e (patch)
treecfee7c86345de2e8f32f96c601362d4f236d5523 /src
parent5030725cdf24e036d95200f1fe5aac88e29febe7 (diff)
Added selector loop thread to peer.
Diffstat (limited to 'src')
-rw-r--r--src/org/gnu/clojure/gnunet/peer.clj32
-rw-r--r--src/org/gnu/clojure/gnunet/udp.clj6
2 files changed, 28 insertions, 10 deletions
diff --git a/src/org/gnu/clojure/gnunet/peer.clj b/src/org/gnu/clojure/gnunet/peer.clj
index d681ff5..61acbf9 100644
--- a/src/org/gnu/clojure/gnunet/peer.clj
+++ b/src/org/gnu/clojure/gnunet/peer.clj
@@ -32,7 +32,10 @@
:transports-agent
;; java.nio.channels.Selector
- :selector))))
+ :selector
+
+ ;; Thread which selects on :selector
+ :selector-thread))))
(defstruct peer-options
:keypair)
@@ -44,12 +47,23 @@
(def id-size (count (sha-512 ())))
+(defn selector-loop
+ [selector]
+ (.select selector)
+ (let [selected-keys (.selectedKeys selector)]
+ (for [selection-key (enumeration-seq (.iterator selected-keys))]
+ ((.attachment selection-key)))
+ (.clear selected-keys))
+ (recur selector))
+
(defn new-peer [options]
- (struct-map peer
- :public-key (.getPublic (:keypair options))
- :id (generate-id (.getPublic (:keypair options)))
- :transport-addresses-agent (agent {})
- :private-key (.getPrivate (:keypair options))
- :remote-peers-agent (agent {})
- :transports-agent (agent nil)
- :selector (Selector/open)))
+ (let [selector (Selector/open)]
+ (struct-map peer
+ :public-key (.getPublic (:keypair options))
+ :id (generate-id (.getPublic (:keypair options)))
+ :transport-addresses-agent (agent {})
+ :private-key (.getPrivate (:keypair options))
+ :remote-peers-agent (agent {})
+ :transports-agent (agent nil)
+ :selector selector
+ :selector-thread (Thread. (partial selector-loop selector))))
diff --git a/src/org/gnu/clojure/gnunet/udp.clj b/src/org/gnu/clojure/gnunet/udp.clj
index 9fea876..5934cdf 100644
--- a/src/org/gnu/clojure/gnunet/udp.clj
+++ b/src/org/gnu/clojure/gnunet/udp.clj
@@ -42,6 +42,10 @@
[peer remote-peer address]
)
+(defn admit-message-udp!
+ [peer socket]
+ )
+
(defn activate-udp!
[peer port]
(let [datagram-channel (DatagramChannel/open)
@@ -52,7 +56,7 @@
(.register datagram-channel
(:selector peer)
SelectionKey/OP_READ
- nil))]
+ (partial admit-message-udp! peer socket)))]
(send (:transports-agent peer)
(fn [transports]
(assoc transports "udp"