Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b0fea98

Browse files
committedJul 6, 2015
make the relay service use msmux
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent fe23f76 commit b0fea98

File tree

4 files changed

+6
-245
lines changed

4 files changed

+6
-245
lines changed
 

‎p2p/protocol/mux.go

-142
This file was deleted.

‎p2p/protocol/mux_test.go

-67
This file was deleted.

‎p2p/protocol/protocol.go

-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
package protocol
22

3-
import (
4-
"io"
5-
6-
msgio "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
7-
)
8-
93
// ID is an identifier used to write protocol headers in streams.
104
type ID string
115

126
// These are reserved protocol.IDs.
137
const (
148
TestingID ID = "/p2p/_testing"
159
)
16-
17-
// WriteHeader writes a protocol.ID header to an io.Writer. This is so
18-
// multiple protocols can be multiplexed on top of the same transport.
19-
//
20-
// We use go-msgio varint encoding:
21-
// <varint length><string name>\n
22-
// (the varint includes the \n)
23-
func WriteHeader(w io.Writer, id ID) error {
24-
vw := msgio.NewVarintWriter(w)
25-
s := string(id) + "\n" // add \n
26-
return vw.WriteMsg([]byte(s))
27-
}
28-
29-
// ReadHeader reads a protocol.ID header from an io.Reader. This is so
30-
// multiple protocols can be multiplexed on top of the same transport.
31-
// See WriteHeader.
32-
func ReadHeader(r io.Reader) (ID, error) {
33-
vr := msgio.NewVarintReader(r)
34-
msg, err := vr.ReadMsg()
35-
if err != nil {
36-
return ID(""), err
37-
}
38-
msg = msg[:len(msg)-1] // remove \n
39-
return ID(msg), nil
40-
}

‎p2p/protocol/relay/relay_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
testutil "github.com/ipfs/go-ipfs/p2p/test/util"
1111
eventlog "github.com/ipfs/go-ipfs/thirdparty/eventlog"
1212

13+
msmux "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/go-multistream"
1314
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
1415
)
1516

@@ -62,7 +63,7 @@ func TestRelaySimple(t *testing.T) {
6263

6364
// ok now the header's there, we can write the next protocol header.
6465
log.Debug("write testing header")
65-
if err := protocol.WriteHeader(s, protocol.TestingID); err != nil {
66+
if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
6667
t.Fatal(err)
6768
}
6869

@@ -155,15 +156,15 @@ func TestRelayAcrossFour(t *testing.T) {
155156
}
156157

157158
log.Debugf("write relay header n1->n4 (%s -> %s)", n1p, n4p)
158-
if err := protocol.WriteHeader(s, relay.ID); err != nil {
159+
if err := msmux.SelectProtoOrFail(string(relay.ID), s); err != nil {
159160
t.Fatal(err)
160161
}
161162
if err := relay.WriteHeader(s, n1p, n4p); err != nil {
162163
t.Fatal(err)
163164
}
164165

165166
log.Debugf("write relay header n1->n5 (%s -> %s)", n1p, n5p)
166-
if err := protocol.WriteHeader(s, relay.ID); err != nil {
167+
if err := msmux.SelectProtoOrFail(string(relay.ID), s); err != nil {
167168
t.Fatal(err)
168169
}
169170
if err := relay.WriteHeader(s, n1p, n5p); err != nil {
@@ -172,7 +173,7 @@ func TestRelayAcrossFour(t *testing.T) {
172173

173174
// ok now the header's there, we can write the next protocol header.
174175
log.Debug("write testing header")
175-
if err := protocol.WriteHeader(s, protocol.TestingID); err != nil {
176+
if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
176177
t.Fatal(err)
177178
}
178179

@@ -257,7 +258,7 @@ func TestRelayStress(t *testing.T) {
257258

258259
// ok now the header's there, we can write the next protocol header.
259260
log.Debug("write testing header")
260-
if err := protocol.WriteHeader(s, protocol.TestingID); err != nil {
261+
if err := msmux.SelectProtoOrFail(string(protocol.TestingID), s); err != nil {
261262
t.Fatal(err)
262263
}
263264

0 commit comments

Comments
 (0)