Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
ac26b705
Commit
ac26b705
authored
Jan 14, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #547 from jbenet/fix/mux-privatize
fix: privatize Mux fields
parents
abe6cad3
c341df0a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
41 additions
and
31 deletions
+41
-31
p2p/host/basic/basic_host.go
p2p/host/basic/basic_host.go
+3
-3
p2p/protocol/mux.go
p2p/protocol/mux.go
+33
-23
p2p/protocol/mux_test.go
p2p/protocol/mux_test.go
+5
-5
No files found.
p2p/host/basic/basic_host.go
View file @
ac26b705
...
...
@@ -16,7 +16,7 @@ var log = eventlog.Logger("p2p/host/basic")
type
BasicHost
struct
{
network
inet
.
Network
mux
protocol
.
Mux
mux
*
protocol
.
Mux
ids
*
identify
.
IDService
relay
*
relay
.
RelayService
}
...
...
@@ -25,7 +25,7 @@ type BasicHost struct {
func
New
(
net
inet
.
Network
)
*
BasicHost
{
h
:=
&
BasicHost
{
network
:
net
,
mux
:
protocol
.
Mux
{
Handlers
:
protocol
.
StreamHandlerMap
{}}
,
mux
:
protocol
.
NewMux
()
,
}
// setup host services
...
...
@@ -65,7 +65,7 @@ func (h *BasicHost) Network() inet.Network {
// Mux returns the Mux multiplexing incoming streams to protocol handlers
func
(
h
*
BasicHost
)
Mux
()
*
protocol
.
Mux
{
return
&
h
.
mux
return
h
.
mux
}
func
(
h
*
BasicHost
)
IDService
()
*
identify
.
IDService
{
...
...
p2p/protocol/mux.go
View file @
ac26b705
...
...
@@ -14,7 +14,7 @@ import (
var
log
=
eventlog
.
Logger
(
"net/mux"
)
type
S
treamHandlerMap
map
[
ID
]
inet
.
StreamHandler
type
s
treamHandlerMap
map
[
ID
]
inet
.
StreamHandler
// Mux provides simple stream multixplexing.
// It helps you precisely when:
...
...
@@ -23,24 +23,28 @@ type StreamHandlerMap map[ID]inet.StreamHandler
//
// It contains the handlers for each protocol accepted.
// 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
{
Default
inet
.
StreamHandler
// handles unknown protocols.
Handlers
StreamHandlerMap
// defaultHandler handles unknown protocols. Callers modify at your own risk.
defaultHandler
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
func
(
m
*
Mux
)
Protocols
()
[]
ID
{
m
.
RLock
()
l
:=
make
([]
ID
,
0
,
len
(
m
.
H
andlers
))
for
p
:=
range
m
.
H
andlers
{
m
.
lock
.
RLock
()
l
:=
make
([]
ID
,
0
,
len
(
m
.
h
andlers
))
for
p
:=
range
m
.
h
andlers
{
l
=
append
(
l
,
p
)
}
m
.
RUnlock
()
m
.
lock
.
RUnlock
()
return
l
}
...
...
@@ -54,14 +58,14 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
}
// log.Debug("readHeader got:", p)
m
.
RLock
()
h
,
found
:=
m
.
H
andlers
[
p
]
m
.
RUnlock
()
m
.
lock
.
RLock
()
h
,
found
:=
m
.
h
andlers
[
p
]
m
.
lock
.
RUnlock
()
switch
{
case
!
found
&&
m
.
D
efault
!=
nil
:
return
p
,
m
.
D
efault
,
nil
case
!
found
&&
m
.
D
efault
==
nil
:
case
!
found
&&
m
.
d
efault
Handler
!=
nil
:
return
p
,
m
.
d
efault
Handler
,
nil
case
!
found
&&
m
.
d
efault
Handler
==
nil
:
return
p
,
nil
,
fmt
.
Errorf
(
"%s no handler with name: %s (%d)"
,
m
,
p
,
len
(
p
))
default
:
return
p
,
h
,
nil
...
...
@@ -70,18 +74,24 @@ func (m *Mux) readHeader(s io.Reader) (ID, inet.StreamHandler, error) {
// String returns the muxer's printing representation
func
(
m
*
Mux
)
String
()
string
{
m
.
RLock
()
defer
m
.
RUnlock
()
return
fmt
.
Sprintf
(
"<Muxer %p %d>"
,
m
,
len
(
m
.
Handlers
))
m
.
lock
.
RLock
()
defer
m
.
lock
.
RUnlock
()
return
fmt
.
Sprintf
(
"<Muxer %p %d>"
,
m
,
len
(
m
.
handlers
))
}
func
(
m
*
Mux
)
SetDefaultHandler
(
h
inet
.
StreamHandler
)
{
m
.
lock
.
Lock
()
m
.
defaultHandler
=
h
m
.
lock
.
Unlock
()
}
// SetHandler sets the protocol handler on the Network's Muxer.
// This operation is threadsafe.
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
.
H
andlers
[
p
]
=
h
m
.
Unlock
()
m
.
lock
.
Lock
()
m
.
h
andlers
[
p
]
=
h
m
.
lock
.
Unlock
()
}
// Handle reads the next name off the Stream, and calls a handler function
...
...
p2p/protocol/mux_test.go
View file @
ac26b705
...
...
@@ -38,12 +38,12 @@ func TestHandler(t *testing.T) {
}
}
m
:=
Mux
{
Handlers
:
StreamHandlerMap
{}}
m
.
Default
=
h
(
"default"
)
m
.
Handler
s
[
"/dht"
]
=
h
(
"bitswap"
)
m
:=
NewMux
()
m
.
Set
Default
Handler
(
h
(
"default"
)
)
m
.
Set
Handler
(
"/dht"
,
h
(
"bitswap"
)
)
// m.Handlers["/ipfs"] = h("bitswap") // default!
m
.
Handler
s
[
"/bitswap"
]
=
h
(
"bitswap"
)
m
.
Handler
s
[
"/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"
]
=
h
(
"bitswap"
)
m
.
Set
Handler
(
"/bitswap"
,
h
(
"bitswap"
)
)
m
.
Set
Handler
(
"/ipfs/dksnafkasnfkdajfkdajfdsjadosiaaodj"
,
h
(
"bitswap"
)
)
for
k
,
v
:=
range
testCases
{
var
buf
bytes
.
Buffer
...
...
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