Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-kad-dht
Commits
8a7747dc
Commit
8a7747dc
authored
Sep 16, 2014
by
Juan Batiz-Benet
Committed by
Brian Tiger Chow
Sep 22, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
simpler, clearer dht message
parent
a5b87051
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
108 deletions
+102
-108
Message.go
Message.go
+13
-31
messages.pb.go
messages.pb.go
+71
-67
messages.proto
messages.proto
+18
-10
No files found.
Message.go
View file @
8a7747dc
...
...
@@ -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
}
messages.pb.go
View file @
8a7747dc
// Code generated by protoc-gen-go.
// Code generated by protoc-gen-go
go
.
// source: messages.proto
// DO NOT EDIT!
...
...
@@ -9,30 +9,32 @@ It is generated from these files:
messages.proto
It has these top-level messages:
PBDHT
Message
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 error
s
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
PBDHT
Message_MessageType
int32
type
Message_MessageType
int32
const
(
PBDHT
Message_PUT_VALUE
PBDHT
Message_MessageType
=
0
PBDHT
Message_GET_VALUE
PBDHT
Message_MessageType
=
1
PBDHT
Message_ADD_PROVIDER
PBDHT
Message_MessageType
=
2
PBDHT
Message_GET_PROVIDERS
PBDHT
Message_MessageType
=
3
PBDHT
Message_FIND_NODE
PBDHT
Message_MessageType
=
4
PBDHT
Message_PING
PBDHT
Message_MessageType
=
5
PBDHT
Message_DIAGNOSTIC
PBDHT
Message_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
PBDHT
Message_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
PBDHT
Message_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
PBDHT
Message_MessageType
)
Enum
()
*
PBDHT
Message_MessageType
{
p
:=
new
(
PBDHT
Message_MessageType
)
func
(
x
Message_MessageType
)
Enum
()
*
Message_MessageType
{
p
:=
new
(
Message_MessageType
)
*
p
=
x
return
p
}
func
(
x
PBDHT
Message_MessageType
)
String
()
string
{
return
proto
.
EnumName
(
PBDHT
Message_MessageType_name
,
int32
(
x
))
func
(
x
Message_MessageType
)
String
()
string
{
return
proto
.
EnumName
(
Message_MessageType_name
,
int32
(
x
))
}
func
(
x
*
PBDHT
Message_MessageType
)
UnmarshalJSON
(
data
[]
byte
)
error
{
value
,
err
:=
proto
.
UnmarshalJSONEnum
(
PBDHT
Message_MessageType_value
,
data
,
"
PBDHT
Message_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
=
PBDHT
Message_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
PBDHT
Message_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
*
PBDHT
Message
)
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
*
PBDHT
Message
)
GetPeers
()
[]
*
PBDHT
Message_
PB
Peer
{
func
(
m
*
Message
)
GetP
roviderP
eers
()
[]
*
Message_Peer
{
if
m
!=
nil
{
return
m
.
Peers
return
m
.
Provider
Peers
}
return
nil
}
type
PBDHT
Message_
PB
Peer
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
*
PBDHT
Message_
PB
Peer
)
Reset
()
{
*
m
=
PBDHT
Message_
PB
Peer
{}
}
func
(
m
*
PBDHT
Message_
PB
Peer
)
String
()
string
{
return
proto
.
CompactTextString
(
m
)
}
func
(
*
PBDHT
Message_
PB
Peer
)
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
*
PBDHT
Message_
PB
Peer
)
GetId
()
string
{
func
(
m
*
Message_Peer
)
GetId
()
string
{
if
m
!=
nil
&&
m
.
Id
!=
nil
{
return
*
m
.
Id
}
return
""
}
func
(
m
*
PBDHT
Message_
PB
Peer
)
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.
PBDHT
Message_MessageType"
,
PBDHT
Message_MessageType_name
,
PBDHT
Message_MessageType_value
)
proto
.
RegisterEnum
(
"dht.Message_MessageType"
,
Message_MessageType_name
,
Message_MessageType_value
)
}
messages.proto
View file @
8a7747dc
...
...
@@ -2,7 +2,7 @@ package dht;
//run `protoc --go_out=. *.proto` to generate
message
PBDHT
Message
{
message
Message
{
enum
MessageType
{
PUT_VALUE
=
0
;
GET_VALUE
=
1
;
...
...
@@ -13,22 +13,30 @@ message PBDHTMessage {
DIAGNOSTIC
=
6
;
}
message
PB
Peer
{
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 respon
se to a
nother message
optional
bool
response
=
5
;
optional
bool
succes
s
=
6
;
//
Used to return peers clo
se
r
to a
key in a query
// GET_VALUE, GET_PROVIDERS, FIND_NODE
repeated
Peer
closerPeer
s
=
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
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment