Commit 2a1ee3ae authored by Jeromy's avatar Jeromy Committed by Juan Batiz-Benet

use datastore for local data

parent 87739b3a
...@@ -3,9 +3,12 @@ package dht ...@@ -3,9 +3,12 @@ package dht
import ( import (
"sync" "sync"
peer "github.com/jbenet/go-ipfs/peer" peer "github.com/jbenet/go-ipfs/peer"
swarm "github.com/jbenet/go-ipfs/swarm" swarm "github.com/jbenet/go-ipfs/swarm"
u "github.com/jbenet/go-ipfs/util" u "github.com/jbenet/go-ipfs/util"
ds "github.com/jbenet/datastore.go"
"code.google.com/p/goprotobuf/proto" "code.google.com/p/goprotobuf/proto"
) )
...@@ -18,8 +21,11 @@ type IpfsDHT struct { ...@@ -18,8 +21,11 @@ type IpfsDHT struct {
network *swarm.Swarm network *swarm.Swarm
// local data (TEMPORARY: until we formalize data storage with datastore) // Local peer (yourself)
data map[string][]byte self *peer.Peer
// Local data
datastore ds.Datastore
// map of channels waiting for reply messages // map of channels waiting for reply messages
listeners map[uint64]chan *swarm.Message listeners map[uint64]chan *swarm.Message
...@@ -29,6 +35,15 @@ type IpfsDHT struct { ...@@ -29,6 +35,15 @@ type IpfsDHT struct {
shutdown chan struct{} shutdown chan struct{}
} }
func NewDHT(p *peer.Peer) *IpfsDHT {
dht := new(IpfsDHT)
dht.self = p
dht.network = swarm.NewSwarm(p)
dht.listeners = make(map[uint64]chan *swarm.Message)
dht.shutdown = make(chan struct{})
return dht
}
// Read in all messages from swarm and handle them appropriately // Read in all messages from swarm and handle them appropriately
// NOTE: this function is just a quick sketch // NOTE: this function is just a quick sketch
func (dht *IpfsDHT) handleMessages() { func (dht *IpfsDHT) handleMessages() {
...@@ -61,10 +76,13 @@ func (dht *IpfsDHT) handleMessages() { ...@@ -61,10 +76,13 @@ func (dht *IpfsDHT) handleMessages() {
case DHTMessage_GET_VALUE: case DHTMessage_GET_VALUE:
dht.handleGetValue(mes.Peer, pmes) dht.handleGetValue(mes.Peer, pmes)
case DHTMessage_PUT_VALUE: case DHTMessage_PUT_VALUE:
dht.handlePutValue(mes.Peer, pmes)
case DHTMessage_FIND_NODE: case DHTMessage_FIND_NODE:
dht.handleFindNode(mes.Peer, pmes)
case DHTMessage_ADD_PROVIDER: case DHTMessage_ADD_PROVIDER:
case DHTMessage_GET_PROVIDERS: case DHTMessage_GET_PROVIDERS:
case DHTMessage_PING: case DHTMessage_PING:
dht.handleFindNode(mes.Peer, pmes)
} }
case <-dht.shutdown: case <-dht.shutdown:
...@@ -74,15 +92,22 @@ func (dht *IpfsDHT) handleMessages() { ...@@ -74,15 +92,22 @@ func (dht *IpfsDHT) handleMessages() {
} }
func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) { func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) {
val, found := dht.data[pmes.GetKey()] dskey := ds.NewKey(pmes.GetKey())
if found { i_val, err := dht.datastore.Get(dskey)
if err == nil {
isResponse := true isResponse := true
resp := new(DHTMessage) resp := new(DHTMessage)
resp.Response = &isResponse resp.Response = &isResponse
resp.Id = pmes.Id resp.Id = pmes.Id
resp.Key = pmes.Key resp.Key = pmes.Key
val := i_val.([]byte)
resp.Value = val resp.Value = val
} else {
mes := new(swarm.Message)
mes.Peer = p
mes.Data = []byte(resp.String())
} else if err == ds.ErrNotFound {
// Find closest node(s) to desired key and reply with that info // Find closest node(s) to desired key and reply with that info
// TODO: this will need some other metadata in the protobuf message // TODO: this will need some other metadata in the protobuf message
// to signal to the querying node that the data its receiving // to signal to the querying node that the data its receiving
...@@ -90,8 +115,14 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) { ...@@ -90,8 +115,14 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *DHTMessage) {
} }
} }
// Store a value in this nodes local storage
func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) { func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *DHTMessage) {
panic("Not implemented.") 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)
}
} }
func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) { func (dht *IpfsDHT) handleFindNode(p *peer.Peer, pmes *DHTMessage) {
...@@ -121,6 +152,17 @@ func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message { ...@@ -121,6 +152,17 @@ func (dht *IpfsDHT) ListenFor(mesid uint64) <-chan *swarm.Message {
return lchan return lchan
} }
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)
}
// Stop all communications from this node and shut down // Stop all communications from this node and shut down
func (dht *IpfsDHT) Halt() { func (dht *IpfsDHT) Halt() {
dht.shutdown <- struct{}{} dht.shutdown <- struct{}{}
......
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