mirror of
https://github.com/sot-tech/mochi.git
synced 2026-07-11 11:08:11 -07:00
Organize peers by subnet.
This commit restructures PeerMaps to be a map from Subnet to PeerID to Peer. This reduces the complexity require to gather peers from the same subnet.
This commit is contained in:
@@ -8,6 +8,7 @@ package models
|
||||
|
||||
import (
|
||||
"net"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/chihaya/chihaya/config"
|
||||
@@ -45,12 +46,16 @@ func (e NotFoundError) Error() string { return string(e) }
|
||||
type PeerList []Peer
|
||||
type PeerKey string
|
||||
|
||||
func NewPeerKey(peerID string, ipv6 bool) PeerKey {
|
||||
if ipv6 {
|
||||
return PeerKey("6:" + peerID)
|
||||
}
|
||||
func NewPeerKey(peerID string, ip net.IP) PeerKey {
|
||||
return PeerKey(peerID + "//" + ip.String())
|
||||
}
|
||||
|
||||
return PeerKey("4:" + peerID)
|
||||
func (pk PeerKey) IP() net.IP {
|
||||
return net.ParseIP(strings.Split(string(pk), "//")[1])
|
||||
}
|
||||
|
||||
func (pk PeerKey) PeerID() string {
|
||||
return strings.Split(string(pk), "//")[0]
|
||||
}
|
||||
|
||||
// Peer is a participant in a swarm.
|
||||
@@ -79,7 +84,7 @@ func (p *Peer) HasIPv6() bool {
|
||||
}
|
||||
|
||||
func (p *Peer) Key() PeerKey {
|
||||
return NewPeerKey(p.ID, p.HasIPv6())
|
||||
return NewPeerKey(p.ID, p.IP)
|
||||
}
|
||||
|
||||
// Torrent is a swarm for a given torrent file.
|
||||
|
||||
Reference in New Issue
Block a user