signaler

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

  • ORBIT - Interlink Remote Applications *
  • The MIT License (MIT) *
  • Copyright (c) 2018 Roland Singer <roland.singer[at]desertbit.com>
  • Copyright (c) 2018 Sebastian Borchers <sebastian[at]desertbit.com> *
  • Permission is hereby granted, free of charge, to any person obtaining a copy
  • of this software and associated documentation files (the "Software"), to deal
  • in the Software without restriction, including without limitation the rights
  • to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  • copies of the Software, and to permit persons to whom the Software is
  • furnished to do so, subject to the following conditions: *
  • The above copyright notice and this permission notice shall be included in all
  • copies or substantial portions of the Software. *
  • THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  • IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  • FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  • AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  • LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  • OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  • SOFTWARE.

Package signaler makes it possible to send events between connected peers in a network. It uses the orbit control package to send and register these events (https://github.com/desertbit/orbit/control).

The signals can carry a payload.

Registering signals

Each peer can register signals during the initialization, which are then available to the remote peer to listen to. A signal must have a unique identifier.

Listening on signals

The remote peer can then listen on the registered signals by using either the On- functions (getting notified every time the signal is triggered) or the Once- functions (getting notified only once). At each point in time, a remote peer can also stop listening on signals to not receive any notifications after that.

Filtering

It is possible when adding a new signal to set a filter function on a signal. Peers can then set filter data on this signal that gets passed to this filter func. Depending on the return of the filter func, the signal may not be triggered for this peer, if its filter is not fulfilled. This allows peers to filter the signals to their liking and prevents a waste of resources.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSignalNotFound is an error indicating that a signal could not be found.
	ErrSignalNotFound = errors.New("signal not found")
	// ErrFilterFuncUndefined is an error indicating that a signal is missing its
	// filter func.
	ErrFilterFuncUndefined = errors.New("could not set filter, filter func is missing")
)
View Source
var (
	// ErrNoContextData is an error indicating that no context data is available.
	ErrNoContextData = errors.New("no context data available to decode")
)

Functions

This section is empty.

Types

type Context

type Context struct {
	// Data is the raw byte representation of the encoded context data.
	Data []byte
	// contains filtered or unexported fields
}

The Context type defines a signal context carrying the payload data that has been sent in the trigger request. It offers a convenience wrapper to the data, as it wraps its codec and offers easy access to the decoded data.

func (*Context) Decode

func (c *Context) Decode(v interface{}) error

Decode decodes the context data to a custom value using the internal codec. The value has to be passed as pointer. Returns ErrNoContextData if there is no context data available to decode.

type Filter

type Filter func(data interface{}) (conforms bool, err error)

The Filter type is a function that takes the trigger data and returns whether the data conforms to some predefined conditions, and is allowed to be triggered. This func is returned by the FilterFunc to create a static filter that is fed with dynamic data at runtime.

type FilterFunc

type FilterFunc func(ctx *Context) (f Filter, err error)

The FilterFunc type takes a request context and returns a new filter func that will be set as the signal's filter.

type Listener

type Listener struct {
	// C is filled up with the trigger actions from the signaler.
	// Use OffChan() to stop your reading routine.
	C <-chan *Context
	// contains filtered or unexported fields
}

The Listener type represents one party that is interested in being notified, when a specific signal gets triggered. It offers a public channel that contains the context of a trigger, each time it gets triggered.

func (*Listener) Off

func (l *Listener) Off()

Off closes this listener, meaning that it will no longer receive triggers for the signal and gets removed from its listeners.

func (*Listener) OffChan

func (l *Listener) OffChan() <-chan struct{}

OffChan is a public getter to the listener's closer channel. It allows users of this package to listen to the event, that their listener gets closed.

type Signaler

type Signaler struct {
	closer.Closer
	// contains filtered or unexported fields
}

The Signaler type is the main type to interact with signals on one peer. It contains the underlying control.Control, codecs, loggers, etc. It takes care of storing signals that have been added to it and keeps track of who is listening on which signal.

func New

func New(conn net.Conn, config *control.Config) (s *Signaler)

New returns a new Signaler.

func (*Signaler) AddSignal

func (s *Signaler) AddSignal(id string)

AddSignal adds a signal with the given id to the signaler.

func (*Signaler) AddSignalFilter

func (s *Signaler) AddSignalFilter(id string, filterFunc FilterFunc)

AddSignalFilter adds a signal with the given id to the signaler. In addition, a filter func is given that is directly set as filter on this signal.

func (*Signaler) AddSignals

func (s *Signaler) AddSignals(ids []string)

AddSignals adds the signals with the given ids to the signaler.

func (*Signaler) OnSignal

func (s *Signaler) OnSignal(id string) *Listener

OnSignal adds a listener to the signal with the given id. The Listener will output a request context on its channel when the signal has been triggered.

func (*Signaler) OnSignalFunc

func (s *Signaler) OnSignalFunc(id string, f func(ctx *Context)) *Listener

OnSignalFunc adds a listener like OnSignal() does, but takes a function that is executed whenever the signal is triggered.

func (*Signaler) OnSignalOpts

func (s *Signaler) OnSignalOpts(id string, channelSize int) *Listener

OnSignalOpts adds a listener like OnSignal() does, but allows to configure the channel size of the listener. This determines how many events can be buffered in the listener before the triggerSignal handler func will block and further events can not be processed. The size of the channel must be greater than 0 (Unbuffered channels are not allowed).

func (*Signaler) OnceSignal

func (s *Signaler) OnceSignal(id string) *Listener

OnceSignal adds a listener like OnSignal() does, but the listener can only be triggered once and gets removed after the first event.

func (*Signaler) OnceSignalFunc

func (s *Signaler) OnceSignalFunc(id string, f func(ctx *Context)) *Listener

OnSignalFunc adds a listener like OnceSignal() does, but takes a function that is executed when the signal is triggered.

func (*Signaler) OnceSignalOpts

func (s *Signaler) OnceSignalOpts(id string, channelSize int) *Listener

OnceSignalOpts adds a listener like OnceSignal() does, but allows to configure the channel size of the listener. This determines how many events can be buffered in the listener before the triggerSignal handler func will block and further events can not be processed. The size of the channel must be greater than 0 (Unbuffered channels are not allowed).

func (*Signaler) Ready

func (s *Signaler) Ready()

Ready signalizes that the initialization is done. Signaler can now be triggered. This should be only called once.

func (*Signaler) SetSignalFilter

func (s *Signaler) SetSignalFilter(id string, data interface{}) (err error)

SetSignalFilter sets the filter data on the signal with the given id. The data will be passed to the filter function and determine, when the signal is triggered. Returns ErrFilterFuncUndefined, if no filter func is defined on the signal. Returns ErrSignalNotFound, if the signal does not exist.

func (*Signaler) TriggerSignal

func (s *Signaler) TriggerSignal(id string, data interface{}) (err error)

TriggerSignal triggers the signal with the given id and sends the given data along to the peers. If data is nil, the signal carries no payload. Returns ErrSignalNotFound, if the signal could not be found.

Jump to

Keyboard shortcuts

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