2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
2014-07-28 22:14:27 -07:00
|
|
|
import (
|
2014-08-07 14:16:24 -07:00
|
|
|
"bytes"
|
2014-08-08 18:09:21 -07:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
2014-08-09 22:28:46 -07:00
|
|
|
kb "github.com/jbenet/go-ipfs/routing/kbucket"
|
2014-08-08 18:09:21 -07:00
|
|
|
swarm "github.com/jbenet/go-ipfs/swarm"
|
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-07-31 17:43:48 -07:00
|
|
|
|
|
|
|
ma "github.com/jbenet/go-multiaddr"
|
2014-07-30 20:16:34 -07:00
|
|
|
|
|
|
|
ds "github.com/jbenet/datastore.go"
|
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
"code.google.com/p/goprotobuf/proto"
|
2014-07-28 22:14:27 -07:00
|
|
|
)
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// 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 {
|
2014-08-07 18:06:50 -07:00
|
|
|
// Array of routing tables for differently distanced nodes
|
|
|
|
// NOTE: (currently, only a single table is used)
|
2014-08-08 19:58:42 -07:00
|
|
|
routes []*kb.RoutingTable
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
network swarm.Network
|
2014-07-29 14:50:33 -07:00
|
|
|
|
2014-07-30 20:16:34 -07:00
|
|
|
// Local peer (yourself)
|
|
|
|
self *peer.Peer
|
|
|
|
|
|
|
|
// Local data
|
|
|
|
datastore ds.Datastore
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-03 21:46:01 -07:00
|
|
|
// Map keys to peers that can provide their value
|
2014-08-08 18:09:21 -07:00
|
|
|
providers map[u.Key][]*providerInfo
|
2014-08-05 09:38:26 -07:00
|
|
|
providerLock sync.RWMutex
|
2014-08-03 21:46:01 -07:00
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
// map of channels waiting for reply messages
|
2014-08-07 14:16:24 -07:00
|
|
|
listeners map[uint64]*listenInfo
|
2014-07-29 14:50:33 -07:00
|
|
|
listenLock sync.RWMutex
|
2014-07-29 19:33:51 -07:00
|
|
|
|
|
|
|
// Signal to shutdown dht
|
|
|
|
shutdown chan struct{}
|
2014-08-06 18:37:45 -07:00
|
|
|
|
|
|
|
// When this peer started up
|
|
|
|
birth time.Time
|
2014-08-07 14:16:24 -07:00
|
|
|
|
|
|
|
//lock to make diagnostics work better
|
|
|
|
diaglock sync.Mutex
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
// The listen info struct holds information about a message that is being waited for
|
2014-08-07 14:16:24 -07:00
|
|
|
type listenInfo struct {
|
2014-08-08 18:09:21 -07:00
|
|
|
// Responses matching the listen ID will be sent through resp
|
2014-08-07 14:16:24 -07:00
|
|
|
resp chan *swarm.Message
|
2014-08-08 18:09:21 -07:00
|
|
|
|
|
|
|
// count is the number of responses to listen for
|
2014-08-07 14:16:24 -07:00
|
|
|
count int
|
2014-08-08 18:09:21 -07:00
|
|
|
|
|
|
|
// eol is the time at which this listener will expire
|
2014-08-07 18:06:50 -07:00
|
|
|
eol time.Time
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
// NewDHT creates a new DHT object with the given peer as the 'local' host
|
2014-08-10 21:40:17 -07:00
|
|
|
func NewDHT(p *peer.Peer, net swarm.Network) *IpfsDHT {
|
2014-08-01 13:21:51 -07:00
|
|
|
dht := new(IpfsDHT)
|
2014-08-10 21:40:17 -07:00
|
|
|
dht.network = net
|
2014-07-31 17:43:48 -07:00
|
|
|
dht.datastore = ds.NewMapDatastore()
|
|
|
|
dht.self = p
|
2014-08-07 14:16:24 -07:00
|
|
|
dht.listeners = make(map[uint64]*listenInfo)
|
2014-08-06 18:37:45 -07:00
|
|
|
dht.providers = make(map[u.Key][]*providerInfo)
|
2014-07-30 20:16:34 -07:00
|
|
|
dht.shutdown = make(chan struct{})
|
2014-08-11 20:11:23 -07:00
|
|
|
|
|
|
|
dht.routes = make([]*kb.RoutingTable, 3)
|
|
|
|
dht.routes[0] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*30)
|
|
|
|
dht.routes[1] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Millisecond*100)
|
|
|
|
dht.routes[2] = kb.NewRoutingTable(20, kb.ConvertPeerID(p.ID), time.Hour)
|
|
|
|
|
2014-08-06 18:37:45 -07:00
|
|
|
dht.birth = time.Now()
|
2014-08-10 21:40:17 -07:00
|
|
|
return dht
|
2014-07-31 17:43:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
// Start up background goroutines needed by the DHT
|
2014-07-31 21:55:44 -07:00
|
|
|
func (dht *IpfsDHT) Start() {
|
|
|
|
go dht.handleMessages()
|
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// Connect to a new peer at the given address, ping and add to the routing table
|
2014-08-05 09:38:26 -07:00
|
|
|
func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) (*peer.Peer, error) {
|
2014-08-08 18:09:21 -07:00
|
|
|
maddrstr, _ := addr.String()
|
2014-08-07 21:52:11 -07:00
|
|
|
u.DOut("Connect to new peer: %s", maddrstr)
|
2014-08-12 15:37:26 -07:00
|
|
|
npeer, err := dht.network.ConnectNew(addr)
|
2014-07-31 17:43:48 -07:00
|
|
|
if err != nil {
|
2014-08-05 09:38:26 -07:00
|
|
|
return nil, err
|
2014-07-31 17:43:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 10:02:53 -07:00
|
|
|
// Ping new peer to register in their routing table
|
|
|
|
// NOTE: this should be done better...
|
2014-08-08 18:09:21 -07:00
|
|
|
err = dht.Ping(npeer, time.Second*2)
|
2014-08-06 10:02:53 -07:00
|
|
|
if err != nil {
|
2014-08-09 22:28:46 -07:00
|
|
|
return nil, errors.New("failed to ping newly connected peer")
|
2014-08-06 10:02:53 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.Update(npeer)
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
return npeer, nil
|
2014-07-30 20:16:34 -07:00
|
|
|
}
|
|
|
|
|
2014-07-29 14:50:33 -07:00
|
|
|
// Read in all messages from swarm and handle them appropriately
|
|
|
|
// NOTE: this function is just a quick sketch
|
2014-07-28 22:14:27 -07:00
|
|
|
func (dht *IpfsDHT) handleMessages() {
|
2014-08-03 17:35:12 -07:00
|
|
|
u.DOut("Begin message handling routine")
|
2014-08-06 18:37:45 -07:00
|
|
|
|
|
|
|
checkTimeouts := time.NewTicker(time.Minute * 5)
|
2014-08-10 21:02:05 -07:00
|
|
|
ch := dht.network.GetChan()
|
2014-07-29 19:33:51 -07:00
|
|
|
for {
|
|
|
|
select {
|
2014-08-10 21:02:05 -07:00
|
|
|
case mes, ok := <-ch.Incoming:
|
2014-08-05 18:32:22 -07:00
|
|
|
if !ok {
|
|
|
|
u.DOut("handleMessages closing, bad recv on incoming")
|
|
|
|
return
|
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := new(PBDHTMessage)
|
2014-07-29 19:33:51 -07:00
|
|
|
err := proto.Unmarshal(mes.Data, pmes)
|
|
|
|
if err != nil {
|
|
|
|
u.PErr("Failed to decode protobuf message: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
dht.Update(mes.Peer)
|
2014-08-03 17:35:12 -07:00
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
// Note: not sure if this is the correct place for this
|
2014-07-30 17:46:56 -07:00
|
|
|
if pmes.GetResponse() {
|
|
|
|
dht.listenLock.RLock()
|
2014-08-07 14:16:24 -07:00
|
|
|
list, ok := dht.listeners[pmes.GetId()]
|
2014-08-07 18:06:50 -07:00
|
|
|
dht.listenLock.RUnlock()
|
|
|
|
if time.Now().After(list.eol) {
|
|
|
|
dht.Unlisten(pmes.GetId())
|
|
|
|
ok = false
|
|
|
|
}
|
2014-08-07 14:16:24 -07:00
|
|
|
if list.count > 1 {
|
|
|
|
list.count--
|
|
|
|
}
|
2014-07-30 17:46:56 -07:00
|
|
|
if ok {
|
2014-08-07 14:16:24 -07:00
|
|
|
list.resp <- mes
|
2014-08-07 18:06:50 -07:00
|
|
|
if list.count == 1 {
|
|
|
|
dht.Unlisten(pmes.GetId())
|
|
|
|
}
|
2014-08-05 18:32:22 -07:00
|
|
|
} else {
|
|
|
|
u.DOut("Received response with nobody listening...")
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
2014-07-29 14:50:33 -07:00
|
|
|
}
|
2014-07-29 19:33:51 -07:00
|
|
|
//
|
|
|
|
|
2014-08-12 22:10:44 -07:00
|
|
|
u.DOut("[peer: %s]\nGot message type: '%s' [id = %x, from = %s]",
|
|
|
|
dht.self.ID.Pretty(),
|
2014-08-08 18:09:21 -07:00
|
|
|
PBDHTMessage_MessageType_name[int32(pmes.GetType())],
|
2014-08-06 18:37:45 -07:00
|
|
|
pmes.GetId(), mes.Peer.ID.Pretty())
|
2014-07-29 19:33:51 -07:00
|
|
|
switch pmes.GetType() {
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_GET_VALUE:
|
2014-07-30 17:46:56 -07:00
|
|
|
dht.handleGetValue(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_PUT_VALUE:
|
2014-07-30 20:16:34 -07:00
|
|
|
dht.handlePutValue(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_FIND_NODE:
|
2014-08-05 20:31:48 -07:00
|
|
|
dht.handleFindPeer(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_ADD_PROVIDER:
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.handleAddProvider(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_GET_PROVIDERS:
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.handleGetProviders(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_PING:
|
2014-08-01 13:21:51 -07:00
|
|
|
dht.handlePing(mes.Peer, pmes)
|
2014-08-08 18:09:21 -07:00
|
|
|
case PBDHTMessage_DIAGNOSTIC:
|
2014-08-07 14:16:24 -07:00
|
|
|
dht.handleDiagnostic(mes.Peer, pmes)
|
2014-07-29 19:33:51 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
case err := <-ch.Errors:
|
|
|
|
u.PErr("dht err: %s", err)
|
2014-07-29 19:33:51 -07:00
|
|
|
case <-dht.shutdown:
|
2014-08-06 18:37:45 -07:00
|
|
|
checkTimeouts.Stop()
|
2014-07-29 19:33:51 -07:00
|
|
|
return
|
2014-08-06 18:37:45 -07:00
|
|
|
case <-checkTimeouts.C:
|
2014-08-08 18:09:21 -07:00
|
|
|
// Time to collect some garbage!
|
|
|
|
dht.cleanExpiredProviders()
|
|
|
|
dht.cleanExpiredListeners()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) cleanExpiredProviders() {
|
|
|
|
dht.providerLock.Lock()
|
|
|
|
for k, parr := range dht.providers {
|
|
|
|
var cleaned []*providerInfo
|
|
|
|
for _, v := range parr {
|
|
|
|
if time.Since(v.Creation) < time.Hour {
|
|
|
|
cleaned = append(cleaned, v)
|
2014-08-07 18:06:50 -07:00
|
|
|
}
|
2014-07-29 14:50:33 -07:00
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
dht.providers[k] = cleaned
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
dht.providerLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) cleanExpiredListeners() {
|
|
|
|
dht.listenLock.Lock()
|
|
|
|
var remove []uint64
|
|
|
|
now := time.Now()
|
|
|
|
for k, v := range dht.listeners {
|
|
|
|
if now.After(v.eol) {
|
|
|
|
remove = append(remove, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, k := range remove {
|
|
|
|
delete(dht.listeners, k)
|
|
|
|
}
|
|
|
|
dht.listenLock.Unlock()
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
2014-07-29 14:50:33 -07:00
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
func (dht *IpfsDHT) putValueToNetwork(p *peer.Peer, key string, value []byte) error {
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_PUT_VALUE,
|
|
|
|
Key: key,
|
2014-08-07 18:06:50 -07:00
|
|
|
Value: value,
|
2014-08-08 18:09:21 -07:00
|
|
|
Id: GenerateMessageID(),
|
2014-08-07 18:06:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-08-07 18:06:50 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-08-12 22:10:44 -07:00
|
|
|
u.DOut("handleGetValue for key: %s", pmes.GetKey())
|
2014-07-30 20:16:34 -07:00
|
|
|
dskey := ds.NewKey(pmes.GetKey())
|
2014-08-09 22:28:46 -07:00
|
|
|
resp := &DHTMessage{
|
|
|
|
Response: true,
|
|
|
|
Id: pmes.GetId(),
|
|
|
|
Key: pmes.GetKey(),
|
|
|
|
}
|
|
|
|
iVal, err := dht.datastore.Get(dskey)
|
2014-07-30 20:16:34 -07:00
|
|
|
if err == nil {
|
2014-08-09 22:28:46 -07:00
|
|
|
resp.Success = true
|
|
|
|
resp.Value = iVal.([]byte)
|
2014-07-30 20:16:34 -07:00
|
|
|
} else if err == ds.ErrNotFound {
|
2014-08-09 22:28:46 -07:00
|
|
|
// Check if we know any providers for the requested value
|
|
|
|
provs, ok := dht.providers[u.Key(pmes.GetKey())]
|
|
|
|
if ok && len(provs) > 0 {
|
|
|
|
for _, prov := range provs {
|
|
|
|
resp.Peers = append(resp.Peers, prov.Value)
|
|
|
|
}
|
|
|
|
resp.Success = true
|
|
|
|
} else {
|
|
|
|
// No providers?
|
2014-08-10 21:02:05 -07:00
|
|
|
// Find closest peer on given cluster to desired key and reply with that info
|
|
|
|
|
2014-08-11 20:11:23 -07:00
|
|
|
level := 0
|
|
|
|
if len(pmes.GetValue()) < 1 {
|
|
|
|
// TODO: maybe return an error? Defaulting isnt a good idea IMO
|
|
|
|
u.PErr("handleGetValue: no routing level specified, assuming 0")
|
|
|
|
} else {
|
|
|
|
level = int(pmes.GetValue()[0]) // Using value field to specify cluster level
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
|
|
|
|
closer := dht.routes[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
|
|
|
|
|
|
|
|
// If this peer is closer than the one from the table, return nil
|
|
|
|
if kb.Closer(dht.self.ID, closer.ID, u.Key(pmes.GetKey())) {
|
|
|
|
resp.Peers = nil
|
|
|
|
} else {
|
|
|
|
resp.Peers = []*peer.Peer{closer}
|
|
|
|
}
|
2014-08-06 18:37:45 -07:00
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
} else {
|
2014-08-10 21:02:05 -07:00
|
|
|
//temp: what other errors can a datastore return?
|
2014-08-09 22:28:46 -07:00
|
|
|
panic(err)
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
2014-08-06 18:37:45 -07:00
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, resp.ToProtobuf())
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:31:48 -07:00
|
|
|
// Store a value in this peer local storage
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-07-30 20:16:34 -07:00
|
|
|
dskey := ds.NewKey(pmes.GetKey())
|
|
|
|
err := dht.datastore.Put(dskey, pmes.GetValue())
|
|
|
|
if err != nil {
|
|
|
|
// For now, just panic, handle this better later maybe
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *PBDHTMessage) {
|
|
|
|
resp := DHTMessage{
|
|
|
|
Type: pmes.GetType(),
|
2014-08-01 13:21:51 -07:00
|
|
|
Response: true,
|
2014-08-08 18:09:21 -07:00
|
|
|
Id: pmes.GetId(),
|
2014-08-01 13:21:51 -07:00
|
|
|
}
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(swarm.NewMessage(p, resp.ToProtobuf()))
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handleFindPeer(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-08-10 21:02:05 -07:00
|
|
|
resp := DHTMessage{
|
|
|
|
Type: pmes.GetType(),
|
|
|
|
Id: pmes.GetId(),
|
|
|
|
Response: true,
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
mes := swarm.NewMessage(p, resp.ToProtobuf())
|
|
|
|
dht.network.Send(mes)
|
|
|
|
}()
|
|
|
|
level := pmes.GetValue()[0]
|
|
|
|
u.DOut("handleFindPeer: searching for '%s'", peer.ID(pmes.GetKey()).Pretty())
|
|
|
|
closest := dht.routes[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
|
2014-08-05 20:31:48 -07:00
|
|
|
if closest == nil {
|
2014-08-08 18:09:21 -07:00
|
|
|
u.PErr("handleFindPeer: could not find anything.")
|
2014-08-10 21:02:05 -07:00
|
|
|
return
|
2014-08-05 20:31:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(closest.Addresses) == 0 {
|
2014-08-08 18:09:21 -07:00
|
|
|
u.PErr("handleFindPeer: no addresses for connected peer...")
|
2014-08-10 21:02:05 -07:00
|
|
|
return
|
2014-08-05 20:31:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// If the found peer further away than this peer...
|
|
|
|
if kb.Closer(dht.self.ID, closest.ID, u.Key(pmes.GetKey())) {
|
|
|
|
return
|
2014-08-05 20:31:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
u.DOut("handleFindPeer: sending back '%s'", closest.ID.Pretty())
|
|
|
|
resp.Peers = []*peer.Peer{closest}
|
|
|
|
resp.Success = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-08-08 18:09:21 -07:00
|
|
|
resp := DHTMessage{
|
2014-08-10 21:02:05 -07:00
|
|
|
Type: PBDHTMessage_GET_PROVIDERS,
|
|
|
|
Key: pmes.GetKey(),
|
2014-08-08 18:09:21 -07:00
|
|
|
Id: pmes.GetId(),
|
2014-08-10 21:02:05 -07:00
|
|
|
Response: true,
|
2014-08-05 20:31:48 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.providerLock.RLock()
|
2014-08-03 21:46:01 -07:00
|
|
|
providers := dht.providers[u.Key(pmes.GetKey())]
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.providerLock.RUnlock()
|
2014-08-03 21:46:01 -07:00
|
|
|
if providers == nil || len(providers) == 0 {
|
2014-08-10 21:02:05 -07:00
|
|
|
// TODO: work on tiering this
|
|
|
|
closer := dht.routes[0].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
|
|
|
|
resp.Peers = []*peer.Peer{closer}
|
|
|
|
} else {
|
|
|
|
for _, prov := range providers {
|
|
|
|
resp.Peers = append(resp.Peers, prov.Value)
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
resp.Success = true
|
2014-08-03 21:46:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, resp.ToProtobuf())
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-07-31 21:55:44 -07:00
|
|
|
}
|
|
|
|
|
2014-08-06 18:37:45 -07:00
|
|
|
type providerInfo struct {
|
|
|
|
Creation time.Time
|
2014-08-08 18:09:21 -07:00
|
|
|
Value *peer.Peer
|
2014-08-06 18:37:45 -07:00
|
|
|
}
|
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-08-03 21:46:01 -07:00
|
|
|
//TODO: need to implement TTLs on providers
|
|
|
|
key := u.Key(pmes.GetKey())
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.addProviderEntry(key, p)
|
2014-07-31 21:55:44 -07:00
|
|
|
}
|
|
|
|
|
2014-07-29 14:50:33 -07:00
|
|
|
// Register a handler for a specific message ID, used for getting replies
|
|
|
|
// to certain messages (i.e. response to a GET_VALUE message)
|
2014-08-07 18:06:50 -07:00
|
|
|
func (dht *IpfsDHT) ListenFor(mesid uint64, count int, timeout time.Duration) <-chan *swarm.Message {
|
2014-07-29 19:33:51 -07:00
|
|
|
lchan := make(chan *swarm.Message)
|
2014-07-29 14:50:33 -07:00
|
|
|
dht.listenLock.Lock()
|
2014-08-07 18:06:50 -07:00
|
|
|
dht.listeners[mesid] = &listenInfo{lchan, count, time.Now().Add(timeout)}
|
2014-07-29 14:50:33 -07:00
|
|
|
dht.listenLock.Unlock()
|
|
|
|
return lchan
|
|
|
|
}
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
// Unregister the given message id from the listener map
|
2014-07-30 20:16:34 -07:00
|
|
|
func (dht *IpfsDHT) Unlisten(mesid uint64) {
|
|
|
|
dht.listenLock.Lock()
|
2014-08-07 14:16:24 -07:00
|
|
|
list, ok := dht.listeners[mesid]
|
2014-07-30 20:16:34 -07:00
|
|
|
if ok {
|
|
|
|
delete(dht.listeners, mesid)
|
|
|
|
}
|
|
|
|
dht.listenLock.Unlock()
|
2014-08-07 14:16:24 -07:00
|
|
|
close(list.resp)
|
|
|
|
}
|
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
// Check whether or not the dht is currently listening for mesid
|
2014-08-07 14:16:24 -07:00
|
|
|
func (dht *IpfsDHT) IsListening(mesid uint64) bool {
|
|
|
|
dht.listenLock.RLock()
|
2014-08-08 18:09:21 -07:00
|
|
|
li, ok := dht.listeners[mesid]
|
2014-08-07 14:16:24 -07:00
|
|
|
dht.listenLock.RUnlock()
|
2014-08-07 18:06:50 -07:00
|
|
|
if time.Now().After(li.eol) {
|
|
|
|
dht.listenLock.Lock()
|
|
|
|
delete(dht.listeners, mesid)
|
|
|
|
dht.listenLock.Unlock()
|
|
|
|
return false
|
|
|
|
}
|
2014-08-07 14:16:24 -07:00
|
|
|
return ok
|
2014-07-30 20:16:34 -07:00
|
|
|
}
|
|
|
|
|
2014-08-05 20:31:48 -07:00
|
|
|
// Stop all communications from this peer and shut down
|
2014-07-30 17:46:56 -07:00
|
|
|
func (dht *IpfsDHT) Halt() {
|
|
|
|
dht.shutdown <- struct{}{}
|
|
|
|
dht.network.Close()
|
|
|
|
}
|
2014-07-31 17:43:48 -07:00
|
|
|
|
2014-08-05 18:32:22 -07:00
|
|
|
func (dht *IpfsDHT) addProviderEntry(key u.Key, p *peer.Peer) {
|
|
|
|
u.DOut("Adding %s as provider for '%s'", p.Key().Pretty(), key)
|
|
|
|
dht.providerLock.Lock()
|
|
|
|
provs := dht.providers[key]
|
2014-08-06 18:37:45 -07:00
|
|
|
dht.providers[key] = append(provs, &providerInfo{time.Now(), p})
|
2014-08-05 18:32:22 -07:00
|
|
|
dht.providerLock.Unlock()
|
|
|
|
}
|
2014-08-07 14:16:24 -07:00
|
|
|
|
2014-08-09 22:28:46 -07:00
|
|
|
// NOTE: not yet finished, low priority
|
2014-08-08 18:09:21 -07:00
|
|
|
func (dht *IpfsDHT) handleDiagnostic(p *peer.Peer, pmes *PBDHTMessage) {
|
2014-08-07 14:16:24 -07:00
|
|
|
dht.diaglock.Lock()
|
|
|
|
if dht.IsListening(pmes.GetId()) {
|
|
|
|
//TODO: ehhh..........
|
|
|
|
dht.diaglock.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dht.diaglock.Unlock()
|
|
|
|
|
2014-08-08 19:58:42 -07:00
|
|
|
seq := dht.routes[0].NearestPeers(kb.ConvertPeerID(dht.self.ID), 10)
|
2014-08-09 22:28:46 -07:00
|
|
|
listenChan := dht.ListenFor(pmes.GetId(), len(seq), time.Second*30)
|
2014-08-07 14:16:24 -07:00
|
|
|
|
2014-08-08 18:09:21 -07:00
|
|
|
for _, ps := range seq {
|
2014-08-07 14:16:24 -07:00
|
|
|
mes := swarm.NewMessage(ps, pmes)
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-08-07 14:16:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2014-08-07 18:06:50 -07:00
|
|
|
di := dht.getDiagInfo()
|
|
|
|
buf.Write(di.Marshal())
|
|
|
|
|
2014-08-07 14:16:24 -07:00
|
|
|
// NOTE: this shouldnt be a hardcoded value
|
|
|
|
after := time.After(time.Second * 20)
|
|
|
|
count := len(seq)
|
|
|
|
for count > 0 {
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
//Timeout, return what we have
|
|
|
|
goto out
|
2014-08-09 22:28:46 -07:00
|
|
|
case req_resp := <-listenChan:
|
2014-08-08 18:09:21 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
2014-08-07 18:06:50 -07:00
|
|
|
err := proto.Unmarshal(req_resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
// It broke? eh, whatever, keep going
|
|
|
|
continue
|
|
|
|
}
|
2014-08-07 14:16:24 -07:00
|
|
|
buf.Write(req_resp.Data)
|
|
|
|
count--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2014-08-08 18:09:21 -07:00
|
|
|
resp := DHTMessage{
|
|
|
|
Type: PBDHTMessage_DIAGNOSTIC,
|
|
|
|
Id: pmes.GetId(),
|
|
|
|
Value: buf.Bytes(),
|
2014-08-07 14:16:24 -07:00
|
|
|
Response: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, resp.ToProtobuf())
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-08-07 14:16:24 -07:00
|
|
|
}
|
2014-08-07 21:52:11 -07:00
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// getValueSingle simply performs the get value RPC with the given parameters
|
|
|
|
func (dht *IpfsDHT) getValueSingle(p *peer.Peer, key u.Key, timeout time.Duration, level int) (*PBDHTMessage, error) {
|
2014-08-09 22:28:46 -07:00
|
|
|
pmes := DHTMessage{
|
2014-08-10 21:02:05 -07:00
|
|
|
Type: PBDHTMessage_GET_VALUE,
|
|
|
|
Key: string(key),
|
|
|
|
Value: []byte{byte(level)},
|
|
|
|
Id: GenerateMessageID(),
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
response_chan := dht.ListenFor(pmes.Id, 1, time.Minute)
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
2014-08-11 20:11:23 -07:00
|
|
|
t := time.Now()
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
2014-08-09 22:28:46 -07:00
|
|
|
|
|
|
|
// Wait for either the response or a timeout
|
|
|
|
timeup := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-timeup:
|
|
|
|
dht.Unlisten(pmes.Id)
|
|
|
|
return nil, u.ErrTimeout
|
|
|
|
case resp, ok := <-response_chan:
|
|
|
|
if !ok {
|
|
|
|
u.PErr("response channel closed before timeout, please investigate.")
|
|
|
|
return nil, u.ErrTimeout
|
|
|
|
}
|
2014-08-11 20:11:23 -07:00
|
|
|
roundtrip := time.Since(t)
|
|
|
|
resp.Peer.SetLatency(roundtrip)
|
2014-08-09 22:28:46 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
return pmes_out, nil
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// TODO: Im not certain on this implementation, we get a list of peers/providers
|
|
|
|
// from someone what do we do with it? Connect to each of them? randomly pick
|
|
|
|
// one to get the value from? Or just connect to one at a time until we get a
|
|
|
|
// successful connection and request the value from it?
|
|
|
|
func (dht *IpfsDHT) getFromPeerList(key u.Key, timeout time.Duration,
|
|
|
|
peerlist []*PBDHTMessage_PBPeer, level int) ([]byte, error) {
|
|
|
|
for _, pinfo := range peerlist {
|
|
|
|
p, _ := dht.Find(peer.ID(pinfo.GetId()))
|
|
|
|
if p == nil {
|
|
|
|
maddr, err := ma.NewMultiaddr(pinfo.GetAddr())
|
2014-08-09 22:28:46 -07:00
|
|
|
if err != nil {
|
|
|
|
u.PErr("getValue error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-08-11 20:11:23 -07:00
|
|
|
|
2014-08-12 15:37:26 -07:00
|
|
|
p, err = dht.network.GetConnection(peer.ID(pinfo.GetId()), maddr)
|
2014-08-09 22:28:46 -07:00
|
|
|
if err != nil {
|
|
|
|
u.PErr("getValue error: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
pmes, err := dht.getValueSingle(p, key, timeout, level)
|
2014-08-09 22:28:46 -07:00
|
|
|
if err != nil {
|
2014-08-10 21:02:05 -07:00
|
|
|
u.DErr("getFromPeers error: %s", err)
|
2014-08-09 22:28:46 -07:00
|
|
|
continue
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.addProviderEntry(key, p)
|
2014-08-09 22:28:46 -07:00
|
|
|
|
2014-08-10 21:02:05 -07:00
|
|
|
// Make sure it was a successful get
|
|
|
|
if pmes.GetSuccess() && pmes.Value != nil {
|
|
|
|
return pmes.GetValue(), nil
|
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
}
|
|
|
|
return nil, u.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2014-08-07 21:52:11 -07:00
|
|
|
func (dht *IpfsDHT) GetLocal(key u.Key) ([]byte, error) {
|
2014-08-08 18:09:21 -07:00
|
|
|
v, err := dht.datastore.Get(ds.NewKey(string(key)))
|
2014-08-07 21:52:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v.([]byte), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) PutLocal(key u.Key, value []byte) error {
|
|
|
|
return dht.datastore.Put(ds.NewKey(string(key)), value)
|
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
|
|
|
|
func (dht *IpfsDHT) Update(p *peer.Peer) {
|
2014-08-11 20:11:23 -07:00
|
|
|
for _, route := range dht.routes {
|
|
|
|
removed := route.Update(p)
|
|
|
|
// Only drop the connection if no tables refer to this peer
|
|
|
|
if removed != nil {
|
|
|
|
found := false
|
|
|
|
for _, r := range dht.routes {
|
|
|
|
if r.Find(removed.ID) != nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
dht.network.Drop(removed)
|
|
|
|
}
|
|
|
|
}
|
2014-08-08 18:09:21 -07:00
|
|
|
}
|
|
|
|
}
|
2014-08-09 22:28:46 -07:00
|
|
|
|
|
|
|
// Look for a peer with a given ID connected to this dht
|
|
|
|
func (dht *IpfsDHT) Find(id peer.ID) (*peer.Peer, *kb.RoutingTable) {
|
|
|
|
for _, table := range dht.routes {
|
|
|
|
p := table.Find(id)
|
|
|
|
if p != nil {
|
|
|
|
return p, table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
2014-08-10 21:02:05 -07:00
|
|
|
|
|
|
|
func (dht *IpfsDHT) findPeerSingle(p *peer.Peer, id peer.ID, timeout time.Duration, level int) (*PBDHTMessage, error) {
|
|
|
|
pmes := DHTMessage{
|
|
|
|
Type: PBDHTMessage_FIND_NODE,
|
|
|
|
Key: string(id),
|
|
|
|
Id: GenerateMessageID(),
|
|
|
|
Value: []byte{byte(level)},
|
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
|
|
|
listenChan := dht.ListenFor(pmes.Id, 1, time.Minute)
|
2014-08-11 20:11:23 -07:00
|
|
|
t := time.Now()
|
2014-08-10 21:02:05 -07:00
|
|
|
dht.network.Send(mes)
|
|
|
|
after := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-after:
|
|
|
|
dht.Unlisten(pmes.Id)
|
|
|
|
return nil, u.ErrTimeout
|
|
|
|
case resp := <-listenChan:
|
2014-08-11 20:11:23 -07:00
|
|
|
roundtrip := time.Since(t)
|
|
|
|
resp.Peer.SetLatency(roundtrip)
|
2014-08-10 21:02:05 -07:00
|
|
|
pmes_out := new(PBDHTMessage)
|
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pmes_out, nil
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 20:11:23 -07:00
|
|
|
|
|
|
|
func (dht *IpfsDHT) PrintTables() {
|
|
|
|
for _, route := range dht.routes {
|
|
|
|
route.Print()
|
|
|
|
}
|
|
|
|
}
|