network

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2019 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Framework added in v0.0.5

type Framework interface {
	// Set anything up you want with Legion when the Listen method is called.
	// Should block until the framework is ready to accept messages.
	Configure(*Legion) error

	// Called before any message is passed to plugins
	ValidateMessage(*MessageContext) bool

	// Methods to interact with legion
	NewMessage(*MessageContext)
	PeerAdded(*PeerContext)
	PeerDisconnect(*PeerContext)
	Startup(*NetworkContext)
	Close(*NetworkContext)
}

Framework is an interface that allows you to modify the underlying communication of legion

type GenericFramework added in v0.0.5

type GenericFramework struct{}

GenericFramework is a type used to expose methods so a framework doesn't need to have all of the required methods (it is also used as the default framework)

func (*GenericFramework) Close added in v0.0.5

func (*GenericFramework) Close(ctx *NetworkContext)

Close is called when the network is shutdown

func (*GenericFramework) Configure added in v0.0.5

func (*GenericFramework) Configure(*Legion) error

Configure is used to set anything up you want with Legion (loading plugins etc), it is called at startup

func (*GenericFramework) NewMessage added in v0.0.5

func (*GenericFramework) NewMessage(ctx *MessageContext)

NewMessage is called when a message is received by the network

func (*GenericFramework) PeerAdded added in v0.0.5

func (*GenericFramework) PeerAdded(ctx *PeerContext)

PeerAdded is called when a peer is added to the network

func (*GenericFramework) PeerDisconnect added in v0.0.5

func (*GenericFramework) PeerDisconnect(ctx *PeerContext)

PeerDisconnect is called when a peer is deleted

func (*GenericFramework) Startup added in v0.0.5

func (*GenericFramework) Startup(ctx *NetworkContext)

Startup is called when the local peer starts listening

func (*GenericFramework) ValidateMessage added in v0.0.5

func (*GenericFramework) ValidateMessage(ctx *MessageContext) bool

ValidateMessage is called before any message is passed to plugins

type Legion

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

Legion is a type with methods to interface with the network

func NewLegion

func NewLegion(conf *config.LegionConfig, f Framework) *Legion

NewLegion creates a legion object from a config

func (*Legion) AddPeer

func (l *Legion) AddPeer(addresses ...utils.LegionAddress) error

AddPeer adds the specified peer(s) to the network by dialing it and opening a stream, as well as adding it to the list of all peers.

func (*Legion) Broadcast

func (l *Legion) Broadcast(message *transport.Message, addresses ...utils.LegionAddress)

Broadcast sends the message to all peers, unless a specified list of peers is provided

func (*Legion) BroadcastRandom

func (l *Legion) BroadcastRandom(message *transport.Message, n int)

BroadcastRandom broadcasts a message to N random peers

func (*Legion) DeletePeer

func (l *Legion) DeletePeer(addresses ...utils.LegionAddress) error

DeletePeer closes all connections to a peer(s) and removes it from all peer lists. Returns an error if there is an error closing one or more peers. No matter the error, there will be an attempt to close all peers.

func (*Legion) DoAllPeers

func (l *Legion) DoAllPeers(f func(p *Peer))

DoAllPeers runs the function f on all peers

func (*Legion) FireMessageEvent

func (l *Legion) FireMessageEvent(eventType events.MessageEvent, message *transport.Message)

FireMessageEvent fires a new message event and sends context to the correct plugin methods based on the event type

func (*Legion) FireNetworkEvent

func (l *Legion) FireNetworkEvent(eventType events.NetworkEvent)

FireNetworkEvent fires a network event and sends network context to the correct plugin method based on the event type. NOTE: This method blocks until all are completed

func (*Legion) FirePeerEvent

func (l *Legion) FirePeerEvent(eventType events.PeerEvent, peer *Peer, isIncoming bool)

FirePeerEvent fires a peer event and sends context to the correct plugin methods based on the event type

func (*Legion) Listen

func (l *Legion) Listen() error

Listen will listen on the configured address for incoming connections, it will also wait for all plugin's Startup() methods to return before binding.

func (*Legion) Me

func (l *Legion) Me() utils.LegionAddress

Me returns the local bindaddress

func (*Legion) NewMessage

func (l *Legion) NewMessage(messageType string, body []byte) *transport.Message

NewMessage returns a message with the sender field set to the bind address of the network and no extra data

func (*Legion) PeerExists

func (l *Legion) PeerExists(address utils.LegionAddress) bool

PeerExists returns whether or not a peer has been connected to previously

func (*Legion) Request added in v0.0.5

func (l *Legion) Request(message *transport.Message, timeout time.Duration, address utils.LegionAddress) (*transport.Message, error)

Request sends a request message to the specified peer

func (*Legion) Started

func (l *Legion) Started()

Started blocks until the network is running

func (*Legion) Stop

func (l *Legion) Stop() error

Stop closes the listener and fires the plugin stop event

type MessageContext

type MessageContext struct {
	Sender  utils.LegionAddress
	Message *transport.Message
	Legion  *Legion
}

MessageContext has context for a given message such as the legion object and methods to interact with the remote peer that sent the message

func (*MessageContext) Reply

func (mc *MessageContext) Reply(msg *transport.Message) error

Reply is a helper method to reply to an incoming message

type NetworkContext

type NetworkContext struct {
	Legion *Legion
}

NetworkContext is general context of the network, gives access to just the legion object and a few other helpers

type Peer

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

Peer is an type that allows easy communication with a remote peer

func NewPeer

func NewPeer(remote utils.LegionAddress) *Peer

NewPeer returns a new peer from the given remote. It also sets up the reading and writing channels

func (*Peer) BlockUntilDisconnected

func (p *Peer) BlockUntilDisconnected()

BlockUntilDisconnected blocks until the remote is disconnected

func (*Peer) Close

func (p *Peer) Close() error

Close closes the stream if it exists

func (*Peer) CreateClient

func (p *Peer) CreateClient(conn net.Conn) error

CreateClient takes an outgoing connection and creates a client session from it

func (*Peer) CreateServer

func (p *Peer) CreateServer(conn net.Conn) error

CreateServer takes an incoming connection and creates a server session from it

func (*Peer) IncomingMessages

func (p *Peer) IncomingMessages() chan *transport.Message

IncomingMessages registers a new listen channel and returns it

func (*Peer) QueueMessage

func (p *Peer) QueueMessage(m *transport.Message)

QueueMessage queues the specified message to be sent to the remote

func (*Peer) QueueReply added in v0.0.5

func (p *Peer) QueueReply(rpcID uint64, m *transport.Message)

QueueReply queues the specified message to be sent to the remote and appends the desired rpcid

func (*Peer) Remote

func (p *Peer) Remote() utils.LegionAddress

Remote returns the address of the remote peer

func (*Peer) Request added in v0.0.5

func (p *Peer) Request(timeout time.Duration, m *transport.Message) (*transport.Message, error)

Request will ask a remote peer and wait for the response

type PeerContext

type PeerContext struct {
	Legion     *Legion
	Peer       *Peer
	IsIncoming bool
}

PeerContext has context for a peer event such as the legion object and the peer change that fired the event

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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