summaryrefslogtreecommitdiff
path: root/src/org/gnu
diff options
context:
space:
mode:
authorDavid Barksdale <amatus.amongus@gmail.com>2010-05-02 16:57:09 -0700
committerDavid Barksdale <amatus.amongus@gmail.com>2010-05-02 16:57:09 -0700
commita929aaa11eb141790fd1c0543c127b218248ca26 (patch)
treeaa7a1b1e3a1d97a50f85e812dde5ec8bb16bba9e /src/org/gnu
parent9de15a35d0a4adeec78d335447db9d534aea1465 (diff)
Do some io and hostlist fetching.
Diffstat (limited to 'src/org/gnu')
-rw-r--r--src/org/gnu/clojure/gnunet/hostlist.clj11
-rw-r--r--src/org/gnu/clojure/gnunet/iostream.clj14
-rw-r--r--src/org/gnu/clojure/gnunet/message.clj7
-rw-r--r--src/org/gnu/clojure/gnunet/parser.clj1
4 files changed, 33 insertions, 0 deletions
diff --git a/src/org/gnu/clojure/gnunet/hostlist.clj b/src/org/gnu/clojure/gnunet/hostlist.clj
new file mode 100644
index 0000000..f1e20cb
--- /dev/null
+++ b/src/org/gnu/clojure/gnunet/hostlist.clj
@@ -0,0 +1,11 @@
+(ns org.gnu.clojure.gnunet.hostlist
+ (:use (org.gnu.clojure.gnunet parser message hello iostream)))
+
+(defn download-hostlist
+ "Calls the hello-processor on all parsed hello messages at the given URL."
+ [url hello-processor]
+ (doseq [msg (first ((none-or-more parse-message) (read-url url)))
+ :while (== (:message-type msg) message-type-hello)]
+ (try
+ (hello-processor (first (parse-hello (:bytes msg))))
+ (catch Exception e nil))))
diff --git a/src/org/gnu/clojure/gnunet/iostream.clj b/src/org/gnu/clojure/gnunet/iostream.clj
new file mode 100644
index 0000000..b4cb066
--- /dev/null
+++ b/src/org/gnu/clojure/gnunet/iostream.clj
@@ -0,0 +1,14 @@
+(ns org.gnu.clojure.gnunet.iostream
+ (:import java.net.URL))
+
+(defn reader
+ "Converts a java.io.InputStream into a lazy seq of bytes."
+ [in]
+ (lazy-seq (let [c (.read in)] (when (>= c 0) (cons (byte c) (reader in))))))
+
+(defn read-url
+ "Open a URL and return a seq of its data."
+ [url]
+ (try
+ (reader (.openStream (URL. url)))
+ (catch Exception e nil)))
diff --git a/src/org/gnu/clojure/gnunet/message.clj b/src/org/gnu/clojure/gnunet/message.clj
index 4c74b71..6deca23 100644
--- a/src/org/gnu/clojure/gnunet/message.clj
+++ b/src/org/gnu/clojure/gnunet/message.clj
@@ -50,6 +50,7 @@
(domonad parser-m [xs (items 8)] (decode-uint xs)))
(defn encode-utf8
+ "Converts a string into a null-terminated sequence of bytes in UTF-8."
[string]
(concat
(.getBytes string "UTF-8")
@@ -88,3 +89,9 @@
(concat
(encode-header (+ (count (:bytes msg)) header-size) (:message-type msg))
(:bytes msg)))
+
+(def parse-message
+ (domonad parser-m [header parse-header
+ message (items (- (:size header) header-size))]
+ {:message-type (:message-type header)
+ :bytes message}))
diff --git a/src/org/gnu/clojure/gnunet/parser.clj b/src/org/gnu/clojure/gnunet/parser.clj
index 73ecd86..a0ba7c3 100644
--- a/src/org/gnu/clojure/gnunet/parser.clj
+++ b/src/org/gnu/clojure/gnunet/parser.clj
@@ -18,6 +18,7 @@
(cons x xs)))))
(defn is
+ "Produces a parser that matches an item which satisfies the given predicate."
[p]
(domonad parser-m [x item
:when (p x)]