2020-01-29 21:23:07 +00:00
|
|
|
#!/usr/bin/env racket
|
|
|
|
#lang racket
|
2020-01-30 23:29:08 +00:00
|
|
|
; vim:syntax=racket
|
2020-01-29 21:23:07 +00:00
|
|
|
|
2020-01-30 23:33:13 +00:00
|
|
|
(require json
|
|
|
|
"iputil.rkt"
|
|
|
|
"unix-socket.rkt")
|
|
|
|
|
|
|
|
;; info : Peer
|
|
|
|
;; sock-in : Input-Port
|
|
|
|
;; sock-out : Output-Port
|
|
|
|
(struct peer-conn
|
|
|
|
[info sock-in sock-out]
|
|
|
|
#:transparent)
|
|
|
|
|
|
|
|
;; Str [Listof Peer-Conn] -> [-> Void]
|
|
|
|
;; --
|
|
|
|
;; runs router logic given peer connections. returns thunk that, when called, aborts the
|
|
|
|
;; router.
|
|
|
|
(define (run-router/conns ans peer-conns)
|
|
|
|
(define mail
|
|
|
|
(make-channel))
|
|
|
|
|
|
|
|
(define peer-threads
|
|
|
|
(for/list ([pc (in-list peer-conns)])
|
|
|
|
(match-define (peer-conn peer sock-in sock-out) pc)
|
|
|
|
(thread (λ ()
|
|
|
|
(define buf (make-bytes 65536))
|
|
|
|
(let loop ()
|
|
|
|
(define len (read-bytes-avail! buf sock-in))
|
2020-01-30 23:56:17 +00:00
|
|
|
(printf "got ~a bytes from ~a...\n" len peer)
|
2020-01-30 23:33:13 +00:00
|
|
|
(define jsexpr (bytes->jsexpr (subbytes buf 0 len)))
|
|
|
|
(channel-put mail (list peer jsexpr))
|
|
|
|
(loop))))))
|
|
|
|
|
|
|
|
(define (loop)
|
|
|
|
(match-define (list peer data) (channel-get mail))
|
|
|
|
(printf "from ~a:\n~s\n" peer data)
|
|
|
|
(loop))
|
|
|
|
|
2020-01-30 23:56:17 +00:00
|
|
|
(printf "waiting for messages...\n")
|
|
|
|
(loop))
|
2020-01-29 21:23:07 +00:00
|
|
|
|
|
|
|
;; Str [Listof Peer] ->
|
|
|
|
;; Router main
|
|
|
|
(define (run-router asn peers)
|
|
|
|
(displayln asn)
|
|
|
|
(map displayln peers)
|
2020-01-30 23:56:17 +00:00
|
|
|
(displayln "------------")
|
|
|
|
(with-handlers ([exn:break:terminate? (λ (e)
|
|
|
|
(printf "time to die.\n"))])
|
|
|
|
(run-router/conns
|
|
|
|
asn
|
|
|
|
(for/list ([peer (in-list peers)])
|
|
|
|
(define-values [sock-in sock-out]
|
|
|
|
(unix-socket-connect (ip->string (peer-ip peer))
|
|
|
|
'SOCK-SEQPACKET))
|
|
|
|
(peer-conn peer
|
|
|
|
sock-in
|
|
|
|
sock-out)))))
|
2020-01-29 21:23:07 +00:00
|
|
|
|
|
|
|
(module+ main
|
2020-01-30 23:33:13 +00:00
|
|
|
(command-line
|
|
|
|
#:program "router"
|
|
|
|
#:args
|
|
|
|
(asn . peers)
|
2020-01-30 23:56:17 +00:00
|
|
|
(with-output-to-file "log.txt"
|
|
|
|
#:exists 'replace
|
|
|
|
(λ ()
|
|
|
|
;; Run the router
|
|
|
|
(run-router asn (map string->peer peers))))))
|
2020-01-30 23:33:13 +00:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(define-values [in1 out1] (make-pipe))
|
|
|
|
(define-values [in2 out2] (make-pipe))
|
|
|
|
(define p1 (peer-conn (string->peer "1.2.3.4-cust") in1 out1))
|
|
|
|
(define p2 (peer-conn (string->peer "1.2.3.5-peer") in2 out2))
|
|
|
|
|
2020-01-30 23:56:17 +00:00
|
|
|
; (define abort-router
|
2020-01-30 23:33:13 +00:00
|
|
|
(run-router/conns "123"
|
2020-01-30 23:56:17 +00:00
|
|
|
(list p1 p2))
|
2020-01-30 23:33:13 +00:00
|
|
|
|
|
|
|
(void
|
|
|
|
(write-string "{\"a\": 1, \"b\": [1,2,3]}" out1)))
|