router stub

This commit is contained in:
Milo 2020-01-31 21:31:56 -05:00
parent edc229cb07
commit c9b6e44fc7
1 changed files with 50 additions and 2 deletions

52
router
View File

@ -14,6 +14,25 @@
[info sock-in sock-out]
#:transparent)
;; alist : [Listof [Cons IP Route]]
(struct router
[(alist #:mutable)]
#:transparent)
;; -> Router
(define (make-router)
(router '()))
;; Router Route IP -> Void
(define (router-add! rs r src)
(set-router-alist! rs
(cons (cons src r)
(router-alist rs))))
;; Router IP -> (U IP #f)
(define (router-find-best rs src)
#f)
;; Str [Listof Peer-Conn] -> Void
;; --
;; Runs router logic, given a list of peer connections.
@ -33,9 +52,38 @@
(channel-put mail (list peer msg))
(loop))))))
(define (ip->peer-conn ip)
(findf (λ (p)
(equal? ip (peer-ip (peer-conn-info p))))
peer-conns))
(define router
(make-router))
(define (loop)
(match-define (list peer data) (channel-get mail))
(printf "from ~a:\n~s\n" peer data)
(match-define (list src-peer msg) (channel-get mail))
(printf "====\nfrom ~a:\n~s\n" src-peer msg)
(match msg
[(msg:update src dst r)
(router-add! router r src)]
[(msg:data src dst data)
(define-values [dst-peer resp-msg]
(match (router-find-best router dst)
[#f
(values peer
(msg:no-route src
dst))]
[dst-peer-ip
(values (ip->peer-conn dst-peer-ip)
msg)]))
(printf "----\nwant to send back ~a to ~a\n"
dst-peer
resp-msg)]
[_
(printf "----\nignored\n")])
(loop))
(printf "waiting for messages...\n")