Commit bc2618df authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

dht interface beginnings

parent 41a725c2
package dht
import (
"time"
mh "github.com/jbenet/go-multihash"
peer "github.com/jbenet/go-ipfs/peer"
"errors"
"net"
)
var NotFound = errors.New("Not Found")
var NotAvailable = errors.New("Not Available")
var TimeoutExceeded = errors.New("Timeout Exceeded")
// The IPFS DHT is an implementation of Kademlia with
// Coral and S/Kademlia modifications. It is used to
// implement the base IPFS Routing module.
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
type DHT struct {
//Network
Network net.Conn
// DHT Configuration Settings
Config DHTConfig
//Republish
Republish *DHTRepublish
}
// TODO: not call this republish
type DHTRepublish struct {
Strict []*DHTObject
Sloppy []*DHTObject
}
type DHTObject struct {
Key string
Value *DHTValue
LastPublished *time.Time
}
func (o *DHTObject) ShouldRepublish(interval time.Duration) bool {
return (time.Now().Second() - o.LastPublished.Second()) > int(interval.Seconds())
}
// A struct representing a value in the DHT
type DHTValue struct {}
type DHTConfig struct {
// Time to wait between republishing intervals
RepublishInterval time.Duration
// Multihash hash function
HashType int
}
// Looks for a particular node
func (dht *DHT) FindNode(id *peer.ID /* and a callback? */) error {
panic("Not implemented.")
}
func (dht *DHT) PingNode(id *peer.ID, timeout time.Duration) error {
panic("Not implemented.")
}
// Retrieves a value for a given key
func (dht *DHT) GetValue(key string) *DHTValue {
panic("Not implemented.")
}
// Stores a value for a given key
func (dht *DHT) SetValue(key string, value *DHTValue) error {
panic("Not implemented.")
}
// GetSloppyValues finds (at least) a number of values for given key
func (dht *DHT) GetSloppyValues(key string, count int) ([]*DHTValue, error) {
panic("Not implemented.")
}
func (dht *DHT) SetSloppyValue(key string, value *DHTValue) error {
panic("Not implemented.")
}
func (dht *DHT) periodicRepublish() {
tick := time.NewTicker(time.Second * 5)
for {
select {
case <-tick.C:
for _,v := range dht.Republish.Strict {
if v.ShouldRepublish(dht.Config.RepublishInterval) {
dht.SetValue(v.Key, v.Value)
}
}
for _,v := range dht.Republish.Sloppy {
if v.ShouldRepublish(dht.Config.RepublishInterval) {
dht.SetSloppyValue(v.Key, v.Value)
}
}
}
}
}
func (dht *DHT) handleMessage(message []byte) {
}
func (dht *DHT) coerceMultihash(hash mh.Multihash) {
}
package dht
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
// IpfsDHT is an implementation of Kademlia with Coral and S/Kademlia modifications.
// It is used to implement the base IpfsRouting module.
type IpfsDHT struct {
routes RoutingTable
}
package dht
import (
"time"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
)
// This file implements the Routing interface for the IpfsDHT struct.
// Basic Put/Get
// PutValue adds value corresponding to given Key.
func (s *IpfsDHT) PutValue(key u.Key, value []byte) (error) {
return u.ErrNotImplemented
}
// GetValue searches for the value corresponding to given Key.
func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
return nil, u.ErrNotImplemented
}
// Value provider layer of indirection.
// This is what DSHTs (Coral and MainlineDHT) do to store large values in a DHT.
// Announce that this node can provide value for given key
func (s *IpfsDHT) Provide(key u.Key) (error) {
return u.ErrNotImplemented
}
// FindProviders searches for peers who can provide the value for given key.
func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) (*peer.Peer, error) {
return nil, u.ErrNotImplemented
}
// Find specific Peer
// FindPeer searches for a peer with given ID.
func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
return nil, u.ErrNotImplemented
}
package dht
import (
"container/list"
)
// ID for IpfsDHT should be a byte slice, to allow for simpler operations
// (xor). DHT ids are based on the peer.IDs.
//
// NOTE: peer.IDs are biased because they are (a) multihashes (first bytes
// biased), and (b) first bits are zeroes when using the S/Kademlia PoW.
// Thus, may need to re-hash keys (uniform dist). TODO(jbenet)
type ID []byte
// Bucket holds a list of peers.
type Bucket []*list.List
// RoutingTable defines the routing table.
type RoutingTable struct {
// kBuckets define all the fingers to other nodes.
Buckets []Bucket
}
func (id ID) commonPrefixLen() int {
for i := 0; i < len(id); i++ {
for j := 0; j < 8; j++ {
if (id[i] >> uint8(7 - j)) & 0x1 != 0 {
return i * 8 + j;
}
}
}
return len(id) * 8 - 1;
}
func xor(a, b ID) ID {
// ids may actually be of different sizes.
var ba ID
var bb ID
if len(a) >= len(b) {
ba = a
bb = b
} else {
ba = b
bb = a
}
c := make(ID, len(ba))
for i := 0; i < len(ba); i++ {
if len(bb) > i {
c[i] = ba[i] ^ bb[i]
} else {
c[i] = ba[i] ^ 0
}
}
return c
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment