]> Sergey Matveev's repositories - btrtrc.git/blob - web_seed.go
6e88d34b0626e5a883f7931caea7f718b25bcfec
[btrtrc.git] / web_seed.go
1 package torrent
2
3 import (
4         "net/http"
5
6         pp "github.com/anacrolix/torrent/peer_protocol"
7         "github.com/anacrolix/torrent/segments"
8         "github.com/anacrolix/torrent/webseed"
9 )
10
11 type httpRequestResult struct {
12         resp *http.Response
13         err  error
14 }
15
16 type requestPart struct {
17         req    *http.Request
18         e      segments.Extent
19         result chan httpRequestResult
20 }
21
22 type webseedRequest struct {
23         cancel func()
24 }
25
26 type webSeed struct {
27         client webseed.Client
28         peer   peer
29 }
30
31 type webseedClientEvent interface{}
32
33 type webseedRequestFailed struct {
34         r   request
35         err error
36 }
37
38 var _ PeerImpl = (*webSeed)(nil)
39
40 func (ws *webSeed) PostCancel(r request) {
41         ws.Cancel(r)
42 }
43
44 func (ws *webSeed) WriteInterested(interested bool) bool {
45         return true
46 }
47
48 func (ws *webSeed) Cancel(r request) bool {
49         //panic("implement me")
50         return true
51 }
52
53 func (ws *webSeed) Request(r request) bool {
54         ws.client.Request(webseed.RequestSpec{ws.peer.t.requestOffset(r), int64(r.Length)})
55         return true
56 }
57
58 func (ws *webSeed) ConnectionFlags() string {
59         return "WS"
60 }
61
62 func (ws *webSeed) Drop() {
63 }
64
65 func (ws *webSeed) UpdateRequests() {
66         ws.peer.doRequestState()
67 }
68
69 func (ws *webSeed) Close() {}
70
71 func (ws *webSeed) eventProcessor() {
72         for ev := range ws.client.Events {
73                 if ev.Err != nil {
74                         panic(ev)
75                 }
76                 r, ok := ws.peer.t.offsetRequest(ev.RequestSpec.Start)
77                 if !ok {
78                         panic(ev)
79                 }
80                 ws.peer.t.cl.lock()
81                 err := ws.peer.receiveChunk(&pp.Message{
82                         Type:  pp.Piece,
83                         Index: r.Index,
84                         Begin: r.Begin,
85                         Piece: ev.Bytes,
86                 })
87                 ws.peer.t.cl.unlock()
88                 if err != nil {
89                         panic(err)
90                 }
91         }
92 }