Skip to content

Commit

Permalink
Be better about network errors in discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
conormcd committed Jun 19, 2015
1 parent a41a873 commit 5fe050e
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/rangeview/discovery.clj
Expand Up @@ -20,22 +20,32 @@
(defn advertise
"Advertise this host as listening on a specific port."
[port interval]
(let [sock (socket adv-address adv-port)
pack (packet adv-address adv-port (pr-str {:port port}))]
(async/go
(while true
(.send sock pack)
(Thread/sleep (* interval 1000))))))
(async/go
(while true
(try
(let [sock (socket adv-address adv-port)
pack (packet adv-address adv-port (pr-str {:port port}))]
(while true
(try
(.send sock pack)
(catch java.io.IOException e))
(Thread/sleep (* interval 1000))))
(catch java.io.IOException e))
(Thread/sleep 1000))))

(defn listen
"Listen for advertisements from other instances of rangeview."
[interval]
(let [sock (socket adv-address adv-port)
packet (DatagramPacket. (byte-array 1024) 1024)]
(async/go
(while true
(.receive sock packet)
(let [source (-> packet .getAddress .getHostAddress)
payload (-> packet .getData (String.) read-string)]
(peers/add {:host source :port (:port payload)}))
(Thread/sleep (* interval 1000))))))
(async/go
(while true
(let [sock (socket adv-address adv-port)
packet (DatagramPacket. (byte-array 1024) 1024)]
(while true
(try
(.receive sock packet)
(let [source (-> packet .getAddress .getHostAddress)
payload (-> packet .getData (String.) read-string)]
(peers/add {:host source :port (:port payload)}))
(catch java.io.IOException e))
(Thread/sleep (* interval 1000))))
(Thread/sleep 1000))))

0 comments on commit 5fe050e

Please sign in to comment.