Commit 57b79033 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet
Browse files

combined protocol and mux

parent a103a2da
package mux package protocol
import ( import (
"fmt" "fmt"
...@@ -8,14 +8,13 @@ import ( ...@@ -8,14 +8,13 @@ import (
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
inet "github.com/jbenet/go-ipfs/p2p/net" inet "github.com/jbenet/go-ipfs/p2p/net"
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
eventlog "github.com/jbenet/go-ipfs/util/eventlog" eventlog "github.com/jbenet/go-ipfs/util/eventlog"
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables" lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
) )
var log = eventlog.Logger("net/mux") var log = eventlog.Logger("net/mux")
type StreamHandlerMap map[protocol.ID]inet.StreamHandler type StreamHandlerMap map[ID]inet.StreamHandler
// Mux provides simple stream multixplexing. // Mux provides simple stream multixplexing.
// It helps you precisely when: // It helps you precisely when:
...@@ -35,9 +34,9 @@ type Mux struct { ...@@ -35,9 +34,9 @@ type Mux struct {
} }
// Protocols returns the list of protocols this muxer has handlers for // Protocols returns the list of protocols this muxer has handlers for
func (m *Mux) Protocols() []protocol.ID { func (m *Mux) Protocols() []ID {
m.RLock() m.RLock()
l := make([]protocol.ID, 0, len(m.Handlers)) l := make([]ID, 0, len(m.Handlers))
for p := range m.Handlers { for p := range m.Handlers {
l = append(l, p) l = append(l, p)
} }
...@@ -47,9 +46,9 @@ func (m *Mux) Protocols() []protocol.ID { ...@@ -47,9 +46,9 @@ func (m *Mux) Protocols() []protocol.ID {
// readHeader reads the stream and returns the next Handler function // readHeader reads the stream and returns the next Handler function
// according to the muxer encoding. // 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") // log.Error("ReadProtocolHeader")
p, err := protocol.ReadHeader(s) p, err := ReadHeader(s)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
...@@ -78,7 +77,7 @@ func (m *Mux) String() string { ...@@ -78,7 +77,7 @@ func (m *Mux) String() string {
// SetHandler sets the protocol handler on the Network's Muxer. // SetHandler sets the protocol handler on the Network's Muxer.
// This operation is threadsafe. // 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)) log.Debugf("%s setting handler for protocol: %s (%d)", m, p, len(p))
m.Lock() m.Lock()
m.Handlers[p] = h m.Handlers[p] = h
......
package mux package protocol
import ( import (
"bytes" "bytes"
"testing" "testing"
inet "github.com/jbenet/go-ipfs/p2p/net" inet "github.com/jbenet/go-ipfs/p2p/net"
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
) )
var testCases = map[string]string{ var testCases = map[string]string{
...@@ -18,7 +17,7 @@ var testCases = map[string]string{ ...@@ -18,7 +17,7 @@ var testCases = map[string]string{
func TestWrite(t *testing.T) { func TestWrite(t *testing.T) {
for k, v := range testCases { for k, v := range testCases {
var buf bytes.Buffer 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) t.Fatal(err)
} }
...@@ -53,13 +52,13 @@ func TestHandler(t *testing.T) { ...@@ -53,13 +52,13 @@ func TestHandler(t *testing.T) {
continue continue
} }
name, err := protocol.ReadHeader(&buf) name, err := ReadHeader(&buf)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
continue continue
} }
if name != protocol.ID(k) { if name != ID(k) {
t.Errorf("name mismatch: %s != %s", k, name) t.Errorf("name mismatch: %s != %s", k, name)
continue continue
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment