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"
15 type webseedPeer struct {
17 requests map[request]webseed.Request
21 var _ peerImpl = (*webseedPeer)(nil)
23 func (ws *webseedPeer) String() string {
24 return fmt.Sprintf("webseed peer for %q", ws.client.Url)
27 func (ws *webseedPeer) onGotInfo(info *metainfo.Info) {
28 ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
32 func (ws *webseedPeer) _postCancel(r request) {
36 func (ws *webseedPeer) writeInterested(interested bool) bool {
40 func (ws *webseedPeer) cancel(r request) bool {
41 ws.requests[r].Cancel()
45 func (ws *webseedPeer) intoSpec(r request) webseed.RequestSpec {
46 return webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)}
49 func (ws *webseedPeer) request(r request) bool {
50 webseedRequest := ws.client.NewRequest(ws.intoSpec(r))
51 ws.requests[r] = webseedRequest
52 go ws.requestResultHandler(r, webseedRequest)
56 func (ws *webseedPeer) connectionFlags() string {
60 // TODO: This is called when banning peers. Perhaps we want to be able to ban webseeds too.
61 func (ws *webseedPeer) drop() {}
63 func (ws *webseedPeer) updateRequests() {
64 ws.peer.doRequestState()
67 func (ws *webseedPeer) _close() {}
69 func (ws *webseedPeer) requestResultHandler(r request, webseedRequest webseed.Request) {
70 result := <-webseedRequest.Result
72 defer ws.peer.t.cl.unlock()
73 if result.Err != nil {
74 ws.peer.logger.Printf("request %v rejected: %v", r, result.Err)
75 if strings.Contains(errors.Cause(result.Err).Error(), "unsupported protocol scheme") {
78 ws.peer.remoteRejectedRequest(r)
81 err := ws.peer.receiveChunk(&pp.Message{