core

package
v0.0.0-...-babd247 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2017 License: MIT Imports: 21 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chan

type Chan struct {
	// contains filtered or unexported fields
}

Chan holds the current state of a channel

type ChanID

type ChanID uint32

ChanID identifies a channel

type Chunk

type Chunk struct {
	//io.Reader{}
	ID ChunkID
	B  []byte
}

Chunk represents a PPSPP chunk of content

type ChunkID

type ChunkID uint32

ChunkID identifies a chunk of content

type DataMsg

type DataMsg struct {
	Start ChunkID
	End   ChunkID
	Data  []byte
}

DataMsg holds a data message data payload

type Datagram

type Datagram struct {
	ChanID ChanID
	Msgs   []Msg
}

Datagram holds a protocol datagram

type HandshakeMsg

type HandshakeMsg struct {
	C ChanID
	S SwarmID
}

HandshakeMsg holds a handshake message data payload

type HaveMsg

type HaveMsg struct {
	// TODO: start chunk / end chunk
	Start ChunkID
	End   ChunkID
}

HaveMsg holds a have message data payload

type Msg

type Msg struct {
	Op   Opcode
	Data MsgData
}

Msg holds a protocol message

func (*Msg) MarshalJSON

func (m *Msg) MarshalJSON() ([]byte, error)

MarshalJSON handles the serializing of a message.

See note above UnmarshalJSON for the reason for the custom MarshalJSON

func (*Msg) UnmarshalJSON

func (m *Msg) UnmarshalJSON(b []byte) error

UnmarshalJSON handles the deserializing of a message.

We can't get away with off-the-shelf JSON, because we're using an interface type for MsgData, which causes problems on the decode side.

type MsgData

type MsgData interface{}

MsgData holds the data payload of a message

type MsgError

type MsgError struct {
	// contains filtered or unexported fields
}

MsgError is an error that happens while handling an incoming message

func (MsgError) Error

func (e MsgError) Error() string

type Network

type Network interface {

	// SendDatagram sends a datagram to the remote peer
	SendDatagram(d Datagram, remote PeerID) error

	// Connect connects to the remote peer and creates any io resources necessary for the connection
	Connect(remote PeerID) error

	// Disconnect disconnects from the remote peer and destroys any io resources created for the connection
	Disconnect(remote PeerID) error

	// ID returns the ID of this peer
	ID() PeerID

	// SetDatagramHandler sets the function that will be called on receipt of a new datagram
	// f gets called every time a new Datagram is received.
	SetDatagramHandler(f func(*Datagram, PeerID) error)

	// AddAddrs adds multiaddresses for the remote peer to this peer's store
	AddAddrs(id PeerID, addrs []ma.Multiaddr)

	// Addrs returns multiaddresses for this peer
	Addrs() []ma.Multiaddr
}

Network handles interactions with the underlying network

type Opcode

type Opcode uint8

Opcode identifies the type of message

const (
	Handshake Opcode = 0
	Data      Opcode = 1
	Have      Opcode = 3
	Request   Opcode = 8
)

From the RFC:

+----------+------------------+
| Msg Type | Description      |
+----------+------------------+
| 0        | HANDSHAKE        |
| 1        | DATA             |
| 2        | ACK              |
| 3        | HAVE             |
| 4        | INTEGRITY        |
| 5        | PEX_RESv4        |
| 6        | PEX_REQ          |
| 7        | SIGNED_INTEGRITY |
| 8        | REQUEST          |
| 9        | CANCEL           |
| 10       | CHOKE            |
| 11       | UNCHOKE          |
| 12       | PEX_RESv6        |
| 13       | PEX_REScert      |
| 14-254   | Unassigned       |
| 255      | Reserved         |
+----------+------------------+

type Peer

type Peer struct {

	// P handles protocol logic.
	P Protocol
	// contains filtered or unexported fields
}

Peer implements protocol logic and underlying network

func NewLibp2pPeer

func NewLibp2pPeer(port int, p Protocol) (*Peer, error)

NewLibp2pPeer makes a new peer with a libp2p network

func NewPeer

func NewPeer(n Network, p Protocol) *Peer

NewPeer makes a new peer

func NewPpsppPeer

func NewPpsppPeer(n Network) (*Peer, error)

NewPpsppPeer makes a new peer with a ppspp protocol

func (*Peer) AddAddrs

func (p *Peer) AddAddrs(remote PeerID, addrs []ma.Multiaddr)

AddAddrs adds multiaddresses for the remote peer to this peer's store

func (*Peer) Addrs

func (p *Peer) Addrs() []ma.Multiaddr

Addrs returns multiaddresses for this peer

func (*Peer) Connect

func (p *Peer) Connect(id PeerID) error

Connect creates a stream from p to the peer at id and sets a stream handler

func (*Peer) Disconnect

func (p *Peer) Disconnect(id PeerID) error

Disconnect closes the stream that p is using to connect to the peer at id

func (*Peer) ID

func (p *Peer) ID() PeerID

ID returns the peer ID

type PeerID

type PeerID interface {
	String() string
}

PeerID identifies a peer

type Ppspp

type Ppspp struct {
	// contains filtered or unexported fields
}

Ppspp is a PPSPP implementation

func NewPpspp

func NewPpspp() *Ppspp

NewPpspp creates a new PPSPP protocol object

func (*Ppspp) AddLocalChunk

func (p *Ppspp) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error

AddLocalChunk stores a chunk locally for the given swarm

func (*Ppspp) AddSwarm

func (p *Ppspp) AddSwarm(config SwarmConfig) error

AddSwarm adds a swarm with a given ID

func (*Ppspp) HandleDatagram

func (p *Ppspp) HandleDatagram(d *Datagram, id PeerID) error

HandleDatagram handles an incoming datagram from a remote peer with the given id

func (*Ppspp) ProtocolState

func (p *Ppspp) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)

ProtocolState returns the current ProtocolState in a swarm for a given remote peer if this returns unknown state, check error for reason

func (*Ppspp) SendClosingHandshake

func (p *Ppspp) SendClosingHandshake(remote PeerID, sid SwarmID) error

SendClosingHandshake sends a closing handshake message to the remote peer on swarm sid

func (*Ppspp) SendData

func (p *Ppspp) SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendData sends the chunk range in a data message

func (*Ppspp) SendHave

func (p *Ppspp) SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendHave sends a have message for the chunk range to the remote peer on the Swarm

func (*Ppspp) SendRequest

func (p *Ppspp) SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendRequest sends a request for the chunk range to the remote peer on the swarm

func (*Ppspp) SetDatagramSender

func (p *Ppspp) SetDatagramSender(f func(Datagram, PeerID) error)

SetDatagramSender sets the datagram sender function

func (*Ppspp) StartHandshake

func (p *Ppspp) StartHandshake(remote PeerID, sid SwarmID) error

StartHandshake sends a starting handshake message to the remote peer on swarm sid

func (*Ppspp) Swarm

func (p *Ppspp) Swarm(id SwarmID) (*Swarm, error)

Swarm returns the swarm at the given id

type Protocol

type Protocol interface {
	HandleDatagram(d *Datagram, id PeerID) error
	SetDatagramSender(f func(Datagram, PeerID) error)
	StartHandshake(remote PeerID, sid SwarmID) error
	SendClosingHandshake(remote PeerID, sid SwarmID) error
	ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)
	AddSwarm(config SwarmConfig) error
	Swarm(id SwarmID) (*Swarm, error)
	AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error
	SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
	SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
	SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error
}

Protocol is the interface to the PPSPP logic

type ProtocolState

type ProtocolState uint

ProtocolState is a per-channel state local to a peer

const (
	// Unknown state is used for errors.
	Unknown ProtocolState = 0

	// Begin is the initial state before a handshake.
	Begin ProtocolState = 1

	// WaitHandshake means waiting for ack of the first handshake.
	WaitHandshake ProtocolState = 2

	// Ready means the handshake is complete and the peer is ready for other types of messages.
	Ready ProtocolState = 3
)

type RequestMsg

type RequestMsg struct {
	// TODO: start chunk / end chunk
	Start ChunkID
	End   ChunkID
}

RequestMsg holds a have message data payload

type StringPeerID

type StringPeerID struct {
	// contains filtered or unexported fields
}

StringPeerID is a simple implementation of PeerID using an underlying string Used for stubs

func (StringPeerID) String

func (s StringPeerID) String() string

type StubNetwork

type StubNetwork struct {
	// contains filtered or unexported fields
}

StubNetwork stores all sent datagrams without sending them anywhere

func NewStubNetwork

func NewStubNetwork(s string) *StubNetwork

NewStubNetwork creates a new StubNetwork

func (*StubNetwork) AddAddrs

func (n *StubNetwork) AddAddrs(id PeerID, addrs []ma.Multiaddr)

AddAddrs is a nop for StubNetwork

func (*StubNetwork) Addrs

func (n *StubNetwork) Addrs() []ma.Multiaddr

Addrs is a nop for StubNetwork

func (*StubNetwork) Connect

func (n *StubNetwork) Connect(remote PeerID) error

Connect is a nop for StubNetwork

func (*StubNetwork) Disconnect

func (n *StubNetwork) Disconnect(remote PeerID) error

Disconnect is a nop for StubNetwork

func (*StubNetwork) ID

func (n *StubNetwork) ID() PeerID

ID returns the PeerID for this peer on the network

func (*StubNetwork) InjectIncomingDatagram

func (n *StubNetwork) InjectIncomingDatagram(d *Datagram, id PeerID) error

InjectIncomingDatagram injects a datagram as if it came from the remote peer id

func (*StubNetwork) NumSentDatagrams

func (n *StubNetwork) NumSentDatagrams() int

NumSentDatagrams returns the number of unread sent datagrams

func (*StubNetwork) ReadSentDatagram

func (n *StubNetwork) ReadSentDatagram() *Datagram

ReadSentDatagram pops the oldest sent datagram and returns it Intended to be used by tests that want to inspect what is being sent out on this stub network

func (*StubNetwork) SendDatagram

func (n *StubNetwork) SendDatagram(d Datagram, remote PeerID) error

SendDatagram stores the datagram without sending it anywhere

func (*StubNetwork) SetDatagramHandler

func (n *StubNetwork) SetDatagramHandler(f func(*Datagram, PeerID) error)

SetDatagramHandler sets the datagram handler function for incoming datagrams

type StubProtocol

type StubProtocol struct {
	// contains filtered or unexported fields
}

StubProtocol handles incoming datagrams by storing them

func (*StubProtocol) AddLocalChunk

func (p *StubProtocol) AddLocalChunk(sid SwarmID, cid ChunkID, b []byte) error

AddLocalChunk is a noop for StubProtocol

func (*StubProtocol) AddSwarm

func (p *StubProtocol) AddSwarm(config SwarmConfig) error

AddSwarm is a noop for StubProtocol

func (*StubProtocol) HandleDatagram

func (p *StubProtocol) HandleDatagram(d *Datagram, id PeerID) error

HandleDatagram stores incoming datagram, peerid pairs

func (*StubProtocol) NumHandledDatagrams

func (p *StubProtocol) NumHandledDatagrams() int

NumHandledDatagrams returns the number of unread handled datagrams

func (*StubProtocol) ProtocolState

func (p *StubProtocol) ProtocolState(sid SwarmID, pid PeerID) (ProtocolState, error)

ProtocolState is a noop for StubProtocol

func (*StubProtocol) ReadHandledDatagram

func (p *StubProtocol) ReadHandledDatagram() (*Datagram, PeerID, error)

ReadHandledDatagram pops the oldest handled datagram and returns it, along with the peer it came from Intended to be used by tests that want to inspect what is being handled by this stub protocol

func (*StubProtocol) SendClosingHandshake

func (p *StubProtocol) SendClosingHandshake(remote PeerID, sid SwarmID) error

SendClosingHandshake is a noop for StubProtocol

func (*StubProtocol) SendData

func (p *StubProtocol) SendData(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendData is a noop for StubProtocol

func (*StubProtocol) SendHave

func (p *StubProtocol) SendHave(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendHave is a noop for StubProtocol

func (*StubProtocol) SendRequest

func (p *StubProtocol) SendRequest(start ChunkID, end ChunkID, remote PeerID, sid SwarmID) error

SendRequest is a noop for StubProtocol

func (*StubProtocol) SetDatagramSender

func (p *StubProtocol) SetDatagramSender(f func(Datagram, PeerID) error)

SetDatagramSender is a noop for StubProtocol

func (*StubProtocol) StartHandshake

func (p *StubProtocol) StartHandshake(remote PeerID, sid SwarmID) error

StartHandshake is a noop for StubProtocol

func (*StubProtocol) Swarm

func (p *StubProtocol) Swarm(id SwarmID) (*Swarm, error)

Swarm is a noop for StubProtocol

type Swarm

type Swarm struct {
	// contains filtered or unexported fields
}

Swarm tracks info related to a swarm

func NewSwarm

func NewSwarm(config SwarmConfig) *Swarm

NewSwarm creates a new Swarm

func (*Swarm) AddLocalChunk

func (s *Swarm) AddLocalChunk(cid ChunkID, c *Chunk) error

AddLocalChunk stores the chunk locally

func (*Swarm) AddLocalChunks

func (s *Swarm) AddLocalChunks(start ChunkID, end ChunkID, data []byte) error

AddLocalChunks stores a contiguous chunk range, with input data batched in one array

func (*Swarm) AddRemoteHave

func (s *Swarm) AddRemoteHave(c ChunkID, p PeerID)

AddRemoteHave tells this Swarm that the remote peer p has ChunkID c

func (*Swarm) ChanID

func (s *Swarm) ChanID(id PeerID) (ChanID, bool)

ChanID returns the channel ID for the given peer, also returns bool indicating success of the lookup

func (*Swarm) ChunkSize

func (s *Swarm) ChunkSize() int

ChunkSize returns the chunk size for this Swarm

func (*Swarm) DataFromLocalChunks

func (s *Swarm) DataFromLocalChunks(start ChunkID, end ChunkID) ([]byte, error)

DataFromLocalChunks returns packs the data from the chunk range into a single array

func (*Swarm) LocalChunks

func (s *Swarm) LocalChunks() map[ChunkID]*Chunk

LocalChunks returns the local chunks store for this Swarm

func (*Swarm) WantChunk

func (s *Swarm) WantChunk(id ChunkID) bool

WantChunk returns whether this Swarm wants the chunk locally

type SwarmConfig

type SwarmConfig struct {
	// Metadata stores the metadata of a Swarm.  See the docs for
	// SwarmMetadata for details.
	Metadata SwarmMetadata
	// DataHandler is invoked in a new goroutine whenever new data chunks
	// have been received.
	DataHandler SwarmDataHandler
}

SwarmConfig is used to create a Swarm.

type SwarmDataHandler

type SwarmDataHandler func(DataMsg)

SwarmDataHandler is a function that's invoked with new data chunks that have just been received.

type SwarmID

type SwarmID uint32

SwarmID identifies a swarm

func (SwarmID) String

func (id SwarmID) String() string

type SwarmMetadata

type SwarmMetadata struct {
	ID        SwarmID
	ChunkSize int
}

SwarmMetadata stores the metadata of a Swarm See: https://tools.ietf.org/html/rfc7574#section-3.1

type WrappedStream

type WrappedStream struct {
	// contains filtered or unexported fields
}

WrappedStream wraps a libp2p stream. We encode/decode whenever we write/read from a stream, so we can just carry the encoders and bufios with us

func WrapStream

func WrapStream(s inet.Stream) *WrappedStream

WrapStream takes a stream and complements it with r/w bufios and decoder/encoder. In order to write raw data to the stream we can use wrap.w.Write(). To encode something into it we can wrap.enc.Encode(). Finally, we should wrap.w.Flush() to actually send the data. Handling incoming data works similarly with wrap.r.Read() for raw-reading and wrap.dec.Decode() to decode.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL