@cryptotaxi247 / kubo / commits / 1461feec3

simpler, clearer dht message

Juan Batiz-Benet committed Sep 16, 2014 at 00:52 UTC 1461feec3f2b51241914234747597613e7d59804
3 files changed +102 -108
routing/dht/Message.go
+13 -31
@@ -5,19 +5,8 @@ import (
5 peer "github.com/jbenet/go-ipfs/peer"
6 )
7
8 -// Message is a a helper struct which makes working with protbuf types easier
9 -type Message struct {
10 - Type PBDHTMessage_MessageType
11 - Key string
12 - Value []byte
13 - Response bool
14 - ID string
15 - Success bool
16 - Peers []*peer.Peer
17 -}
18 -
19 -func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
20 - pbp := new(PBDHTMessage_PBPeer)
8 +func peerInfo(p *peer.Peer) *Message_Peer {
9 + pbp := new(Message_Peer)
10 if len(p.Addresses) == 0 || p.Addresses[0] == nil {
11 pbp.Addr = proto.String("")
12 } else {
@@ -33,23 +22,16 @@ func peerInfo(p *peer.Peer) *PBDHTMessage_PBPeer {
22 return pbp
23 }
24
36 -// ToProtobuf takes a Message and produces a protobuf with it.
37 -// TODO: building the protobuf message this way is a little wasteful
38 -// Unused fields wont be omitted, find a better way to do this
39 -func (m *Message) ToProtobuf() *PBDHTMessage {
40 - pmes := new(PBDHTMessage)
41 - if m.Value != nil {
42 - pmes.Value = m.Value
43 - }
44 -
45 - pmes.Type = &m.Type
46 - pmes.Key = &m.Key
47 - pmes.Response = &m.Response
48 - pmes.Id = &m.ID
49 - pmes.Success = &m.Success
50 - for _, p := range m.Peers {
51 - pmes.Peers = append(pmes.Peers, peerInfo(p))
52 - }
25 +// GetClusterLevel gets and adjusts the cluster level on the message.
26 +// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
27 +// default "no value" protobuf behavior (0)
28 +func (m *Message) GetClusterLevel() int32 {
29 + return m.GetClusterLevelRaw() - 1
30 +}
31
54 - return pmes
32 +// SetClusterLevel adjusts and sets the cluster level on the message.
33 +// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
34 +// default "no value" protobuf behavior (0)
35 +func (m *Message) SetClusterLevel(level int32) {
36 + m.ClusterLevelRaw = &level
37 }
routing/dht/messages.pb.go
+71 -67
@@ -1,4 +1,4 @@
1 -// Code generated by protoc-gen-go.
1 +// Code generated by protoc-gen-gogo.
2 // source: messages.proto
3 // DO NOT EDIT!
4
@@ -9,30 +9,32 @@ It is generated from these files:
9 messages.proto
10
11 It has these top-level messages:
12 - PBDHTMessage
12 + Message
13 */
14 package dht
15
16 -import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
16 +import proto "code.google.com/p/gogoprotobuf/proto"
17 +import json "encoding/json"
18 import math "math"
19
19 -// Reference imports to suppress errors if they are not otherwise used.
20 +// Reference proto, json, and math imports to suppress error if they are not otherwise used.
21 var _ = proto.Marshal
22 +var _ = &json.SyntaxError{}
23 var _ = math.Inf
24
23 -type PBDHTMessage_MessageType int32
25 +type Message_MessageType int32
26
27 const (
26 - PBDHTMessage_PUT_VALUE PBDHTMessage_MessageType = 0
27 - PBDHTMessage_GET_VALUE PBDHTMessage_MessageType = 1
28 - PBDHTMessage_ADD_PROVIDER PBDHTMessage_MessageType = 2
29 - PBDHTMessage_GET_PROVIDERS PBDHTMessage_MessageType = 3
30 - PBDHTMessage_FIND_NODE PBDHTMessage_MessageType = 4
31 - PBDHTMessage_PING PBDHTMessage_MessageType = 5
32 - PBDHTMessage_DIAGNOSTIC PBDHTMessage_MessageType = 6
28 + Message_PUT_VALUE Message_MessageType = 0
29 + Message_GET_VALUE Message_MessageType = 1
30 + Message_ADD_PROVIDER Message_MessageType = 2
31 + Message_GET_PROVIDERS Message_MessageType = 3
32 + Message_FIND_NODE Message_MessageType = 4
33 + Message_PING Message_MessageType = 5
34 + Message_DIAGNOSTIC Message_MessageType = 6
35 )
36
35 -var PBDHTMessage_MessageType_name = map[int32]string{
37 +var Message_MessageType_name = map[int32]string{
38 0: "PUT_VALUE",
39 1: "GET_VALUE",
40 2: "ADD_PROVIDER",
@@ -41,7 +43,7 @@ var PBDHTMessage_MessageType_name = map[int32]string{
43 5: "PING",
44 6: "DIAGNOSTIC",
45 }
44 -var PBDHTMessage_MessageType_value = map[string]int32{
46 +var Message_MessageType_value = map[string]int32{
47 "PUT_VALUE": 0,
48 "GET_VALUE": 1,
49 "ADD_PROVIDER": 2,
@@ -51,105 +53,107 @@ var PBDHTMessage_MessageType_value = map[string]int32{
53 "DIAGNOSTIC": 6,
54 }
55
54 -func (x PBDHTMessage_MessageType) Enum() *PBDHTMessage_MessageType {
55 - p := new(PBDHTMessage_MessageType)
56 +func (x Message_MessageType) Enum() *Message_MessageType {
57 + p := new(Message_MessageType)
58 *p = x
59 return p
60 }
59 -func (x PBDHTMessage_MessageType) String() string {
60 - return proto.EnumName(PBDHTMessage_MessageType_name, int32(x))
61 +func (x Message_MessageType) String() string {
62 + return proto.EnumName(Message_MessageType_name, int32(x))
63 }
62 -func (x *PBDHTMessage_MessageType) UnmarshalJSON(data []byte) error {
63 - value, err := proto.UnmarshalJSONEnum(PBDHTMessage_MessageType_value, data, "PBDHTMessage_MessageType")
64 +func (x *Message_MessageType) UnmarshalJSON(data []byte) error {
65 + value, err := proto.UnmarshalJSONEnum(Message_MessageType_value, data, "Message_MessageType")
66 if err != nil {
67 return err
68 }
67 - *x = PBDHTMessage_MessageType(value)
69 + *x = Message_MessageType(value)
70 return nil
71 }
72
71 -type PBDHTMessage struct {
72 - Type *PBDHTMessage_MessageType `protobuf:"varint,1,req,name=type,enum=dht.PBDHTMessage_MessageType" json:"type,omitempty"`
73 - Key *string `protobuf:"bytes,2,opt,name=key" json:"key,omitempty"`
74 - Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"`
75 - Id *string `protobuf:"bytes,4,req,name=id" json:"id,omitempty"`
76 - Response *bool `protobuf:"varint,5,opt,name=response" json:"response,omitempty"`
77 - Success *bool `protobuf:"varint,6,opt,name=success" json:"success,omitempty"`
78 - Peers []*PBDHTMessage_PBPeer `protobuf:"bytes,7,rep,name=peers" json:"peers,omitempty"`
79 - XXX_unrecognized []byte `json:"-"`
80 -}
81 -
82 -func (m *PBDHTMessage) Reset() { *m = PBDHTMessage{} }
83 -func (m *PBDHTMessage) String() string { return proto.CompactTextString(m) }
84 -func (*PBDHTMessage) ProtoMessage() {}
85 -
86 -func (m *PBDHTMessage) GetType() PBDHTMessage_MessageType {
73 +type Message struct {
74 + // defines what type of message it is.
75 + Type *Message_MessageType `protobuf:"varint,1,req,name=type,enum=dht.Message_MessageType" json:"type,omitempty"`
76 + // defines what coral cluster level this query/response belongs to.
77 + ClusterLevelRaw *int32 `protobuf:"varint,10,opt,name=clusterLevelRaw" json:"clusterLevelRaw,omitempty"`
78 + // Used to specify the key associated with this message.
79 + // PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
80 + Key *string `protobuf:"bytes,2,opt,name=key" json:"key,omitempty"`
81 + // Used to return a value
82 + // PUT_VALUE, GET_VALUE
83 + Value []byte `protobuf:"bytes,3,opt,name=value" json:"value,omitempty"`
84 + // Used to return peers closer to a key in a query
85 + // GET_VALUE, GET_PROVIDERS, FIND_NODE
86 + CloserPeers []*Message_Peer `protobuf:"bytes,8,rep,name=closerPeers" json:"closerPeers,omitempty"`
87 + // Used to return Providers
88 + // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
89 + ProviderPeers []*Message_Peer `protobuf:"bytes,9,rep,name=providerPeers" json:"providerPeers,omitempty"`
90 + XXX_unrecognized []byte `json:"-"`
91 +}
92 +
93 +func (m *Message) Reset() { *m = Message{} }
94 +func (m *Message) String() string { return proto.CompactTextString(m) }
95 +func (*Message) ProtoMessage() {}
96 +
97 +func (m *Message) GetType() Message_MessageType {
98 if m != nil && m.Type != nil {
99 return *m.Type
100 }
90 - return PBDHTMessage_PUT_VALUE
101 + return Message_PUT_VALUE
102 }
103
93 -func (m *PBDHTMessage) GetKey() string {
104 +func (m *Message) GetClusterLevelRaw() int32 {
105 + if m != nil && m.ClusterLevelRaw != nil {
106 + return *m.ClusterLevelRaw
107 + }
108 + return 0
109 +}
110 +
111 +func (m *Message) GetKey() string {
112 if m != nil && m.Key != nil {
113 return *m.Key
114 }
115 return ""
116 }
117
100 -func (m *PBDHTMessage) GetValue() []byte {
118 +func (m *Message) GetValue() []byte {
119 if m != nil {
120 return m.Value
121 }
122 return nil
123 }
124
107 -func (m *PBDHTMessage) GetId() string {
108 - if m != nil && m.Id != nil {
109 - return *m.Id
110 - }
111 - return ""
112 -}
113 -
114 -func (m *PBDHTMessage) GetResponse() bool {
115 - if m != nil && m.Response != nil {
116 - return *m.Response
117 - }
118 - return false
119 -}
120 -
121 -func (m *PBDHTMessage) GetSuccess() bool {
122 - if m != nil && m.Success != nil {
123 - return *m.Success
125 +func (m *Message) GetCloserPeers() []*Message_Peer {
126 + if m != nil {
127 + return m.CloserPeers
128 }
125 - return false
129 + return nil
130 }
131
128 -func (m *PBDHTMessage) GetPeers() []*PBDHTMessage_PBPeer {
132 +func (m *Message) GetProviderPeers() []*Message_Peer {
133 if m != nil {
130 - return m.Peers
134 + return m.ProviderPeers
135 }
136 return nil
137 }
138
135 -type PBDHTMessage_PBPeer struct {
139 +type Message_Peer struct {
140 Id *string `protobuf:"bytes,1,req,name=id" json:"id,omitempty"`
141 Addr *string `protobuf:"bytes,2,req,name=addr" json:"addr,omitempty"`
142 XXX_unrecognized []byte `json:"-"`
143 }
144
141 -func (m *PBDHTMessage_PBPeer) Reset() { *m = PBDHTMessage_PBPeer{} }
142 -func (m *PBDHTMessage_PBPeer) String() string { return proto.CompactTextString(m) }
143 -func (*PBDHTMessage_PBPeer) ProtoMessage() {}
145 +func (m *Message_Peer) Reset() { *m = Message_Peer{} }
146 +func (m *Message_Peer) String() string { return proto.CompactTextString(m) }
147 +func (*Message_Peer) ProtoMessage() {}
148
145 -func (m *PBDHTMessage_PBPeer) GetId() string {
149 +func (m *Message_Peer) GetId() string {
150 if m != nil && m.Id != nil {
151 return *m.Id
152 }
153 return ""
154 }
155
152 -func (m *PBDHTMessage_PBPeer) GetAddr() string {
156 +func (m *Message_Peer) GetAddr() string {
157 if m != nil && m.Addr != nil {
158 return *m.Addr
159 }
@@ -157,5 +161,5 @@ func (m *PBDHTMessage_PBPeer) GetAddr() string {
161 }
162
163 func init() {
160 - proto.RegisterEnum("dht.PBDHTMessage_MessageType", PBDHTMessage_MessageType_name, PBDHTMessage_MessageType_value)
164 + proto.RegisterEnum("dht.Message_MessageType", Message_MessageType_name, Message_MessageType_value)
165 }
routing/dht/messages.proto
+18 -10
@@ -2,7 +2,7 @@ package dht;
2
3 //run `protoc --go_out=. *.proto` to generate
4
5 -message PBDHTMessage {
5 +message Message {
6 enum MessageType {
7 PUT_VALUE = 0;
8 GET_VALUE = 1;
@@ -13,22 +13,30 @@ message PBDHTMessage {
13 DIAGNOSTIC = 6;
14 }
15
16 - message PBPeer {
16 + message Peer {
17 required string id = 1;
18 required string addr = 2;
19 }
20
21 + // defines what type of message it is.
22 required MessageType type = 1;
23 +
24 + // defines what coral cluster level this query/response belongs to.
25 + optional int32 clusterLevelRaw = 10;
26 +
27 + // Used to specify the key associated with this message.
28 + // PUT_VALUE, GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
29 optional string key = 2;
23 - optional bytes value = 3;
30
25 - // Unique ID of this message, used to match queries with responses
26 - required string id = 4;
31 + // Used to return a value
32 + // PUT_VALUE, GET_VALUE
33 + optional bytes value = 3;
34
28 - // Signals whether or not this message is a response to another message
29 - optional bool response = 5;
30 - optional bool success = 6;
35 + // Used to return peers closer to a key in a query
36 + // GET_VALUE, GET_PROVIDERS, FIND_NODE
37 + repeated Peer closerPeers = 8;
38
32 - // Used for returning peers from queries (normally, peers closer to X)
33 - repeated PBPeer peers = 7;
39 + // Used to return Providers
40 + // GET_VALUE, ADD_PROVIDER, GET_PROVIDERS
41 + repeated Peer providerPeers = 9;
42 }