router stub
This commit is contained in:
parent
edc229cb07
commit
c9b6e44fc7
52
router
52
router
|
@ -14,6 +14,25 @@
|
||||||
[info sock-in sock-out]
|
[info sock-in sock-out]
|
||||||
#:transparent)
|
#: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
|
;; Str [Listof Peer-Conn] -> Void
|
||||||
;; --
|
;; --
|
||||||
;; Runs router logic, given a list of peer connections.
|
;; Runs router logic, given a list of peer connections.
|
||||||
|
@ -33,9 +52,38 @@
|
||||||
(channel-put mail (list peer msg))
|
(channel-put mail (list peer msg))
|
||||||
(loop))))))
|
(loop))))))
|
||||||
|
|
||||||
|
(define (ip->peer-conn ip)
|
||||||
|
(findf (λ (p)
|
||||||
|
(equal? ip (peer-ip (peer-conn-info p))))
|
||||||
|
peer-conns))
|
||||||
|
|
||||||
|
(define router
|
||||||
|
(make-router))
|
||||||
|
|
||||||
(define (loop)
|
(define (loop)
|
||||||
(match-define (list peer data) (channel-get mail))
|
(match-define (list src-peer msg) (channel-get mail))
|
||||||
(printf "from ~a:\n~s\n" peer data)
|
(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))
|
(loop))
|
||||||
|
|
||||||
(printf "waiting for messages...\n")
|
(printf "waiting for messages...\n")
|
||||||
|
|
Loading…
Reference in New Issue