Created
October 8, 2012 21:25
-
-
Save lfranchi/3855083 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns ono.net | |
(:import (java.net InetAddress DatagramPacket DatagramSocket))) | |
;; Namespace for network related operations | |
;; | |
;; Listens on UDP port 50210 for broadcasts | |
;; Handles socket connections to other tomahawk/ono instances | |
(def port 55555) | |
(def dgram-size 16384) | |
(def udp-listener (agent nil)) | |
(def udp-sock (ref nil)) | |
(def running true) | |
(defn listen | |
"UDP listener" | |
[_] | |
(let [buf (byte-array dgram-size) | |
packet (DatagramPacket. buf dgram-size) | |
strval (dosync (.receive @udp-sock packet) (String. (.getData packet) 0 (.getLength packet)))] | |
(println "Received packet:" strval) | |
;; We only support v2 broadcasts | |
(let [parts (clojure.string/split strval #":")] | |
(if (and (= (count parts)))) | |
(send udp-listener listen))) | |
(defn start-udp | |
"Starts the UDP listener and periodically sends | |
UDP broacasts" | |
[] | |
(dosync (ref-set udp-sock (DatagramSocket. port))) | |
(println "Beginning to listen on port " port) | |
(send-off udp-listener listen)) | |
(defn stop-udp | |
"Stops all UDP sockets" | |
[] | |
(dosync | |
(.close @udp-sock))) |
stanistan
commented
Oct 8, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment