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
00fa6646
Commit
00fa6646
authored
Aug 22, 2016
by
Jeromy
Browse files
use peerstore for protocol preferencce storage
parent
f21d06fb
Changes
2
Hide whitespace changes
Inline
Side-by-side
p2p/host/basic/basic_host.go
View file @
00fa6646
...
...
@@ -2,7 +2,6 @@ package basichost
import
(
"io"
"sync"
peer
"github.com/ipfs/go-libp2p-peer"
pstore
"github.com/ipfs/go-libp2p-peerstore"
...
...
@@ -47,9 +46,6 @@ type BasicHost struct {
relay
*
relay
.
RelayService
natmgr
*
natManager
protoPrefs
map
[
peer
.
ID
]
map
[
protocol
.
ID
]
struct
{}
prefsLk
sync
.
Mutex
proc
goprocess
.
Process
bwc
metrics
.
Reporter
...
...
@@ -58,10 +54,9 @@ type BasicHost struct {
// New constructs and sets up a new *BasicHost with given Network
func
New
(
net
inet
.
Network
,
opts
...
interface
{})
*
BasicHost
{
h
:=
&
BasicHost
{
network
:
net
,
mux
:
msmux
.
NewMultistreamMuxer
(),
bwc
:
metrics
.
NewBandwidthCounter
(),
protoPrefs
:
make
(
map
[
peer
.
ID
]
map
[
protocol
.
ID
]
struct
{}),
network
:
net
,
mux
:
msmux
.
NewMultistreamMuxer
(),
bwc
:
metrics
.
NewBandwidthCounter
(),
}
h
.
proc
=
goprocess
.
WithTeardown
(
func
()
error
{
...
...
@@ -182,7 +177,11 @@ func (h *BasicHost) RemoveStreamHandler(pid protocol.ID) {
// to create one. If ProtocolID is "", writes no header.
// (Threadsafe)
func
(
h
*
BasicHost
)
NewStream
(
ctx
context
.
Context
,
p
peer
.
ID
,
pids
...
protocol
.
ID
)
(
inet
.
Stream
,
error
)
{
pref
:=
h
.
preferredProtocol
(
p
,
pids
)
pref
,
err
:=
h
.
preferredProtocol
(
p
,
pids
)
if
err
!=
nil
{
return
nil
,
err
}
if
pref
!=
""
{
return
h
.
newStream
(
ctx
,
p
,
pref
)
}
...
...
@@ -204,50 +203,31 @@ func (h *BasicHost) NewStream(ctx context.Context, p peer.ID, pids ...protocol.I
}
selpid
:=
protocol
.
ID
(
selected
)
s
.
SetProtocol
(
selpid
)
h
.
setPreferre
dProtocol
(
p
,
sel
pi
d
)
h
.
Peerstore
()
.
Ad
dProtocol
s
(
p
,
sel
ecte
d
)
return
mstream
.
WrapStream
(
s
,
h
.
bwc
),
nil
}
func
(
h
*
BasicHost
)
preferredProtocol
(
p
peer
.
ID
,
pids
[]
protocol
.
ID
)
protocol
.
ID
{
h
.
prefsLk
.
Lock
()
defer
h
.
prefsLk
.
Unlock
()
prefs
,
ok
:=
h
.
protoPrefs
[
p
]
if
!
ok
{
supported
,
err
:=
h
.
Peerstore
()
.
GetProtocols
(
p
)
if
err
!=
nil
{
log
.
Warningf
(
"error getting protocol for peer %s: %s"
,
p
,
err
)
return
""
}
prefs
=
make
(
map
[
protocol
.
ID
]
struct
{})
for
_
,
proto
:=
range
supported
{
prefs
[
protocol
.
ID
(
proto
)]
=
struct
{}{}
}
h
.
protoPrefs
[
p
]
=
prefs
func
pidsToStrings
(
pids
[]
protocol
.
ID
)
[]
string
{
out
:=
make
([]
string
,
len
(
pids
))
for
i
,
p
:=
range
pids
{
out
[
i
]
=
string
(
p
)
}
for
_
,
pid
:=
range
pids
{
if
_
,
ok
:=
prefs
[
pid
];
ok
{
return
pid
}
}
return
""
return
out
}
func
(
h
*
BasicHost
)
setPreferredProtocol
(
p
peer
.
ID
,
proto
protocol
.
ID
)
{
h
.
prefsLk
.
Lock
()
defer
h
.
prefsLk
.
Unlock
()
prefs
,
ok
:=
h
.
protoPrefs
[
p
]
if
!
ok
{
prefs
=
make
(
map
[
protocol
.
ID
]
struct
{})
h
.
protoPrefs
[
p
]
=
prefs
func
(
h
*
BasicHost
)
preferredProtocol
(
p
peer
.
ID
,
pids
[]
protocol
.
ID
)
(
protocol
.
ID
,
error
)
{
pidstrs
:=
pidsToStrings
(
pids
)
supported
,
err
:=
h
.
Peerstore
()
.
SupportsProtocols
(
p
,
pidstrs
...
)
if
err
!=
nil
{
return
""
,
err
}
prefs
[
proto
]
=
struct
{}{}
var
out
protocol
.
ID
if
len
(
supported
)
>
0
{
out
=
protocol
.
ID
(
supported
[
0
])
}
return
out
,
nil
}
func
(
h
*
BasicHost
)
newStream
(
ctx
context
.
Context
,
p
peer
.
ID
,
pid
protocol
.
ID
)
(
inet
.
Stream
,
error
)
{
...
...
package.json
View file @
00fa6646
...
...
@@ -54,9 +54,9 @@
"version"
:
"0.0.0"
},
{
"hash"
:
"Qm
NQynaz7qfriSUJkiEZUrm2Wen1u3Kj9goZzWtrPyu7XR
"
,
"hash"
:
"Qm
SpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52
"
,
"name"
:
"go-log"
,
"version"
:
"1.
1.2
"
"version"
:
"1.
2.0
"
},
{
"hash"
:
"QmPpRcbNUXauP3zWZ1NJMLWpe4QnmEHrd2ba2D3yqWznw7"
,
...
...
@@ -147,21 +147,21 @@
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
enZRWtBU6n19k2wYDEwGWSs4Dh472t8xPp9JyYJjXmLj
"
,
"hash"
:
"Qm
SVddpXhD2QziagxjdRs7eUeNypGugGxWqDsFeauv33XR
"
,
"name"
:
"go-libp2p-loggables"
,
"version"
:
"1.0.
8
"
"version"
:
"1.0.
9
"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qmb
kwpCqdshmuq2T1RB78RzGiPczToJd5pC91r1qQnb3zn
"
,
"hash"
:
"Qmb
P93111oShRbdjoWvP3NZCUApTLkaPXgaNDaXZPfHQHR
"
,
"name"
:
"go-libp2p-secio"
,
"version"
:
"1.0.1
0
"
"version"
:
"1.0.1
1
"
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qme
zbDCPpw9hBtXdXHEajg6UEufALJLMtoQQ1YrZEMyMwf
"
,
"hash"
:
"Qme
p1A25MDNZxMno8oHZ5ForA5T2gw1EXAGdZzGCCpDD4n
"
,
"name"
:
"go-libp2p-transport"
,
"version"
:
"1.3.
3
"
"version"
:
"1.3.
4
"
},
{
"author"
:
"whyrusleeping"
,
...
...
@@ -183,9 +183,9 @@
},
{
"author"
:
"whyrusleeping"
,
"hash"
:
"Qm
cb7oW2zriXQSCn9GKKDbewm9qM2NF1mR551Zf1HfoQPZ
"
,
"hash"
:
"Qm
SZi9ygLohBUGyHMqE5N6eToPwqcg7bZQTULeVLFu7Q6d
"
,
"name"
:
"go-libp2p-peerstore"
,
"version"
:
"1.2.
1
"
"version"
:
"1.2.
2
"
}
],
"gxVersion"
:
"0.4.0"
,
...
...
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