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
...
@@ -3,9 +3,10 @@ package dht
import
(
import
(
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
peer
"github.com/jbenet/go-ipfs/peer"
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
)
pbp
:=
new
(
Message_Peer
)
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
(
""
)
...
@@ -22,11 +23,24 @@ func peerInfo(p *peer.Peer) *Message_Peer {
...
@@ -22,11 +23,24 @@ func peerInfo(p *peer.Peer) *Message_Peer {
return
pbp
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.
// GetClusterLevel gets and adjusts the cluster level on the message.
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// a +/- 1 adjustment is needed to distinguish a valid first level (1) and
// default "no value" protobuf behavior (0)
// default "no value" protobuf behavior (0)
func
(
m
*
Message
)
GetClusterLevel
()
int32
{
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.
// 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 {
...
@@ -169,14 +169,14 @@ func (dht *IpfsDHT) handlerForMsgType(t Message_MessageType) dhtHandler {
return
dht
.
handleGetValue
return
dht
.
handleGetValue
// case Message_PUT_VALUE:
// case Message_PUT_VALUE:
// return dht.handlePutValue
// return dht.handlePutValue
//
case Message_FIND_NODE:
case
Message_FIND_NODE
:
//
return dht.handleFindPeer
return
dht
.
handleFindPeer
// case Message_ADD_PROVIDER:
// case Message_ADD_PROVIDER:
// return dht.handleAddProvider
// return dht.handleAddProvider
// case Message_GET_PROVIDERS:
// case Message_GET_PROVIDERS:
// return dht.handleGetProviders
// return dht.handleGetProviders
//
case Message_PING:
case
Message_PING
:
//
return dht.handlePing
return
dht
.
handlePing
// case Message_DIAGNOSTIC:
// case Message_DIAGNOSTIC:
// return dht.handleDiagnostic
// return dht.handleDiagnostic
default
:
default
:
...
@@ -240,7 +240,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
...
@@ -240,7 +240,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
provs
:=
dht
.
providers
.
GetProviders
(
u
.
Key
(
pmes
.
GetKey
()))
provs
:=
dht
.
providers
.
GetProviders
(
u
.
Key
(
pmes
.
GetKey
()))
if
len
(
provs
)
>
0
{
if
len
(
provs
)
>
0
{
u
.
DOut
(
"handleGetValue returning %d provider[s]
\n
"
,
len
(
provs
))
u
.
DOut
(
"handleGetValue returning %d provider[s]
\n
"
,
len
(
provs
))
resp
.
ProviderPeers
=
provs
resp
.
ProviderPeers
=
peersToPBPeers
(
provs
)
return
resp
,
nil
return
resp
,
nil
}
}
...
@@ -249,11 +249,6 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
...
@@ -249,11 +249,6 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
// stored levels are > 1, to distinguish missing levels.
// stored levels are > 1, to distinguish missing levels.
level
:=
pmes
.
GetClusterLevel
()
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
)
u
.
DOut
(
"handleGetValue searching level %d clusters
\n
"
,
level
)
ck
:=
kb
.
ConvertKey
(
u
.
Key
(
pmes
.
GetKey
()))
ck
:=
kb
.
ConvertKey
(
u
.
Key
(
pmes
.
GetKey
()))
...
@@ -275,7 +270,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
...
@@ -275,7 +270,7 @@ func (dht *IpfsDHT) handleGetValue(p *peer.Peer, pmes *Message) (*Message, error
// we got a closer peer, it seems. return it.
// we got a closer peer, it seems. return it.
u
.
DOut
(
"handleGetValue returning a closer peer: '%s'
\n
"
,
closer
.
ID
.
Pretty
())
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
return
resp
,
nil
}
}
...
@@ -291,48 +286,37 @@ func (dht *IpfsDHT) handlePutValue(p *peer.Peer, pmes *Message) {
...
@@ -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
())
u
.
DOut
(
"[%s] Responding to ping from [%s]!
\n
"
,
dht
.
self
.
ID
.
Pretty
(),
p
.
ID
.
Pretty
())
resp
:=
Message
{
return
&
Message
{
Type
:
pmes
.
Type
},
nil
Type
:
pmes
.
GetType
(),
Response
:
true
,
ID
:
pmes
.
GetId
(),
}
dht
.
netChan
.
Outgoing
<-
swarm
.
NewMessage
(
p
,
resp
.
ToProtobuf
())
}
}
func
(
dht
*
IpfsDHT
)
handleFindPeer
(
p
*
peer
.
Peer
,
pmes
*
Message
)
{
func
(
dht
*
IpfsDHT
)
handleFindPeer
(
p
*
peer
.
Peer
,
pmes
*
Message
)
(
*
Message
,
error
)
{
resp
:=
Message
{
resp
:=
&
Message
{
Type
:
pmes
.
Type
}
Type
:
pmes
.
GetType
(),
ID
:
pmes
.
GetId
(),
level
:=
pmes
.
GetClusterLevel
()
Response
:
true
,
}
defer
func
()
{
mes
:=
swarm
.
NewMessage
(
p
,
resp
.
ToProtobuf
())
dht
.
netChan
.
Outgoing
<-
mes
}()
level
:=
pmes
.
GetValue
()[
0
]
u
.
DOut
(
"handleFindPeer: searching for '%s'
\n
"
,
peer
.
ID
(
pmes
.
GetKey
())
.
Pretty
())
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
{
if
closest
==
nil
{
u
.
PErr
(
"handleFindPeer: could not find anything.
\n
"
)
u
.
PErr
(
"handleFindPeer: could not find anything.
\n
"
)
return
return
resp
,
nil
}
}
if
len
(
closest
.
Addresses
)
==
0
{
if
len
(
closest
.
Addresses
)
==
0
{
u
.
PErr
(
"handleFindPeer: no addresses for connected peer...
\n
"
)
u
.
PErr
(
"handleFindPeer: no addresses for connected peer...
\n
"
)
return
return
resp
,
nil
}
}
// If the found peer further away than this peer...
// If the found peer further away than this peer...
if
kb
.
Closer
(
dht
.
self
.
ID
,
closest
.
ID
,
u
.
Key
(
pmes
.
GetKey
()))
{
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
())
u
.
DOut
(
"handleFindPeer: sending back '%s'
\n
"
,
closest
.
ID
.
Pretty
())
resp
.
Peers
=
[]
*
peer
.
Peer
{
closest
}
resp
.
Closer
Peers
=
peersToPBPeers
(
[]
*
peer
.
Peer
{
closest
}
)
re
sp
.
Success
=
true
re
turn
resp
,
nil
}
}
func
(
dht
*
IpfsDHT
)
handleGetProviders
(
p
*
peer
.
Peer
,
pmes
*
Message
)
{
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