tx.SetLeecher/Seeder added
This commit is contained in:
parent
2d40a3ab97
commit
f7906b9089
3 changed files with 51 additions and 28 deletions
|
@ -65,40 +65,8 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for the user in in the pool of seeders and leechers
|
// Create a new peer object from the request
|
||||||
sp, seeder := torrent.Seeders[peerID]
|
peer := &storage.Peer{
|
||||||
lp, leecher := torrent.Leechers[peerID]
|
|
||||||
peer := &storage.Peer{}
|
|
||||||
switch {
|
|
||||||
// Guarantee that no user is in both pools
|
|
||||||
case seeder && leecher:
|
|
||||||
if left == 0 {
|
|
||||||
peer = &sp
|
|
||||||
err := tx.RmLeecher(torrent, peer)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("server: %s", err)
|
|
||||||
}
|
|
||||||
leecher = false
|
|
||||||
} else {
|
|
||||||
peer = &lp
|
|
||||||
err := tx.RmSeeder(torrent, peer)
|
|
||||||
if err != nil {
|
|
||||||
log.Panicf("server: %s", err)
|
|
||||||
}
|
|
||||||
seeder = false
|
|
||||||
}
|
|
||||||
|
|
||||||
case seeder:
|
|
||||||
peer = &sp
|
|
||||||
// TODO update stats
|
|
||||||
|
|
||||||
case leecher:
|
|
||||||
peer = &lp
|
|
||||||
// TODO update stats
|
|
||||||
|
|
||||||
default:
|
|
||||||
// The user is a new peer
|
|
||||||
peer = &storage.Peer{
|
|
||||||
ID: peerID,
|
ID: peerID,
|
||||||
UserID: user.ID,
|
UserID: user.ID,
|
||||||
TorrentID: torrent.ID,
|
TorrentID: torrent.ID,
|
||||||
|
@ -110,7 +78,43 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
LastAnnounce: time.Now().Unix(),
|
LastAnnounce: time.Now().Unix(),
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the new user's slots
|
// Look for the user in in the pool of seeders and leechers
|
||||||
|
_, seeder := torrent.Seeders[peerID]
|
||||||
|
_, leecher := torrent.Leechers[peerID]
|
||||||
|
|
||||||
|
switch {
|
||||||
|
// Guarantee that no user is in both pools
|
||||||
|
case seeder && leecher:
|
||||||
|
if left == 0 {
|
||||||
|
err := tx.RmLeecher(torrent, peer)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("server: %s", err)
|
||||||
|
}
|
||||||
|
leecher = false
|
||||||
|
} else {
|
||||||
|
err := tx.RmSeeder(torrent, peer)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("server: %s", err)
|
||||||
|
}
|
||||||
|
seeder = false
|
||||||
|
}
|
||||||
|
|
||||||
|
case seeder:
|
||||||
|
// Update the peer with the stats from the request
|
||||||
|
err := tx.SetSeeder(torrent, peer)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("server: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
case leecher:
|
||||||
|
// Update the peer with the stats from the request
|
||||||
|
err := tx.SetLeecher(torrent, peer)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicf("server: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
// Check the user's slots to see if they're allowed to leech
|
||||||
if s.conf.Slots && user.Slots != -1 && left != 0 {
|
if s.conf.Slots && user.Slots != -1 && left != 0 {
|
||||||
if user.UsedSlots >= user.Slots {
|
if user.UsedSlots >= user.Slots {
|
||||||
fail(errors.New("You've run out of download slots."), w, r)
|
fail(errors.New("You've run out of download slots."), w, r)
|
||||||
|
@ -119,11 +123,13 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if left == 0 {
|
if left == 0 {
|
||||||
|
// Save the peer as a new seeder
|
||||||
err := tx.NewSeeder(torrent, peer)
|
err := tx.NewSeeder(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
// Save the peer as a new leecher and increment the user's slots
|
||||||
err := tx.IncrementSlots(user)
|
err := tx.IncrementSlots(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
|
@ -135,7 +141,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle any events given to us by the user
|
// Handle any events in the request
|
||||||
switch {
|
switch {
|
||||||
case event == "stopped" || event == "paused":
|
case event == "stopped" || event == "paused":
|
||||||
if seeder {
|
if seeder {
|
||||||
|
@ -172,7 +178,7 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
case leecher && left == 0:
|
case leecher && left == 0:
|
||||||
// Completed event from the peer was never received
|
// A leecher completed but the event was never received
|
||||||
err := tx.RmLeecher(torrent, peer)
|
err := tx.RmLeecher(torrent, peer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicf("server: %s", err)
|
log.Panicf("server: %s", err)
|
||||||
|
@ -184,7 +190,6 @@ func (s *Server) serveAnnounce(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO compact, response, etc...
|
// TODO compact, response, etc...
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
|
func (s *Server) validateAnnounceQuery(r *http.Request) (compact bool, numWant int, infohash, peerID, event, ip string, port, uploaded, downloaded, left uint64, err error) {
|
||||||
|
|
|
@ -197,6 +197,14 @@ func (tx *Tx) NewLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) SetLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
|
if tx.done {
|
||||||
|
return storage.ErrTxDone
|
||||||
|
}
|
||||||
|
// TODO
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Tx) RmLeecher(t *storage.Torrent, p *storage.Peer) error {
|
func (tx *Tx) RmLeecher(t *storage.Torrent, p *storage.Peer) error {
|
||||||
if tx.done {
|
if tx.done {
|
||||||
return storage.ErrTxDone
|
return storage.ErrTxDone
|
||||||
|
@ -213,6 +221,14 @@ func (tx *Tx) NewSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (tx *Tx) SetSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
|
if tx.done {
|
||||||
|
return storage.ErrTxDone
|
||||||
|
}
|
||||||
|
// TODO
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *Tx) RmSeeder(t *storage.Torrent, p *storage.Peer) error {
|
func (tx *Tx) RmSeeder(t *storage.Torrent, p *storage.Peer) error {
|
||||||
if tx.done {
|
if tx.done {
|
||||||
return storage.ErrTxDone
|
return storage.ErrTxDone
|
||||||
|
|
|
@ -78,9 +78,11 @@ type Tx interface {
|
||||||
|
|
||||||
// Peers
|
// Peers
|
||||||
NewLeecher(t *Torrent, p *Peer) error
|
NewLeecher(t *Torrent, p *Peer) error
|
||||||
|
SetLeecher(t *Torrent, p *Peer) error
|
||||||
RmLeecher(t *Torrent, p *Peer) error
|
RmLeecher(t *Torrent, p *Peer) error
|
||||||
|
|
||||||
NewSeeder(t *Torrent, p *Peer) error
|
NewSeeder(t *Torrent, p *Peer) error
|
||||||
|
SetSeeder(t *Torrent, p *Peer) error
|
||||||
RmSeeder(t *Torrent, p *Peer) error
|
RmSeeder(t *Torrent, p *Peer) error
|
||||||
|
|
||||||
// Users
|
// Users
|
||||||
|
|
Loading…
Add table
Reference in a new issue