dht

package
v1.8.6 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MSG_PING_REQ int = iota
	MSG_PING_RESP
	MSG_FIND_CONTACTS_REQ
	MSG_FIND_CONTACTS_RESP
	MSG_PUT_REQ
	MSG_PUT_RESP
	MSG_GET_REQ
	MSG_GET_RESP
)
View Source
const (
	FIND_CONTACTS_STATUS_SUCCESS = 0
	FIND_CONTACTS_STATUS_ERROR   = 1
)
View Source
const (
	GET_STATUS_SUCCESS = 0
	GET_STATUS_ERROR   = 1
)
View Source
const (
	PING_STATUS_SUCCESS = 0
	PING_STATUS_ERROR   = 1
)
View Source
const (
	PUT_STATUS_SUCCESS = 0
	PUT_STATUS_ERROR   = 1
)
View Source
const (
	ADD_CONTACT   int = 0
	FIND_CONTACTS int = 1
	PUT           int = 2
	GET           int = 3
	STOP          int = 100
)
View Source
const IDLength = 32
View Source
const MAX_COUNT = 100
View Source
const MaxPendingRequests = 10000
View Source
const RegistrationReplicationFactor = 10

Variables

This section is empty.

Functions

func ValidateKV

func ValidateKV(kv *KV) (bool, error)

Types

type Contact

type Contact struct {
	ID   KademliaID `json:"kademliaid"`
	Node p2p.Node   `json:"node"`
	// contains filtered or unexported fields
}

func CreateContact

func CreateContact(node p2p.Node, prvKey string) (Contact, error)

func (*Contact) CalcDistance

func (contact *Contact) CalcDistance(target KademliaID)

func (*Contact) Less

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

func (*Contact) String

func (contact *Contact) String() string

type ContactCandidates

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

func (*ContactCandidates) Append

func (candidates *ContactCandidates) Append(contacts []Contact)

func (*ContactCandidates) GetContacts

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

func (*ContactCandidates) Len

func (candidates *ContactCandidates) Len() int

func (*ContactCandidates) Less

func (candidates *ContactCandidates) Less(i, j int) bool

func (*ContactCandidates) Sort

func (candidates *ContactCandidates) Sort()

func (*ContactCandidates) Swap

func (candidates *ContactCandidates) Swap(i, j int)

type DHT

type DHT interface {

	// Register adds a new node to the DHT network using a bootstrap node's address and a unique KademliaID.
	// The context allows for request cancellation and timeout control.
	RegisterNetwork(bootstrapNode p2p.Node, kademliaID string, ctx context.Context) error

	// RegisterNetworkWithAddr adds a new node to the DHT network using a bootstrap node's address and a unique KademliaID.
	RegisterNetworkWithAddr(bootstrapNodeAddr string, kademliaID string, ctx context.Context) error

	// FindContact retrieves information about a node identified by its KademliaID.
	// It returns a Contact structure with the node's information or an error if the node is not found.
	FindContact(kademliaID string, ctx context.Context) (Contact, error)

	// FindContacts finds up to 'count' number of contacts closest to the specified KademliaID.
	// It returns a slice of Contact structures or an error if the operation fails.
	FindContacts(kademliaID string, count int, ctx context.Context) ([]Contact, error)

	// Put stores a value in the DHT under a specified key. The key must adhere to a specific format
	// '/key1/key2/.../keyN' with 1 to 5 alphanumeric sublevels and no trailing slash. The 'replicationFactor'
	// determines the number of nodes across which the value is replicated.
	// The first key (key1) is referred to as the root key and is used by the DHT to find the correct node to store the value.
	Put(id string, prvKey string, key string, value string, replicationFactor int, ctx context.Context) error

	// Get retrieves all values stored under the subkeys of a given path from the DHT.
	// The key provided must follow the defined format '/key1/key2/.../keyN' with 1 to 5 alphanumeric sublevels
	// and no trailing slash. This method fetched data stored under the hierarchical key structure.
	// It returns a map of subkey-value pairs if successful or an error if the retrieval operation fails or if the
	// specified path does not exist.
	Get(id string, key string, replicationFactor int, ctx context.Context) ([]KV, error)

	// GetContact returns the contact information of the local DHT node.
	GetContact() Contact

	RegisterNode(id string, prvKey string, node *p2p.Node, ctx context.Context) error

	LookupNode(id string, name string, ctx context.Context) (*p2p.Node, error)

	// Shutdown gracefully stops the DHT node and releases all resources.
	Shutdown()
}

DHT defines the operations supported by a Distributed Hash Table in a Kademlia network.

func CreateDHT

func CreateDHT(port int, name string) (DHT, error)

type FindContactsReq

type FindContactsReq struct {
	Header     RPCHeader `json:"header"`
	KademliaID string    `json:"kademliaid"`
	Count      int       `json:"count"`
}

func ConvertJSONToFindContactsReq

func ConvertJSONToFindContactsReq(jsonStr string) (*FindContactsReq, error)

func (*FindContactsReq) ToJSON

func (req *FindContactsReq) ToJSON() (string, error)

type FindContactsResp

type FindContactsResp struct {
	Header   RPCHeader `json:"header"`
	Contacts []Contact `json:"contacts"`
	Status   int       `json:"status"`
	Error    string    `json:"error"`
}

func ConvertJSONToFindContactsResp

func ConvertJSONToFindContactsResp(jsonStr string) (*FindContactsResp, error)

func (*FindContactsResp) ToJSON

func (resp *FindContactsResp) ToJSON() (string, error)

type GetReq

type GetReq struct {
	Header RPCHeader `json:"header"`
	Key    string    `json:"key"`
}

func ConvertJSONToGetReq

func ConvertJSONToGetReq(jsonStr string) (*GetReq, error)

func (*GetReq) ToJSON

func (req *GetReq) ToJSON() (string, error)

type GetResp

type GetResp struct {
	Header RPCHeader `json:"header"`
	KVS    []KV      `json:"kvs"`
	Status int       `json:"status"`
	Error  string    `json:"error"`
}

func ConvertJSONToGetResp

func ConvertJSONToGetResp(jsonStr string) (*GetResp, error)

func (*GetResp) ToJSON

func (resp *GetResp) ToJSON() (string, error)

type KV

type KV struct {
	ID    string `json:"id"`
	Key   string `json:"key"`
	Value string `json:"value"`
	Sig   string `json:"sig"`
}

func (*KV) String

func (kv *KV) String() string

type Kademlia

type Kademlia struct {
	Contact Contact
	// contains filtered or unexported fields
}

func CreateKademlia

func CreateKademlia(messenger p2p.Messenger, contact Contact) (*Kademlia, error)

func (*Kademlia) FindContact

func (k *Kademlia) FindContact(kademliaID string, ctx context.Context) (Contact, error)

func (*Kademlia) FindContacts

func (k *Kademlia) FindContacts(kademliaID string, count int, ctx context.Context) ([]Contact, error)

func (*Kademlia) Get

func (k *Kademlia) Get(id string, key string, replicationFactor int, ctx context.Context) ([]KV, error)

func (*Kademlia) GetContact

func (k *Kademlia) GetContact() Contact

func (*Kademlia) LookupNode

func (k *Kademlia) LookupNode(id string, name string, ctx context.Context) (*p2p.Node, error)

func (*Kademlia) Put

func (k *Kademlia) Put(id string, prvKey string, key string, value string, replicationFactor int, ctx context.Context) error

func (*Kademlia) RegisterNetwork

func (k *Kademlia) RegisterNetwork(bootstrapNode p2p.Node, kademliaID string, ctx context.Context) error

func (*Kademlia) RegisterNetworkWithAddr

func (k *Kademlia) RegisterNetworkWithAddr(bootstrapNodeAddr string, kademliaID string, ctx context.Context) error

func (*Kademlia) RegisterNode

func (k *Kademlia) RegisterNode(id string, prvKey string, node *p2p.Node, ctx context.Context) error

func (*Kademlia) Shutdown

func (k *Kademlia) Shutdown()

type KademliaID

type KademliaID [IDLength]byte

func CreateKademliaID

func CreateKademliaID(data string) KademliaID

func NewRandomKademliaID

func NewRandomKademliaID() KademliaID

func (KademliaID) CalcDistance

func (kademliaID KademliaID) CalcDistance(target KademliaID) KademliaID

func (KademliaID) Equals

func (kademliaID KademliaID) Equals(otherKademliaID KademliaID) bool

func (KademliaID) Less

func (kademliaID KademliaID) Less(otherKademliaID KademliaID) bool

func (*KademliaID) String

func (kademliaID *KademliaID) String() string

type PingReq

type PingReq struct {
	Header RPCHeader `json:"header"`
}

func ConvertJSONToPingReq

func ConvertJSONToPingReq(jsonStr string) (*PingReq, error)

func (*PingReq) ToJSON

func (req *PingReq) ToJSON() (string, error)

type PingResp

type PingResp struct {
	Header RPCHeader `json:"header"`
	Status int       `json:"status"`
	Error  string    `json:"error"`
}

func ConvertJSONToPingResp

func ConvertJSONToPingResp(jsonStr string) (*PingResp, error)

func (*PingResp) ToJSON

func (req *PingResp) ToJSON() (string, error)

type PutReq

type PutReq struct {
	Header RPCHeader `json:"header"`
	KV     KV        `json:"kv"`
}

func ConvertJSONToPutReq

func ConvertJSONToPutReq(jsonStr string) (*PutReq, error)

func (*PutReq) ToJSON

func (req *PutReq) ToJSON() (string, error)

type PutResp

type PutResp struct {
	Header RPCHeader `json:"header"`
	Status int       `json:"status"`
	Error  string    `json:"error"`
}

func ConvertJSONToPutResp

func ConvertJSONToPutResp(jsonStr string) (*PutResp, error)

func (*PutResp) ToJSON

func (resp *PutResp) ToJSON() (string, error)

type RPCHeader

type RPCHeader struct {
	Sender Contact `json:"contact"`
}

Jump to

Keyboard shortcuts

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