Last active
September 10, 2017 18:18
-
-
Save a4180p/91d1beaac0d2efb8c2f23751496dcea5 to your computer and use it in GitHub Desktop.
logging ssdp messages in local network in hylang
This file contains hidden or 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
| (import asyncio | |
| logging | |
| socket | |
| struct) | |
| (setv *logger-format* "%(asctime)s %(message)s" | |
| log (.getLogger logging)) | |
| (.basicConfig logging :format *logger-format* :datefmt "[%H:%M:%S]") | |
| (.setLevel log logging.INFO) | |
| (defn ->mkast-socket [address port] | |
| ;; dunno if there is some convinient method in stdlib to do this | |
| (setv | |
| group (.inet_aton socket address) | |
| mreq (.pack struct "4sL" group socket.INADDR_ANY)) | |
| (doto | |
| (.socket socket socket.AF_INET socket.SOCK_DGRAM) | |
| (.setsockopt socket.IPPROTO_IP socket.IP_ADD_MEMBERSHIP mreq) | |
| (.setsockopt socket.SOL_SOCKET socket.SO_REUSEADDR 1) | |
| (.setsockopt socket.SOL_SOCKET socket.SO_REUSEPORT 1) | |
| (.bind (, "" port)))) | |
| (defclass SSDPListener [(. asyncio DatagramProtocol)] | |
| (defn connection-lost [self exc] | |
| (.error log exc)) | |
| ;; DatagramProtocol | |
| (defn datagram-received [self data addr] | |
| (.info log "got datagramm from %s:%d" (first addr) (second addr)) | |
| (print (.decode data))) | |
| (defn err-received [self exc] | |
| (.error log exc))) | |
| (defn listen [ioloop] | |
| (.create-datagram-endpoint | |
| ioloop SSDPListener | |
| :sock (->mkast-socket "239.255.255.250" 1900))) | |
| (defmain [&rest args] | |
| (setv | |
| ioloop (.get-event-loop asyncio) | |
| [transport protocol] | |
| (.run-until-complete ioloop (listen ioloop))) | |
| (try | |
| (.run-forever ioloop) | |
| (except [KeyboardInterrupt])) | |
| (.close transport) | |
| (.close ioloop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment