dht

package module
v0.0.0-...-a077040 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2016 License: Apache-2.0 Imports: 12 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Documentation

Index

Constants

View Source
const IDLen = 20

IDLen []byte len

Variables

View Source
var ZeroID = new(ID)

ZeroID "0000000000000000000000000000000000000000"

Functions

func ResolveNodes

func ResolveNodes(nodes []byte) (peers map[ID][]byte)

ResolveNodes returns peers

func ResolvePeer

func ResolvePeer(peer []byte) (ip string, port int)

ResolvePeer returns ip and port

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

Bucket manage node

func NewBucket

func NewBucket(first *ID, cap int) *Bucket

NewBucket return a bucket

func (*Bucket) Capacity

func (b *Bucket) Capacity() int

Capacity returns bucket cap

func (*Bucket) Count

func (b *Bucket) Count() int

Count returns count of all nodes

func (*Bucket) Find

func (b *Bucket) Find(id *ID) (node *Node)

Find returns node

func (*Bucket) Insert

func (b *Bucket) Insert(id *ID, addr *net.UDPAddr) (n *Node)

Insert a node, move to back if exist node

func (*Bucket) Map

func (b *Bucket) Map(f func(n *Node) bool)

Map all node

func (*Bucket) Random

func (b *Bucket) Random() *Node

Random returns a random node

func (*Bucket) Remove

func (b *Bucket) Remove(id *ID)

Remove a node

func (*Bucket) String

func (b *Bucket) String() string

func (*Bucket) Time

func (b *Bucket) Time() time.Time

Time returns last contact time

func (*Bucket) Update

func (b *Bucket) Update()

Update contact time

type CallBack

type CallBack func(tor *ID, peer []byte)

CallBack function

type DHT

type DHT struct {
	// contains filtered or unexported fields
}

DHT server

func NewDHT

func NewDHT(id *ID, conn *net.UDPConn, ksize int) *DHT

NewDHT returns DHT

func (*DHT) Addr

func (d *DHT) Addr() *net.UDPAddr

Addr returns dht address

func (*DHT) Conn

func (d *DHT) Conn() *net.UDPConn

Conn returns dht connection

func (*DHT) DoTimer

func (d *DHT) DoTimer(secret, node, peer, search time.Duration)

DoTimer update secret, clean nodes and peers

func (*DHT) FindNode

func (d *DHT) FindNode(id *ID) (err error)

FindNode find node

func (*DHT) FindNodeFromAddr

func (d *DHT) FindNodeFromAddr(id *ID, addr *net.UDPAddr) error

FindNodeFromAddr find node from address

func (*DHT) FindNodeFromAddrs

func (d *DHT) FindNodeFromAddrs(id *ID, addrs []*net.UDPAddr) (int, error)

FindNodeFromAddrs find node from some address

func (*DHT) GetPeers

func (d *DHT) GetPeers(tor *ID) [][]byte

GetPeers returns all peers

func (*DHT) HandleMessage

func (d *DHT) HandleMessage(addr *net.UDPAddr, data []byte, t *Tracker) (err error)

HandleMessage handle udp packet

func (*DHT) ID

func (d *DHT) ID() *ID

ID returns dht id

func (*DHT) Ping

func (d *DHT) Ping(addr *net.UDPAddr) error

Ping a address

func (*DHT) Route

func (d *DHT) Route() *Table

Route returns route table

func (*DHT) Search

func (d *DHT) Search(tor *ID, cb CallBack) (tid int16, err error)

Search info hash

type ErrorTracker

type ErrorTracker interface {
	Error(val int, err string)
}

ErrorTracker interface

type ID

type ID [IDLen]byte

ID consists of 160 bits

func NewID

func NewID(b []byte) (*ID, error)

NewID returns a id

func ResolveID

func ResolveID(s string) (*ID, error)

ResolveID returns a id

func (*ID) Bytes

func (id *ID) Bytes() []byte

Bytes return 20 bytes

func (*ID) Compare

func (id *ID) Compare(o *ID) int

Compare two id

func (*ID) GetBit

func (id *ID) GetBit(i int) (bool, error)

GetBit return bit

func (*ID) LowBit

func (id *ID) LowBit() int

LowBit find the lowest 1 bit in an id

func (*ID) SetBit

func (id *ID) SetBit(i int, b bool) error

SetBit set bit

func (*ID) String

func (id *ID) String() string

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node represent a dht node

func NewNode

func NewNode(id *ID, addr *net.UDPAddr) *Node

NewNode returns a node

func (*Node) Addr

func (n *Node) Addr() *net.UDPAddr

Addr returns udb address

func (*Node) ID

func (n *Node) ID() *ID

ID returns id

func (*Node) String

func (n *Node) String() string

func (*Node) Time

func (n *Node) Time() time.Time

Time returns last contact time

func (*Node) Update

func (n *Node) Update()

Update contact time

type QueryTracker

type QueryTracker interface {
	Ping(id *ID)
	FindNode(id *ID, target *ID)
	GetPeers(id *ID, tor *ID)
	AnnouncePeer(id *ID, tor *ID, peer []byte)
}

QueryTracker interface

type ReplyTracker

type ReplyTracker interface {
	Ping(id *ID)
	FindNode(id *ID, nodes []byte)
	GetPeers(id *ID, peers [][]byte, nodes []byte)
	AnnouncePeer(id *ID)
}

ReplyTracker interface

type Table

type Table struct {
	// contains filtered or unexported fields
}

Table store all nodes

func NewTable

func NewTable(id *ID, ksize int) *Table

NewTable returns a table

func (*Table) Find

func (t *Table) Find(id *ID) *Bucket

Find returns bucket

func (*Table) Insert

func (t *Table) Insert(id *ID, addr *net.UDPAddr) (*Node, error)

Insert a node

func (*Table) KSize

func (t *Table) KSize() int

KSize returns bucket capaticy

func (*Table) Lookup

func (t *Table) Lookup(id *ID) []*Node

Lookup returns the K(8) closest good nodes

func (*Table) Map

func (t *Table) Map(f func(b *Bucket) bool)

Map all buckets

func (*Table) NumNodes

func (t *Table) NumNodes() (n int)

NumNodes returns all node count

func (*Table) String

func (t *Table) String() (s string)

type Tracker

type Tracker struct {
	// contains filtered or unexported fields
}

Tracker struct

func NewTracker

func NewTracker(q QueryTracker, r ReplyTracker, e ErrorTracker) *Tracker

NewTracker returns tracker

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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