Browse Source

combined protocol and mux

pull/2/head
Juan Batiz-Benet 10 years ago
parent
commit
57b7903347
  1. 15
      protocol/mux.go
  2. 9
      protocol/mux_test.go

15
protocol/mux/mux.go → 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

9
protocol/mux/mux_test.go → 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
}
Loading…
Cancel
Save