Example #1
0
func NewPiece(index, offset, length int, hash string, file *File) *Piece {
	p := new(Piece)

	p.Complete = bitarray.New(length / PieceChunkLenght)
	p.Requested = bitarray.New(p.Complete.Len())

	p.File = file
	p.Hash = hash
	p.Index = index
	p.Offset = offset
	p.Length = length

	return p
}
Example #2
0
func NewTorrent(clientId ClientId, port int, torrent string, downloadPath string) *Torrent {
	t := new(Torrent)
	t.ClientId = clientId
	t.Port = port
	t.DownloadPath = downloadPath
	t.Downloaded = 0
	t.Uploaded = 0
	t.IsComplete = false

	metaInfo := readTorrent(torrent)

	t.Announce = metaInfo.Announce
	t.InfoHash = metaInfo.InfoHash
	t.CreationDate = metaInfo.CreationDate
	t.Name = metaInfo.Info.Name
	t.Length = metaInfo.Info.Length
	t.PieceHashes = metaInfo.Info.Pieces
	t.PieceLength = metaInfo.Info.PieceLength
	t.PieceCount = t.Length / t.PieceLength

	t.BitField = bitarray.New(t.PieceCount)
	t.Tracker = NewTracker(t.Announce)

	log.Debugf("File Length: %v", t.Length)
	log.Debugf("Piece Length: %v", t.PieceLength)
	log.Debugf("Piece Count: %v", t.PieceCount)
	return t
}
Example #3
0
func (torrent *Torrent) AddPeer(peerAddr net.TCPAddr) {
	peer := Peer{
		Addr:        peerAddr,
		PieceLength: uint32(torrent.PieceLength),
		PieceCount:  uint32(torrent.PieceCount),
		BitField:    bitarray.New(torrent.PieceCount),
	}

	torrent.Peers = append(torrent.Peers, &peer)
}