Repo-Init
 
Loading...
Searching...
No Matches
ZeroMQ Class Reference

#include <ZeroMQ.hpp>

Inheritance diagram for ZeroMQ:

Public Member Functions

 ZeroMQ (const zmq::socket_type &type, const std::string &addr, bool isBind)
 
 ZeroMQ (const std::shared_ptr< zmq::context_t > &ctx, const zmq::socket_type &type, const std::string &addr, bool isBind)
 
 ZeroMQ (const ZeroMQ &)=delete
 Copy constructor.
 
 ZeroMQ (ZeroMQ &&)=delete
 Move constructor.
 
ZeroMQoperator= (ZeroMQ)=delete
 Copy assignment operator.
 
ZeroMQoperator= (ZeroMQ &&)=delete
 Move assignment operator.
 
bool start ()
 
void stop ()
 
std::vector< zmq::message_t > recvMessages ()
 
size_t sendMessages (std::vector< zmq::message_t > &msg)
 
const std::unique_ptr< zmq::socket_t > & getSocket () const
 
const std::shared_ptr< zmq::context_t > & getContext () const
 
const std::string & getAddress () const
 
 ~ZeroMQ ()
 

Private Member Functions

void init (const std::shared_ptr< zmq::context_t > &ctx, const zmq::socket_type &type, const std::string_view &addr, bool isBind)
 

Private Attributes

std::shared_ptr< zmq::context_t > _contextPtr
 
std::unique_ptr< zmq::socket_t > _socketPtr
 
bool _isActive {false}
 
bool _isBinded {false}
 
std::string _socketAddr
 

Detailed Description

A class that provides a wrapper for ZeroMQ functionality.

This class encapsulates the ZeroMQ library and provides methods to initialize, start, stop, send, and receive messages using ZeroMQ sockets. It supports both binding and connecting to sockets and provides a convenient interface for working with multipart messages.

Definition at line 13 of file ZeroMQ.hpp.

Constructor & Destructor Documentation

◆ ZeroMQ() [1/4]

ZeroMQ::ZeroMQ ( const zmq::socket_type & type,
const std::string & addr,
bool isBind )

Construct a new ZeroMQ class

Parameters
[in]typeType of the socket
[in]addrFull socket address
[in]isBindTrue if should be binded, false if should be connected

Definition at line 32 of file ZeroMQ.cpp.

33{
34 init(std::make_shared<zmq::context_t>(1), type, addr, isBind);
35}
void init(const std::shared_ptr< zmq::context_t > &ctx, const zmq::socket_type &type, const std::string_view &addr, bool isBind)
Definition ZeroMQ.cpp:14
Here is the call graph for this function:

◆ ZeroMQ() [2/4]

ZeroMQ::ZeroMQ ( const std::shared_ptr< zmq::context_t > & ctx,
const zmq::socket_type & type,
const std::string & addr,
bool isBind )

Construct a new ZeroMQ class

Parameters
[in]ctxZeroMQ context
[in]typeType of the socket
[in]addrFull socket address
[in]isBindTrue if should be binded, false if should be connected

Definition at line 37 of file ZeroMQ.cpp.

39{
40 init(ctx, type, addr, isBind);
41}
Here is the call graph for this function:

◆ ZeroMQ() [3/4]

ZeroMQ::ZeroMQ ( const ZeroMQ & )
delete

Copy constructor.

◆ ZeroMQ() [4/4]

ZeroMQ::ZeroMQ ( ZeroMQ && )
delete

Move constructor.

◆ ~ZeroMQ()

ZeroMQ::~ZeroMQ ( )

Destroy the ZeroMQ class

Definition at line 111 of file ZeroMQ.cpp.

112{
113 try
114 {
115 stop();
116 }
117 catch (const std::exception &e)
118 {
119 try
120 {
121 spdlog::error("Error while stopping ZeroMQ connection {} ({})", _socketAddr, e.what());
122 }
123 catch (const std::exception &e2)
124 {
125 std::cerr << "Error while stopping ZeroMQ connection and logger for connection " << _socketAddr << " ("
126 << e.what() << ")" << '\n'
127 << e2.what() << '\n';
128 }
129 }
130}
void stop()
Definition ZeroMQ.cpp:63
std::string _socketAddr
Definition ZeroMQ.hpp:25
Here is the call graph for this function:

Member Function Documentation

◆ getAddress()

const std::string & ZeroMQ::getAddress ( ) const
inlinenodiscard

Get the address of the socket

Returns
const std::string& Address of the socket

Definition at line 102 of file ZeroMQ.hpp.

102{ return _socketAddr; }

◆ getContext()

const std::shared_ptr< zmq::context_t > & ZeroMQ::getContext ( ) const
inlinenodiscard

Get the reference of context

Returns
const std::shared_ptr<zmq::context_t>&

Definition at line 96 of file ZeroMQ.hpp.

96{ return _contextPtr; }
std::shared_ptr< zmq::context_t > _contextPtr
Definition ZeroMQ.hpp:16

◆ getSocket()

const std::unique_ptr< zmq::socket_t > & ZeroMQ::getSocket ( ) const
inlinenodiscard

Get the reference of socket

Returns
const std::unique_ptr<zmq::socket_t>&

Definition at line 90 of file ZeroMQ.hpp.

90{ return _socketPtr; }
std::unique_ptr< zmq::socket_t > _socketPtr
Definition ZeroMQ.hpp:18
Here is the caller graph for this function:

◆ init()

void ZeroMQ::init ( const std::shared_ptr< zmq::context_t > & ctx,
const zmq::socket_type & type,
const std::string_view & addr,
bool isBind )
private

Definition at line 14 of file ZeroMQ.cpp.

16{
17 _contextPtr = ctx;
18 _socketAddr = addr;
19 _isBinded = isBind;
20 _isActive = false;
21
22 // Init ZMQ connection
23 _socketPtr = std::make_unique<zmq::socket_t>(*_contextPtr, type);
24 _socketPtr->set(zmq::sockopt::linger, 0);
25 _socketPtr->set(zmq::sockopt::sndtimeo, ZEROMQ_MSG_TIMEOUT_MS);
26 _socketPtr->set(zmq::sockopt::rcvtimeo, ZEROMQ_MSG_TIMEOUT_MS);
27 _socketPtr->set(zmq::sockopt::heartbeat_ivl, ZEROMQ_HEARTBEAT_TIMEOUT_MS);
28 _socketPtr->set(zmq::sockopt::heartbeat_ttl, ZEROMQ_HEARTBEAT_TIMEOUT_MS * 3);
29 _socketPtr->set(zmq::sockopt::heartbeat_timeout, ZEROMQ_HEARTBEAT_TIMEOUT_MS);
30}
constexpr int ZEROMQ_HEARTBEAT_TIMEOUT_MS
Definition ZeroMQ.cpp:12
constexpr int ZEROMQ_MSG_TIMEOUT_MS
Definition ZeroMQ.cpp:10
bool _isActive
Definition ZeroMQ.hpp:21
bool _isBinded
Definition ZeroMQ.hpp:23
Here is the caller graph for this function:

◆ operator=() [1/2]

ZeroMQ & ZeroMQ::operator= ( ZeroMQ && )
delete

Move assignment operator.

◆ operator=() [2/2]

ZeroMQ & ZeroMQ::operator= ( ZeroMQ )
delete

Copy assignment operator.

◆ recvMessages()

std::vector< zmq::message_t > ZeroMQ::recvMessages ( )

Receives multipart message

Returns
std::vector<zmq::message_t> Received messages

Definition at line 81 of file ZeroMQ.cpp.

82{
83 std::vector<zmq::message_t> recvMsgs;
84 if (!_isActive)
85 {
86 spdlog::warn("Connection needs to starting");
87 }
88 else
89 {
90 auto nMsgs = zmq::recv_multipart(*_socketPtr, std::back_inserter(recvMsgs));
91 spdlog::debug("Received {} messages", nMsgs.value_or(0));
92 }
93 return recvMsgs;
94}
Here is the caller graph for this function:

◆ sendMessages()

size_t ZeroMQ::sendMessages ( std::vector< zmq::message_t > & msg)

Sends multipart message

Parameters
[in]msgMessages to send
Returns
size_t Number of sent messages

Definition at line 96 of file ZeroMQ.cpp.

97{
98 zmq::send_result_t res;
99 if (!_isActive)
100 {
101 spdlog::warn("Connection needs to starting");
102 }
103 else
104 {
105 res = zmq::send_multipart(*_socketPtr, msg);
106 }
107
108 return res.value_or(0);
109}
Here is the caller graph for this function:

◆ start()

bool ZeroMQ::start ( )

Starts the connection

Returns
True if successfully initialized

Definition at line 43 of file ZeroMQ.cpp.

44{
45 if (_isActive)
46 {
47 return false;
48 }
49
50 if (_isBinded)
51 {
53 }
54 else
55 {
56 _socketPtr->connect(_socketAddr);
57 }
58 _isActive = true;
59
60 return true;
61}
Here is the caller graph for this function:

◆ stop()

void ZeroMQ::stop ( )

Stops the connection

Definition at line 63 of file ZeroMQ.cpp.

64{
65 if (!_isActive)
66 {
67 return;
68 }
69
70 if (_isBinded)
71 {
72 _socketPtr->unbind(_socketAddr);
73 }
74 else
75 {
76 _socketPtr->disconnect(_socketAddr);
77 }
78 _isActive = false;
79}
Here is the caller graph for this function:

Member Data Documentation

◆ _contextPtr

std::shared_ptr<zmq::context_t> ZeroMQ::_contextPtr
private

Definition at line 16 of file ZeroMQ.hpp.

◆ _isActive

bool ZeroMQ::_isActive {false}
private

Definition at line 21 of file ZeroMQ.hpp.

21{false};

◆ _isBinded

bool ZeroMQ::_isBinded {false}
private

Definition at line 23 of file ZeroMQ.hpp.

23{false};

◆ _socketAddr

std::string ZeroMQ::_socketAddr
private

Definition at line 25 of file ZeroMQ.hpp.

◆ _socketPtr

std::unique_ptr<zmq::socket_t> ZeroMQ::_socketPtr
private

Definition at line 18 of file ZeroMQ.hpp.


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