Commit 8a7747dc authored by Juan Batiz-Benet's avatar Juan Batiz-Benet Committed by Brian Tiger Chow

simpler, clearer dht message

parent a5b87051
......@@ -5,19 +5,8 @@ import (
peer "github.com/jbenet/go-ipfs/peer"
)
// Message is a a helper struct which makes working with protbuf types easier
type Message struct {
Type PBDHTMessage_MessageType
Key string
Value []byte
Response bool
ID string
Success bool
Peers []*peer.Peer
}
func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
pbp := new(PBDHTMessage_PBPeer)
func peerInfo(p *peer.Peer) *Message_Peer {
pbp := new(Message_Peer)
if len(p.Addresses) == 0 || p.Addresses[0] == nil {
pbp.Addr = proto.String("")
} else {
......@@ -33,23 +22,16 @@ func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
return pbp
}
// ToProtobuf takes a Message and produces a protobuf with it.
// TODO: building the protobuf message this way is a little wasteful
// Unused fields wont be omitted, find a better way to do this
func (m *Message) ToProtobuf() *PBDHTMessage {
pmes := new(PBDHTMessage)
if m.Value != nil {
pmes.Value = m.Value
}
pmes.Type = &m.Type
pmes.Key = &m.Key
pmes.Response = &m.Response
pmes.Id = &m.ID
pmes.Success = &m.Success
for _, p := range m.Peers {
pmes.Peers = append(pmes.Peers, peerInfo(p))
}
// GetClusterLevel gets and adjusts the cluster level on the message.
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// default "no value" protobuf behavior (0)
func (m *Message) GetClusterLevel() int32 {
return m.GetClusterLevelRaw() - 1
}
return pmes
// SetClusterLevel adjusts and sets the cluster level on the message.
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// default "no value" protobuf behavior (0)
func (m *Message) SetClusterLevel(level int32) {
m.ClusterLevelRaw = &level
}
// Code generated by protoc-gen-go.
// Code generated by protoc-gen-gogo.
// source: messages.proto
// DO NOT EDIT!
......@@ -9,30 +9,32 @@ It is generated from these files:
messages.proto
It has these top-level messages:
PBDHTMessage
Message
*/
package dht
import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
import proto "code.google.com/p/gogoprotobuf/proto"
import json "encoding/json"
import math "math"
// Reference imports to suppress errors if they are not otherwise used.
// Reference proto, json, and math imports to suppress error if they are not otherwise used.
var _ = proto.Marshal
var _ = &json.SyntaxError{}
var _ = math.Inf
type PBDHTMessage_MessageType int32
type Message_MessageType int32
const (
PBDHTMessage_PUT_VALUE PBDHTMessage_MessageType = 0
PBDHTMessage_GET_VALUE PBDHTMessage_MessageType = 1
PBDHTMessage_ADD_PROVIDER PBDHTMessage_MessageType = 2
PBDHTMessage_GET_PROVIDERS PBDHTMessage_MessageType = 3
PBDHTMessage_FIND_NODE PBDHTMessage_MessageType = 4
PBDHTMessage_PING PBDHTMessage_MessageType = 5
PBDHTMessage_DIAGNOSTIC PBDHTMessage_MessageType = 6
Message_PUT_VALUE Message_MessageType = 0
Message_GET_VALUE Message_MessageType = 1
Message_ADD_PROVIDER Message_MessageType = 2
Message_GET_PROVIDERS Message_MessageType = 3
Message_FIND_NODE Message_MessageType = 4
Message_PING Message_MessageType = 5
Message_DIAGNOSTIC Message_MessageType = 6
)
var PBDHTMessage_MessageType_name = map[int32]string{
var Message_MessageType_name = map[int32]string{
0: "PUT_VALUE",
1: "GET_VALUE",
2: "ADD_PROVIDER",
......@@ -41,7 +43,7 @@ var PBDHTMessage_MessageType_name = map[int32]string{
5: "PING",
6: "DIAGNOSTIC",
}
var PBDHTMessage_MessageType_value = map[string]int32{
var Message_MessageType_value = map[string]int32{
"PUT_VALUE": 0,
"GET_VALUE": 1,
"ADD_PROVIDER": 2,
......@@ -51,105 +53,107 @@ var PBDHTMessage_MessageType_value = map[string]int32{
"DIAGNOSTIC": 6,
}
func (x PBDHTMessage_MessageType) Enum() *PBDHTMessage_MessageType {
p := new(PBDHTMessage_MessageType)
func (x Message_MessageType) Enum() *Message_MessageType {
p := new(Message_MessageType)
*p = x
return p
}
func (x PBDHTMessage_MessageType) String() string {
return proto.EnumName(PBDHTMessage_MessageType_name, int32(x))
func (x Message_MessageType) String() string {
return proto.EnumName(Message_MessageType_name, int32(x))
}
func (x *PBDHTMessage_MessageType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(PBDHTMessage_MessageType_value, data, "PBDHTMessage_MessageType")
func (x *Message_MessageType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(Message_MessageType_value, data, "Message_MessageType")
if err != nil {
return err
}
*x = PBDHTMessage_MessageType(value)
*x = Message_MessageType(value)
return nil
}
type PBDHTMessage struct {
Type *PBDHTMessage_MessageType `protobuf:"varint,1,req,name=type,enum=dht.PBDHTMessage_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"`
Id *string `protobuf:"bytes,4,req,name=id" json:"id,omitempty"`
Response *bool `protobuf:"varint,5,opt,name=response" json:"response,omitempty"`
Success *bool `protobuf:"varint,6,opt,name=success" json:"success,omitempty"`
Peers []*PBDHTMessage_PBPeer `protobuf:"bytes,7,rep,name=peers" json:"peers,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *PBDHTMessage) Reset() { *m = PBDHTMessage{} }
func (m *PBDHTMessage) String() string { return proto.CompactTextString(m) }
func (*PBDHTMessage) ProtoMessage() {}
func (m *PBDHTMessage) GetType() PBDHTMessage_MessageType {
type Message struct {
// defines what type of message it is.
Type *Message_MessageType `protobuf:"varint,1,req,name=type,enum=dht.Message_MessageType" json:"type,omitempty"`
// defines what coral cluster level this query/response belongs to.
ClusterLevelRaw *int32 `protobuf:"varint,10,opt,name=clusterLevelRaw" json:"clusterLevelRaw,omitempty"`
// Used to specify the key associated with this message.
// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
Key *string `protobuf:"bytes,2,opt,name=key" json:"key,omitempty"`
// Used to return a value
// PUT_VALUE, GET_VALUE
Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"`
// Used to return peers closer to a key in a query
// GET_VALUE, GET_PROVIDERS, FIND_NODE
CloserPeers []*Message_Peer `protobuf:"bytes,8,rep,name=closerPeers" json:"closerPeers,omitempty"`
// Used to return Providers
// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
ProviderPeers []*Message_Peer `protobuf:"bytes,9,rep,name=providerPeers" json:"providerPeers,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *Message) Reset() { *m = Message{} }
func (m *Message) String() string { return proto.CompactTextString(m) }
func (*Message) ProtoMessage() {}
func (m *Message) GetType() Message_MessageType {
if m != nil && m.Type != nil {
return *m.Type
}
return PBDHTMessage_PUT_VALUE
return Message_PUT_VALUE
}
func (m *PBDHTMessage) GetKey() string {
func (m *Message) GetClusterLevelRaw() int32 {
if m != nil && m.ClusterLevelRaw != nil {
return *m.ClusterLevelRaw
}
return 0
}
func (m *Message) GetKey() string {
if m != nil && m.Key != nil {
return *m.Key
}
return ""
}
func (m *PBDHTMessage) GetValue() []byte {
func (m *Message) GetValue() []byte {
if m != nil {
return m.Value
}
return nil
}
func (m *PBDHTMessage) GetId() string {
if m != nil && m.Id != nil {
return *m.Id
}
return ""
}
func (m *PBDHTMessage) GetResponse() bool {
if m != nil && m.Response != nil {
return *m.Response
}
return false
}
func (m *PBDHTMessage) GetSuccess() bool {
if m != nil && m.Success != nil {
return *m.Success
func (m *Message) GetCloserPeers() []*Message_Peer {
if m != nil {
return m.CloserPeers
}
return false
return nil
}
func (m *PBDHTMessage) GetPeers() []*PBDHTMessage_PBPeer {
func (m *Message) GetProviderPeers() []*Message_Peer {
if m != nil {
return m.Peers
return m.ProviderPeers
}
return nil
}
type PBDHTMessage_PBPeer struct {
type Message_Peer struct {
Id *string `protobuf:"bytes,1,req,name=id" json:"id,omitempty"`
Addr *string `protobuf:"bytes,2,req,name=addr" json:"addr,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *PBDHTMessage_PBPeer) Reset() { *m = PBDHTMessage_PBPeer{} }
func (m *PBDHTMessage_PBPeer) String() string { return proto.CompactTextString(m) }
func (*PBDHTMessage_PBPeer) ProtoMessage() {}
func (m *Message_Peer) Reset() { *m = Message_Peer{} }
func (m *Message_Peer) String() string { return proto.CompactTextString(m) }
func (*Message_Peer) ProtoMessage() {}
func (m *PBDHTMessage_PBPeer) GetId() string {
func (m *Message_Peer) GetId() string {
if m != nil && m.Id != nil {
return *m.Id
}
return ""
}
func (m *PBDHTMessage_PBPeer) GetAddr() string {
func (m *Message_Peer) GetAddr() string {
if m != nil && m.Addr != nil {
return *m.Addr
}
......@@ -157,5 +161,5 @@ func (m *PBDHTMessage_PBPeer) GetAddr() string {
}
func init() {
proto.RegisterEnum("dht.PBDHTMessage_MessageType", PBDHTMessage_MessageType_name, PBDHTMessage_MessageType_value)
proto.RegisterEnum("dht.Message_MessageType", Message_MessageType_name, Message_MessageType_value)
}
......@@ -2,7 +2,7 @@ package dht;
//run `protoc --go_out=. *.proto` to generate
message PBDHTMessage {
message Message {
enum MessageType {
PUT_VALUE = 0;
GET_VALUE = 1;
......@@ -13,22 +13,30 @@ message PBDHTMessage {
DIAGNOSTIC = 6;
}
message PBPeer {
message Peer {
required string id = 1;
required string addr = 2;
}
// defines what type of message it is.
required MessageType type = 1;
// defines what coral cluster level this query/response belongs to.
optional int32 clusterLevelRaw = 10;
// Used to specify the key associated with this message.
// PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
optional string key = 2;
optional bytes value = 3;
// Unique ID of this message, used to match queries with responses
required string id = 4;
// Used to return a value
// PUT_VALUE, GET_VALUE
optional bytes value = 3;
// Signals whether or not this message is a response to another message
optional bool response = 5;
optional bool success = 6;
// Used to return peers closer to a key in a query
// GET_VALUE, GET_PROVIDERS, FIND_NODE
repeated Peer closerPeers = 8;
// Used for returning peers from queries (normally, peers closer to X)
repeated PBPeer peers = 7;
// Used to return Providers
// GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
repeated Peer providerPeers = 9;
}
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