Commit 5e4d6c22 authored by Julien Lepiller's avatar Julien Lepiller

torrent: Add potential-peer merging procedure.

* torrent/protocol.scm (merge-potential-peers): New procedure.
parent 072d7e47
......@@ -493,6 +493,12 @@ Determines whether we learned about that peer a short or a long time ago.
@end table
@end deffn
@deffn {Scheme Procedure} merge-potential-peers @var{old-peers} @var{new-peers}
Returns a new list of potential peers that adds peers from @var{new-peers} to
@var{old-peers}, either by adding peers that are not yet in @var{old-peers}
or updating the freshness of already known peers.
@end deffn
@deffn {Scheme Procedure} peer-fiber @var{parent-channel} @var{peer} @
@var{peerid} @var{infohash} @var{bitfield}
Runs a fiber that connects to a peer, listens for commands and reports peer
......@@ -63,6 +63,7 @@
(define-record-type peer
......@@ -267,6 +268,28 @@ it is made readable in this format: -<two-letter program id><four-digits version
bitfield (div piece 8)
(update-bit (mod piece 8) (bytevector-u8-ref bitfield (div piece 8)))))
(define (merge-potential-peer old-peers new-peer)
(make-potential-peer (assoc-ref new-peer "host") (assoc-ref new-peer "port") 1)
(filter (lambda (peer)
(not (equal? (potential-peer-host peer) (assoc-ref new-peer "host"))))
(define (merge-potential-peers old-peers new-peers)
(let ((peers (map (lambda (peer)
(potential-peer-host peer)
(potential-peer-port peer)
(+ (potential-peer-freshness peer) 1)))
(lambda (peer)
(< (potential-peer-freshness peer) 5))
(let loop ((peers peers) (new-peers new-peers))
(if (null? new-peers)
(loop (merge-potential-peer peers (car new-peers)) (cdr new-peers)))))))
(define (peer-read-fiber channel sock)
(lambda ()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment