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
1082edeb
Commit
1082edeb
authored
Aug 05, 2014
by
Jeromy
Committed by
Juan Batiz-Benet
Aug 07, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
a little error handling and some work on providers
parent
3f61f676
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
5 deletions
+41
-5
dht.go
dht.go
+12
-4
routing.go
routing.go
+29
-1
No files found.
dht.go
View file @
1082edeb
...
...
@@ -35,6 +35,7 @@ type IpfsDHT struct {
// Map keys to peers that can provide their value
// TODO: implement a TTL on each of these keys
providers
map
[
u
.
Key
][]
*
peer
.
Peer
providerLock
sync
.
RWMutex
// map of channels waiting for reply messages
listeners
map
[
uint64
]
chan
*
swarm
.
Message
...
...
@@ -46,6 +47,9 @@ type IpfsDHT struct {
// Create a new DHT object with the given peer as the 'local' host
func
NewDHT
(
p
*
peer
.
Peer
)
(
*
IpfsDHT
,
error
)
{
if
p
==
nil
{
panic
(
"Tried to create new dht with nil peer"
)
}
network
:=
swarm
.
NewSwarm
(
p
)
err
:=
network
.
Listen
()
if
err
!=
nil
{
...
...
@@ -68,24 +72,27 @@ func (dht *IpfsDHT) Start() {
}
// Connect to a new peer at the given address
func
(
dht
*
IpfsDHT
)
Connect
(
addr
*
ma
.
Multiaddr
)
error
{
func
(
dht
*
IpfsDHT
)
Connect
(
addr
*
ma
.
Multiaddr
)
(
*
peer
.
Peer
,
error
)
{
if
addr
==
nil
{
panic
(
"addr was nil!"
)
}
peer
:=
new
(
peer
.
Peer
)
peer
.
AddAddress
(
addr
)
conn
,
err
:=
swarm
.
Dial
(
"tcp"
,
peer
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
err
=
identify
.
Handshake
(
dht
.
self
,
peer
,
conn
.
Incoming
.
MsgChan
,
conn
.
Outgoing
.
MsgChan
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
dht
.
network
.
StartConn
(
conn
)
dht
.
routes
.
Update
(
peer
)
return
nil
return
peer
,
nil
}
// Read in all messages from swarm and handle them appropriately
...
...
@@ -195,6 +202,7 @@ func (dht *IpfsDHT) handleGetProviders(p *peer.Peer, pmes *DHTMessage) {
// ?????
}
// This is just a quick hack, formalize method of sending addrs later
var
addrs
[]
string
for
_
,
prov
:=
range
providers
{
ma
:=
prov
.
NetAddress
(
"tcp"
)
...
...
routing.go
View file @
1082edeb
...
...
@@ -3,9 +3,12 @@ package dht
import
(
"math/rand"
"time"
"encoding/json"
proto
"code.google.com/p/goprotobuf/proto"
ma
"github.com/jbenet/go-multiaddr"
peer
"github.com/jbenet/go-ipfs/peer"
swarm
"github.com/jbenet/go-ipfs/swarm"
u
"github.com/jbenet/go-ipfs/util"
...
...
@@ -125,7 +128,32 @@ func (s *IpfsDHT) FindProviders(key u.Key, timeout time.Duration) ([]*peer.Peer,
if
err
!=
nil
{
return
nil
,
err
}
panic
(
"Not yet implemented."
)
var
addrs
map
[
string
]
string
err
:=
json
.
Unmarshal
(
pmes_out
.
GetValue
(),
&
addrs
)
if
err
!=
nil
{
return
nil
,
err
}
for
key
,
addr
:=
range
addrs
{
p
:=
s
.
network
.
Find
(
u
.
Key
(
key
))
if
p
==
nil
{
maddr
,
err
:=
ma
.
NewMultiaddr
(
addr
)
if
err
!=
nil
{
u
.
PErr
(
"error connecting to new peer: %s"
,
err
)
continue
}
p
,
err
:=
s
.
Connect
(
maddr
)
if
err
!=
nil
{
u
.
PErr
(
"error connecting to new peer: %s"
,
err
)
continue
}
}
s
.
providerLock
.
Lock
()
prov_arr
:=
s
.
providers
[
key
]
s
.
providers
[
key
]
=
append
(
prov_arr
,
p
)
s
.
providerLock
.
Unlock
()
}
}
}
...
...
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