提交 f18bbde3 作者: Juan Batiz-Benet

net: StreamHandlerMap + protocol ids

上级 b039ebce
...@@ -11,6 +11,12 @@ import ( ...@@ -11,6 +11,12 @@ import (
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
) )
const (
ProtocolBitswap = "/ipfs/bitswap"
ProtocolDHT = "/ipfs/dht"
ProtocolDiag = "/ipfs/diagnostics"
)
// Stream represents a bidirectional channel between two agents in // Stream represents a bidirectional channel between two agents in
// the IPFS network. "agent" is as granular as desired, potentially // the IPFS network. "agent" is as granular as desired, potentially
// being a "request -> reply" pair, or whole protocols. // being a "request -> reply" pair, or whole protocols.
...@@ -28,6 +34,8 @@ type Stream interface { ...@@ -28,6 +34,8 @@ type Stream interface {
// incoming streams must implement. // incoming streams must implement.
type StreamHandler func(Stream) type StreamHandler func(Stream)
type StreamHandlerMap map[string]StreamHandler
// Conn is a connection to a remote peer. It multiplexes streams. // Conn is a connection to a remote peer. It multiplexes streams.
// Usually there is no need to use a Conn directly, but it may // Usually there is no need to use a Conn directly, but it may
// be useful to get information about the peer on the other side: // be useful to get information about the peer on the other side:
...@@ -58,7 +66,7 @@ type Conn interface { ...@@ -58,7 +66,7 @@ type Conn interface {
// do not modify it once the network is using it. // do not modify it once the network is using it.
type Mux struct { type Mux struct {
Default StreamHandler // handles unknown protocols. Default StreamHandler // handles unknown protocols.
Handlers map[string]StreamHandler Handlers StreamHandlerMap
} }
// Network is the interface IPFS uses for connecting to the world. // Network is the interface IPFS uses for connecting to the world.
......
...@@ -34,7 +34,7 @@ func TestHandler(t *testing.T) { ...@@ -34,7 +34,7 @@ func TestHandler(t *testing.T) {
} }
} }
m := Mux{Handlers: map[string]StreamHandler{}} m := Mux{Handlers: StreamHandlerMap{}}
m.Default = h("default") m.Default = h("default")
m.Handlers["dht"] = h("bitswap") m.Handlers["dht"] = h("bitswap")
// m.Handlers["ipfs"] = h("bitswap") // default! // m.Handlers["ipfs"] = h("bitswap") // default!
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论