D7024E

package module
v0.0.0-...-3d666a7 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: GPL-3.0 Imports: 15 Imported by: 0

README

D7024E

A distrubuted systems using docker containers and the Kademila algorithm.

Building the system

Prerequisite
  • Docker
  • Go
Clone git

Start by cloning the git to your own workspace:

git clone https://github.com/Janaza/D7024E.git
Building main

Change directory to main:

cd D7024E\main

Run the following command to build the main:

go build .\main.go
Run main

You are now ready to run the main using the following command:

.\main.exe -port *port* -bootstrapIP *IP* (optional)

where <-port> specifies which port to use and <-bootstrapIP> specifies which ip to connect to, just starts listener if ommited.

Docker script

However, the main itself is not an effective way to spin up many nodes so you can use a docker script that provides you with 50 nodes all part of the same Kademlia network.

Start cluster with main/run.sh (OBS terminates any running containers!) chmod +x run.sh if necessary

Command Line Interface

The system provides a Command Line Interface (CLI) in order to save values, retrieve values, terminate a node and retrieve the k closest contacts of from the node.

Put

The CLI for saving objects to the system.

PUT <value>

PUT takes a single argument , that is the content of the value you are uploading. After Successful save it outputs the hash of that object.

Get

The CLI for receiving an object that is stored within the Kademlia network.

GET <hash>

GET takes a single argument , if the hash is stored somewhere in the system you get the value returned, else you receive a list of the k closest contacts.

Exit

CLI for terminating the node.

EXIT

Terminates the node after three seconds.

Contacts

CLI for retrieving the k closest contacts from a node.

CONTACTS

Documentation

Index

Constants

View Source
const IDLength = 20

the static number of bytes in a KademliaID

View Source
const ID_INDEX = 40

Variables

This section is empty.

Functions

func BitsToKademliaID

func BitsToKademliaID(bArr []string) [IDLength]byte

func ContactToByte

func ContactToByte(contactArr []Contact) []byte

func ErrorHandler

func ErrorHandler(err error)

log any errors

func Eth0IP

func Eth0IP() (string, error)

func HashData

func HashData(data []byte) string

func SendFindContactMessage

func SendFindContactMessage(contact *Contact, found chan []Contact, sl *Shortlist, target *Contact)

func SendFindDataMessage

func SendFindDataMessage(hash string, contact *Contact, found chan []Contact, value chan string)

func SendPingMessage

func SendPingMessage(contact *Contact, me *Contact) (data, error)

func SendPongMessage

func SendPongMessage(r response, me *Contact)

func SendStoreMessage

func SendStoreMessage(contact *Contact, b []byte)

Types

type Contact

type Contact struct {
	ID      *KademliaID
	Address string
	// contains filtered or unexported fields
}

Contact definition stores the KademliaID, the ip address and the distance

func ByteToContact

func ByteToContact(msg []byte) []Contact

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

func (candidates *ContactCandidates) GetContacts(count int) []Contact

GetContacts returns the first 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 {
	Rtable *RoutingTable
	// contains filtered or unexported fields
}

func InitKad

func InitKad(me Contact) *Kademlia

func (*Kademlia) LookupContact

func (kademlia *Kademlia) LookupContact(me *Contact, result chan []Contact, target Contact)

func (*Kademlia) LookupData

func (kademlia *Kademlia) LookupData(me *Contact, target Contact, hash string) string

func (*Kademlia) Store

func (kademlia *Kademlia) Store(data []byte, me *Contact)

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 {
	Contact *Contact
	Kad     *Kademlia
}

func InitBootstrap

func InitBootstrap(myport string) *Network

InitBootstrap inits a new node with no known nodes

func InitJoin

func InitJoin(myport string, bIP string) *Network

InitJoin joins a network with given IP address 1. Grabs ip from eth0 and generates a new nodeID 2. Inserts some known node by pinging a given bootstrap ip 3. Finds new nodes with IterativeFindNode on nodeID (me) 4. Refreshes all buckets further away than its closest neighbor.

func InitNode

func InitNode(me *Contact) *Network

func (*Network) Cli

func (network *Network) Cli(input io.Reader)

func (*Network) CliHelper

func (network *Network) CliHelper(input io.Reader)

func (*Network) HandleFindDataMsg

func (network *Network) HandleFindDataMsg(msg string, r response)

func (*Network) HandleFindNodeMsg

func (network *Network) HandleFindNodeMsg(msg data, r response)

func (*Network) HandlePingMsg

func (network *Network) HandlePingMsg(msg data, r response)

handles incoming ping msgs

func (*Network) HandlePongMsg

func (network *Network) HandlePongMsg(msg data)

func (*Network) HandleStoreMsg

func (network *Network) HandleStoreMsg(msg string, resp response)

func (*Network) IterativeFindData

func (network *Network) IterativeFindData(hash string)

Helper function to call LookupData()

func (*Network) IterativeFindNode

func (network *Network) IterativeFindNode(target *Contact) []Contact

Helper function to call LookupContact()

func (*Network) IterativeStore

func (network *Network) IterativeStore(data []byte)

Helper function to call Store()

func (*Network) Listen

func (network *Network) Listen(contact Contact)

Starts UDP listener

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

func (routingTable *RoutingTable) FindClosestContacts(target *KademliaID, count int) []Contact

FindClosestContacts finds the count closest Contacts to the target in the RoutingTable

func (*RoutingTable) GetBucketByID

func (routingTable *RoutingTable) GetBucketByID(id *KademliaID) *bucket

Get k-bucket

func (*RoutingTable) GetBucketIndex

func (routingTable *RoutingTable) GetBucketIndex(id *KademliaID) int

getBucketIndex get the correct Bucket index for the KademliaID

type Shortlist

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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