]> Sergey Matveev's repositories - btrtrc.git/blob - webseed-peer.go
Expose more callbacks and Request and ChunkSpec
[btrtrc.git] / webseed-peer.go
1 package torrent
2
3 import (
4         "fmt"
5         "strings"
6
7         "github.com/anacrolix/torrent/common"
8         "github.com/anacrolix/torrent/metainfo"
9         pp "github.com/anacrolix/torrent/peer_protocol"
10         "github.com/anacrolix/torrent/segments"
11         "github.com/anacrolix/torrent/webseed"
12         "github.com/pkg/errors"
13 )
14
15 type webseedPeer struct {
16         client   webseed.Client
17         requests map[Request]webseed.Request
18         peer     Peer
19 }
20
21 var _ peerImpl = (*webseedPeer)(nil)
22
23 func (me *webseedPeer) connStatusString() string {
24         return me.client.Url
25 }
26
27 func (ws *webseedPeer) String() string {
28         return fmt.Sprintf("webseed peer for %q", ws.client.Url)
29 }
30
31 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
32         ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
33         ws.client.Info = info
34 }
35
36 func (ws *webseedPeer) _postCancel(r Request) {
37         ws.cancel(r)
38 }
39
40 func (ws *webseedPeer) writeInterested(interested bool) bool {
41         return true
42 }
43
44 func (ws *webseedPeer) cancel(r Request) bool {
45         ws.requests[r].Cancel()
46         return true
47 }
48
49 func (ws *webseedPeer) intoSpec(r Request) webseed.RequestSpec {
50         return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
51 }
52
53 func (ws *webseedPeer) request(r Request) bool {
54         webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
55         ws.requests[r] = webseedRequest
56         go ws.requestResultHandler(r, webseedRequest)
57         return true
58 }
59
60 func (ws *webseedPeer) connectionFlags() string {
61         return "WS"
62 }
63
64 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too. We could
65 // return bool if this is even possible, and if it isn't, skip to the next drop candidate.
66 func (ws *webseedPeer) drop() {}
67
68 func (ws *webseedPeer) updateRequests() {
69         ws.peer.doRequestState()
70 }
71
72 func (ws *webseedPeer) onClose() {}
73
74 func (ws *webseedPeer) requestResultHandler(r Request, webseedRequest webseed.Request) {
75         result := <-webseedRequest.Result
76         ws.peer.t.cl.lock()
77         defer ws.peer.t.cl.unlock()
78         if result.Err != nil {
79                 ws.peer.logger.Printf("Request %v rejected: %v", r, result.Err)
80                 // Always close for now. We need to filter out temporary errors, but this is a nightmare in
81                 // Go. Currently a bad webseed URL can starve out the good ones due to the chunk selection
82                 // algorithm.
83                 const closeOnAllErrors = false
84                 if closeOnAllErrors || strings.Contains(errors.Cause(result.Err).Error(), "unsupported protocol scheme") {
85                         ws.peer.close()
86                 } else {
87                         ws.peer.remoteRejectedRequest(r)
88                 }
89         } else {
90                 err := ws.peer.receiveChunk(&pp.Message{
91                         Type:  pp.Piece,
92                         Index: r.Index,
93                         Begin: r.Begin,
94                         Piece: result.Bytes,
95                 })
96                 if err != nil {
97                         panic(err)
98                 }
99         }
100 }