提交 1461feec 作者: Juan Batiz-Benet 提交者: Brian Tiger Chow

simpler, clearer dht message

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