From 57b790334720f920c1bcb1513620fd88497690d9 Mon Sep 17 00:00:00 2001 From: Juan Batiz-Benet Date: Mon, 29 Dec 2014 19:38:13 -0800 Subject: [PATCH] combined protocol and mux --- protocol/{mux => }/mux.go | 15 +++++++-------- protocol/{mux => }/mux_test.go | 9 ++++----- 2 files changed, 11 insertions(+), 13 deletions(-) rename protocol/{mux => }/mux.go (90%) rename protocol/{mux => }/mux_test.go (85%) diff --git a/protocol/mux/mux.go b/protocol/mux.go similarity index 90% rename from protocol/mux/mux.go rename to protocol/mux.go index c0455396e..3eaa8cd11 100644 --- a/protocol/mux/mux.go +++ b/protocol/mux.go @@ -1,4 +1,4 @@ -package mux +package protocol import ( "fmt" @@ -8,14 +8,13 @@ import ( context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" inet "github.com/jbenet/go-ipfs/p2p/net" - protocol "github.com/jbenet/go-ipfs/p2p/protocol" eventlog "github.com/jbenet/go-ipfs/util/eventlog" lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables" ) var log = eventlog.Logger("net/mux") -type StreamHandlerMap map[protocol.ID]inet.StreamHandler +type StreamHandlerMap map[ID]inet.StreamHandler // Mux provides simple stream multixplexing. // It helps you precisely when: @@ -35,9 +34,9 @@ type Mux struct { } // Protocols returns the list of protocols this muxer has handlers for -func (m *Mux) Protocols() []protocol.ID { +func (m *Mux) Protocols() []ID { m.RLock() - l := make([]protocol.ID, 0, len(m.Handlers)) + l := make([]ID, 0, len(m.Handlers)) for p := range m.Handlers { l = append(l, p) } @@ -47,9 +46,9 @@ func (m *Mux) Protocols() []protocol.ID { // readHeader reads the stream and returns the next Handler function // according to the muxer encoding. -func (m *Mux) readHeader(s io.Reader) (protocol.ID, inet.StreamHandler, error) { +func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) { // log.Error("ReadProtocolHeader") - p, err := protocol.ReadHeader(s) + p, err := ReadHeader(s) if err != nil { return "", nil, err } @@ -78,7 +77,7 @@ func (m *Mux) String() string { // SetHandler sets the protocol handler on the Network's Muxer. // This operation is threadsafe. -func (m *Mux) SetHandler(p protocol.ID, h inet.StreamHandler) { +func (m *Mux) SetHandler(p ID, h inet.StreamHandler) { log.Debugf("%s setting handler for protocol: %s (%d)", m, p, len(p)) m.Lock() m.Handlers[p] = h diff --git a/protocol/mux/mux_test.go b/protocol/mux_test.go similarity index 85% rename from protocol/mux/mux_test.go rename to protocol/mux_test.go index ff98b817c..dfac205da 100644 --- a/protocol/mux/mux_test.go +++ b/protocol/mux_test.go @@ -1,11 +1,10 @@ -package mux +package protocol import ( "bytes" "testing" inet "github.com/jbenet/go-ipfs/p2p/net" - protocol "github.com/jbenet/go-ipfs/p2p/protocol" ) var testCases = map[string]string{ @@ -18,7 +17,7 @@ var testCases = map[string]string{ func TestWrite(t *testing.T) { for k, v := range testCases { var buf bytes.Buffer - if err := protocol.WriteHeader(&buf, protocol.ID(k)); err != nil { + if err := WriteHeader(&buf, ID(k)); err != nil { t.Fatal(err) } @@ -53,13 +52,13 @@ func TestHandler(t *testing.T) { continue } - name, err := protocol.ReadHeader(&buf) + name, err := ReadHeader(&buf) if err != nil { t.Error(err) continue } - if name != protocol.ID(k) { + if name != ID(k) { t.Errorf("name mismatch: %s != %s", k, name) continue }