Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
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
6635e3d7
Commit
6635e3d7
authored
8 years ago
by
Jeromy
Browse files
Options
Download
Email Patches
Plain Diff
p2p/host: expose multistream function matching and add semver func
parent
1868c67c
master
2018-Q4-OKR
docs-improvements
feat/backoff-listing
feat/p2p-multiaddr
feat/pnet/working3
feat/protobuf
feat/relay-integrate
feat/udp
feat/update/go-reuseport
feature/standardize-readme
fix/473
fix/no-custom-field
fix/reset-ping-stream
fix/revert-correct-external-addr
gx/update-jccl6u
gx/update-nza0mn
jenkinsfile
kevina/fix-go-vet
multistream-ping
punching
revert-276-update-go-detect-race
v6.0.23
v6.0.22
v6.0.21
v6.0.20
v6.0.19
v6.0.18
v6.0.17
v6.0.16
v6.0.15
v6.0.14
v6.0.13
v6.0.12
v6.0.11
v6.0.10
v6.0.9
v6.0.8
v6.0.7
v6.0.6
v6.0.5
v6.0.4
v6.0.3
v6.0.2
v6.0.1
v6.0.0
v5.0.21
v5.0.20
v5.0.19
v5.0.18
v5.0.17
v5.0.16
v5.0.15
v5.0.14
v5.0.13
v5.0.12
v5.0.11
v5.0.10
v5.0.9
v5.0.8
v5.0.7
v5.0.6
v5.0.5
v5.0.4
v5.0.3
v5.0.2
v5.0.1
v5.0.0
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.5
v4.4.4
v4.4.3
v4.4.2
v4.4.1
v4.4.0
v4.3.12
v4.3.11
v4.3.10
v4.3.9
v4.3.8
v4.3.7
v4.3.6
v4.3.5
v4.3.4
v4.3.3
v4.3.2
v4.3.1
v4.3.0
v4.2.0
v4.1.0
v4.0.4
v4.0.3
v4.0.2
v4.0.1
v4.0.0
v3.6.0
v3.5.4
v3.5.3
v3.5.2
v3.5.1
v3.5.0
v3.4.3
v3.4.2
v3.4.1
v3.4.0
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
p2p/host/basic/basic_host.go
+11
-0
p2p/host/basic/basic_host.go
p2p/host/host.go
+4
-0
p2p/host/host.go
p2p/host/match.go
+35
-0
p2p/host/match.go
p2p/host/match_test.go
+33
-0
p2p/host/match_test.go
p2p/host/routed/routed.go
+6
-0
p2p/host/routed/routed.go
with
89 additions
and
0 deletions
+89
-0
p2p/host/basic/basic_host.go
View file @
6635e3d7
...
...
@@ -156,6 +156,17 @@ func (h *BasicHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandler
})
}
// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function to do protocol comparisons
func
(
h
*
BasicHost
)
SetStreamHandlerMatch
(
pid
protocol
.
ID
,
m
func
(
string
)
bool
,
handler
inet
.
StreamHandler
)
{
h
.
Mux
()
.
AddHandlerWithFunc
(
string
(
pid
),
m
,
func
(
p
string
,
rwc
io
.
ReadWriteCloser
)
error
{
is
:=
rwc
.
(
inet
.
Stream
)
is
.
SetProtocol
(
p
)
handler
(
is
)
return
nil
})
}
// RemoveStreamHandler returns ..
func
(
h
*
BasicHost
)
RemoveStreamHandler
(
pid
protocol
.
ID
)
{
h
.
Mux
()
.
RemoveHandler
(
string
(
pid
))
...
...
This diff is collapsed.
Click to expand it.
p2p/host/host.go
View file @
6635e3d7
...
...
@@ -48,6 +48,10 @@ type Host interface {
// (Threadsafe)
SetStreamHandler
(
pid
protocol
.
ID
,
handler
inet
.
StreamHandler
)
// SetStreamHandlerMatch sets the protocol handler on the Host's Mux
// using a matching function for protocol selection.
SetStreamHandlerMatch
(
protocol
.
ID
,
func
(
string
)
bool
,
inet
.
StreamHandler
)
// RemoveStreamHandler removes a handler on the mux that was set by
// SetStreamHandler
RemoveStreamHandler
(
pid
protocol
.
ID
)
...
...
This diff is collapsed.
Click to expand it.
p2p/host/match.go
0 → 100644
View file @
6635e3d7
package
host
import
(
"strings"
semver
"github.com/coreos/go-semver/semver"
)
func
MultistreamSemverMatcher
(
base
string
)
(
func
(
string
)
bool
,
error
)
{
parts
:=
strings
.
Split
(
base
,
"/"
)
vers
,
err
:=
semver
.
NewVersion
(
parts
[
len
(
parts
)
-
1
])
if
err
!=
nil
{
return
nil
,
err
}
return
func
(
check
string
)
bool
{
chparts
:=
strings
.
Split
(
check
,
"/"
)
if
len
(
chparts
)
!=
len
(
parts
)
{
return
false
}
for
i
,
v
:=
range
chparts
[
:
len
(
chparts
)
-
1
]
{
if
parts
[
i
]
!=
v
{
return
false
}
}
chvers
,
err
:=
semver
.
NewVersion
(
chparts
[
len
(
chparts
)
-
1
])
if
err
!=
nil
{
return
false
}
return
vers
.
Major
==
chvers
.
Major
&&
vers
.
Minor
>=
chvers
.
Minor
},
nil
}
This diff is collapsed.
Click to expand it.
p2p/host/match_test.go
0 → 100644
View file @
6635e3d7
package
host
import
(
"testing"
)
func
TestSemverMatching
(
t
*
testing
.
T
)
{
m
,
err
:=
MultistreamSemverMatcher
(
"/testing/4.3.5"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
cases
:=
map
[
string
]
bool
{
"/testing/4.3.0"
:
true
,
"/testing/4.3.7"
:
true
,
"/testing/4.3.5"
:
true
,
"/testing/4.2.7"
:
true
,
"/testing/4.0.0"
:
true
,
"/testing/5.0.0"
:
false
,
"/cars/dogs/4.3.5"
:
false
,
"/foo/1.0.0"
:
false
,
""
:
false
,
"dogs"
:
false
,
"/foo"
:
false
,
"/foo/1.1.1.1"
:
false
,
}
for
p
,
ok
:=
range
cases
{
if
m
(
p
)
!=
ok
{
t
.
Fatalf
(
"expected %s to be %t"
,
p
,
ok
)
}
}
}
This diff is collapsed.
Click to expand it.
p2p/host/routed/routed.go
View file @
6635e3d7
...
...
@@ -110,6 +110,10 @@ func (rh *RoutedHost) SetStreamHandler(pid protocol.ID, handler inet.StreamHandl
rh
.
host
.
SetStreamHandler
(
pid
,
handler
)
}
func
(
rh
*
RoutedHost
)
SetStreamHandlerMatch
(
pid
protocol
.
ID
,
m
func
(
string
)
bool
,
handler
inet
.
StreamHandler
)
{
rh
.
host
.
SetStreamHandlerMatch
(
pid
,
m
,
handler
)
}
func
(
rh
*
RoutedHost
)
RemoveStreamHandler
(
pid
protocol
.
ID
)
{
rh
.
host
.
RemoveStreamHandler
(
pid
)
}
...
...
@@ -125,3 +129,5 @@ func (rh *RoutedHost) Close() error {
func
(
rh
*
RoutedHost
)
GetBandwidthReporter
()
metrics
.
Reporter
{
return
rh
.
host
.
GetBandwidthReporter
()
}
var
_
(
host
.
Host
)
=
(
*
RoutedHost
)(
nil
)
This diff is collapsed.
Click to expand it.
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
Menu
Projects
Groups
Snippets
Help