Documentation ¶
Index ¶
- Constants
- Variables
- type Adapter
- type Message
- func (m *Message) Metadata() maps.Map
- func (m *Message) PayloadGet() []byte
- func (m *Message) PayloadGetString() string
- func (m *Message) PayloadSet(value []byte)
- func (m *Message) PayloadSetString(value string)
- func (m *Message) ReadFromConn(c net.Conn) error
- func (m *Message) ReadFromReader(reader *bufio.Reader) error
- func (m *Message) WriteIntoConn(c net.Conn) error
- func (m *Message) WriteIntoWriter(writer *bufio.Writer) error
- type Middleware
- type MiddlewareFunc
- type MiddlewareResult
- type NetworkConnection
- func (nc *NetworkConnection) ConnectTo(network string, addr string)
- func (nc *NetworkConnection) DisconnectFrom(addr string)
- func (nc *NetworkConnection) OnMessage(handler func(p *Peer, msg *Message))
- func (nc *NetworkConnection) OnPeer(handler func(p *Peer))
- func (nc *NetworkConnection) OnPeerDisconnect(handler func(p *Peer))
- func (nc *NetworkConnection) OnPeerError(handler func(p *Peer, err error))
- func (nc *NetworkConnection) Send(msg *Message, addr string)
- func (nc *NetworkConnection) SendBroadcast(msg *Message)
- func (nc *NetworkConnection) Start() error
- func (nc *NetworkConnection) Stop()
- type NetworkConnectionBuilder
- type OperatorTCP
- type Peer
- type PeerOperator
- type Pipe
- type PipeOperation
- type RoutingTable
Constants ¶
const DisconnectedError = errorConstant("disconnected")
DisconnectedError represents Error when a peer is disconnected
Variables ¶
var EmptyRoutesTable = *new(RoutingTable)
EmptyRoutesTable is a table without any routes
var ErrInvalidNetwork = errors.New("invalid network")
ErrInvalidNetwork represents an invalid network part in the given address
var ErrPipeStopProcessing = errors.New("pipe stopped")
ErrPipeStopProcessing is returned when the pipe has stopped it execution
Functions ¶
This section is empty.
Types ¶
type Adapter ¶
type Adapter interface { // ReadMessage should read from the underline connection // and return a Message object until all data was readed // The call should block until an entire Message was readed, // an error occoured or the underline connection was closed ReadMessage() (*Message, error) // WriteMessage write the given message to the underline connection WriteMessage(m *Message) error // Close should close the underline connection Close() // LocalAddress returns the local address (example: tcp:127.0.0.1:7000) LocalAddress() string // RemoteAddress returns the remote address (example: tcp:127.0.0.1:7000) RemoteAddress() string }
Adapter represents a wrapper around a network connection
func NewAdapter ¶
NewAdapter creates a new TCP adapter that wraps the given net.Conn instance
type Message ¶
type Message struct {
// contains filtered or unexported fields
}
Message represents a p2p message
func NewMessageFromData ¶
NewMessageFromData creates a new Message from the given data
func NewMessageFromString ¶
NewMessageFromString creates a new Message from the given string
func NewMessageRoutedFromData ¶
NewMessageRoutedFromData creates a new routed message to the handler given by path with the provided data
func NewMessageRoutedFromString ¶
NewMessageRoutedFromString creates a new routed message to the handler given by path with the provided string content
func (*Message) PayloadGet ¶
PayloadGet returns the payload data
func (*Message) PayloadGetString ¶
PayloadGetString returns the payload of the message as a string
func (*Message) PayloadSet ¶
PayloadSet sets the payload with the given value
func (*Message) PayloadSetString ¶
PayloadSetString sets the given string as payload of the message
func (*Message) ReadFromConn ¶
ReadFromConn read all data from the given conn object into the payload of the message instance
func (*Message) ReadFromReader ¶
ReadFromReader read all data from the given reader object into the payload of the message instance
func (*Message) WriteIntoConn ¶
WriteIntoConn writes the message payload into the given conn instance
type Middleware ¶
type Middleware struct {
// contains filtered or unexported fields
}
Middleware represents a wrapped middleware function with additional information for internal usage
func NewMiddleware ¶
func NewMiddleware(name string, action MiddlewareFunc) *Middleware
NewMiddleware wraps the provided action into a Middleware instance
func (*Middleware) String ¶
func (m *Middleware) String() string
String returns the string representation of this instance
type MiddlewareFunc ¶
type MiddlewareFunc func(peer *Peer, pipe *Pipe, msg *Message) (MiddlewareResult, error)
MiddlewareFunc represents a middleware implementation function
func Crypt ¶
func Crypt() (string, MiddlewareFunc)
Crypt returns the crypto middleware. This middleware handles encryption in your communication PublicKeys are exchanged on first peer communication
func Headers ¶
func Headers() (string, MiddlewareFunc)
Headers creates the *headers* middleware store the Message.Annotations() within the payload. With this middleware you can provide (http protocol) "header" like behavior into your communication. You can use it to annotate messages with id's or other information
func Log ¶
func Log() (string, MiddlewareFunc)
Log creates a logging middleware for in and outgoing messages
func Routes ¶
func Routes(rt RoutingTable) (string, MiddlewareFunc)
Routes provides an route based middleware You can listen to specific endpoints and send messages to them This is similar to a controller/action pattern in HTTP frameworks
type MiddlewareResult ¶
type MiddlewareResult int
MiddlewareResult represents a result returned by a middleware possible values are *Stop* and *Next*
const ( // Stop will be returned by a middleware when the pipe execution should be stopped Stop MiddlewareResult = iota // Next will be returned by a middleware when the pipe execution should be continued Next MiddlewareResult = iota )
type NetworkConnection ¶
type NetworkConnection struct {
// contains filtered or unexported fields
}
NetworkConnection is the main entry point to the p2p network
func NewNetworkConnectionTCP ¶
func NewNetworkConnectionTCP(localAddr string, routes RoutingTable) *NetworkConnection
NewNetworkConnectionTCP provides a full configured TCP based network It use the _DefaultMiddleware_ a TCP based operator and the following middleware:
Routes, Headers, Crypt, Log
func (*NetworkConnection) ConnectTo ¶
func (nc *NetworkConnection) ConnectTo(network string, addr string)
ConnectTo will Dial the provided peer by the given network
func (*NetworkConnection) DisconnectFrom ¶
func (nc *NetworkConnection) DisconnectFrom(addr string)
DisconnectFrom will disconnects the given peer
func (*NetworkConnection) OnMessage ¶
func (nc *NetworkConnection) OnMessage(handler func(p *Peer, msg *Message))
OnMessage regsiters the given handler and call it when a new message is received
func (*NetworkConnection) OnPeer ¶
func (nc *NetworkConnection) OnPeer(handler func(p *Peer))
OnPeer registers the provided handler and call it when a new peer connection is created
func (*NetworkConnection) OnPeerDisconnect ¶
func (nc *NetworkConnection) OnPeerDisconnect(handler func(p *Peer))
OnPeerDisconnect regsiters the given handler and call it when an the connection is lost
func (*NetworkConnection) OnPeerError ¶
func (nc *NetworkConnection) OnPeerError(handler func(p *Peer, err error))
OnPeerError regsiters the given handler and call it when an error during the peer communication occurs
func (*NetworkConnection) Send ¶
func (nc *NetworkConnection) Send(msg *Message, addr string)
Send will send the provided message to the given address
func (*NetworkConnection) SendBroadcast ¶
func (nc *NetworkConnection) SendBroadcast(msg *Message)
SendBroadcast will send the given message to all peers
func (*NetworkConnection) Start ¶
func (nc *NetworkConnection) Start() error
Start will start up the p2p network stack
func (*NetworkConnection) Stop ¶
func (nc *NetworkConnection) Stop()
Stop will shutdown the entire p2p network stack
type NetworkConnectionBuilder ¶
type NetworkConnectionBuilder struct {
// contains filtered or unexported fields
}
NetworkConnectionBuilder provides a fluent interface to create a NetworkConnection
func NewNetworkConnection ¶
func NewNetworkConnection() *NetworkConnectionBuilder
NewNetworkConnection creates a new NetworkBuilder instance to setup a new NetworkConnection
func (*NetworkConnectionBuilder) Build ¶
func (b *NetworkConnectionBuilder) Build() *NetworkConnection
Build finalize the NetworkConnection setup and creates the new instance
func (*NetworkConnectionBuilder) WithMiddleware ¶
func (b *NetworkConnectionBuilder) WithMiddleware(name string, impl MiddlewareFunc) *NetworkConnectionBuilder
WithMiddleware attach a new Middleware to the NetworkConnection setup
func (*NetworkConnectionBuilder) WithOperator ¶
func (b *NetworkConnectionBuilder) WithOperator(op PeerOperator) *NetworkConnectionBuilder
WithOperator attach a new PeerOperator to the NetworkConnection setup
type OperatorTCP ¶
type OperatorTCP struct {
// contains filtered or unexported fields
}
OperatorTCP is an implementation of the PeerOperator inteface that handles TCP based connections. It use an net.Listener for incoming connections and and tcp.Dialer for outgoing.
func NewTCPOperator ¶
func NewTCPOperator(network string, localAddr string) *OperatorTCP
NewTCPOperator creates a new TCP based PeerOperator instance
func (*OperatorTCP) Dial ¶
func (o *OperatorTCP) Dial(network string, addr string) error
Dial connects to the address by the given network
func (*OperatorTCP) OnError ¶
func (o *OperatorTCP) OnError(handler func(err error))
OnError registers the given handler and calls it when a peer error occurs
func (*OperatorTCP) OnPeer ¶
func (o *OperatorTCP) OnPeer(handler func(p Adapter))
OnPeer registers the given handler and calls it when a new peer connection is established
func (*OperatorTCP) Start ¶
func (o *OperatorTCP) Start() error
Start will start the net.Listener and waits for incoming connections
func (*OperatorTCP) Stop ¶
func (o *OperatorTCP) Stop()
Stop will close the underlining net.Listener
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer represents a connection to a remote peer
func (*Peer) LocalAddress ¶
LocalAddress returns the local address of the current peer
func (*Peer) RemoteAddress ¶
RemoteAddress returns the remote address of the current peer
type PeerOperator ¶
type PeerOperator interface { // Dial connects to the given address by the given network Dial(network string, addr string) error // OnPeer registers a handler function that should be called // when a new peer connection is established OnPeer(handler func(p Adapter)) // Start the background listening jobs for the operator Start() error // Stop the background listening jobs for the operator Stop() }
PeerOperator connect peers to the current network connection I provides functionalities for dialing (active connection) and listening (passive connections) over a protocol (tcp/udp/etc)
type Pipe ¶
type Pipe struct {
// contains filtered or unexported fields
}
Pipe handles the processing of an message
func (*Pipe) Operation ¶
func (p *Pipe) Operation() PipeOperation
Operation returns the current pipe operation (Send or Receive)
type PipeOperation ¶
type PipeOperation int
PipeOperation represents the pipe direction (Send or Receive)
const ( // Send represents an outgoing message pipe processing Send PipeOperation = iota // Receive represents an incoming message pipe processing Receive PipeOperation = iota )
func (PipeOperation) String ¶
func (po PipeOperation) String() string
type RoutingTable ¶
RoutingTable represents handler registered by a path. A message will be checked for the existence of an annotation with the name "__routes_path" and this value will be used to find a route within the routing table