2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
2014-07-28 22:14:27 -07:00
|
|
|
import (
|
2014-07-30 17:46:56 -07:00
|
|
|
"sync"
|
2014-07-31 17:43:48 -07:00
|
|
|
"time"
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-07-30 20:16:34 -07:00
|
|
|
peer "github.com/jbenet/go-ipfs/peer"
|
|
|
|
swarm "github.com/jbenet/go-ipfs/swarm"
|
|
|
|
u "github.com/jbenet/go-ipfs/util"
|
2014-07-31 17:43:48 -07:00
|
|
|
identify "github.com/jbenet/go-ipfs/identify"
|
|
|
|
|
|
|
|
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-03 17:35:12 -07:00
|
|
|
routes *RoutingTable
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-07-29 14:50:33 -07:00
|
|
|
network *swarm.Swarm
|
|
|
|
|
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-07-29 19:33:51 -07:00
|
|
|
// map of channels waiting for reply messages
|
|
|
|
listeners map[uint64]chan *swarm.Message
|
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-07-28 22:14:27 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 17:43:48 -07:00
|
|
|
// Create a new DHT object with the given peer as the 'local' host
|
|
|
|
func NewDHT(p *peer.Peer) (*IpfsDHT, error) {
|
2014-08-01 13:21:51 -07:00
|
|
|
network := swarm.NewSwarm(p)
|
|
|
|
err := network.Listen()
|
|
|
|
if err != nil {
|
|
|
|
return nil,err
|
|
|
|
}
|
2014-07-31 17:43:48 -07:00
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
dht := new(IpfsDHT)
|
|
|
|
dht.network = network
|
2014-07-31 17:43:48 -07:00
|
|
|
dht.datastore = ds.NewMapDatastore()
|
|
|
|
dht.self = p
|
2014-07-30 20:16:34 -07:00
|
|
|
dht.listeners = make(map[uint64]chan *swarm.Message)
|
|
|
|
dht.shutdown = make(chan struct{})
|
2014-08-03 17:35:12 -07:00
|
|
|
dht.routes = NewRoutingTable(20, convertPeerID(p.ID))
|
2014-07-31 17:43:48 -07:00
|
|
|
return dht, nil
|
|
|
|
}
|
|
|
|
|
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-07-31 17:43:48 -07:00
|
|
|
// Connect to a new peer at the given address
|
|
|
|
func (dht *IpfsDHT) Connect(addr *ma.Multiaddr) error {
|
|
|
|
peer := new(peer.Peer)
|
|
|
|
peer.AddAddress(addr)
|
|
|
|
|
|
|
|
conn,err := swarm.Dial("tcp", peer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 21:55:44 -07:00
|
|
|
err = identify.Handshake(dht.self, peer, conn.Incoming.MsgChan, conn.Outgoing.MsgChan)
|
2014-07-31 17:43:48 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-07-31 21:55:44 -07:00
|
|
|
dht.network.StartConn(conn)
|
2014-07-31 17:43:48 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
dht.routes.Update(peer)
|
2014-07-31 17:43:48 -07:00
|
|
|
return 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-07-29 19:33:51 -07:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case mes := <-dht.network.Chan.Incoming:
|
2014-07-31 21:55:44 -07:00
|
|
|
u.DOut("recieved message from swarm.")
|
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
pmes := new(DHTMessage)
|
|
|
|
err := proto.Unmarshal(mes.Data, pmes)
|
|
|
|
if err != nil {
|
|
|
|
u.PErr("Failed to decode protobuf message: %s", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
// Update peers latest visit in routing table
|
|
|
|
dht.routes.Update(mes.Peer)
|
|
|
|
|
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()
|
|
|
|
ch, ok := dht.listeners[pmes.GetId()]
|
|
|
|
dht.listenLock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
ch <- mes
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is expected behaviour during a timeout
|
|
|
|
u.DOut("Received response with nobody listening...")
|
|
|
|
continue
|
2014-07-29 14:50:33 -07:00
|
|
|
}
|
2014-07-29 19:33:51 -07:00
|
|
|
//
|
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
u.DOut("Got message type: %d", pmes.GetType())
|
2014-07-29 19:33:51 -07:00
|
|
|
switch pmes.GetType() {
|
2014-07-30 17:46:56 -07:00
|
|
|
case DHTMessage_GET_VALUE:
|
|
|
|
dht.handleGetValue(mes.Peer, pmes)
|
|
|
|
case DHTMessage_PUT_VALUE:
|
2014-07-30 20:16:34 -07:00
|
|
|
dht.handlePutValue(mes.Peer, pmes)
|
2014-07-29 19:33:51 -07:00
|
|
|
case DHTMessage_FIND_NODE:
|
2014-07-30 20:16:34 -07:00
|
|
|
dht.handleFindNode(mes.Peer, pmes)
|
2014-07-30 17:46:56 -07:00
|
|
|
case DHTMessage_ADD_PROVIDER:
|
2014-07-29 19:33:51 -07:00
|
|
|
case DHTMessage_GET_PROVIDERS:
|
|
|
|
case DHTMessage_PING:
|
2014-08-01 13:21:51 -07:00
|
|
|
dht.handlePing(mes.Peer, pmes)
|
2014-07-29 19:33:51 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 21:55:44 -07:00
|
|
|
case err := <-dht.network.Chan.Errors:
|
|
|
|
panic(err)
|
2014-07-29 19:33:51 -07:00
|
|
|
case <-dht.shutdown:
|
|
|
|
return
|
2014-07-29 14:50:33 -07:00
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
2014-07-29 14:50:33 -07:00
|
|
|
|
2014-07-30 17:46:56 -07:00
|
|
|
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) {
|
2014-07-30 20:16:34 -07:00
|
|
|
dskey := ds.NewKey(pmes.GetKey())
|
|
|
|
i_val, err := dht.datastore.Get(dskey)
|
|
|
|
if err == nil {
|
2014-08-01 13:21:51 -07:00
|
|
|
resp := &pDHTMessage{
|
|
|
|
Response: true,
|
|
|
|
Id: *pmes.Id,
|
|
|
|
Key: *pmes.Key,
|
|
|
|
Value: i_val.([]byte),
|
|
|
|
}
|
|
|
|
|
|
|
|
mes := swarm.NewMessage(p, resp.ToProtobuf())
|
|
|
|
dht.network.Chan.Outgoing <- mes
|
2014-07-30 20:16:34 -07:00
|
|
|
} else if err == ds.ErrNotFound {
|
2014-07-30 17:46:56 -07:00
|
|
|
// Find closest node(s) to desired key and reply with that info
|
|
|
|
// TODO: this will need some other metadata in the protobuf message
|
|
|
|
// to signal to the querying node that the data its receiving
|
|
|
|
// is actually a list of other nodes
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-30 20:16:34 -07:00
|
|
|
// Store a value in this nodes local storage
|
2014-07-30 17:46:56 -07:00
|
|
|
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *DHTMessage) {
|
2014-08-01 13:21:51 -07:00
|
|
|
resp := &pDHTMessage{
|
|
|
|
Type: pmes.GetType(),
|
|
|
|
Response: true,
|
|
|
|
Id: pmes.GetId(),
|
|
|
|
}
|
2014-07-30 17:46:56 -07:00
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
dht.network.Chan.Outgoing <-swarm.NewMessage(p, resp.ToProtobuf())
|
2014-07-30 17:46:56 -07:00
|
|
|
}
|
|
|
|
|
2014-07-31 21:55:44 -07:00
|
|
|
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
|
|
|
|
panic("Not implemented.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *DHTMessage) {
|
|
|
|
panic("Not implemented.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dht *IpfsDHT) handleAddProvider(p *peer.Peer, pmes *DHTMessage) {
|
|
|
|
panic("Not implemented.")
|
|
|
|
}
|
|
|
|
|
2014-07-30 17:46:56 -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-07-29 19:33:51 -07:00
|
|
|
func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message {
|
|
|
|
lchan := make(chan *swarm.Message)
|
2014-07-29 14:50:33 -07:00
|
|
|
dht.listenLock.Lock()
|
|
|
|
dht.listeners[mesid] = lchan
|
|
|
|
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()
|
|
|
|
ch, ok := dht.listeners[mesid]
|
|
|
|
if ok {
|
|
|
|
delete(dht.listeners, mesid)
|
|
|
|
}
|
|
|
|
dht.listenLock.Unlock()
|
|
|
|
close(ch)
|
|
|
|
}
|
|
|
|
|
2014-07-30 17:46:56 -07:00
|
|
|
// Stop all communications from this node and shut down
|
|
|
|
func (dht *IpfsDHT) Halt() {
|
|
|
|
dht.shutdown <- struct{}{}
|
|
|
|
dht.network.Close()
|
|
|
|
}
|
2014-07-31 17:43:48 -07:00
|
|
|
|
|
|
|
// Ping a node, log the time it took
|
2014-08-01 13:21:51 -07:00
|
|
|
func (dht *IpfsDHT) Ping(p *peer.Peer, timeout time.Duration) error {
|
2014-07-31 17:43:48 -07:00
|
|
|
// Thoughts: maybe this should accept an ID and do a peer lookup?
|
2014-07-31 21:55:44 -07:00
|
|
|
u.DOut("Enter Ping.")
|
|
|
|
|
2014-08-01 13:21:51 -07:00
|
|
|
pmes := pDHTMessage{Id: GenerateMessageID(), Type: DHTMessage_PING}
|
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
2014-07-31 17:43:48 -07:00
|
|
|
|
|
|
|
before := time.Now()
|
2014-08-01 13:21:51 -07:00
|
|
|
response_chan := dht.ListenFor(pmes.Id)
|
2014-07-31 17:43:48 -07:00
|
|
|
dht.network.Chan.Outgoing <- mes
|
|
|
|
|
|
|
|
tout := time.After(timeout)
|
|
|
|
select {
|
|
|
|
case <-response_chan:
|
|
|
|
roundtrip := time.Since(before)
|
2014-08-01 13:21:51 -07:00
|
|
|
u.POut("Ping took %s.", roundtrip.String())
|
|
|
|
return nil
|
2014-07-31 17:43:48 -07:00
|
|
|
case <-tout:
|
|
|
|
// Timed out, think about removing node from network
|
|
|
|
u.DOut("Ping node timed out.")
|
2014-08-01 13:21:51 -07:00
|
|
|
return u.ErrTimeout
|
2014-07-31 17:43:48 -07:00
|
|
|
}
|
|
|
|
}
|