Chatsync
bbffde5c0c672a526bdc83637acf66dc20a80fbf
|
#include <channel.hpp>
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 | |
Channel & | operator>> (const message_ptr, Channel &channel) |
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.
channeling::Channel::Channel | ( | Hub::Hub *const | hub, |
const std::string & | config | ||
) |
hub | Hub to add channel to |
config | configuration data or path (see config.hpp for details) |
|
virtual |
|
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.
activate_error | in case of problems |
Implemented in ircChannel::IrcChannel, fileChannel::FileChannel, and toxChannel::ToxChannel.
|
virtual |
Open socket to server:port using IPv4 and then (if failed) IPv6
hostname | hostname to connect to |
port | port to use |
activate_error | with message in case of problems |
|
inline |
Return a channel direction
Transmission | direction |
|
virtual |
Close a socket
fd | Socket descriptor |
|
inlinevirtual |
Close an _fd socket
|
protectedpure virtual |
Parse line and send it to needed output place in case of Output direction
msg | Incoming 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.
|
virtual |
Return channel name
Channel | name |
|
protectedpure virtual |
Parse a text line and generate a corresponding Message
line | Text line from socket. |
nullptr | If message was technical for protocol. |
message_ptr | If useful payload found. |
Implemented in ircChannel::IrcChannel, toxChannel::ToxChannel, and fileChannel::FileChannel.
|
protectedvirtual |
Thread which selects the descriptor and send messages when new ones come
Reimplemented in toxChannel::ToxChannel.
|
private |
Wait for _reconnect_timeout and try to call activate()
Function is called when descriptor _fd suddenly closes from outside.
|
virtual |
Send a line msg to a socket fd
fd | Socket descriptor |
msg | line to send |
std::runtime_error | with message if socket fails |
|
inlinevirtual |
Send a line msg to a default socket _fd
msg | line to send |
|
protected |
Start thread which polls the descriptor Channel::_fd
Descriptor must be opened before running.
std::runtime_error(ERR_FD) | If descriptor is not opened. |
|
protected |
Stop and join polling thread
|
inlinevirtual |
Performs heartbeat actions on tick
Reimplemented in ircChannel::IrcChannel.
|
pure virtual |
Return channel "type" — unique string to identify channel in config file
std::string | Type line |
Implemented in ircChannel::IrcChannel, toxChannel::ToxChannel, and fileChannel::FileChannel.
|
friend |
Operator >> is used to push data into output channels
Example:
std::logic_error | on writing to input channel |
|
protected |
Channel is prepared and active
|
protected |
Configuration storage
|
protected |
The channel direction for the whole transmission task
|
protected |
File descriptor to select
|
private |
The hub is alive and we can try reconnecting
const uint16_t channeling::Channel::_id |
Unique channel id
|
protected |
The channel name in config file
|
protected |
Pipe reading thread is running
|
private |
Number of reconnection attempt
|
protected |
Pointer to reader thread in case of input channel