Change PeerMap key to PeerID
This commit is contained in:
parent
b962f49c90
commit
78d94d1e99
4 changed files with 20 additions and 23 deletions
|
@ -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":
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue