Change PeerMap key to PeerID

This commit is contained in:
Jimmy Zelinskie 2014-07-19 04:21:28 -04:00
parent b962f49c90
commit 78d94d1e99
4 changed files with 20 additions and 23 deletions

View file

@ -99,14 +99,14 @@ func updateSwarm(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent
if err != nil { if err != nil {
return return
} }
t.Seeders[p.Key()] = *p t.Seeders[p.ID] = *p
case t.InLeecherPool(p): case t.InLeecherPool(p):
err = c.PutLeecher(t.Infohash, p) err = c.PutLeecher(t.Infohash, p)
if err != nil { if err != nil {
return return
} }
t.Leechers[p.Key()] = *p t.Leechers[p.ID] = *p
default: default:
if ann.Left == 0 { if ann.Left == 0 {
@ -114,13 +114,13 @@ func updateSwarm(c Conn, ann *models.Announce, p *models.Peer, t *models.Torrent
if err != nil { if err != nil {
return return
} }
t.Seeders[p.Key()] = *p t.Seeders[p.ID] = *p
} else { } else {
err = c.PutLeecher(t.Infohash, p) err = c.PutLeecher(t.Infohash, p)
if err != nil { if err != nil {
return return
} }
t.Leechers[p.Key()] = *p t.Leechers[p.ID] = *p
} }
created = true created = true
} }
@ -134,17 +134,17 @@ func handleEvent(c Conn, ann *models.Announce, p *models.Peer, u *models.User, t
switch { switch {
case ann.Event == "stopped" || ann.Event == "paused": case ann.Event == "stopped" || ann.Event == "paused":
if t.InSeederPool(p) { if t.InSeederPool(p) {
err = c.DeleteSeeder(t.Infohash, p.Key()) err = c.DeleteSeeder(t.Infohash, p.ID)
if err != nil { if err != nil {
return return
} }
delete(t.Seeders, p.Key()) delete(t.Seeders, p.ID)
} else if t.InLeecherPool(p) { } else if t.InLeecherPool(p) {
err = c.DeleteLeecher(t.Infohash, p.Key()) err = c.DeleteLeecher(t.Infohash, p.ID)
if err != nil { if err != nil {
return return
} }
delete(t.Leechers, p.Key()) delete(t.Leechers, p.ID)
} }
case ann.Event == "completed": case ann.Event == "completed":

View file

@ -97,7 +97,7 @@ type Conn interface {
// leecherFinished moves a peer from the leeching pool to the seeder pool. // leecherFinished moves a peer from the leeching pool to the seeder pool.
func leecherFinished(c Conn, infohash string, p *models.Peer) error { 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 { if err != nil {
return err return err
} }

View file

@ -88,7 +88,7 @@ func (c *Conn) AddLeecher(infohash string, p *models.Peer) error {
if !ok { if !ok {
return tracker.ErrTorrentDNE return tracker.ErrTorrentDNE
} }
t.Leechers[p.Key()] = *p t.Leechers[p.ID] = *p
return nil return nil
} }
@ -101,7 +101,7 @@ func (c *Conn) AddSeeder(infohash string, p *models.Peer) error {
if !ok { if !ok {
return tracker.ErrTorrentDNE return tracker.ErrTorrentDNE
} }
t.Seeders[p.Key()] = *p t.Seeders[p.ID] = *p
return nil return nil
} }
@ -140,7 +140,7 @@ func (c *Conn) PutLeecher(infohash string, p *models.Peer) error {
if !ok { if !ok {
return tracker.ErrTorrentDNE return tracker.ErrTorrentDNE
} }
t.Leechers[p.Key()] = *p t.Leechers[p.ID] = *p
return nil return nil
} }
@ -153,7 +153,7 @@ func (c *Conn) PutSeeder(infohash string, p *models.Peer) error {
if !ok { if !ok {
return tracker.ErrTorrentDNE return tracker.ErrTorrentDNE
} }
t.Seeders[p.Key()] = *p t.Seeders[p.ID] = *p
return nil return nil
} }

View file

@ -7,7 +7,6 @@ package models
import ( import (
"errors" "errors"
"net" "net"
"strconv"
"time" "time"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
@ -35,6 +34,8 @@ type Peer struct {
} }
type PeerList []Peer type PeerList []Peer
// PeerMap is a map from PeerIDs to Peers.
type PeerMap map[string]Peer type PeerMap map[string]Peer
// NewPeer returns the Peer representation of an Announce. When provided nil // 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. // Torrent is a swarm for a given torrent file.
type Torrent struct { type Torrent struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
@ -88,18 +84,19 @@ type Torrent struct {
LastAction int64 `json:"last_action"` 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) { func (t *Torrent) InSeederPool(p *Peer) (exists bool) {
_, exists = t.Seeders[p.Key()] _, exists = t.Seeders[p.ID]
return 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) { func (t *Torrent) InLeecherPool(p *Peer) (exists bool) {
_, exists = t.Leechers[p.Key()] _, exists = t.Leechers[p.ID]
return return
} }
// PeerCount returns the total number of peers in a swarm.
func (t *Torrent) PeerCount() int { func (t *Torrent) PeerCount() int {
return len(t.Seeders) + len(t.Leechers) return len(t.Seeders) + len(t.Leechers)
} }