...

Source file src/github.com/concurrency-8/parser/types.go

Documentation: github.com/concurrency-8/parser

     1  package parser
     2  
     3  import (
     4  	"github.com/zeebo/bencode"
     5  	"math/rand"
     6  	"os"
     7  	"time"
     8  )
     9  
    10  //Struct Tags help bencode in identifying which fields to fill.
    11  
    12  //FileMetaData contains MetaData about a File.
    13  type FileMetaData struct {
    14  	Path   []string `bencode:"path"`
    15  	Length uint64   `bencode:"length"`
    16  }
    17  
    18  //InfoMetaData contains MetaData about the torrent.
    19  type InfoMetaData struct {
    20  	PieceLength uint32             `bencode:"piece length"`
    21  	Piece       []byte             `bencode:"pieces"`
    22  	Name        string             `bencode:"name"`
    23  	Length      uint64             `bencode:"length"`
    24  	Files       bencode.RawMessage `bencode:"files"`
    25  }
    26  
    27  //MetaData contains MetaData about the file.
    28  type MetaData struct {
    29  	Announce     string             `bencode:"announce"`
    30  	AnnounceList [][]string         `bencode:"announce-list"`
    31  	Comment      string             `bencode:"comment"`
    32  	CreatedBy    string             `bencode:"created by"`
    33  	CreatedAt    int64              `bencode:"creation date"`
    34  	Info         bencode.RawMessage `bencode:"info"`
    35  }
    36  
    37  //File contains length and path of a File in the torrent.
    38  type File struct {
    39  	Path        []string
    40  	Length      uint64
    41  	FilePointer *os.File
    42  }
    43  
    44  //TorrentFile contains information about the torrent.
    45  type TorrentFile struct {
    46  	Name        string
    47  	Announce    []string
    48  	Comment     string
    49  	CreatedBy   string
    50  	CreatedAt   time.Time
    51  	InfoHash    string
    52  	Length      uint64
    53  	Files       []*File
    54  	PieceLength uint32
    55  	Piece       []byte
    56  }
    57  
    58  // PieceBlock is struct for a block of a piece
    59  type PieceBlock struct {
    60  	Index   uint32
    61  	Begin   uint32
    62  	Length  uint32
    63  	Nblocks uint32
    64  	Bytes   []byte
    65  }
    66  
    67  // Piece Holds a piece
    68  type Piece struct {
    69  	Blocks []PieceBlock
    70  }
    71  
    72  // RandomPieceBlock returns a random PieceBlock object from torrent
    73  func RandomPieceBlock(torrent TorrentFile) PieceBlock {
    74  	pieceIndex := rand.Uint32() % uint32(len(torrent.Piece)/20)
    75  	blocksPerPiece, err := BlocksPerPiece(torrent, pieceIndex)
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  	blockIndex := rand.Uint32() % blocksPerPiece
    80  	blockLength, err := BlockLen(torrent, pieceIndex, blockIndex)
    81  	if err != nil {
    82  		panic(err)
    83  	}
    84  	return PieceBlock{
    85  		Index:   pieceIndex,
    86  		Begin:   blockIndex * BLOCK_LEN,
    87  		Length:  blockLength,
    88  		Nblocks: blocksPerPiece,
    89  	}
    90  }
    91  
    92  // GetTorrentFileList gives the list of torrentfiles (these should be alive, in test_torrents)
    93  func GetTorrentFileList() []string {
    94  	return []string{"../test_torrents/ubuntu.iso.torrent", "../test_torrents/big-buck-bunny.torrent"}
    95  }
    96  

View as plain text