Commit 8bbcc999 authored by Brian Tiger Chow's avatar Brian Tiger Chow
Browse files

fix: privatize Mux fields

parent 83e6bbf5
...@@ -16,7 +16,7 @@ var log = eventlog.Logger("p2p/host/basic") ...@@ -16,7 +16,7 @@ var log = eventlog.Logger("p2p/host/basic")
type BasicHost struct { type BasicHost struct {
network inet.Network network inet.Network
mux protocol.Mux mux *protocol.Mux
ids *identify.IDService ids *identify.IDService
relay *relay.RelayService relay *relay.RelayService
} }
...@@ -25,7 +25,7 @@ type BasicHost struct { ...@@ -25,7 +25,7 @@ type BasicHost struct {
func New(net inet.Network) *BasicHost { func New(net inet.Network) *BasicHost {
h := &BasicHost{ h := &BasicHost{
network: net, network: net,
mux: protocol.Mux{Handlers: protocol.StreamHandlerMap{}}, mux: protocol.NewMux(),
} }
// setup host services // setup host services
...@@ -65,7 +65,7 @@ func (h *BasicHost) Network() inet.Network { ...@@ -65,7 +65,7 @@ func (h *BasicHost) Network() inet.Network {
// Mux returns the Mux multiplexing incoming streams to protocol handlers // Mux returns the Mux multiplexing incoming streams to protocol handlers
func (h *BasicHost) Mux() *protocol.Mux { func (h *BasicHost) Mux() *protocol.Mux {
return &h.mux return h.mux
} }
func (h *BasicHost) IDService() *identify.IDService { func (h *BasicHost) IDService() *identify.IDService {
......
...@@ -14,7 +14,7 @@ import ( ...@@ -14,7 +14,7 @@ import (
var log = eventlog.Logger("net/mux") var log = eventlog.Logger("net/mux")
type StreamHandlerMap map[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:
...@@ -23,24 +23,28 @@ type StreamHandlerMap map[ID]inet.StreamHandler ...@@ -23,24 +23,28 @@ type StreamHandlerMap map[ID]inet.StreamHandler
// //
// It contains the handlers for each protocol accepted. // It contains the handlers for each protocol accepted.
// It dispatches handlers for streams opened by remote peers. // It dispatches handlers for streams opened by remote peers.
//
// WARNING: this datastructure IS NOT threadsafe.
// do not modify it once the network is using it.
type Mux struct { type Mux struct {
Default inet.StreamHandler // handles unknown protocols. // Default handles unknown protocols. Callers modify at your own risk.
Handlers StreamHandlerMap Default inet.StreamHandler
lock sync.RWMutex
handlers streamHandlerMap
}
sync.RWMutex func NewMux() *Mux {
return &Mux{
handlers: streamHandlerMap{},
}
} }
// 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() []ID { func (m *Mux) Protocols() []ID {
m.RLock() m.lock.RLock()
l := make([]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)
} }
m.RUnlock() m.lock.RUnlock()
return l return l
} }
...@@ -54,9 +58,9 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) { ...@@ -54,9 +58,9 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
} }
// log.Debug("readHeader got:", p) // log.Debug("readHeader got:", p)
m.RLock() m.lock.RLock()
h, found := m.Handlers[p] h, found := m.handlers[p]
m.RUnlock() m.lock.RUnlock()
switch { switch {
case !found && m.Default != nil: case !found && m.Default != nil:
...@@ -70,18 +74,18 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) { ...@@ -70,18 +74,18 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
// String returns the muxer's printing representation // String returns the muxer's printing representation
func (m *Mux) String() string { func (m *Mux) String() string {
m.RLock() m.lock.RLock()
defer m.RUnlock() defer m.lock.RUnlock()
return fmt.Sprintf("<Muxer %p %d>", m, len(m.Handlers)) return fmt.Sprintf("<Muxer %p %d>", m, len(m.handlers))
} }
// 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 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.Lock()
m.Handlers[p] = h m.handlers[p] = h
m.Unlock() m.lock.Unlock()
} }
// Handle reads the next name off the Stream, and calls a handler function // Handle reads the next name off the Stream, and calls a handler function
......
...@@ -38,12 +38,12 @@ func TestHandler(t *testing.T) { ...@@ -38,12 +38,12 @@ func TestHandler(t *testing.T) {
} }
} }
m := Mux{Handlers: StreamHandlerMap{}} m := NewMux()
m.Default = h("default") m.Default = h("default")
m.Handlers["/dht"] = h("bitswap") m.SetHandler("/dht", h("bitswap"))
// m.Handlers["/ipfs"] = h("bitswap") // default! // m.Handlers["/ipfs"] = h("bitswap") // default!
m.Handlers["/bitswap"] = h("bitswap") m.SetHandler("/bitswap", h("bitswap"))
m.Handlers["/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"] = h("bitswap") m.SetHandler("/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj", h("bitswap"))
for k, v := range testCases { for k, v := range testCases {
var buf bytes.Buffer var buf bytes.Buffer
......
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