aegis.cpp
 All Classes Functions Variables Typedefs Enumerations Enumerator Friends Pages
Public Types | Public Member Functions | Public Attributes | List of all members
aegis::shards::shard_mgr Class Reference

Primary class for managing a bot interface. More...

#include <shard_mgr.hpp>

Public Types

using websocket = websocketpp::client< websocketpp::config::asio_tls_client >
 Type of a pointer to the Websocket++ client.
 
using connection_ptr = websocketpp::client< websocketpp::config::asio_tls_client >::connection_type::ptr
 Type of a pointer to the Websocket++ TLS connection.
 
using message_ptr = websocketpp::config::asio_client::message_type::ptr
 Type of a pointer to the Websocket++ message payload.
 
using t_on_message = std::function< void(websocketpp::connection_hdl hdl, std::string msg, shard *_shard)>
 Websocket on_message handler type.
 
using t_on_connect = std::function< void(websocketpp::connection_hdl hdl, shard *_shard)>
 Websocket on_connect handler type.
 
using t_on_close = std::function< void(websocketpp::connection_hdl hdl, shard *_shard)>
 Websocket on_close handler type.
 

Public Member Functions

 shard_mgr (std::string token, asio::io_context &_io, std::shared_ptr< spdlog::logger > log)
 Constructs the aegis object that tracks all of the shards, guilds, channels, and members. More...
 
 ~shard_mgr ()
 Destroys the shards, stops the asio::work object, destroys the websocket object, and attempts to join the rest_thread thread.
 
 shard_mgr (const shard_mgr &)=delete
 
 shard_mgr (shard_mgr &&)=delete
 
shard_mgroperator= (const shard_mgr &)=delete
 
void setup_callbacks (shard *_shard) noexcept
 Assign the message, connect, and close callbacks to the websocket object. More...
 
void debug_trace (shard *_shard, bool extended=false) noexcept
 Outputs the last 5 messages received from the gateway. More...
 
asio::io_context & get_io_context ()
 Get the internal (or external) io_service object. More...
 
void shutdown ()
 
websocketget_websocket () noexcept
 Get the internal Websocket++ instance. More...
 
bot_status get_state () const noexcept
 Get the current state of the shard manager. More...
 
void set_state (bot_status s) noexcept
 Set the current shard manager state. More...
 
std::string uptime () const noexcept
 Return bot uptime as {days hours minutes seconds}. More...
 
void send_all_shards (const std::string &msg)
 Send a websocket message to a single shard. More...
 
void send_all_shards (const json &msg)
 Send a websocket message to a single shard. More...
 
void start ()
 Start shard connections and shard status timer.
 
void set_on_message (t_on_message cb) noexcept
 Set handler for websocket messages. More...
 
void set_on_connect (t_on_connect cb) noexcept
 Set handler for websocket connections. More...
 
void set_on_close (t_on_close cb) noexcept
 Set handler for websocket disconnections. More...
 
void set_gateway_url (const std::string &url) noexcept
 Set the gateway url the shards will connect to. More...
 
std::string get_gateway_url () const noexcept
 Get the gateway url the shards will connect to. More...
 
void reset_shard (shard *_shard, shard_status _status=shard_status::closing) noexcept
 Resets the shard's state. More...
 
void queue_reconnect (shard *_shard) noexcept
 Queue the shard for reconnection. Typically only called internally. More...
 
void queue_reconnect (shard &_shard)
 Queue the shard for reconnection. Typically only called internally. More...
 
void connect (shard *_shard) noexcept
 Connect the shard to the gateway. Typically only called internally. More...
 
shardget_shard (uint16_t shard_id)
 Get the shard object. More...
 
const std::vector
< std::unique_ptr< shard > > & 
get_shards () const noexcept
 Get a const vector of all the shards. More...
 
void close (shard *_shard, int32_t code=1001, const std::string &reason="", shard_status connection_state=shard_status::closing) noexcept
 Close the shard's websocket connection. More...
 
void close (shard &_shard, int32_t code=1001, const std::string &reason="", shard_status connection_state=shard_status::closing) noexcept
 Close the shard's websocket connection. More...
 
uint32_t shard_count () const noexcept
 Get the amount of shards that exist. More...
 

Public Attributes

asio::io_context & _io_context
 Asio context.
 
std::string ws_gateway
 Gateway URL.
 
std::unordered_map
< std::string, uint64_t > 
message_count
 Websocket event message counters.
 
uint32_t force_shard_count
 Shard count to force manager to use.
 
uint32_t shard_max_count
 Shard count retrieved from gateway.
 
std::shared_ptr< spdlog::logger > log
 Logging instance.
 

Detailed Description

Primary class for managing a bot interface.

Only one instance of this object can exist safely

Constructor & Destructor Documentation

aegis::shards::shard_mgr::shard_mgr ( std::string  token,
asio::io_context &  _io,
std::shared_ptr< spdlog::logger >  log 
)

Constructs the aegis object that tracks all of the shards, guilds, channels, and members.

See also
shard
guild
channel
member
Parameters
tokenA string of the authentication token
_ioReference to asio::io_context
logstd::shared_ptr of spdlog::logger

Member Function Documentation

void aegis::shards::shard_mgr::close ( shard _shard,
int32_t  code = 1001,
const std::string &  reason = "",
shard_status  connection_state = shard_status::closing 
)
noexcept

Close the shard's websocket connection.

See also
aegis::shard_status
Parameters
_shardPointer to shard
codeWebsocket close code (default: 1001)
reasonWebsocket close reason (default: "")
connection_stateState the set the shard to after close (default: shard_status::Closing)
void aegis::shards::shard_mgr::close ( shard _shard,
int32_t  code = 1001,
const std::string &  reason = "",
shard_status  connection_state = shard_status::closing 
)
noexcept

Close the shard's websocket connection.

See also
aegis::shard_status
Parameters
_shardReference to shard
codeWebsocket close code (default: 1001)
reasonWebsocket close reason (default: "")
connection_stateState the set the shard to after close (default: shard_status::Closing)
void aegis::shards::shard_mgr::connect ( shard _shard)
noexcept

Connect the shard to the gateway. Typically only called internally.

Parameters
_shardPointer to shard
void aegis::shards::shard_mgr::debug_trace ( shard _shard,
bool  extended = false 
)
noexcept

Outputs the last 5 messages received from the gateway.

Parameters
_shardPointer to shard
extendedWhether extended info is output
std::string aegis::shards::shard_mgr::get_gateway_url ( ) const
noexcept

Get the gateway url the shards will connect to.

Returns
std::string Shard connection url
asio::io_context& aegis::shards::shard_mgr::get_io_context ( )

Get the internal (or external) io_service object.

Returns
Asio io_context
shard & aegis::shards::shard_mgr::get_shard ( uint16_t  shard_id)

Get the shard object.

See also
aegis::shards::shard
Parameters
shard_idInternal index of shard to retrieve
Returns
reference to aegis::shards::shard object
const std::vector<std::unique_ptr<shard> >& aegis::shards::shard_mgr::get_shards ( ) const
noexcept

Get a const vector of all the shards.

Returns
const std::vector of all the shards as an std::unique_ptr
bot_status aegis::shards::shard_mgr::get_state ( ) const
noexcept

Get the current state of the shard manager.

See also
aegis::bot_status
Returns
aegis::bot_status
websocket& aegis::shards::shard_mgr::get_websocket ( )
noexcept

Get the internal Websocket++ instance.

Returns
Websocket++ internal instance
void aegis::shards::shard_mgr::queue_reconnect ( shard _shard)
noexcept

Queue the shard for reconnection. Typically only called internally.

Parameters
_shardPointer to shard
void aegis::shards::shard_mgr::queue_reconnect ( shard _shard)

Queue the shard for reconnection. Typically only called internally.

Parameters
_shardReference to shard
void aegis::shards::shard_mgr::reset_shard ( shard _shard,
shard_status  _status = shard_status::closing 
)
noexcept

Resets the shard's state.

Parameters
_shardPointer to shard
void aegis::shards::shard_mgr::send_all_shards ( const std::string &  msg)

Send a websocket message to a single shard.

Parameters
msgJSON encoded message to be sent
void aegis::shards::shard_mgr::send_all_shards ( const json &  msg)

Send a websocket message to a single shard.

Parameters
msgJSON encoded message to be sent
void aegis::shards::shard_mgr::set_gateway_url ( const std::string &  url)
noexcept

Set the gateway url the shards will connect to.

Parameters
urlString to gateway url
void aegis::shards::shard_mgr::set_on_close ( t_on_close  cb)
noexcept

Set handler for websocket disconnections.

See also
t_on_close
Parameters
cbCallback
void aegis::shards::shard_mgr::set_on_connect ( t_on_connect  cb)
noexcept

Set handler for websocket connections.

See also
t_on_connect
Parameters
cbCallback
void aegis::shards::shard_mgr::set_on_message ( t_on_message  cb)
noexcept

Set handler for websocket messages.

See also
t_on_message
Parameters
cbCallback
void aegis::shards::shard_mgr::set_state ( bot_status  s)
noexcept

Set the current shard manager state.

See also
aegis::bot_status
Parameters
sState to set the shard manager to
void aegis::shards::shard_mgr::setup_callbacks ( shard _shard)
noexcept

Assign the message, connect, and close callbacks to the websocket object.

Parameters
_shardThe shard object this websocket belong to
uint32_t aegis::shards::shard_mgr::shard_count ( ) const
noexcept

Get the amount of shards that exist.

Returns
uint32_t of shard count
void aegis::shards::shard_mgr::shutdown ( )

Invokes a shutdown on the entire lib. Sets internal state to Shutdown, stops the asio::work object and propagates the Shutdown state along with closing all websockets within the shard vector

std::string aegis::shards::shard_mgr::uptime ( ) const
noexcept

Return bot uptime as {days hours minutes seconds}.

Returns
std::string of ## h ## m ## s formatted time

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