...

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

Documentation: github.com/concurrency-8/tracker

     1  package tracker
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"encoding/binary"
     7  	"github.com/concurrency-8/parser"
     8  )
     9  
    10  // ConnectResponse is struture to hoild details from ConnectResponse
    11  type ConnectResponse struct {
    12  	Action        uint32
    13  	TransactionID uint32
    14  	ConnectionID  uint64
    15  }
    16  
    17  // GetMockConnectResponseBuf returns a test buffer with the input transactionID and connectionID
    18  func GetMockConnectResponseBuf(transactionID uint32, connectionID uint64) bytes.Buffer {
    19  	var mockConnectResponseBuf bytes.Buffer
    20  
    21  	binary.Write(&mockConnectResponseBuf, binary.BigEndian, uint32(0)) // action=0 for connect response
    22  	binary.Write(&mockConnectResponseBuf, binary.BigEndian, transactionID)
    23  	binary.Write(&mockConnectResponseBuf, binary.BigEndian, connectionID)
    24  
    25  	return mockConnectResponseBuf
    26  }
    27  
    28  // AnnounceResponse is structure to hold details from announce request sent to tracker
    29  type AnnounceResponse struct {
    30  	Action        uint32
    31  	TransactionID uint32
    32  	Leechers      uint32
    33  	Seeders       uint32
    34  	Complete      uint   `bencode:"complete"`
    35  	Downloaded    uint   `bencode:"downloaded"`
    36  	Incomplete    uint   `bencode:"incomplete"`
    37  	Interval      uint32 `bencode:"interval"`
    38  	MinInterval   uint   `bencode:"min interval"`
    39  	PeerBytes     []byte `bencode:"peers"`
    40  	Peers         []Peer
    41  }
    42  
    43  // GetMockAnnounceResponseBuf returns a test buffer with input transactionID, interval, leechers and seeders
    44  func GetMockAnnounceResponseBuf(transactionID, interval, leechers, seeders uint32, peers []Peer) bytes.Buffer {
    45  	var mockAnnounceResponseBuf bytes.Buffer
    46  	writer := bufio.NewWriter(&mockAnnounceResponseBuf)
    47  
    48  	binary.Write(writer, binary.BigEndian, uint32(1)) // action=1 for announce response
    49  	binary.Write(writer, binary.BigEndian, transactionID)
    50  	binary.Write(writer, binary.BigEndian, interval)
    51  	binary.Write(writer, binary.BigEndian, leechers)
    52  	binary.Write(writer, binary.BigEndian, seeders)
    53  
    54  	for i := 0; i < len(peers); i++ {
    55  		binary.Write(writer, binary.BigEndian, peers[i].IPAdress)
    56  		binary.Write(writer, binary.BigEndian, peers[i].Port)
    57  	}
    58  
    59  	writer.Flush()
    60  	return mockAnnounceResponseBuf
    61  }
    62  
    63  // Peer is a structure contains IP Address of a peer
    64  type Peer struct {
    65  	IPAdress uint32
    66  	Port     uint16
    67  }
    68  
    69  // ClientStatusReport is a structure storing current status for client and relevant information
    70  type ClientStatusReport struct {
    71  	Event       string
    72  	TorrentFile parser.TorrentFile
    73  	PeerID      string
    74  	Port        uint16
    75  	Uploaded    uint64
    76  	Downloaded  uint64
    77  	Left        uint64
    78  	Data        []parser.Piece // This is for seeding
    79  }
    80  
    81  // GetRandomClientReport gives a test ClientStatusReport object pointer.
    82  func GetRandomClientReport() (report *ClientStatusReport) {
    83  
    84  	torrent, _ := parser.ParseFromFile(parser.GetTorrentFileList()[0])
    85  	report = &ClientStatusReport{}
    86  	report.TorrentFile = torrent
    87  	report.PeerID = string(getRandomByteArr(20))
    88  	report.Left = torrent.Length
    89  	report.Port = uint16(6464)
    90  	report.Event = ""
    91  	return report
    92  }
    93  

View as plain text