From 78d94d1e99b24663a26471b24124f9d4dc6a1efc Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Sat, 19 Jul 2014 04:21:28 -0400 Subject: [PATCH] Change PeerMap key to PeerID --- tracker/announce.go | 16 ++++++++-------- tracker/conn.go | 2 +- tracker/memory/conn.go | 8 ++++---- tracker/models/models.go | 17 +++++++---------- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/tracker/announce.go b/tracker/announce.go index 8ef3dbf..6734a15 100644 --- a/tracker/announce.go +++ b/tracker/announce.go @@ -99,14 +99,14 @@ func updateSwarm(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent if err != nil { return } - t.Seeders[p.Key()] = *p + t.Seeders[p.ID] = *p case t.InLeecherPool(p): err = c.PutLeecher(t.Infohash, p) if err != nil { return } - t.Leechers[p.Key()] = *p + t.Leechers[p.ID] = *p default: if ann.Left == 0 { @@ -114,13 +114,13 @@ func updateSwarm(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent if err != nil { return } - t.Seeders[p.Key()] = *p + t.Seeders[p.ID] = *p } else { err = c.PutLeecher(t.Infohash, p) if err != nil { return } - t.Leechers[p.Key()] = *p + t.Leechers[p.ID] = *p } created = true } @@ -134,17 +134,17 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t switch { case ann.Event == "stopped" || ann.Event == "paused": if t.InSeederPool(p) { - err = c.DeleteSeeder(t.Infohash, p.Key()) + err = c.DeleteSeeder(t.Infohash, p.ID) if err != nil { return } - delete(t.Seeders, p.Key()) + delete(t.Seeders, p.ID) } else if t.InLeecherPool(p) { - err = c.DeleteLeecher(t.Infohash, p.Key()) + err = c.DeleteLeecher(t.Infohash, p.ID) if err != nil { return } - delete(t.Leechers, p.Key()) + delete(t.Leechers, p.ID) } case ann.Event == "completed": diff --git a/tracker/conn.go b/tracker/conn.go index dd83cc2..971f90f 100644 --- a/tracker/conn.go +++ b/tracker/conn.go @@ -97,7 +97,7 @@ type Conn interface { // leecherFinished moves a peer from the leeching pool to the seeder pool. func leecherFinished(c Conn, infohash string, p *models.Peer) error { - err := c.DeleteLeecher(infohash, p.Key()) + err := c.DeleteLeecher(infohash, p.ID) if err != nil { return err } diff --git a/tracker/memory/conn.go b/tracker/memory/conn.go index bfe59d7..694a5e3 100644 --- a/tracker/memory/conn.go +++ b/tracker/memory/conn.go @@ -88,7 +88,7 @@ func (c *Conn) AddLeecher(infohash string, p *models.Peer) error { if !ok { return tracker.ErrTorrentDNE } - t.Leechers[p.Key()] = *p + t.Leechers[p.ID] = *p return nil } @@ -101,7 +101,7 @@ func (c *Conn) AddSeeder(infohash string, p *models.Peer) error { if !ok { return tracker.ErrTorrentDNE } - t.Seeders[p.Key()] = *p + t.Seeders[p.ID] = *p return nil } @@ -140,7 +140,7 @@ func (c *Conn) PutLeecher(infohash string, p *models.Peer) error { if !ok { return tracker.ErrTorrentDNE } - t.Leechers[p.Key()] = *p + t.Leechers[p.ID] = *p return nil } @@ -153,7 +153,7 @@ func (c *Conn) PutSeeder(infohash string, p *models.Peer) error { if !ok { return tracker.ErrTorrentDNE } - t.Seeders[p.Key()] = *p + t.Seeders[p.ID] = *p return nil } diff --git a/tracker/models/models.go b/tracker/models/models.go index 6e89c47..1b7ae43 100644 --- a/tracker/models/models.go +++ b/tracker/models/models.go @@ -7,7 +7,6 @@ package models import ( "errors" "net" - "strconv" "time" "github.com/chihaya/chihaya/config" @@ -35,6 +34,8 @@ type Peer struct { } type PeerList []Peer + +// PeerMap is a map from PeerIDs to Peers. type PeerMap map[string]Peer // NewPeer returns the Peer representation of an Announce. When provided nil @@ -69,11 +70,6 @@ func NewPeer(a *Announce, u *User, t *Torrent) *Peer { } } -// Key returns the unique key used to look-up a peer in a PeerMap. -func (p Peer) Key() string { - return p.ID + ":" + strconv.FormatUint(p.UserID, 36) -} - // Torrent is a swarm for a given torrent file. type Torrent struct { ID uint64 `json:"id"` @@ -88,18 +84,19 @@ type Torrent struct { LastAction int64 `json:"last_action"` } -// InSeederPool returns true if a peer is within a Torrent's pool of seeders. +// InSeederPool returns true if a peer is within a Torrent's map of seeders. func (t *Torrent) InSeederPool(p *Peer) (exists bool) { - _, exists = t.Seeders[p.Key()] + _, exists = t.Seeders[p.ID] return } -// InLeecherPool returns true if a peer is within a Torrent's pool of leechers. +// InLeecherPool returns true if a peer is within a Torrent's map of leechers. func (t *Torrent) InLeecherPool(p *Peer) (exists bool) { - _, exists = t.Leechers[p.Key()] + _, exists = t.Leechers[p.ID] return } +// PeerCount returns the total number of peers in a swarm. func (t *Torrent) PeerCount() int { return len(t.Seeders) + len(t.Leechers) }