Commit 2ab325b8 authored by legoc's avatar legoc
Browse files

Corrected implementation of timeout in RequestSocketImpl

parent 22fcb678
......@@ -100,27 +100,23 @@ const std::string& Services::getStatusEndpoint() const {
}
bool Services::isAvailable(int timeout) const {
string strRequestType = m_impl->createRequestType(PROTO_INIT);
string strRequestData = m_impl->createInitRequest();
return m_impl->isAvailable(strRequestType, strRequestData, m_serverEndpoint, timeout);
return m_impl->isAvailable(m_requestSocket.get(), timeout);
}
void Services::initStatus() {
// Get the status port.
string requestTypePart = m_impl->createRequestType(PROTO_STATUS);
string requestDataPart = m_impl->createShowStatusRequest();
unique_ptr<zmq::message_t> reply = m_requestSocket->request(requestTypePart, requestDataPart);
unique_ptr<zmq::message_t> reply = m_requestSocket->request(m_impl->createRequestType(PROTO_STATUS), m_impl->createShowStatusRequest());
proto::RequestResponse requestResponse;
requestResponse.ParseFromArray((*reply).data(), (*reply).size());
// reply ok
// Check response.
if (requestResponse.value() == -1) {
return;
}
// Get the status port.
m_statusPort = requestResponse.value();
stringstream ss;
......
......@@ -51,7 +51,7 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
if (timeout > 0) {
// Polling.
zmq_pollitem_t items[1];
items[0].socket = static_cast<void *>(m_socket.get());
items[0].socket = static_cast<void *>(*m_socket.get());
items[0].fd = 0;
items[0].events = ZMQ_POLLIN;
items[0].revents = 0;
......
......@@ -20,6 +20,7 @@
#include <sstream>
#include "../SocketException.h"
#include "../ConnectionTimeout.h"
#include "RequestSocketImpl.h"
// Using Visual Studio preprocessor.
// It must be improved in case of other compilers.
......@@ -505,4 +506,23 @@ void ServicesImpl::subscribeToPublisher(const std::string& endpoint) {
requestResponse.ParseFromArray((*reply).data(), (*reply).size());
}
bool ServicesImpl::isAvailable(RequestSocketImpl * socket, int timeout) {
string requestTypePart = createRequestType(PROTO_INIT);
string requestDataPart = createInitRequest();
try {
unique_ptr<zmq::message_t> reply = socket->request(requestTypePart, requestDataPart, timeout);
if (reply.get() != nullptr) {
return true;
}
} catch (const ConnectionTimeout&) {
// The server is not accessible.
}
return false;
}
}
......@@ -25,6 +25,8 @@
namespace cameo {
class RequestSocketImpl;
class ServicesImpl {
public:
......@@ -72,6 +74,8 @@ public:
void waitForSubscriber(zmq::socket_t * subscriber, const std::string& strRequestType, const std::string& strRequestData, const std::string& endpoint);
void subscribeToPublisher(const std::string& endpoint);
bool isAvailable(RequestSocketImpl * socket, int timeout);
zmq::context_t m_context;
int m_timeout;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment