Commit a0f9e19a authored by legoc's avatar legoc
Browse files

Responder reviewed

parent e6c49dcd
......@@ -97,7 +97,8 @@ public:
bool isCanceled() const;
private:
Responder(int responderPort, const std::string &name);
Responder(const std::string &name);
void init(const std::string &name);
std::string m_name;
std::unique_ptr<ResponderImpl> m_impl;
......
......@@ -139,12 +139,11 @@ std::unique_ptr<Server> Request::getServer() {
///////////////////////////////////////////////////////////////////////////
// Responder
Responder::Responder(int responderPort, const std::string& name) :
Responder::Responder(const std::string& name) :
m_name(name) {
//TODO Replace with a factory.
m_impl = std::unique_ptr<ResponderImpl>(new ResponderZmq());
m_impl->init(responderPort);
// Create the waiting here.
m_waiting.reset(new Waiting(std::bind(&Responder::cancel, this)));
......@@ -154,7 +153,7 @@ Responder::~Responder() {
application::This::getCom().removePort(ResponderImpl::RESPONDER_PREFIX + m_name);
}
std::unique_ptr<Responder> Responder::create(const std::string& name) {
void Responder::init(const std::string &name) {
std::string portName = ResponderImpl::RESPONDER_PREFIX + name;
json::Object response = application::This::getCom().requestJSON(createRequestPortV0Request(application::This::getId(), portName));
......@@ -164,7 +163,15 @@ std::unique_ptr<Responder> Responder::create(const std::string& name) {
throw ResponderCreationException(response[message::RequestResponse::MESSAGE].GetString());
}
return std::unique_ptr<Responder>(new Responder(responderPort, name));
m_impl->init(responderPort);
}
std::unique_ptr<Responder> Responder::create(const std::string& name) {
std::unique_ptr<Responder> responder(new Responder(name));
responder->init(name);
return responder;
}
const std::string& Responder::getName() const {
......
......@@ -37,7 +37,7 @@ public class Responder {
throw new ResponderCreationException(JSON.getString(response, Messages.RequestResponse.MESSAGE));
}
impl.init(port, name);
impl.init(port);
}
/**
......@@ -77,6 +77,13 @@ public class Responder {
public void terminate() {
waiting.remove();
impl.terminate();
try {
This.getCom().removePort(ResponderImpl.RESPONDER_PREFIX + name);
} catch (Exception e) {
System.err.println("Cannot terminate responder: " + e.getMessage());
}
}
@Override
......
......@@ -22,7 +22,7 @@ public interface ResponderImpl {
public static final String RESPONDER_PREFIX = "rep.";
void init(int responderPort, String name);
void init(int responderPort);
Request receive();
void cancel();
boolean isEnded();
......
......@@ -31,7 +31,6 @@ import fr.ill.ics.cameo.strings.Endpoint;
public class ResponderZmq implements ResponderImpl {
private int responderPort;
private String name;
private Zmq.Context context;
private Zmq.Socket responder;
......@@ -39,9 +38,8 @@ public class ResponderZmq implements ResponderImpl {
private boolean ended = false;
private boolean canceled = false;
public void init(int responderPort, String name) {
public void init(int responderPort) {
this.responderPort = responderPort;
this.name = name;
this.context = ((ContextZmq)This.getCom().getContext()).getContext();
// create a socket REP
......@@ -141,15 +139,7 @@ public class ResponderZmq implements ResponderImpl {
}
public void terminate() {
context.destroySocket(responder);
try {
This.getCom().removePort(RESPONDER_PREFIX + name);
} catch (Exception e) {
System.err.println("Cannot terminate responder: " + e.getMessage());
}
}
......
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