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

working on upper level dht implementations, protbuf, etc

parent 71d5f6fc
package dht
import (
swarm "github.com/jbenet/go-ipfs/swarm"
)
// TODO. SEE https://github.com/jbenet/node-ipfs/blob/master/submodules/ipfs-dht/index.js
......@@ -7,4 +11,12 @@ package dht
// It is used to implement the base IpfsRouting module.
type IpfsDHT struct {
routes RoutingTable
network *swarm.Swarm
}
func (dht *IpfsDHT) handleMessages() {
for mes := range dht.network.Chan.Incoming {
}
}
// Code generated by protoc-gen-go.
// source: messages.proto
// DO NOT EDIT!
/*
Package dht is a generated protocol buffer package.
It is generated from these files:
messages.proto
It has these top-level messages:
DHTMessage
*/
package dht
import proto "code.google.com/p/goprotobuf/proto"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = math.Inf
type DHTMessage_MessageType int32
const (
DHTMessage_PUT_VALUE DHTMessage_MessageType = 0
DHTMessage_GET_VALUE DHTMessage_MessageType = 1
DHTMessage_PING DHTMessage_MessageType = 2
DHTMessage_FIND_NODE DHTMessage_MessageType = 3
)
var DHTMessage_MessageType_name = map[int32]string{
0: "PUT_VALUE",
1: "GET_VALUE",
2: "PING",
3: "FIND_NODE",
}
var DHTMessage_MessageType_value = map[string]int32{
"PUT_VALUE": 0,
"GET_VALUE": 1,
"PING": 2,
"FIND_NODE": 3,
}
func (x DHTMessage_MessageType) Enum() *DHTMessage_MessageType {
p := new(DHTMessage_MessageType)
*p = x
return p
}
func (x DHTMessage_MessageType) String() string {
return proto.EnumName(DHTMessage_MessageType_name, int32(x))
}
func (x *DHTMessage_MessageType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(DHTMessage_MessageType_value, data, "DHTMessage_MessageType")
if err != nil {
return err
}
*x = DHTMessage_MessageType(value)
return nil
}
type DHTMessage struct {
Type *DHTMessage_MessageType `protobuf:"varint,1,req,name=type,enum=dht.DHTMessage_MessageType" json:"type,omitempty"`
Key *string `protobuf:"bytes,2,opt,name=key" json:"key,omitempty"`
Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *DHTMessage) Reset() { *m = DHTMessage{} }
func (m *DHTMessage) String() string { return proto.CompactTextString(m) }
func (*DHTMessage) ProtoMessage() {}
func (m *DHTMessage) GetType() DHTMessage_MessageType {
if m != nil && m.Type != nil {
return *m.Type
}
return DHTMessage_PUT_VALUE
}
func (m *DHTMessage) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
func (m *DHTMessage) GetValue() []byte {
if m != nil {
return m.Value
}
return nil
}
func init() {
proto.RegisterEnum("dht.DHTMessage_MessageType", DHTMessage_MessageType_name, DHTMessage_MessageType_value)
}
package dht;
//run `protoc --go_out=. *.proto` to generate
message DHTMessage {
enum MessageType {
PUT_VALUE = 0;
GET_VALUE = 1;
PING = 2;
FIND_NODE = 3;
}
required MessageType type = 1;
optional string key = 2;
optional bytes value = 3;
}
......@@ -4,6 +4,7 @@ import (
"time"
peer "github.com/jbenet/go-ipfs/peer"
u "github.com/jbenet/go-ipfs/util"
swarm "github.com/jbenet/go-ipfs/swarm"
)
......@@ -13,12 +14,43 @@ import (
// PutValue adds value corresponding to given Key.
func (s *IpfsDHT) PutValue(key u.Key, value []byte) (error) {
return u.ErrNotImplemented
var p *peer.Peer
p = s.routes.NearestNode(key)
pmes := new(PutValue)
pmes.Key = &key
pmes.Value = value
mes := new(swarm.Message)
mes.Data = []byte(pmes.String())
mes.Peer = p
s.network.Chan.Outgoing <- mes
return nil
}
// 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
var p *peer.Peer
p = s.routes.NearestNode(key)
// protobuf structure
pmes := new(GetValue)
pmes.Key = &key
pmes.Id = GenerateMessageID()
mes := new(swarm.Message)
mes.Data = []byte(pmes.String())
mes.Peer = p
response_chan := s.network.ListenFor(pmes.Id)
timeup := time.After(timeout)
select {
case <-timeup:
return nil, timeoutError
case resp := <-response_chan:
}
}
......
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