]> Sergey Matveev's repositories - btrtrc.git/blob - torrent.go
Break up client.go into several files and a few fixes
[btrtrc.git] / torrent.go
1 package torrent
2
3 import (
4         "container/list"
5         "fmt"
6         "net"
7         "sort"
8
9         "bitbucket.org/anacrolix/go.torrent/peer_protocol"
10         "bitbucket.org/anacrolix/go.torrent/tracker"
11         metainfo "github.com/nsf/libtorgo/torrent"
12 )
13
14 func (t *Torrent) PieceNumPendingBytes(index peer_protocol.Integer) (count peer_protocol.Integer) {
15         pendingChunks := t.Pieces[index].PendingChunkSpecs
16         count = peer_protocol.Integer(len(pendingChunks)) * chunkSize
17         _lastChunkSpec := lastChunkSpec(t.PieceLength(index))
18         if _lastChunkSpec.Length != chunkSize {
19                 if _, ok := pendingChunks[_lastChunkSpec]; ok {
20                         count += _lastChunkSpec.Length - chunkSize
21                 }
22         }
23         return
24 }
25
26 type Torrent struct {
27         InfoHash   InfoHash
28         Pieces     []*piece
29         Data       MMapSpan
30         MetaInfo   *metainfo.MetaInfo
31         Conns      []*Connection
32         Peers      []Peer
33         Priorities *list.List
34         // BEP 12 Multitracker Metadata Extension. The tracker.Client instances
35         // mirror their respective URLs from the announce-list key.
36         Trackers [][]tracker.Client
37 }
38
39 func (t *Torrent) NumPieces() int {
40         return len(t.MetaInfo.Pieces) / PieceHash.Size()
41 }
42
43 func (t *Torrent) NumPiecesCompleted() (num int) {
44         for _, p := range t.Pieces {
45                 if p.Complete() {
46                         num++
47                 }
48         }
49         return
50 }
51
52 func (t *Torrent) Length() int64 {
53         return int64(t.PieceLength(peer_protocol.Integer(len(t.Pieces)-1))) + int64(len(t.Pieces)-1)*int64(t.PieceLength(0))
54 }
55
56 func (t *Torrent) Close() (err error) {
57         t.Data.Close()
58         for _, conn := range t.Conns {
59                 conn.Close()
60         }
61         return
62 }
63
64 func (t *Torrent) piecesByPendingBytesDesc() (indices []peer_protocol.Integer) {
65         slice := pieceByBytesPendingSlice{
66                 Pending: make([]peer_protocol.Integer, 0, len(t.Pieces)),
67                 Indices: make([]peer_protocol.Integer, 0, len(t.Pieces)),
68         }
69         for i := range t.Pieces {
70                 slice.Pending = append(slice.Pending, t.PieceNumPendingBytes(peer_protocol.Integer(i)))
71                 slice.Indices = append(slice.Indices, peer_protocol.Integer(i))
72         }
73         sort.Sort(sort.Reverse(slice))
74         return slice.Indices
75 }
76
77 func (t *Torrent) offsetRequest(off int64) (req Request, ok bool) {
78         req.Index = peer_protocol.Integer(off / t.MetaInfo.PieceLength)
79         if req.Index < 0 || int(req.Index) >= len(t.Pieces) {
80                 return
81         }
82         off %= t.MetaInfo.PieceLength
83         pieceLeft := t.PieceLength(req.Index) - peer_protocol.Integer(off)
84         if pieceLeft <= 0 {
85                 return
86         }
87         req.Begin = chunkSize * (peer_protocol.Integer(off) / chunkSize)
88         req.Length = chunkSize
89         if req.Length > pieceLeft {
90                 req.Length = pieceLeft
91         }
92         ok = true
93         return
94 }
95
96 func (t *Torrent) WriteChunk(piece int, begin int64, data []byte) (err error) {
97         _, err = t.Data.WriteAt(data, int64(piece)*t.MetaInfo.PieceLength+begin)
98         return
99 }
100
101 func (t *Torrent) bitfield() (bf []bool) {
102         for _, p := range t.Pieces {
103                 bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
104         }
105         return
106 }
107
108 func (t *Torrent) pendAllChunkSpecs(index peer_protocol.Integer) {
109         piece := t.Pieces[index]
110         if piece.PendingChunkSpecs == nil {
111                 piece.PendingChunkSpecs = make(
112                         map[ChunkSpec]struct{},
113                         (t.MetaInfo.PieceLength+chunkSize-1)/chunkSize)
114         }
115         c := ChunkSpec{
116                 Begin: 0,
117         }
118         cs := piece.PendingChunkSpecs
119         for left := peer_protocol.Integer(t.PieceLength(index)); left != 0; left -= c.Length {
120                 c.Length = left
121                 if c.Length > chunkSize {
122                         c.Length = chunkSize
123                 }
124                 cs[c] = struct{}{}
125                 c.Begin += c.Length
126         }
127         return
128 }
129
130 func (t *Torrent) requestHeat() (ret map[Request]int) {
131         ret = make(map[Request]int)
132         for _, conn := range t.Conns {
133                 for req, _ := range conn.Requests {
134                         ret[req]++
135                 }
136         }
137         return
138 }
139
140 type Peer struct {
141         Id   [20]byte
142         IP   net.IP
143         Port int
144 }
145
146 func (t *Torrent) PieceLength(piece peer_protocol.Integer) (len_ peer_protocol.Integer) {
147         if int(piece) == t.NumPieces()-1 {
148                 len_ = peer_protocol.Integer(t.Data.Size() % t.MetaInfo.PieceLength)
149         }
150         if len_ == 0 {
151                 len_ = peer_protocol.Integer(t.MetaInfo.PieceLength)
152         }
153         return
154 }
155
156 func (t *Torrent) HashPiece(piece peer_protocol.Integer) (ps pieceSum) {
157         hash := PieceHash.New()
158         n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
159         if err != nil {
160                 panic(err)
161         }
162         if peer_protocol.Integer(n) != t.PieceLength(piece) {
163                 panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceLength(piece), n, piece))
164         }
165         copyHashSum(ps[:], hash.Sum(nil))
166         return
167 }
168 func (t *Torrent) haveAllPieces() bool {
169         for _, piece := range t.Pieces {
170                 if !piece.Complete() {
171                         return false
172                 }
173         }
174         return true
175 }
176
177 func (me *Torrent) haveAnyPieces() bool {
178         for _, piece := range me.Pieces {
179                 if piece.Complete() {
180                         return true
181                 }
182         }
183         return false
184 }
185
186 func (t *Torrent) wantPiece(index int) bool {
187         p := t.Pieces[index]
188         return p.EverHashed && len(p.PendingChunkSpecs) != 0
189 }