forward update msgs

This commit is contained in:
Milo 2020-01-31 23:38:48 -05:00
parent 38be205744
commit a914535c7d
1 changed files with 32 additions and 17 deletions

35
router
View File

@ -105,29 +105,44 @@
(let loop () (let loop ()
(match-define (list src-peer-conn msg) (channel-get mail)) (match-define (list src-peer-conn msg) (channel-get mail))
(printf "====\nfrom ~a:\n~s\n" src-peer-conn msg) (printf "====\nfrom ~a:\n~s\n" src-peer-conn msg)
;; [Listof [Cons Peer-Conn Msg]]
(define to-send
(match msg (match msg
[(msg:update src dst r) [(msg:update src dst r)
(router-add! router (router-add! router
r r
(ip->peer src)) (ip->peer src))
(rt-dump router)] (rt-dump router)
(for/list ([pc (in-list peer-conns)]
#:when (not (eq? pc src-peer-conn)))
(cons pc msg))]
[(msg:data src dst data) [(msg:data src dst data)
(define-values [dst-peer-conn resp-msg]
(match (router-find-best router dst) (match (router-find-best router dst)
[#f [#f
(values src-peer-conn (list (cons src-peer-conn
(msg:no-route src dst))] (msg:no-route (sub1 src)
src)))]
[dst-peer [dst-peer
(values (peer->peer-conn dst-peer) (list (cons (peer->peer-conn dst-peer)
msg)])) msg))])]
(printf "----\nwant to send back to ~a:\n~a\n"
(peer-conn-info dst-peer-conn)
resp-msg)]
[_ [_
(printf "----\nignored\n")]) (printf "----\nignored\n")
'()]))
(for ([dst+msg (in-list to-send)])
(match-define (cons dst-peer-conn resp-msg) dst+msg)
(printf "----\nwant to send to ~a:\n~a\n"
(peer-conn-info dst-peer-conn)
resp-msg)
(define dst-port
(peer-conn-sock-out dst-peer-conn))
(write-bytes (msg->bytes resp-msg) dst-port)
(flush-output dst-port))
(loop))) (loop)))
;; Str [Listof Peer] -> ;; Str [Listof Peer] ->