2014-07-23 04:48:30 -07:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
2014-07-30 17:46:56 -07:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
proto "code.google.com/p/goprotobuf/proto"
|
|
|
|
|
2014-07-29 17:55:19 -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-23 04:48:30 -07:00
|
|
|
)
|
|
|
|
|
2014-07-29 19:33:51 -07:00
|
|
|
// TODO: determine a way of creating and managing message IDs
|
|
|
|
func GenerateMessageID() uint64 {
|
2014-07-30 17:46:56 -07:00
|
|
|
return uint64(rand.Uint32()) << 32 & uint64(rand.Uint32())
|
2014-07-29 19:33:51 -07:00
|
|
|
}
|
|
|
|
|
2014-07-23 04:48:30 -07:00
|
|
|
// This file implements the Routing interface for the IpfsDHT struct.
|
|
|
|
|
|
|
|
// Basic Put/Get
|
|
|
|
|
|
|
|
// PutValue adds value corresponding to given Key.
|
2014-07-29 17:55:19 -07:00
|
|
|
func (s *IpfsDHT) PutValue(key u.Key, value []byte) error {
|
2014-07-28 22:14:27 -07:00
|
|
|
var p *peer.Peer
|
2014-08-03 13:37:09 -07:00
|
|
|
p = s.routes.NearestPeer(convertKey(key))
|
2014-08-03 17:35:12 -07:00
|
|
|
if p == nil {
|
|
|
|
u.POut("nbuckets: %d", len(s.routes.Buckets))
|
|
|
|
u.POut("%d", s.routes.Buckets[0].Len())
|
|
|
|
panic("Table returned nil peer!")
|
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
pmes := pDHTMessage{
|
|
|
|
Type: DHTMessage_PUT_VALUE,
|
|
|
|
Key: string(key),
|
|
|
|
Value: value,
|
|
|
|
Id: GenerateMessageID(),
|
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
2014-07-28 22:14:27 -07:00
|
|
|
s.network.Chan.Outgoing <- mes
|
|
|
|
return nil
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetValue searches for the value corresponding to given Key.
|
|
|
|
func (s *IpfsDHT) GetValue(key u.Key, timeout time.Duration) ([]byte, error) {
|
2014-07-28 22:14:27 -07:00
|
|
|
var p *peer.Peer
|
2014-08-03 13:37:09 -07:00
|
|
|
p = s.routes.NearestPeer(convertKey(key))
|
2014-08-03 17:35:12 -07:00
|
|
|
if p == nil {
|
|
|
|
panic("Table returned nil peer!")
|
|
|
|
}
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
pmes := pDHTMessage{
|
|
|
|
Type: DHTMessage_GET_VALUE,
|
|
|
|
Key: string(key),
|
|
|
|
Id: GenerateMessageID(),
|
|
|
|
}
|
|
|
|
response_chan := s.ListenFor(pmes.Id)
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-08-03 17:35:12 -07:00
|
|
|
mes := swarm.NewMessage(p, pmes.ToProtobuf())
|
|
|
|
s.network.Chan.Outgoing <- mes
|
2014-07-28 22:14:27 -07:00
|
|
|
|
2014-07-29 14:50:33 -07:00
|
|
|
// Wait for either the response or a timeout
|
2014-07-28 22:14:27 -07:00
|
|
|
timeup := time.After(timeout)
|
|
|
|
select {
|
2014-07-29 17:55:19 -07:00
|
|
|
case <-timeup:
|
|
|
|
// TODO: unregister listener
|
2014-07-29 19:33:51 -07:00
|
|
|
return nil, u.ErrTimeout
|
2014-07-29 17:55:19 -07:00
|
|
|
case resp := <-response_chan:
|
2014-08-03 17:35:12 -07:00
|
|
|
pmes_out := new(DHTMessage)
|
|
|
|
err := proto.Unmarshal(resp.Data, pmes_out)
|
|
|
|
if err != nil {
|
|
|
|
return nil,err
|
|
|
|
}
|
|
|
|
return pmes_out.GetValue(), nil
|
2014-07-28 22:14:27 -07:00
|
|
|
}
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2014-07-29 17:55:19 -07:00
|
|
|
func (s *IpfsDHT) Provide(key u.Key) error {
|
|
|
|
return u.ErrNotImplemented
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2014-07-29 17:55:19 -07:00
|
|
|
return nil, u.ErrNotImplemented
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find specific Peer
|
|
|
|
|
|
|
|
// FindPeer searches for a peer with given ID.
|
|
|
|
func (s *IpfsDHT) FindPeer(id peer.ID, timeout time.Duration) (*peer.Peer, error) {
|
2014-07-29 17:55:19 -07:00
|
|
|
return nil, u.ErrNotImplemented
|
2014-07-23 04:48:30 -07:00
|
|
|
}
|