Commit fc6e576d authored by legoc's avatar legoc
Browse files

Request has linger value but just to remember the code because tests show the...

Request has linger value but just to remember the code because tests show the value is no interest in our case
parent 791720c5
......@@ -4,8 +4,8 @@
#
# -----------------------------------------------------------------------------
AC_INIT(cameo-api-cpp, 0.2.2)
LIBRARY_VERSION=0:2:2
AC_INIT(cameo-api-cpp, 0.3.0)
LIBRARY_VERSION=0:3:0
AC_CONFIG_AUX_DIR(config)
AC_CONFIG_SRCDIR(src/cameo/Application.cpp)
......
......@@ -968,8 +968,8 @@ Request::Request(std::unique_ptr<RequestImpl> & impl) :
Request::~Request() {
}
void Request::setTimeout(int value) {
m_impl->setTimeout(value);
void Request::setTimeout(int value, bool linger) {
m_impl->setTimeout(value, linger);
}
const std::string& Request::getBinary() const {
......
......@@ -396,7 +396,7 @@ public:
std::string get() const;
const std::string& getSecondBinaryPart() const;
void setTimeout(int value);
void setTimeout(int value, bool linger = true);
void replyBinary(const std::string& response);
void reply(const std::string& response);
......
......@@ -31,7 +31,8 @@ RequestImpl::RequestImpl(application::This * application, const std::string & re
m_message(message),
m_requesterApplicationName(requesterApplicationName),
m_requesterApplicationId(requesterApplicationId),
m_timeout(0) {
m_timeout(0),
m_linger(true) {
stringstream requesterEndpoint;
requesterEndpoint << serverUrl << ":" << requesterPort;
......@@ -45,15 +46,16 @@ RequestImpl::RequestImpl(application::This * application, const std::string & re
RequestImpl::~RequestImpl() {
}
void RequestImpl::setTimeout(int value) {
void RequestImpl::setTimeout(int value, bool linger) {
m_timeout = value;
m_linger = linger;
}
void RequestImpl::replyBinary(const std::string& response) {
// Create a request socket. It is created for each request that could be optimized.
unique_ptr<RequestSocketImpl> requestSocket = m_application->createRequestSocket(m_requesterEndpoint, m_timeout);
//requestSocket->requestAsync(m_application->m_impl->createRequestType(PROTO_RESPONSE), response);
requestSocket->setLinger(m_linger);
try {
requestSocket->request(m_application->m_impl->createRequestType(PROTO_RESPONSE), response);
......
......@@ -33,7 +33,7 @@ public:
RequestImpl(application::This * application, const std::string & requesterApplicationName, int requesterApplicationId, const std::string& message, const std::string& serverUrl, int serverPort, int requesterPort);
~RequestImpl();
void setTimeout(int value);
void setTimeout(int value, bool linger);
void replyBinary(const std::string& response);
void reply(const std::string& response);
......@@ -46,6 +46,7 @@ public:
int m_requesterApplicationId;
std::string m_requesterServerEndpoint;
int m_timeout;
bool m_linger;
};
}
......
......@@ -32,6 +32,15 @@ RequestSocketImpl::RequestSocketImpl(zmq::socket_t * socket, int timeout) :
RequestSocketImpl::~RequestSocketImpl() {
}
void RequestSocketImpl::setLinger(bool linger) {
// Set the value only in the case linger=false.
// This value has effect when terminating the context.
if (!linger) {
int lingerValue = 0;
m_socket->setsockopt(ZMQ_LINGER, &lingerValue, sizeof(int));
}
}
std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& requestTypePart, const std::string& requestDataPart, int overrideTimeout) {
// Prepare the request parts.
......@@ -51,6 +60,10 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
timeout = overrideTimeout;
}
if (timeout == -2) {
return unique_ptr<zmq::message_t>(nullptr);
}
if (timeout > 0) {
// Polling.
zmq_pollitem_t items[1];
......@@ -61,6 +74,11 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
int rc = zmq::poll(items, 1, timeout);
if (rc == 0) {
int lingerValue = 0;
m_socket->setsockopt(ZMQ_LINGER, &lingerValue, sizeof(int));
cout << "linger 0" << endl;
// Timeout occurred.
throw ConnectionTimeout();
}
......@@ -73,24 +91,4 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
return reply;
}
void RequestSocketImpl::requestAsync(const std::string& requestTypePart, const std::string& requestDataPart) {
// Prepare the request parts.
int requestTypeSize = requestTypePart.length();
int requestDataSize = requestDataPart.length();
zmq::message_t requestType(requestTypeSize);
zmq::message_t requestData(requestDataSize);
memcpy(static_cast<void *>(requestType.data()), requestTypePart.c_str(), requestTypeSize);
memcpy(static_cast<void *>(requestData.data()), requestDataPart.c_str(), requestDataSize);
// Send the request in two parts.
m_socket->send(requestType, ZMQ_SNDMORE);
m_socket->send(requestData);
// ...
// Close the socket as we do not need to wait for the reply.
m_socket->close();
}
}
......@@ -29,8 +29,9 @@ public:
RequestSocketImpl(zmq::socket_t * socket, int timeout = 0);
virtual ~RequestSocketImpl();
void setLinger(bool linger);
std::unique_ptr<zmq::message_t> request(const std::string& requestTypePart, const std::string& requestDataPart, int overrideTimeout = -1);
void requestAsync(const std::string& requestTypePart, const std::string& requestDataPart);
std::unique_ptr<zmq::socket_t> m_socket;
int m_timeout;
......
......@@ -257,8 +257,8 @@ zmq::socket_t * ServicesImpl::createRequestSocket(const std::string& endpoint) {
try {
// Set the linger value to 0 to ensure that pending requests are destroyed in case of timeout.
// int value = 0;
// socket->setsockopt(ZMQ_LINGER, &value, sizeof(int));
//int value = 0;
//socket->setsockopt(ZMQ_LINGER, &value, sizeof(int));
// Connect to the endpoint.
socket->connect(endpoint.c_str());
......
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