forked from cerc-io/ipld-eth-server
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package network
|
|
|
|
import (
|
|
"context"
|
|
|
|
bsmsg "github.com/ipfs/go-bitswap/message"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
ifconnmgr "github.com/libp2p/go-libp2p-interface-connmgr"
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
protocol "github.com/libp2p/go-libp2p-protocol"
|
|
)
|
|
|
|
var (
|
|
// These two are equivalent, legacy
|
|
ProtocolBitswapOne protocol.ID = "/ipfs/bitswap/1.0.0"
|
|
ProtocolBitswapNoVers protocol.ID = "/ipfs/bitswap"
|
|
|
|
ProtocolBitswap protocol.ID = "/ipfs/bitswap/1.1.0"
|
|
)
|
|
|
|
// BitSwapNetwork provides network connectivity for BitSwap sessions.
|
|
type BitSwapNetwork interface {
|
|
|
|
// SendMessage sends a BitSwap message to a peer.
|
|
SendMessage(
|
|
context.Context,
|
|
peer.ID,
|
|
bsmsg.BitSwapMessage) error
|
|
|
|
// SetDelegate registers the Reciver to handle messages received from the
|
|
// network.
|
|
SetDelegate(Receiver)
|
|
|
|
ConnectTo(context.Context, peer.ID) error
|
|
|
|
NewMessageSender(context.Context, peer.ID) (MessageSender, error)
|
|
|
|
ConnectionManager() ifconnmgr.ConnManager
|
|
|
|
Stats() NetworkStats
|
|
|
|
Routing
|
|
}
|
|
|
|
type MessageSender interface {
|
|
SendMsg(context.Context, bsmsg.BitSwapMessage) error
|
|
Close() error
|
|
Reset() error
|
|
}
|
|
|
|
// Implement Receiver to receive messages from the BitSwapNetwork.
|
|
type Receiver interface {
|
|
ReceiveMessage(
|
|
ctx context.Context,
|
|
sender peer.ID,
|
|
incoming bsmsg.BitSwapMessage)
|
|
|
|
ReceiveError(error)
|
|
|
|
// Connected/Disconnected warns bitswap about peer connections.
|
|
PeerConnected(peer.ID)
|
|
PeerDisconnected(peer.ID)
|
|
}
|
|
|
|
type Routing interface {
|
|
// FindProvidersAsync returns a channel of providers for the given key.
|
|
FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.ID
|
|
|
|
// Provide provides the key to the network.
|
|
Provide(context.Context, cid.Cid) error
|
|
}
|
|
|
|
// NetworkStats is a container for statistics about the bitswap network
|
|
// the numbers inside are specific to bitswap, and not any other protocols
|
|
// using the same underlying network.
|
|
type NetworkStats struct {
|
|
MessagesSent uint64
|
|
MessagesRecvd uint64
|
|
}
|