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
53f0b117
Commit
53f0b117
authored
Sep 26, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update net with peerstore
parent
0817ffa3
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
50 additions
and
23 deletions
+50
-23
core/core.go
core/core.go
+1
-1
net/conn/conn.go
net/conn/conn.go
+6
-11
net/net.go
net/net.go
+2
-2
net/swarm/conn.go
net/swarm/conn.go
+25
-6
net/swarm/swarm.go
net/swarm/swarm.go
+15
-2
routing/dht/dht_test.go
routing/dht/dht_test.go
+1
-1
No files found.
core/core.go
View file @
53f0b117
...
...
@@ -106,7 +106,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
return
nil
,
err
}
net
,
err
=
inet
.
NewIpfsNetwork
(
context
.
TODO
(),
local
,
&
mux
.
ProtocolMap
{
net
,
err
=
inet
.
NewIpfsNetwork
(
context
.
TODO
(),
local
,
peerstore
,
&
mux
.
ProtocolMap
{
mux
.
ProtocolID_Routing
:
dhtService
,
mux
.
ProtocolID_Exchange
:
exchangeService
,
})
...
...
net/conn/conn.go
View file @
53f0b117
...
...
@@ -48,17 +48,6 @@ func NewConn(peer *peer.Peer, addr *ma.Multiaddr, nconn net.Conn) (*Conn, error)
return
conn
,
nil
}
// NewNetConn constructs a new connection with given net.Conn
func
NewNetConn
(
nconn
net
.
Conn
)
(
*
Conn
,
error
)
{
addr
,
err
:=
ma
.
FromNetAddr
(
nconn
.
RemoteAddr
())
if
err
!=
nil
{
return
nil
,
err
}
return
NewConn
(
new
(
peer
.
Peer
),
addr
,
nconn
)
}
// Dial connects to a particular peer, over a given network
// Example: Dial("udp", peer)
func
Dial
(
network
string
,
peer
*
peer
.
Peer
)
(
*
Conn
,
error
)
{
...
...
@@ -112,3 +101,9 @@ func (c *Conn) Close() error {
c
.
Closed
<-
true
return
err
}
// NetConnMultiaddr returns the net.Conn's address, recast as a multiaddr.
// (consider moving this directly into the multiaddr package)
func
NetConnMultiaddr
(
nconn
net
.
Conn
)
(
*
ma
.
Multiaddr
,
error
)
{
return
ma
.
FromNetAddr
(
nconn
.
RemoteAddr
())
}
net/net.go
View file @
53f0b117
...
...
@@ -30,7 +30,7 @@ type IpfsNetwork struct {
// NewIpfsNetwork is the structure that implements the network interface
func
NewIpfsNetwork
(
ctx
context
.
Context
,
local
*
peer
.
Peer
,
pmap
*
mux
.
ProtocolMap
)
(
*
IpfsNetwork
,
error
)
{
peers
peer
.
Peerstore
,
pmap
*
mux
.
ProtocolMap
)
(
*
IpfsNetwork
,
error
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
...
...
@@ -47,7 +47,7 @@ func NewIpfsNetwork(ctx context.Context, local *peer.Peer,
return
nil
,
err
}
in
.
swarm
,
err
=
swarm
.
NewSwarm
(
ctx
,
local
)
in
.
swarm
,
err
=
swarm
.
NewSwarm
(
ctx
,
local
,
peers
)
if
err
!=
nil
{
cancel
()
return
nil
,
err
...
...
net/swarm/conn.go
View file @
53f0b117
...
...
@@ -76,15 +76,19 @@ func (s *Swarm) connListen(maddr *ma.Multiaddr) error {
// Handle getting ID from this peer, handshake, and adding it into the map
func
(
s
*
Swarm
)
handleIncomingConn
(
nconn
net
.
Conn
)
{
c
,
err
:=
conn
.
New
NetConn
(
nconn
)
addr
,
err
:=
conn
.
NetConn
Multiaddr
(
nconn
)
if
err
!=
nil
{
s
.
errChan
<-
err
return
}
//TODO(jbenet) the peer might potentially already be in the global PeerBook.
// maybe use the handshake to populate peer.
c
.
Peer
.
AddAddress
(
c
.
Addr
)
// Construct conn with nil peer for now, because we don't know its ID yet.
// connSetup will figure this out, and pull out / construct the peer.
c
,
err
:=
conn
.
NewConn
(
nil
,
addr
,
nconn
)
if
err
!=
nil
{
s
.
errChan
<-
err
return
}
// Setup the new connection
err
=
s
.
connSetup
(
c
)
...
...
@@ -101,7 +105,11 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
return
errors
.
New
(
"Tried to start nil connection."
)
}
u
.
DOut
(
"Starting connection: %s
\n
"
,
c
.
Peer
.
Key
()
.
Pretty
())
if
c
.
Peer
!=
nil
{
u
.
DOut
(
"Starting connection: %s
\n
"
,
c
.
Peer
.
Key
()
.
Pretty
())
}
else
{
u
.
DOut
(
"Starting connection: [unknown peer]
\n
"
)
}
if
err
:=
s
.
connSecure
(
c
);
err
!=
nil
{
return
fmt
.
Errorf
(
"Conn securing error: %v"
,
err
)
...
...
@@ -109,6 +117,10 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
u
.
DOut
(
"Secured connection: %s
\n
"
,
c
.
Peer
.
Key
()
.
Pretty
())
//TODO(jbenet) the peer might potentially already be in the global PeerBook.
// maybe use the handshake to populate peer.
c
.
Peer
.
AddAddress
(
c
.
Addr
)
// add to conns
s
.
connsLock
.
Lock
()
if
_
,
ok
:=
s
.
conns
[
c
.
Peer
.
Key
()];
ok
{
...
...
@@ -126,7 +138,7 @@ func (s *Swarm) connSetup(c *conn.Conn) error {
// connSecure setups a secure remote connection.
func
(
s
*
Swarm
)
connSecure
(
c
*
conn
.
Conn
)
error
{
sp
,
err
:=
spipe
.
NewSecurePipe
(
s
.
ctx
,
10
,
s
.
local
,
c
.
P
eer
)
sp
,
err
:=
spipe
.
NewSecurePipe
(
s
.
ctx
,
10
,
s
.
local
,
s
.
p
eer
s
)
if
err
!=
nil
{
return
err
}
...
...
@@ -139,6 +151,13 @@ func (s *Swarm) connSecure(c *conn.Conn) error {
return
err
}
if
c
.
Peer
==
nil
{
c
.
Peer
=
sp
.
RemotePeer
()
}
else
if
c
.
Peer
!=
sp
.
RemotePeer
()
{
panic
(
"peers not being constructed correctly."
)
}
c
.
Secure
=
sp
return
nil
}
...
...
net/swarm/swarm.go
View file @
53f0b117
...
...
@@ -46,6 +46,9 @@ type Swarm struct {
// local is the peer this swarm represents
local
*
peer
.
Peer
// peers is a collection of peers for swarm to use
peers
peer
.
Peerstore
// Swarm includes a Pipe object.
*
msg
.
Pipe
...
...
@@ -65,11 +68,12 @@ type Swarm struct {
}
// NewSwarm constructs a Swarm, with a Chan.
func
NewSwarm
(
ctx
context
.
Context
,
local
*
peer
.
Peer
)
(
*
Swarm
,
error
)
{
func
NewSwarm
(
ctx
context
.
Context
,
local
*
peer
.
Peer
,
ps
peer
.
Peerstore
)
(
*
Swarm
,
error
)
{
s
:=
&
Swarm
{
Pipe
:
msg
.
NewPipe
(
10
),
conns
:
conn
.
Map
{},
local
:
local
,
peers
:
ps
,
errChan
:
make
(
chan
error
,
100
),
}
...
...
@@ -112,9 +116,18 @@ func (s *Swarm) Dial(peer *peer.Peer) (*conn.Conn, error) {
// check if we already have an open connection first
c
:=
s
.
GetConnection
(
peer
.
ID
)
if
c
!=
nil
{
return
c
,
nil
}
// check if we don't have the peer in Peerstore
err
:=
s
.
peers
.
Put
(
peer
)
if
err
!=
nil
{
return
nil
,
err
}
// open connection to peer
c
,
err
:
=
conn
.
Dial
(
"tcp"
,
peer
)
c
,
err
=
conn
.
Dial
(
"tcp"
,
peer
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
routing/dht/dht_test.go
View file @
53f0b117
...
...
@@ -31,7 +31,7 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
t
.
Fatal
(
err
)
}
net
,
err
:=
inet
.
NewIpfsNetwork
(
ctx
,
p
,
&
mux
.
ProtocolMap
{
net
,
err
:=
inet
.
NewIpfsNetwork
(
ctx
,
p
,
peerstore
,
&
mux
.
ProtocolMap
{
mux
.
ProtocolID_Routing
:
dhts
,
})
if
err
!=
nil
{
...
...
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