Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect/close/endpoint method to ZmqClient/ZmqServer. #868

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions common/zmqclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@ ZmqClient::ZmqClient(const std::string& endpoint)

ZmqClient::~ZmqClient()
{
std::lock_guard<std::mutex> lock(m_socketMutex);
if (m_socket)
{
int rc = zmq_close(m_socket);
if (rc != 0)
{
SWSS_LOG_ERROR("failed to close zmq socket, zmqerrno: %d",
zmq_errno());
}
}

if (m_context)
{
zmq_ctx_destroy(m_context);
}
close();
}

void ZmqClient::initialize(const std::string& endpoint)
Expand All @@ -48,6 +34,11 @@ void ZmqClient::initialize(const std::string& endpoint)

connect();
}

std::string ZmqClient::endpoint()
{
return m_endpoint;
}

bool ZmqClient::isConnected()
{
Expand Down Expand Up @@ -102,6 +93,30 @@ void ZmqClient::connect()
m_connected = true;
}

void ZmqClient::close()
{
std::lock_guard<std::mutex> lock(m_socketMutex);
if (m_socket)
{
int rc = zmq_close(m_socket);
if (rc != 0)
{
SWSS_LOG_ERROR("failed to close zmq socket, zmqerrno: %d",
zmq_errno());
}

m_socket = nullptr;
}

if (m_context)
{
zmq_ctx_destroy(m_context);
m_context = nullptr;
}

m_connected = false;
}

void ZmqClient::sendMsg(
const std::string& dbName,
const std::string& tableName,
Expand Down
4 changes: 4 additions & 0 deletions common/zmqclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ class ZmqClient

bool isConnected();

std::string endpoint();

void connect();

void close();

void sendMsg(const std::string& dbName,
const std::string& tableName,
const std::vector<KeyOpFieldsValuesTuple>& kcos,
Expand Down
30 changes: 28 additions & 2 deletions common/zmqserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,42 @@ ZmqServer::ZmqServer(const std::string& endpoint)
: m_endpoint(endpoint)
{
m_buffer.resize(MQ_RESPONSE_MAX_COUNT);
m_runThread = true;
m_mqPollThread = std::make_shared<std::thread>(&ZmqServer::mqPollThread, this);
connect();

SWSS_LOG_DEBUG("ZmqServer ctor endpoint: %s", endpoint.c_str());
}

ZmqServer::~ZmqServer()
{
close();
}

std::string ZmqServer::endpoint()
{
return m_endpoint;
}

void ZmqServer::connect()
{
if (m_mqPollThread)
{
return;
}

m_runThread = true;
m_mqPollThread = std::make_shared<std::thread>(&ZmqServer::mqPollThread, this);
}

void ZmqServer::close()
{
if (!m_mqPollThread)
{
return;
}

m_runThread = false;
m_mqPollThread->join();
m_mqPollThread = nullptr;
}

void ZmqServer::registerMessageHandler(
Expand Down
6 changes: 6 additions & 0 deletions common/zmqserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class ZmqServer
ZmqServer(const std::string& endpoint);
~ZmqServer();

std::string endpoint();

void connect();

void close();

void registerMessageHandler(
const std::string dbName,
const std::string tableName,
Expand Down
2 changes: 2 additions & 0 deletions tests/zmq_state_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ static void producerBatchWorker(string tableName, string endpoint, bool dbPersis
}
}

client.close();
cout << "Producer thread ended: " << tableName << endl;
}

Expand Down Expand Up @@ -268,6 +269,7 @@ static void consumerWorker(string tableName, string endpoint, bool dbPersistence
}
}

server.close();
cout << "Consumer thread ended: " << tableName << endl;
}

Expand Down
Loading