Commit 7a310d44 authored by legoc's avatar legoc
Browse files

Test reset socket

parent b587e3da
......@@ -172,11 +172,11 @@ std::unique_ptr<OutputStreamSocket> Services::createOutputStreamSocket(int port)
}
std::unique_ptr<RequestSocketImpl> Services::createRequestSocket(const std::string& endpoint) {
return unique_ptr<RequestSocketImpl>(new RequestSocketImpl(m_impl->createRequestSocket(endpoint), m_impl->m_timeout));
return unique_ptr<RequestSocketImpl>(new RequestSocketImpl(m_impl.get(), endpoint, m_impl->m_timeout));
}
std::unique_ptr<RequestSocketImpl> Services::createRequestSocket(const std::string& endpoint, int timeout) {
return unique_ptr<RequestSocketImpl>(new RequestSocketImpl(m_impl->createRequestSocket(endpoint), timeout));
return unique_ptr<RequestSocketImpl>(new RequestSocketImpl(m_impl.get(), endpoint, timeout));
}
}
......@@ -15,6 +15,7 @@
*/
#include "RequestSocketImpl.h"
#include "ServicesImpl.h"
#include "../ConnectionTimeout.h"
#include <iostream>
......@@ -25,10 +26,12 @@ using namespace std;
namespace cameo {
RequestSocketImpl::RequestSocketImpl(zmq::socket_t * socket, int timeout) :
m_socket(socket) {
RequestSocketImpl::RequestSocketImpl(ServicesImpl * services, const std::string& endpoint, int timeout) :
m_services(services), m_endpoint(endpoint) {
setTimeout(timeout);
init();
}
RequestSocketImpl::~RequestSocketImpl() {
......@@ -48,8 +51,22 @@ void RequestSocketImpl::setTimeout(int timeout) {
}
}
void RequestSocketImpl::init() {
if (m_socket.get() == nullptr) {
m_socket.reset(m_services->createRequestSocket(m_endpoint));
}
}
void RequestSocketImpl::reset() {
m_socket.reset();
}
std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& requestTypePart, const std::string& requestDataPart, int overrideTimeout) {
// Init if not already done or if a timeout occurred.
init();
// Prepare the request parts.
int requestTypeSize = requestTypePart.length();
int requestDataSize = requestDataPart.length();
......@@ -81,6 +98,9 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
int rc = zmq::poll(items, 1, timeout);
if (rc == 0) {
// Reset the socket.
//reset();
// Timeout occurred.
throw ConnectionTimeout();
}
......
......@@ -23,16 +23,23 @@
namespace cameo {
class ServicesImpl;
class RequestSocketImpl {
public:
RequestSocketImpl(zmq::socket_t * socket, int timeout = 0);
RequestSocketImpl(ServicesImpl * services, const std::string& endpoint, int timeout = 0);
virtual ~RequestSocketImpl();
void setTimeout(int timeout);
void init();
void reset();
std::unique_ptr<zmq::message_t> request(const std::string& requestTypePart, const std::string& requestDataPart, int overrideTimeout = -1);
ServicesImpl * m_services;
std::string m_endpoint;
std::unique_ptr<zmq::socket_t> m_socket;
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