Chatsync  bbffde5c0c672a526bdc83637acf66dc20a80fbf
channeling::Channel Class Referenceabstract

#include <channel.hpp>

Inheritance diagram for channeling::Channel:
fileChannel::FileChannel ircChannel::IrcChannel toxChannel::ToxChannel

Public Member Functions

 Channel (Hub::Hub *const hub, const std::string &config)
 
virtual ~Channel ()
 
virtual std::string const & name () const
 
virtual std::string type () const =0
 
ChannelDirection direction () const
 
virtual std::future< void > activate ()=0
 
virtual int connect (const std::string &hostname, const uint32_t port) const
 
virtual int send (const uint32_t fd, const std::string &msg) const
 
virtual int send (const std::string &msg) const
 
virtual int disconnect (const uint32_t fd) const
 
virtual int disconnect () const
 
virtual void tick ()
 

Data Fields

const uint16_t _id
 

Protected Member Functions

virtual void pollThread ()
 
virtual void incoming (const message_ptr &&msg)=0
 
virtual const message_ptr parse (const char *line) const =0
 
void startPolling ()
 
void stopPolling ()
 

Protected Attributes

std::atomic_bool _active
 
std::unique_ptr< std::thread > _thread
 
std::atomic_bool _pipeRunning
 
int _fd
 
const config::ConfigParser _config
 
const std::string _name
 
const ChannelDirection _direction
 
Hub::Hub *const _hub
 

Private Member Functions

void reconnect ()
 

Private Attributes

unsigned int _reconnect_attempt
 
std::shared_ptr< std::atomic< bool > > _hub_alive
 

Friends

Channeloperator>> (const message_ptr, Channel &channel)
 

Detailed Description

A channel to connect to input or output

The deriving class must override: void void incoming(const message_ptr msg) = 0; — for data input in case of output channel void activate(); — to implement specific activities for channel work starting, _active should be set

Input channel should send message to hub->newMessage()

If channel implementation depends on any abstraction represented by file descriptor the pollThread() can be used: _fd : file descriptor, should be prepared during activate() startPolling() and stopPolling() functions start and stop the pollThread which reads _fd (using select()) and sends messages to _hub.

If channel has own abstraction then it can override _pollThread() and implement own message loop.

Constructor & Destructor Documentation

§ Channel()

channeling::Channel::Channel ( Hub::Hub *const  hub,
const std::string &  config 
)
Parameters
hubHub to add channel to
configconfiguration data or path (see config.hpp for details)

§ ~Channel()

channeling::Channel::~Channel ( )
virtual

Member Function Documentation

§ activate()

virtual std::future<void> channeling::Channel::activate ( )
pure virtual

Prepare all prerequisites for polling thread or prepare the output and start working

Opening file descriptors, connecting to network must be done here. After future is valid channel is considered to be ready for work.

Exceptions
activate_errorin case of problems

Implemented in ircChannel::IrcChannel, fileChannel::FileChannel, and toxChannel::ToxChannel.

§ connect()

int channeling::Channel::connect ( const std::string &  hostname,
const uint32_t  port 
) const
virtual

Open socket to server:port using IPv4 and then (if failed) IPv6

Parameters
hostnamehostname to connect to
portport to use
Exceptions
activate_errorwith message in case of problems

§ direction()

ChannelDirection channeling::Channel::direction ( ) const
inline

Return a channel direction

Return values
Transmissiondirection

§ disconnect() [1/2]

int channeling::Channel::disconnect ( const uint32_t  fd) const
virtual

Close a socket

Parameters
fdSocket descriptor

§ disconnect() [2/2]

virtual int channeling::Channel::disconnect ( ) const
inlinevirtual

Close an _fd socket

§ incoming()

virtual void channeling::Channel::incoming ( const message_ptr &&  msg)
protectedpure virtual

Parse line and send it to needed output place in case of Output direction

Parameters
msgIncoming message from hub. Passed by value to preserve ownership and ensure existence during processing in all output channels.

Implemented in ircChannel::IrcChannel, toxChannel::ToxChannel, and fileChannel::FileChannel.

§ name()

std::string const & channeling::Channel::name ( ) const
virtual

Return channel name

Return values
Channelname

§ parse()

virtual const message_ptr channeling::Channel::parse ( const char *  line) const
protectedpure virtual

Parse a text line and generate a corresponding Message

Todo:
Decide if it should throw something
Parameters
lineText line from socket.
Return values
nullptrIf message was technical for protocol.
message_ptrIf useful payload found.

Implemented in ircChannel::IrcChannel, toxChannel::ToxChannel, and fileChannel::FileChannel.

§ pollThread()

void channeling::Channel::pollThread ( )
protectedvirtual

Thread which selects the descriptor and send messages when new ones come

Reimplemented in toxChannel::ToxChannel.

§ reconnect()

void channeling::Channel::reconnect ( )
private

Wait for _reconnect_timeout and try to call activate()

Function is called when descriptor _fd suddenly closes from outside.

§ send() [1/2]

int channeling::Channel::send ( const uint32_t  fd,
const std::string &  msg 
) const
virtual

Send a line msg to a socket fd

Parameters
fdSocket descriptor
msgline to send
Exceptions
std::runtime_errorwith message if socket fails

§ send() [2/2]

virtual int channeling::Channel::send ( const std::string &  msg) const
inlinevirtual

Send a line msg to a default socket _fd

Parameters
msgline to send

§ startPolling()

void channeling::Channel::startPolling ( )
protected

Start thread which polls the descriptor Channel::_fd

Descriptor must be opened before running.

Exceptions
std::runtime_error(ERR_FD)If descriptor is not opened.

§ stopPolling()

void channeling::Channel::stopPolling ( )
protected

Stop and join polling thread

§ tick()

virtual void channeling::Channel::tick ( )
inlinevirtual

Performs heartbeat actions on tick

Reimplemented in ircChannel::IrcChannel.

§ type()

virtual std::string channeling::Channel::type ( ) const
pure virtual

Return channel "type" — unique string to identify channel in config file

Return values
std::stringType line

Implemented in ircChannel::IrcChannel, toxChannel::ToxChannel, and fileChannel::FileChannel.

Friends And Related Function Documentation

§ operator>>

Channel& operator>> ( const message_ptr  msg,
Channel channel 
)
friend

Operator >> is used to push data into output channels

Example:

"Line" >> *output;
Exceptions
std::logic_erroron writing to input channel

Field Documentation

§ _active

std::atomic_bool channeling::Channel::_active
protected

Channel is prepared and active

§ _config

const config::ConfigParser channeling::Channel::_config
protected

Configuration storage

§ _direction

const ChannelDirection channeling::Channel::_direction
protected

The channel direction for the whole transmission task

§ _fd

int channeling::Channel::_fd
protected

File descriptor to select

§ _hub

Hub::Hub* const channeling::Channel::_hub
protected

Hub the channel is attached to

§ _hub_alive

std::shared_ptr<std::atomic<bool> > channeling::Channel::_hub_alive
private

The hub is alive and we can try reconnecting

§ _id

const uint16_t channeling::Channel::_id

Unique channel id

§ _name

const std::string channeling::Channel::_name
protected

The channel name in config file

§ _pipeRunning

std::atomic_bool channeling::Channel::_pipeRunning
protected

Pipe reading thread is running

§ _reconnect_attempt

unsigned int channeling::Channel::_reconnect_attempt
private

Number of reconnection attempt

§ _thread

std::unique_ptr<std::thread> channeling::Channel::_thread
protected

Pointer to reader thread in case of input channel


The documentation for this class was generated from the following files: