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-bitswap
Commits
ee347b8d
Unverified
Commit
ee347b8d
authored
Aug 08, 2019
by
Steven Allen
Committed by
GitHub
Aug 08, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #171 from ipfs/feat/net-prefix
network: Allow specifying protocol prefix
parents
f62bf544
167327fc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
13 deletions
+43
-13
network/ipfs_impl.go
network/ipfs_impl.go
+28
-13
network/options.go
network/options.go
+15
-0
No files found.
network/ipfs_impl.go
View file @
ee347b8d
...
...
@@ -8,15 +8,16 @@ import (
"time"
bsmsg
"github.com/ipfs/go-bitswap/message"
"github.com/libp2p/go-libp2p-core/helpers"
cid
"github.com/ipfs/go-cid"
logging
"github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/helpers"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
peerstore
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/routing"
msgio
"github.com/libp2p/go-msgio"
ma
"github.com/multiformats/go-multiaddr"
...
...
@@ -27,10 +28,19 @@ var log = logging.Logger("bitswap_network")
var
sendMessageTimeout
=
time
.
Minute
*
10
// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host.
func
NewFromIpfsHost
(
host
host
.
Host
,
r
routing
.
ContentRouting
)
BitSwapNetwork
{
func
NewFromIpfsHost
(
host
host
.
Host
,
r
routing
.
ContentRouting
,
opts
...
NetOpt
)
BitSwapNetwork
{
s
:=
Settings
{}
for
_
,
opt
:=
range
opts
{
opt
(
&
s
)
}
bitswapNetwork
:=
impl
{
host
:
host
,
routing
:
r
,
protocolBitswap
:
s
.
ProtocolPrefix
+
ProtocolBitswap
,
protocolBitswapOne
:
s
.
ProtocolPrefix
+
ProtocolBitswapOne
,
protocolBitswapNoVers
:
s
.
ProtocolPrefix
+
ProtocolBitswapNoVers
,
}
return
&
bitswapNetwork
}
...
...
@@ -41,6 +51,10 @@ type impl struct {
host
host
.
Host
routing
routing
.
ContentRouting
protocolBitswap
protocol
.
ID
protocolBitswapOne
protocol
.
ID
protocolBitswapNoVers
protocol
.
ID
// inbound messages from the network are forwarded to the receiver
receiver
Receiver
...
...
@@ -48,7 +62,8 @@ type impl struct {
}
type
streamMessageSender
struct
{
s
network
.
Stream
s
network
.
Stream
bsnet
*
impl
}
func
(
s
*
streamMessageSender
)
Close
()
error
{
...
...
@@ -60,10 +75,10 @@ func (s *streamMessageSender) Reset() error {
}
func
(
s
*
streamMessageSender
)
SendMsg
(
ctx
context
.
Context
,
msg
bsmsg
.
BitSwapMessage
)
error
{
return
msgToStream
(
ctx
,
s
.
s
,
msg
)
return
s
.
bsnet
.
msgToStream
(
ctx
,
s
.
s
,
msg
)
}
func
msgToStream
(
ctx
context
.
Context
,
s
network
.
Stream
,
msg
bsmsg
.
BitSwapMessage
)
error
{
func
(
bsnet
*
impl
)
msgToStream
(
ctx
context
.
Context
,
s
network
.
Stream
,
msg
bsmsg
.
BitSwapMessage
)
error
{
deadline
:=
time
.
Now
()
.
Add
(
sendMessageTimeout
)
if
dl
,
ok
:=
ctx
.
Deadline
();
ok
{
deadline
=
dl
...
...
@@ -74,12 +89,12 @@ func msgToStream(ctx context.Context, s network.Stream, msg bsmsg.BitSwapMessage
}
switch
s
.
Protocol
()
{
case
P
rotocolBitswap
:
case
bsnet
.
p
rotocolBitswap
:
if
err
:=
msg
.
ToNetV1
(
s
);
err
!=
nil
{
log
.
Debugf
(
"error: %s"
,
err
)
return
err
}
case
P
rotocolBitswapOne
,
P
rotocolBitswapNoVers
:
case
bsnet
.
p
rotocolBitswapOne
,
bsnet
.
p
rotocolBitswapNoVers
:
if
err
:=
msg
.
ToNetV0
(
s
);
err
!=
nil
{
log
.
Debugf
(
"error: %s"
,
err
)
return
err
...
...
@@ -100,11 +115,11 @@ func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID) (MessageSend
return
nil
,
err
}
return
&
streamMessageSender
{
s
:
s
},
nil
return
&
streamMessageSender
{
s
:
s
,
bsnet
:
bsnet
},
nil
}
func
(
bsnet
*
impl
)
newStreamToPeer
(
ctx
context
.
Context
,
p
peer
.
ID
)
(
network
.
Stream
,
error
)
{
return
bsnet
.
host
.
NewStream
(
ctx
,
p
,
P
rotocolBitswap
,
P
rotocolBitswapOne
,
P
rotocolBitswapNoVers
)
return
bsnet
.
host
.
NewStream
(
ctx
,
p
,
bsnet
.
p
rotocolBitswap
,
bsnet
.
p
rotocolBitswapOne
,
bsnet
.
p
rotocolBitswapNoVers
)
}
func
(
bsnet
*
impl
)
SendMessage
(
...
...
@@ -117,7 +132,7 @@ func (bsnet *impl) SendMessage(
return
err
}
if
err
=
msgToStream
(
ctx
,
s
,
outgoing
);
err
!=
nil
{
if
err
=
bsnet
.
msgToStream
(
ctx
,
s
,
outgoing
);
err
!=
nil
{
s
.
Reset
()
return
err
}
...
...
@@ -131,9 +146,9 @@ func (bsnet *impl) SendMessage(
func
(
bsnet
*
impl
)
SetDelegate
(
r
Receiver
)
{
bsnet
.
receiver
=
r
bsnet
.
host
.
SetStreamHandler
(
P
rotocolBitswap
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
SetStreamHandler
(
P
rotocolBitswapOne
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
SetStreamHandler
(
P
rotocolBitswapNoVers
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
SetStreamHandler
(
bsnet
.
p
rotocolBitswap
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
SetStreamHandler
(
bsnet
.
p
rotocolBitswapOne
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
SetStreamHandler
(
bsnet
.
p
rotocolBitswapNoVers
,
bsnet
.
handleNewStream
)
bsnet
.
host
.
Network
()
.
Notify
((
*
netNotifiee
)(
bsnet
))
// TODO: StopNotify.
...
...
network/options.go
0 → 100644
View file @
ee347b8d
package
network
import
"github.com/libp2p/go-libp2p-core/protocol"
type
NetOpt
func
(
*
Settings
)
type
Settings
struct
{
ProtocolPrefix
protocol
.
ID
}
func
Prefix
(
prefix
protocol
.
ID
)
NetOpt
{
return
func
(
settings
*
Settings
)
{
settings
.
ProtocolPrefix
=
prefix
}
}
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