Documentation ¶
Index ¶
- Constants
- func CreateConnection(address string) net.Conn
- func CreateProtocol(rpc string, contacts []Contact, data []byte, sender Contact, target Contact) []byte
- func GetOutboundIP() string
- func Hash(data []byte) string
- func ReadFromConnection(conn net.Conn, buf []byte) []byte
- type Contact
- type ContactCandidates
- func (candidates *ContactCandidates) Append(contacts []Contact)
- func (candidates *ContactCandidates) AppendNoDups(contacts []Contact)
- func (candidates *ContactCandidates) GetContacts(count int) []Contact
- func (candidates *ContactCandidates) GetUpToContacts(count int) []Contact
- func (candidates *ContactCandidates) Len() int
- func (candidates *ContactCandidates) Less(i, j int) bool
- func (candidates *ContactCandidates) Sort()
- func (candidates *ContactCandidates) Swap(i, j int)
- type Kademlia
- func (kademlia *Kademlia) CheckValue(id KademliaID) []byte
- func (kademlia *Kademlia) InitNode(ch chan Kademlia)
- func (kademlia *Kademlia) LookupContacts(target *KademliaID) []Contact
- func (kademlia *Kademlia) LookupData(hash string) string
- func (kademlia *Kademlia) Store(data []byte) string
- func (kademlia *Kademlia) StoreValue(data []byte)
- type KademliaID
- type Network
- func (network *Network) HandleConn(conn *net.UDPConn, node *Kademlia)
- func (network *Network) Listen(ip string, port int, node *Kademlia)
- func (network *Network) SendFindContactMessage(contact *Contact, target *KademliaID, node *Kademlia) Protocol
- func (network *Network) SendFindDataMessage(contact *Contact, target *KademliaID, node *Kademlia) Protocol
- func (network *Network) SendPingMessage(contact *Contact, node *Kademlia) bool
- func (network *Network) SendStoreMessage(contact *Contact, data []byte, node *Kademlia)
- type Protocol
- type RoutingTable
Constants ¶
const IDLength = 20
the static number of bytes in a KademliaID
Variables ¶
This section is empty.
Functions ¶
func CreateConnection ¶
func CreateProtocol ¶
func GetOutboundIP ¶
func GetOutboundIP() string
Get preferred outbound ip of this machine Taken from https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
Types ¶
type Contact ¶
type Contact struct { ID *KademliaID Address string Distance *KademliaID }
Contact definition stores the KademliaID, the ip address and the distance
func NewContact ¶
func NewContact(id *KademliaID, address string) Contact
NewContact returns a new instance of a Contact
func (*Contact) CalcDistance ¶
func (contact *Contact) CalcDistance(target *KademliaID)
CalcDistance calculates the distance to the target and fills the contacts distance field
type ContactCandidates ¶
type ContactCandidates struct {
// contains filtered or unexported fields
}
ContactCandidates definition stores an array of Contacts
func (*ContactCandidates) Append ¶
func (candidates *ContactCandidates) Append(contacts []Contact)
Append an array of Contacts to the ContactCandidates
func (*ContactCandidates) AppendNoDups ¶
func (candidates *ContactCandidates) AppendNoDups(contacts []Contact)
Append an array of Contacts to the ContactCandidates without duplicates
func (*ContactCandidates) GetContacts ¶
func (candidates *ContactCandidates) GetContacts(count int) []Contact
GetContacts returns the first count number of Contacts
func (*ContactCandidates) GetUpToContacts ¶
func (candidates *ContactCandidates) GetUpToContacts(count int) []Contact
GetUpToContacts returns up to the count number of Contacts
func (*ContactCandidates) Len ¶
func (candidates *ContactCandidates) Len() int
Len returns the length of the ContactCandidates
func (*ContactCandidates) Less ¶
func (candidates *ContactCandidates) Less(i, j int) bool
Less returns true if the Contact at index i is smaller than the Contact at index j
func (*ContactCandidates) Sort ¶
func (candidates *ContactCandidates) Sort()
Sort the Contacts in ContactCandidates
func (*ContactCandidates) Swap ¶
func (candidates *ContactCandidates) Swap(i, j int)
Swap the position of the Contacts at i and j WARNING does not check if either i or j is within range
type Kademlia ¶
type Kademlia struct {
// contains filtered or unexported fields
}
Stores routing table
func NewKademlia ¶
func NewKademlia(table *RoutingTable, network *Network) *Kademlia
func (*Kademlia) CheckValue ¶
func (kademlia *Kademlia) CheckValue(id KademliaID) []byte
Return data if in current node
func (*Kademlia) LookupContacts ¶
func (kademlia *Kademlia) LookupContacts(target *KademliaID) []Contact
Lookup k closest nodes to target in network, k = bucketSize
func (*Kademlia) LookupData ¶
Returns value stored in hash if it exists in network Else returns empty byte slice
func (*Kademlia) StoreValue ¶
Store data in current node
type KademliaID ¶
type definition of a KademliaID
func NewKademliaID ¶
func NewKademliaID(data string) *KademliaID
NewKademliaID returns a new instance of a KademliaID based on the string input
func NewRandomKademliaID ¶
func NewRandomKademliaID() *KademliaID
NewRandomKademliaID returns a new instance of a random KademliaID, change this to a better version if you like
func (KademliaID) CalcDistance ¶
func (kademliaID KademliaID) CalcDistance(target *KademliaID) *KademliaID
CalcDistance returns a new instance of a KademliaID that is built through a bitwise XOR operation betweeen kademliaID and target
func (KademliaID) Equals ¶
func (kademliaID KademliaID) Equals(otherKademliaID *KademliaID) bool
Equals returns true if kademliaID == otherKademliaID (bitwise)
func (KademliaID) Less ¶
func (kademliaID KademliaID) Less(otherKademliaID *KademliaID) bool
Less returns true if kademliaID < otherKademliaID (bitwise)
func (*KademliaID) String ¶
func (kademliaID *KademliaID) String() string
String returns a simple string representation of a KademliaID
type Network ¶
type Network struct { }
func NewNetwork ¶
func NewNetwork() *Network
func (*Network) HandleConn ¶
Check which message has been recevied and handle it accordingly
func (*Network) SendFindContactMessage ¶
func (network *Network) SendFindContactMessage(contact *Contact, target *KademliaID, node *Kademlia) Protocol
FIND_NODE
func (*Network) SendFindDataMessage ¶
func (network *Network) SendFindDataMessage(contact *Contact, target *KademliaID, node *Kademlia) Protocol
FIND_VALUE
func (*Network) SendPingMessage ¶
PING
type Protocol ¶
type Protocol struct { Rpc string // PING, STORE, FIND_NODE, FIND_VALUE Contacts []Contact Data []byte Sender Contact Target Contact }
func Unserialize ¶
type RoutingTable ¶
type RoutingTable struct {
// contains filtered or unexported fields
}
RoutingTable definition keeps a refrence contact of me and an array of buckets
func NewRoutingTable ¶
func NewRoutingTable(me Contact) *RoutingTable
NewRoutingTable returns a new instance of a RoutingTable
func (*RoutingTable) AddContact ¶
func (routingTable *RoutingTable) AddContact(contact Contact)
AddContact add a new contact to the correct Bucket
func (*RoutingTable) AddContacts ¶
func (routingTable *RoutingTable) AddContacts(contacts []Contact)
func (*RoutingTable) FindClosestContacts ¶
func (routingTable *RoutingTable) FindClosestContacts(target *KademliaID, count int) []Contact
FindClosestContacts finds the count closest Contacts to the target in the RoutingTable