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-swarm
Commits
8d6fa244
Commit
8d6fa244
authored
Nov 03, 2016
by
Jeromy Johnson
Committed by
GitHub
Nov 03, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from libp2p/feat/stream-unity
move protocol methods down into peerstream
parents
368ca206
9fbfd0eb
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
44 additions
and
47 deletions
+44
-47
package.json
package.json
+8
-8
swarm.go
swarm.go
+5
-10
swarm_conn.go
swarm_conn.go
+12
-2
swarm_listen.go
swarm_listen.go
+1
-1
swarm_net_test.go
swarm_net_test.go
+9
-0
swarm_stream.go
swarm_stream.go
+9
-26
No files found.
package.json
View file @
8d6fa244
...
...
@@ -9,9 +9,9 @@
"gxDependencies"
:
[
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qmd
XimY9QHaasZmw6hWojWnCJvfgxETjZQfg9g6ZrA9wMX
"
,
"hash"
:
"Qmd
ysBu77i3YaagNtMAjiCJdeWWvds18ho5XEB784guQ41
"
,
"name"
:
"go-libp2p-net"
,
"version"
:
"1.
2.1
"
"version"
:
"1.
5.0
"
},
{
"author"
:
"whyrusleeping"
,
...
...
@@ -69,21 +69,21 @@
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
RmFKJgjjQhrT1uDyhpS87kE5M9YbMT8RBWam5uk8o4uH
"
,
"hash"
:
"Qm
S9en3mcwW2HRSeRabceJEGVxTZF4vEeFm7JHWQwWsb1U
"
,
"name"
:
"go-peerstream"
,
"version"
:
"1.
1.0
"
"version"
:
"1.
4.1
"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
WpTXhTkpoCDEm9twJd5Rc9jFwy61emzxneeJzrVMfjGF
"
,
"hash"
:
"Qm
VcNzHewFvmVah1CGqg8NV7nHHsPu19U43YE5b2oqWyBp
"
,
"name"
:
"go-libp2p-metrics"
,
"version"
:
"1.
2
.0"
"version"
:
"1.
5
.0"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
diBXu57LDzaoovc89jypRSNEhxVZ4zRig2AsXm9VwrsS
"
,
"hash"
:
"Qm
eFCCpGjB663gcLVS7Kz6ZSXyFgx5aJwQu27xkwyWdWiG
"
,
"name"
:
"go-libp2p-conn"
,
"version"
:
"1.
2.1
"
"version"
:
"1.
4.0
"
},
{
"author"
:
"whyrusleeping"
,
...
...
swarm.go
View file @
8d6fa244
...
...
@@ -12,7 +12,6 @@ import (
"time"
logging
"github.com/ipfs/go-log"
ps
"github.com/jbenet/go-peerstream"
pst
"github.com/jbenet/go-stream-muxer"
"github.com/jbenet/goprocess"
goprocessctx
"github.com/jbenet/goprocess/context"
...
...
@@ -26,6 +25,7 @@ import (
pstore
"github.com/libp2p/go-libp2p-peerstore"
transport
"github.com/libp2p/go-libp2p-transport"
filter
"github.com/libp2p/go-maddr-filter"
ps
"github.com/libp2p/go-peerstream"
tcpt
"github.com/libp2p/go-tcp-transport"
ma
"github.com/multiformats/go-multiaddr"
psmss
"github.com/whyrusleeping/go-smux-multistream"
...
...
@@ -256,7 +256,7 @@ func (s *Swarm) SetConnHandler(handler ConnHandler) {
// See peerstream.
func
(
s
*
Swarm
)
SetStreamHandler
(
handler
inet
.
StreamHandler
)
{
s
.
swarm
.
SetStreamHandler
(
func
(
s
*
ps
.
Stream
)
{
handler
(
wrap
Stream
(
s
))
handler
(
(
*
Stream
)
(
s
))
})
}
...
...
@@ -273,12 +273,7 @@ func (s *Swarm) NewStreamWithPeer(ctx context.Context, p peer.ID) (*Stream, erro
// TODO: think about passing a context down to NewStreamWithGroup
st
,
err
:=
s
.
swarm
.
NewStreamWithGroup
(
p
)
return
wrapStream
(
st
),
err
}
// StreamsWithPeer returns all the live Streams to p
func
(
s
*
Swarm
)
StreamsWithPeer
(
p
peer
.
ID
)
[]
*
Stream
{
return
wrapStreams
(
ps
.
StreamsWithGroup
(
p
,
s
.
swarm
.
Streams
()))
return
(
*
Stream
)(
st
),
err
}
// ConnectionsToPeer returns all the live connections to p
...
...
@@ -387,9 +382,9 @@ func (n *ps2netNotifee) Disconnected(c *ps.Conn) {
}
func
(
n
*
ps2netNotifee
)
OpenedStream
(
s
*
ps
.
Stream
)
{
n
.
not
.
OpenedStream
(
n
.
net
,
&
Stream
{
stream
:
s
}
)
n
.
not
.
OpenedStream
(
n
.
net
,
(
*
Stream
)(
s
)
)
}
func
(
n
*
ps2netNotifee
)
ClosedStream
(
s
*
ps
.
Stream
)
{
n
.
not
.
ClosedStream
(
n
.
net
,
&
Stream
{
stream
:
s
}
)
n
.
not
.
ClosedStream
(
n
.
net
,
(
*
Stream
)(
s
)
)
}
swarm_conn.go
View file @
8d6fa244
...
...
@@ -4,11 +4,11 @@ import (
"context"
"fmt"
ps
"github.com/jbenet/go-peerstream"
ic
"github.com/libp2p/go-libp2p-crypto"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
inet
"github.com/libp2p/go-libp2p-net"
peer
"github.com/libp2p/go-libp2p-peer"
ps
"github.com/libp2p/go-peerstream"
ma
"github.com/multiformats/go-multiaddr"
)
...
...
@@ -77,7 +77,7 @@ func (c *Conn) RemotePublicKey() ic.PubKey {
// NewSwarmStream returns a new Stream from this connection
func
(
c
*
Conn
)
NewSwarmStream
()
(
*
Stream
,
error
)
{
s
,
err
:=
c
.
StreamConn
()
.
NewStream
()
return
wrap
Stream
(
s
),
err
return
(
*
Stream
)
(
s
),
err
}
// NewStream returns a new Stream from this connection
...
...
@@ -91,6 +91,16 @@ func (c *Conn) Close() error {
return
c
.
StreamConn
()
.
Close
()
}
func
(
c
*
Conn
)
GetStreams
()
([]
inet
.
Stream
,
error
)
{
ss
:=
c
.
StreamConn
()
.
Streams
()
out
:=
make
([]
inet
.
Stream
,
len
(
ss
))
for
i
,
s
:=
range
ss
{
out
[
i
]
=
(
*
Stream
)(
s
)
}
return
out
,
nil
}
func
wrapConn
(
psc
*
ps
.
Conn
)
(
*
Conn
,
error
)
{
// grab the underlying connection.
if
_
,
ok
:=
psc
.
NetConn
()
.
(
iconn
.
Conn
);
!
ok
{
...
...
swarm_listen.go
View file @
8d6fa244
...
...
@@ -4,13 +4,13 @@ import (
"context"
"fmt"
ps
"github.com/jbenet/go-peerstream"
conn
"github.com/libp2p/go-libp2p-conn"
iconn
"github.com/libp2p/go-libp2p-interface-conn"
lgbl
"github.com/libp2p/go-libp2p-loggables"
mconn
"github.com/libp2p/go-libp2p-metrics/conn"
inet
"github.com/libp2p/go-libp2p-net"
transport
"github.com/libp2p/go-libp2p-transport"
ps
"github.com/libp2p/go-peerstream"
ma
"github.com/multiformats/go-multiaddr"
)
...
...
swarm_net_test.go
View file @
8d6fa244
...
...
@@ -165,6 +165,15 @@ func TestNetworkOpenStream(t *testing.T) {
t
.
Fatal
(
err
)
}
streams
,
err
:=
nets
[
0
]
.
ConnsToPeer
(
nets
[
1
]
.
LocalPeer
())[
0
]
.
GetStreams
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
streams
)
!=
1
{
t
.
Fatal
(
"should only have one stream there"
)
}
_
,
err
=
s
.
Write
([]
byte
(
"hello ipfs"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
swarm_stream.go
View file @
8d6fa244
...
...
@@ -4,19 +4,16 @@ import (
inet
"github.com/libp2p/go-libp2p-net"
protocol
"github.com/libp2p/go-libp2p-protocol"
ps
"github.com/
jbenet
/go-peerstream"
ps
"github.com/
libp2p
/go-peerstream"
)
// Stream is a wrapper around a ps.Stream that exposes a way to get
// our Conn and Swarm (instead of just the ps.Conn and ps.Swarm)
type
Stream
struct
{
stream
*
ps
.
Stream
protocol
protocol
.
ID
}
type
Stream
ps
.
Stream
// Stream returns the underlying peerstream.Stream
func
(
s
*
Stream
)
Stream
()
*
ps
.
Stream
{
return
s
.
s
tream
return
(
*
p
s
.
S
tream
)(
s
)
}
// Conn returns the Conn associated with this Stream, as an inet.Conn
...
...
@@ -26,43 +23,29 @@ func (s *Stream) Conn() inet.Conn {
// SwarmConn returns the Conn associated with this Stream, as a *Conn
func
(
s
*
Stream
)
SwarmConn
()
*
Conn
{
return
(
*
Conn
)(
s
.
s
tream
.
Conn
())
return
(
*
Conn
)(
s
.
S
tream
()
.
Conn
())
}
// Read reads bytes from a stream.
func
(
s
*
Stream
)
Read
(
p
[]
byte
)
(
n
int
,
err
error
)
{
return
s
.
s
tream
.
Read
(
p
)
return
s
.
S
tream
()
.
Read
(
p
)
}
// Write writes bytes to a stream, flushing for each call.
func
(
s
*
Stream
)
Write
(
p
[]
byte
)
(
n
int
,
err
error
)
{
return
s
.
s
tream
.
Write
(
p
)
return
s
.
S
tream
()
.
Write
(
p
)
}
// Close closes the stream, indicating this side is finished
// with the stream.
func
(
s
*
Stream
)
Close
()
error
{
return
s
.
s
tream
.
Close
()
return
s
.
S
tream
()
.
Close
()
}
func
(
s
*
Stream
)
Protocol
()
protocol
.
ID
{
return
s
.
p
rotocol
return
(
*
ps
.
Stream
)(
s
)
.
P
rotocol
()
}
func
(
s
*
Stream
)
SetProtocol
(
p
protocol
.
ID
)
{
s
.
protocol
=
p
}
func
wrapStream
(
pss
*
ps
.
Stream
)
*
Stream
{
return
&
Stream
{
stream
:
pss
,
}
}
func
wrapStreams
(
st
[]
*
ps
.
Stream
)
[]
*
Stream
{
out
:=
make
([]
*
Stream
,
len
(
st
))
for
i
,
s
:=
range
st
{
out
[
i
]
=
wrapStream
(
s
)
}
return
out
(
*
ps
.
Stream
)(
s
)
.
SetProtocol
(
p
)
}
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