d7024e

package
v0.0.0-...-2e2e60a Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const IDLength = 20

the static number of bytes in a KademliaID

Variables

This section is empty.

Functions

func CreateConnection

func CreateConnection(address string) net.Conn

func CreateProtocol

func CreateProtocol(rpc string, contacts []Contact, data []byte, sender Contact, target Contact) []byte

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

func Hash

func Hash(data []byte) string

Hash data and set size to IDLength

func ReadFromConnection

func ReadFromConnection(conn net.Conn, buf []byte) []byte

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

func (*Contact) Less

func (contact *Contact) Less(otherContact *Contact) bool

Less returns true if contact.distance < otherContact.distance

func (*Contact) String

func (contact *Contact) String() string

String returns a simple string representation of a Contact

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) InitNode

func (kademlia *Kademlia) InitNode(ch chan Kademlia)

func (*Kademlia) LookupContacts

func (kademlia *Kademlia) LookupContacts(target *KademliaID) []Contact

Lookup k closest nodes to target in network, k = bucketSize

func (*Kademlia) LookupData

func (kademlia *Kademlia) LookupData(hash string) string

Returns value stored in hash if it exists in network Else returns empty byte slice

func (*Kademlia) Store

func (kademlia *Kademlia) Store(data []byte) string

Store data at k closest nodes in network

func (*Kademlia) StoreValue

func (kademlia *Kademlia) StoreValue(data []byte)

Store data in current node

type KademliaID

type KademliaID [IDLength]byte

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

func (network *Network) HandleConn(conn *net.UDPConn, node *Kademlia)

Check which message has been recevied and handle it accordingly

func (*Network) Listen

func (network *Network) Listen(ip string, port int, node *Kademlia)

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

func (network *Network) SendPingMessage(contact *Contact, node *Kademlia) bool

PING

func (*Network) SendStoreMessage

func (network *Network) SendStoreMessage(contact *Contact, data []byte, node *Kademlia)

STORE

type Protocol

type Protocol struct {
	Rpc      string // PING, STORE, FIND_NODE, FIND_VALUE
	Contacts []Contact
	Data     []byte
	Sender   Contact
	Target   Contact
}

func Unserialize

func Unserialize(msg []byte) Protocol

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

Jump to

Keyboard shortcuts

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