metadata

package module
v0.0.0-...-67a03e3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2018 License: MIT Imports: 13 Imported by: 1

README

metadata

Build Status

a high-performance torrents collector.

Install
go get -u github.com/bttown/metadata
Usage
func saveTorrentFile(req metadata.Request, torrent metadata.Torrent) {
	torrentName := fmt.Sprintf("%s.torrent", torrent.Info.Name)
	f, err := os.Create(torrentName)
	if err != nil {
		log.Println("fail to create torrent file", err)
		return
	}
	defer f.Close()

	f.Write(torrent.Bytes())
}

func main() {
	var c = metadata.NewCollector()
	defer c.Close()

	ip, peerID, hashInfo := os.Args[1], os.Args[3], os.Args[4]
	port, _ := strconv.Atoi(os.Args[2])

	req := metadata.Request{
		IP:       ip,
		Port:     port,
		HashInfo: hashInfo,
		PeerID:   peerID,
	}

	log.Println(c.GetSync(&req, saveTorrentFile, nil))
}

Documentation

Index

Constants

View Source
const (
	// REQUEST  type
	REQUEST = 0
	// DATA type
	DATA = 1
	// REJECT type
	REJECT = 2
)

Variables

View Source
var (
	// ErrCollectorClosed ...
	ErrCollectorClosed = errors.New("collector colsed")
	// ErrGetPiecesTimeout ...
	ErrGetPiecesTimeout = errors.New("get pieces timeout")
	// ErrRejectByPeer ...
	ErrRejectByPeer = errors.New("reject by peer")
	// ErrTooMuchPieces ...
	ErrTooMuchPieces = errors.New("too much peices")
)
View Source
var ErrInvalidMetadata = errors.New("invalid metadata")

ErrInvalidMetadata ...

Functions

func NewTorrentFromMetadata

func NewTorrentFromMetadata(b []byte, torrent *Torrent) error

NewTorrentFrom ...

Types

type Collector

type Collector struct {
	MaxPendingQueries int
	// contains filtered or unexported fields
}

Collector collects metadata.

func NewCollector

func NewCollector(opts Options) *Collector

NewCollector creates and return a new metadata collector.

func (*Collector) Close

func (c *Collector) Close()

Close the collector.

func (*Collector) Get

func (c *Collector) Get(req *Request) error

Get adds a new query to the collector's queries queue. returns a error when queue is full.

func (*Collector) GetSync

func (c *Collector) GetSync(req *Request, then Then, reject Reject) error

GetSync adds a new query to the collector's queries queue and wait until then query finished.

func (*Collector) OnError

func (c *Collector) OnError(errHandler Reject)

OnError registers error handler.

func (*Collector) OnFinish

func (c *Collector) OnFinish(handler Then)

OnFinish registers metadata handler.

type Options

type Options struct {
	QueriesBufferSize int
	MaxPendingQueries int
}

Option of metadata collector.

type Reject

type Reject func(Request, error)

Reject will be called when a error occur.

type Request

type Request struct {
	IP       string
	Port     int
	HashInfo string
	PeerID   string
}

Request is the metadata request.

func (*Request) RemoteAddr

func (req *Request) RemoteAddr() string

RemoteAddr returns net address of the querying peer.

type Then

type Then func(Request, Torrent)

Then will be called when metadata got.

type Torrent

type Torrent struct {
	Announce string
	Info     TorrentInfo
	// contains filtered or unexported fields
}

Torrent ...

func (*Torrent) Bytes

func (torrent *Torrent) Bytes() []byte

Bytes torrent file bytes.

type TorrentInfo

type TorrentInfo struct {
	Files       []TorrentSubFile
	Length      int64
	Name        string
	PieceLength int64
	Pieces      string
}

TorrentInfo ...

type TorrentSubFile

type TorrentSubFile struct {
	Length int64
	Path   string
}

TorrentSubFile ...

Directories

Path Synopsis
examples
sample command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL