FedP2P/torrent.go

223 lines
5.5 KiB
Go
Raw Normal View History

package torrent
import (
"container/list"
"fmt"
"net"
"sort"
"bitbucket.org/anacrolix/go.torrent/mmap_span"
2014-05-23 18:58:11 +08:00
pp "bitbucket.org/anacrolix/go.torrent/peer_protocol"
"bitbucket.org/anacrolix/go.torrent/tracker"
metainfo "github.com/nsf/libtorgo/torrent"
)
2014-05-23 18:58:11 +08:00
func (t *torrent) PieceNumPendingBytes(index pp.Integer) (count pp.Integer) {
pendingChunks := t.Pieces[index].PendingChunkSpecs
2014-05-23 18:58:11 +08:00
count = pp.Integer(len(pendingChunks)) * chunkSize
_lastChunkSpec := lastChunkSpec(t.PieceLength(index))
if _lastChunkSpec.Length != chunkSize {
if _, ok := pendingChunks[_lastChunkSpec]; ok {
count += _lastChunkSpec.Length - chunkSize
}
}
return
}
type torrent struct {
InfoHash InfoHash
Pieces []*piece
Data mmap_span.MMapSpan
MetaInfo *metainfo.MetaInfo
Conns []*connection
Peers []Peer
Priorities *list.List
// BEP 12 Multitracker Metadata Extension. The tracker.Client instances
// mirror their respective URLs from the announce-list key.
Trackers [][]tracker.Client
lastReadPiece int
}
func (t *torrent) String() string {
return t.MetaInfo.Name
}
func (t *torrent) BytesLeft() (left int64) {
2014-05-23 18:58:11 +08:00
for i := pp.Integer(0); i < pp.Integer(t.NumPieces()); i++ {
left += int64(t.PieceNumPendingBytes(i))
}
return
}
func NumChunksForPiece(chunkSize int, pieceSize int) int {
return (pieceSize + chunkSize - 1) / chunkSize
}
func (t *torrent) ChunkCount() (num int) {
num += (t.NumPieces() - 1) * NumChunksForPiece(chunkSize, int(t.PieceLength(0)))
num += NumChunksForPiece(chunkSize, int(t.PieceLength(pp.Integer(t.NumPieces()-1))))
return
}
func (t *torrent) LastPieceSize() int {
return int(t.PieceLength(pp.Integer(t.NumPieces() - 1)))
}
func (t *torrent) NumPieces() int {
return len(t.MetaInfo.Pieces) / pieceHash.Size()
}
func (t *torrent) NumPiecesCompleted() (num int) {
for _, p := range t.Pieces {
if p.Complete() {
num++
}
}
return
}
func (t *torrent) Length() int64 {
2014-05-23 18:58:11 +08:00
return int64(t.PieceLength(pp.Integer(len(t.Pieces)-1))) + int64(len(t.Pieces)-1)*int64(t.PieceLength(0))
}
func (t *torrent) Close() (err error) {
t.Data.Close()
for _, conn := range t.Conns {
conn.Close()
}
return
}
2014-05-23 18:58:11 +08:00
func (t *torrent) piecesByPendingBytes() (indices []pp.Integer) {
slice := pieceByBytesPendingSlice{
2014-05-23 18:58:11 +08:00
Pending: make([]pp.Integer, 0, len(t.Pieces)),
Indices: make([]pp.Integer, 0, len(t.Pieces)),
}
for i := range t.Pieces {
2014-05-23 18:58:11 +08:00
slice.Pending = append(slice.Pending, t.PieceNumPendingBytes(pp.Integer(i)))
slice.Indices = append(slice.Indices, pp.Integer(i))
}
2014-05-21 15:42:06 +08:00
sort.Sort(slice)
return slice.Indices
}
// Return the request that would include the given offset into the torrent data.
func torrentOffsetRequest(torrentLength, pieceSize, chunkSize, offset int64) (
2014-04-16 19:13:44 +08:00
r request, ok bool) {
if offset < 0 || offset >= torrentLength {
return
}
2014-05-23 18:58:11 +08:00
r.Index = pp.Integer(offset / pieceSize)
r.Begin = pp.Integer(offset % pieceSize / chunkSize * chunkSize)
left := torrentLength - int64(r.Index)*pieceSize - int64(r.Begin)
if chunkSize < left {
2014-05-23 18:58:11 +08:00
r.Length = pp.Integer(chunkSize)
} else {
2014-05-23 18:58:11 +08:00
r.Length = pp.Integer(left)
}
ok = true
return
}
2014-04-16 19:13:44 +08:00
func torrentRequestOffset(torrentLength, pieceSize int64, r request) (off int64) {
off = int64(r.Index)*pieceSize + int64(r.Begin)
if off < 0 || off >= torrentLength {
panic("invalid request")
}
return
}
2014-04-16 19:13:44 +08:00
func (t *torrent) requestOffset(r request) int64 {
return torrentRequestOffset(t.Length(), t.MetaInfo.PieceLength, r)
}
// Return the request that would include the given offset into the torrent data.
2014-04-16 19:13:44 +08:00
func (t *torrent) offsetRequest(off int64) (req request, ok bool) {
return torrentOffsetRequest(t.Length(), t.MetaInfo.PieceLength, chunkSize, off)
}
func (t *torrent) WriteChunk(piece int, begin int64, data []byte) (err error) {
_, err = t.Data.WriteAt(data, int64(piece)*t.MetaInfo.PieceLength+begin)
return
}
func (t *torrent) bitfield() (bf []bool) {
for _, p := range t.Pieces {
bf = append(bf, p.EverHashed && len(p.PendingChunkSpecs) == 0)
}
return
}
2014-05-23 18:58:11 +08:00
func (t *torrent) pendAllChunkSpecs(index pp.Integer) {
piece := t.Pieces[index]
if piece.PendingChunkSpecs == nil {
piece.PendingChunkSpecs = make(
map[chunkSpec]struct{},
(t.MetaInfo.PieceLength+chunkSize-1)/chunkSize)
}
c := chunkSpec{
Begin: 0,
}
cs := piece.PendingChunkSpecs
2014-05-23 18:58:11 +08:00
for left := pp.Integer(t.PieceLength(index)); left != 0; left -= c.Length {
c.Length = left
if c.Length > chunkSize {
c.Length = chunkSize
}
cs[c] = struct{}{}
c.Begin += c.Length
}
return
}
type Peer struct {
Id [20]byte
IP net.IP
Port int
}
2014-05-23 18:58:11 +08:00
func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {
if int(piece) == t.NumPieces()-1 {
2014-05-23 18:58:11 +08:00
len_ = pp.Integer(t.Data.Size() % t.MetaInfo.PieceLength)
}
if len_ == 0 {
2014-05-23 18:58:11 +08:00
len_ = pp.Integer(t.MetaInfo.PieceLength)
}
return
}
2014-05-23 18:58:11 +08:00
func (t *torrent) HashPiece(piece pp.Integer) (ps pieceSum) {
hash := pieceHash.New()
n, err := t.Data.WriteSectionTo(hash, int64(piece)*t.MetaInfo.PieceLength, t.MetaInfo.PieceLength)
if err != nil {
panic(err)
}
2014-05-23 18:58:11 +08:00
if pp.Integer(n) != t.PieceLength(piece) {
panic(fmt.Sprintf("hashed wrong number of bytes: expected %d; did %d; piece %d", t.PieceLength(piece), n, piece))
}
copyHashSum(ps[:], hash.Sum(nil))
return
}
func (t *torrent) haveAllPieces() bool {
for _, piece := range t.Pieces {
if !piece.Complete() {
return false
}
}
return true
}
func (me *torrent) haveAnyPieces() bool {
for _, piece := range me.Pieces {
if piece.Complete() {
return true
}
}
return false
}
func (t *torrent) wantPiece(index int) bool {
p := t.Pieces[index]
return p.EverHashed && len(p.PendingChunkSpecs) != 0
}