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
dms3
go-unixfs
Commits
9eb41e72
Commit
9eb41e72
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
ping + find peer
parent
2522625b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
38 deletions
+36
-38
routing/dht/Message.go
routing/dht/Message.go
+16
-2
routing/dht/dht.go
routing/dht/dht.go
+20
-36
No files found.
routing/dht/Message.go
View file @
9eb41e72
...
...
@@ -3,9 +3,10 @@ package dht
import
(
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
)
func
peer
Info
(
p
*
peer
.
Peer
)
*
Message_Peer
{
func
peer
ToPBPeer
(
p
*
peer
.
Peer
)
*
Message_Peer
{
pbp
:=
new
(
Message_Peer
)
if
len
(
p
.
Addresses
)
==
0
||
p
.
Addresses
[
0
]
==
nil
{
pbp
.
Addr
=
proto
.
String
(
""
)
...
...
@@ -22,11 +23,24 @@ func peerInfo(p *peer.Peer) *Message_Peer {
return
pbp
}
func
peersToPBPeers
(
peers
[]
*
peer
.
Peer
)
[]
*
Message_Peer
{
pbpeers
=
make
([]
*
Message_Peer
,
len
(
peers
))
for
i
,
p
:=
range
peers
{
pbpeers
[
i
]
=
peerToPBPeer
(
p
)
}
return
pbpeers
}
// 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
level
:=
m
.
GetClusterLevelRaw
()
-
1
if
level
<
0
{
u
.
PErr
(
"handleGetValue: no routing level specified, assuming 0
\n
"
)
level
=
0
}
return
level
}
// SetClusterLevel adjusts and sets the cluster level on the message.
...
...
routing/dht/dht.go
View file @
9eb41e72
...
...
@@ -169,14 +169,14 @@ func (dht *IpfsDHT) handlerForMsgType(t Message_MessageType) dhtHandler {
return
dht
.
handleGetValue
// case Message_PUT_VALUE:
// return dht.handlePutValue
//
case Message_FIND_NODE:
//
return dht.handleFindPeer
case
Message_FIND_NODE
:
return
dht
.
handleFindPeer
// case Message_ADD_PROVIDER:
// return dht.handleAddProvider
// case Message_GET_PROVIDERS:
// return dht.handleGetProviders
//
case Message_PING:
//
return dht.handlePing
case
Message_PING
:
return
dht
.
handlePing
// case Message_DIAGNOSTIC:
// return dht.handleDiagnostic
default
:
...
...
@@ -240,7 +240,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
provs
:=
dht
.
providers
.
GetProviders
(
u
.
Key
(
pmes
.
GetKey
()))
if
len
(
provs
)
>
0
{
u
.
DOut
(
"handleGetValue returning %d provider[s]
\n
"
,
len
(
provs
))
resp.ProviderPeers = provs
resp
.
ProviderPeers
=
peersToPBPeers
(
provs
)
return
resp
,
nil
}
...
...
@@ -249,11 +249,6 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
// stored levels are > 1, to distinguish missing levels.
level
:=
pmes
.
GetClusterLevel
()
if level < 0 {
// TODO: maybe return an error? Defaulting isnt a good idea IMO
u.PErr("handleGetValue: no routing level specified, assuming 0\n")
level = 0
}
u
.
DOut
(
"handleGetValue searching level %d clusters
\n
"
,
level
)
ck
:=
kb
.
ConvertKey
(
u
.
Key
(
pmes
.
GetKey
()))
...
...
@@ -275,7 +270,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
// we got a closer peer, it seems. return it.
u
.
DOut
(
"handleGetValue returning a closer peer: '%s'
\n
"
,
closer
.
ID
.
Pretty
())
resp.CloserPeers = []*peer.Peer{closer}
resp
.
CloserPeers
=
peersToPBPeers
(
[]
*
peer
.
Peer
{
closer
}
)
return
resp
,
nil
}
...
...
@@ -291,48 +286,37 @@ func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *Message) {
}
}
func (dht *IpfsDHT) handlePing(p *peer.Peer, pmes *Message) {
func
(
dht
*
IpfsDHT
)
handlePing
(
p
*
peer
.
Peer
,
pmes
*
Message
)
(
*
Message
,
error
)
{
u
.
DOut
(
"[%s] Responding to ping from [%s]!
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
p
.
ID
.
Pretty
())
resp := Message{
Type: pmes.GetType(),
Response: true,
ID: pmes.GetId(),
}
dht.netChan.Outgoing <- swarm.NewMessage(p, resp.ToProtobuf())
return
&
Message
{
Type
:
pmes
.
Type
},
nil
}
func (dht *IpfsDHT) handleFindPeer(p *peer.Peer, pmes *Message) {
resp := Message{
Type: pmes.GetType(),
ID: pmes.GetId(),
Response: true,
}
defer func() {
mes := swarm.NewMessage(p, resp.ToProtobuf())
dht.netChan.Outgoing <- mes
}()
level := pmes.GetValue()[0]
func
(
dht
*
IpfsDHT
)
handleFindPeer
(
p
*
peer
.
Peer
,
pmes
*
Message
)
(
*
Message
,
error
)
{
resp
:=
&
Message
{
Type
:
pmes
.
Type
}
level
:=
pmes
.
GetClusterLevel
()
u
.
DOut
(
"handleFindPeer: searching for '%s'
\n
"
,
peer
.
ID
(
pmes
.
GetKey
())
.
Pretty
())
closest := dht.routingTables[level].NearestPeer(kb.ConvertKey(u.Key(pmes.GetKey())))
ck
:=
kb
.
ConvertKey
(
u
.
Key
(
pmes
.
GetKey
()))
closest
:=
dht
.
routingTables
[
level
]
.
NearestPeer
(
ck
)
if
closest
==
nil
{
u
.
PErr
(
"handleFindPeer: could not find anything.
\n
"
)
return
return
resp
,
nil
}
if
len
(
closest
.
Addresses
)
==
0
{
u
.
PErr
(
"handleFindPeer: no addresses for connected peer...
\n
"
)
return
return
resp
,
nil
}
// If the found peer further away than this peer...
if
kb
.
Closer
(
dht
.
self
.
ID
,
closest
.
ID
,
u
.
Key
(
pmes
.
GetKey
()))
{
return
return
resp
,
nil
}
u
.
DOut
(
"handleFindPeer: sending back '%s'
\n
"
,
closest
.
ID
.
Pretty
())
resp.Peers = []*peer.Peer{closest}
re
sp.Success = true
resp
.
Closer
Peers
=
peersToPBPeers
(
[]
*
peer
.
Peer
{
closest
}
)
re
turn
resp
,
nil
}
func
(
dht
*
IpfsDHT
)
handleGetProviders
(
p
*
peer
.
Peer
,
pmes
*
Message
)
{
...
...
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