Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
1f0bbc47
Commit
1f0bbc47
authored
Feb 15, 2015
by
Jeromy
Browse files
allow removal of stream handlers
parent
32810650
Changes
4
Hide whitespace changes
Inline
Side-by-side
host/basic/basic_host.go
View file @
1f0bbc47
...
@@ -118,6 +118,10 @@ func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler
...
@@ -118,6 +118,10 @@ func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler
h
.
Mux
()
.
SetHandler
(
pid
,
handler
)
h
.
Mux
()
.
SetHandler
(
pid
,
handler
)
}
}
func
(
h
*
BasicHost
)
RemoveStreamHandler
(
pid
protocol
.
ID
)
{
h
.
Mux
()
.
RemoveHandler
(
pid
)
}
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts
// header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// to create one. If ProtocolID is "", writes no header.
...
...
host/host.go
View file @
1f0bbc47
...
@@ -46,6 +46,10 @@ type Host interface {
...
@@ -46,6 +46,10 @@ type Host interface {
// (Threadsafe)
// (Threadsafe)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
// RemoveStreamHandler removes a handler on the mux that was set by
// SetStreamHandler
RemoveStreamHandler
(
pid
protocol
.
ID
)
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// NewStream opens a new stream to given peer p, and writes a p2p/protocol
// header with given protocol.ID. If there is no connection to p, attempts
// header with given protocol.ID. If there is no connection to p, attempts
// to create one. If ProtocolID is "", writes no header.
// to create one. If ProtocolID is "", writes no header.
...
...
host/routed/routed.go
View file @
1f0bbc47
...
@@ -84,21 +84,31 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e
...
@@ -84,21 +84,31 @@ func logRoutingErrDifferentPeers(ctx context.Context, wanted, got peer.ID, err e
func
(
rh
*
RoutedHost
)
ID
()
peer
.
ID
{
func
(
rh
*
RoutedHost
)
ID
()
peer
.
ID
{
return
rh
.
host
.
ID
()
return
rh
.
host
.
ID
()
}
}
func
(
rh
*
RoutedHost
)
Peerstore
()
peer
.
Peerstore
{
func
(
rh
*
RoutedHost
)
Peerstore
()
peer
.
Peerstore
{
return
rh
.
host
.
Peerstore
()
return
rh
.
host
.
Peerstore
()
}
}
func
(
rh
*
RoutedHost
)
Addrs
()
[]
ma
.
Multiaddr
{
func
(
rh
*
RoutedHost
)
Addrs
()
[]
ma
.
Multiaddr
{
return
rh
.
host
.
Addrs
()
return
rh
.
host
.
Addrs
()
}
}
func
(
rh
*
RoutedHost
)
Network
()
inet
.
Network
{
func
(
rh
*
RoutedHost
)
Network
()
inet
.
Network
{
return
rh
.
host
.
Network
()
return
rh
.
host
.
Network
()
}
}
func
(
rh
*
RoutedHost
)
Mux
()
*
protocol
.
Mux
{
func
(
rh
*
RoutedHost
)
Mux
()
*
protocol
.
Mux
{
return
rh
.
host
.
Mux
()
return
rh
.
host
.
Mux
()
}
}
func
(
rh
*
RoutedHost
)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
{
func
(
rh
*
RoutedHost
)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
{
rh
.
host
.
SetStreamHandler
(
pid
,
handler
)
rh
.
host
.
SetStreamHandler
(
pid
,
handler
)
}
}
func
(
rh
*
RoutedHost
)
RemoveStreamHandler
(
pid
protocol
.
ID
)
{
rh
.
host
.
RemoveStreamHandler
(
pid
)
}
func
(
rh
*
RoutedHost
)
NewStream
(
pid
protocol
.
ID
,
p
peer
.
ID
)
(
inet
.
Stream
,
error
)
{
func
(
rh
*
RoutedHost
)
NewStream
(
pid
protocol
.
ID
,
p
peer
.
ID
)
(
inet
.
Stream
,
error
)
{
return
rh
.
host
.
NewStream
(
pid
,
p
)
return
rh
.
host
.
NewStream
(
pid
,
p
)
}
}
...
...
protocol/mux.go
View file @
1f0bbc47
...
@@ -90,6 +90,15 @@ func (m *Mux) SetHandler(p ID, h inet.StreamHandler) {
...
@@ -90,6 +90,15 @@ func (m *Mux) SetHandler(p ID, h inet.StreamHandler) {
m
.
lock
.
Unlock
()
m
.
lock
.
Unlock
()
}
}
// RemoveHandler removes the protocol handler on the Network's Muxer.
// This operation is threadsafe.
func
(
m
*
Mux
)
RemoveHandler
(
p
ID
)
{
log
.
Debugf
(
"%s removing handler for protocol: %s (%d)"
,
m
,
p
,
len
(
p
))
m
.
lock
.
Lock
()
delete
(
m
.
handlers
,
p
)
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
// This is done in its own goroutine, to avoid blocking the caller.
// This is done in its own goroutine, to avoid blocking the caller.
func
(
m
*
Mux
)
Handle
(
s
inet
.
Stream
)
{
func
(
m
*
Mux
)
Handle
(
s
inet
.
Stream
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment