summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-10-03 22:43:34 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-10-03 22:43:34 -0700
commit5030725cdf24e036d95200f1fe5aac88e29febe7 (patch)
treece4bbb777465f684b272ac3c798acccb33739682 /src
parentdecfa471ee11fe1cbb835c20e8c1e4ab9c44e04e (diff)
Now we're creating sockets and selectors and selectable channels, oh my!
Diffstat (limited to 'src')
-rw-r--r--src/org/gnu/clojure/gnunet/peer.clj14
-rw-r--r--src/org/gnu/clojure/gnunet/transport.clj4
-rw-r--r--src/org/gnu/clojure/gnunet/udp.clj23
3 files changed, 30 insertions, 11 deletions
diff --git a/src/org/gnu/clojure/gnunet/peer.clj b/src/org/gnu/clojure/gnunet/peer.clj
index 1424206..d681ff5 100644
--- a/src/org/gnu/clojure/gnunet/peer.clj
+++ b/src/org/gnu/clojure/gnunet/peer.clj
@@ -1,5 +1,6 @@
(ns org.gnu.clojure.gnunet.peer
- (:use (org.gnu.clojure.gnunet crypto message)))
+ (:use (org.gnu.clojure.gnunet crypto message))
+ (:import java.nio.channels.Selector))
(defstruct remote-peer
;; java.security.PublicKey
@@ -13,7 +14,8 @@
;; (java.util.Date) :latency (int, if ever connected)}
:transport-addresses-agent
- ;; agent of ??
+ ;; agent of a map of {:transport (map from peer:transports-agent, if
+ ;; connection is in progress) :transport-name (String)}
:connection-agent)
(def peer (apply create-struct (concat
@@ -27,7 +29,10 @@
;; agent of a map of transport names (String) to maps of {:connect!
;; :emit-message!}
- :transports-agent))))
+ :transports-agent
+
+ ;; java.nio.channels.Selector
+ :selector))))
(defstruct peer-options
:keypair)
@@ -46,4 +51,5 @@
:transport-addresses-agent (agent {})
:private-key (.getPrivate (:keypair options))
:remote-peers-agent (agent {})
- :transports-agent (agent nil)))
+ :transports-agent (agent nil)
+ :selector (Selector/open)))
diff --git a/src/org/gnu/clojure/gnunet/transport.clj b/src/org/gnu/clojure/gnunet/transport.clj
index c35bcaa..19cdb63 100644
--- a/src/org/gnu/clojure/gnunet/transport.clj
+++ b/src/org/gnu/clojure/gnunet/transport.clj
@@ -62,7 +62,7 @@
:id (generate-id (:public-key hello))
:transport-addresses-agent (agent (merge-transport-addresses {}
(:transport-addresses hello)))
- :connection-agent (agent nil)))
+ :connection-agent (agent {})))
;; Event - Peer receives a HELLO message
(defn admit-hello!
@@ -119,7 +119,7 @@
[peer remote-peer]
(send (:connection-agent remote-peer)
(fn [connection]
- (if (nil? connection)
+ (if (contains? connection :transport)
(let [{transport :transport address :address} (best-transport peer
remote-peer)]
(do
diff --git a/src/org/gnu/clojure/gnunet/udp.clj b/src/org/gnu/clojure/gnunet/udp.clj
index fc58e3a..9fea876 100644
--- a/src/org/gnu/clojure/gnunet/udp.clj
+++ b/src/org/gnu/clojure/gnunet/udp.clj
@@ -1,18 +1,20 @@
(ns org.gnu.clojure.gnunet.udp
(:use (org.gnu.clojure.gnunet inet parser message peer transport)
clojure.contrib.monads)
- (:import (java.util Date Calendar) java.net.InetSocketAddress))
+ (:import (java.util Date Calendar)
+ java.net.InetSocketAddress
+ (java.nio.channels DatagramChannel SelectionKey)))
(defn configure-udp-addresses!
"Adds new addresses for the udp transport to peer's transports-agent expiring
in 12 hours and removes expired addresses."
- [peer local-addresses port]
+ [peer reachable-addresses port]
(send (:transport-addresses-agent peer)
(fn [addresses]
(merge-transport-addresses {}
(expire-transport-addresses (Date.)
(concat (list-transport-addresses addresses)
- (for [address local-addresses]
+ (for [address reachable-addresses]
{:transport "udp"
:encoded-address (encode-address (InetSocketAddress. address
port))
@@ -41,9 +43,20 @@
)
(defn activate-udp!
- [peer]
+ [peer port]
+ (let [datagram-channel (DatagramChannel/open)
+ socket (.socket datagram-channel)
+ selection-key (do
+ (.configureBlocking datagram-channel false)
+ (.bind socket (InetSocketAddress. port))
+ (.register datagram-channel
+ (:selector peer)
+ SelectionKey/OP_READ
+ nil))]
(send (:transports-agent peer)
(fn [transports]
(assoc transports "udp"
{:connect! connect-udp!
- :emit-message! emit-message-udp!}))))
+ :emit-message! emit-message-udp!
+ :socket socket
+ :selection-key selection-key})))))