Commit 5c997ca5 authored by legoc's avatar legoc
Browse files

Added connectToRequester method to Request.

parent 40a5e299
......@@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
<triggers>clean,full,incremental,</triggers>
......@@ -78,5 +83,6 @@
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
</natures>
</projectDescription>
......@@ -10,4 +10,5 @@
* Implementation of unmanaged applications.
* Implemented This static instance without pointer so that it is not necessary to call This::terminate() except if the destruction of the static instance is not automatic.
* Added two binary parts message for publisher/subscriber and requester/responder.
* Refined internal requester port name by adding the responder id.
\ No newline at end of file
* Refined internal requester port name by adding the responder id.
* Added connectToRequester function to Request.
\ No newline at end of file
......@@ -960,6 +960,24 @@ void Request::reply(const std::string& response) {
m_impl->reply(response);
}
std::auto_ptr<Instance> Request::connectToRequester() {
// Instantiate the requester server.
m_requesterServer.reset(new Server(m_impl->m_requesterServerEndpoint));
// Connect and find the instance.
InstanceArray instances = m_requesterServer->connectAll(m_impl->m_requesterApplicationName);
for (int i = 0; i < instances.size(); i++) {
if (instances[i]->getId() == m_impl->m_requesterApplicationId) {
return auto_ptr<Instance>(instances[i]);
}
}
// Not found.
return auto_ptr<Instance>(0);
}
///////////////////////////////////////////////////////////////////////////
// Responder
......
......@@ -366,10 +366,13 @@ public:
void replyBinary(const std::string& response);
void reply(const std::string& response);
std::auto_ptr<Instance> connectToRequester();
private:
Request(std::auto_ptr<RequestImpl> & impl);
std::auto_ptr<RequestImpl> m_impl;
std::auto_ptr<Server> m_requesterServer;
};
///////////////////////////////////////////////////////////////////////////
......
......@@ -19,16 +19,25 @@
#include "../Application.h"
#include "../Serializer.h"
#include "ApplicationImpl.h"
#include <sstream>
using namespace std;
namespace cameo {
RequestImpl::RequestImpl(const application::This * application, const std::string & requesterEndpoint, const std::string& message, int requesterApplicationId) :
RequestImpl::RequestImpl(const application::This * application, const std::string & requesterApplicationName, int requesterApplicationId, const std::string& message, const std::string& serverUrl, int serverPort, int requesterPort) :
m_application(application),
m_requesterEndpoint(requesterEndpoint),
m_message(message),
m_requesterApplicationName(requesterApplicationName),
m_requesterApplicationId(requesterApplicationId) {
stringstream requesterEndpoint;
requesterEndpoint << serverUrl << ":" << requesterPort;
m_requesterEndpoint = requesterEndpoint.str();
stringstream requesterServerEndpoint;
requesterServerEndpoint << serverUrl << ":" << serverPort;
m_requesterServerEndpoint = requesterServerEndpoint.str();
}
RequestImpl::~RequestImpl() {
......
......@@ -31,7 +31,7 @@ namespace application {
class RequestImpl {
public:
RequestImpl(const application::This * application, const std::string & requesterEndpoint, const std::string& message, int requesterApplicationId);
RequestImpl(const application::This * application, const std::string & requesterApplicationName, int requesterApplicationId, const std::string& message, const std::string& serverUrl, int serverPort, int requesterPort);
~RequestImpl();
void replyBinary(const std::string& response);
......@@ -41,7 +41,9 @@ public:
std::string m_requesterEndpoint;
std::string m_message;
std::string m_message2;
std::string m_requesterApplicationName;
int m_requesterApplicationId;
std::string m_requesterServerEndpoint;
};
}
......
......@@ -65,16 +65,16 @@ WaitingImpl * RequesterImpl::waiting() {
void RequesterImpl::sendBinary(const std::string& request) {
stringstream requesterEndpoint;
requesterEndpoint << m_application->getUrl() << ":" << m_requesterPort;
string strRequestType = m_application->m_impl->createRequest(PROTO_REQUEST);
string strRequestData;
proto::Request requestCommand;
requestCommand.set_applicationname(m_application->getName());
requestCommand.set_applicationid(m_application->getId());
requestCommand.set_message(request);
requestCommand.set_endpoint(requesterEndpoint.str());
requestCommand.set_serverurl(m_application->getUrl());
requestCommand.set_serverport(m_application->getPort());
requestCommand.set_requesterport(m_requesterPort);
requestCommand.SerializeToString(&strRequestData);
zmq::message_t* reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_responderEndpoint);
......@@ -94,17 +94,17 @@ void RequesterImpl::send(const std::string& request) {
void RequesterImpl::sendTwoBinaryParts(const std::string& request1, const std::string& request2) {
stringstream requesterEndpoint;
requesterEndpoint << m_application->getUrl() << ":" << m_requesterPort;
string strRequestType = m_application->m_impl->createRequest(PROTO_REQUEST);
string strRequestData;
proto::Request requestCommand;
requestCommand.set_applicationname(m_application->getName());
requestCommand.set_applicationid(m_application->getId());
requestCommand.set_message(request1);
requestCommand.set_message2(request2);
requestCommand.set_endpoint(requesterEndpoint.str());
requestCommand.set_serverurl(m_application->getUrl());
requestCommand.set_serverport(m_application->getPort());
requestCommand.set_requesterport(m_requesterPort);
requestCommand.SerializeToString(&strRequestData);
zmq::message_t* reply = m_application->m_impl->tryRequestWithOnePartReply(strRequestType, strRequestData, m_responderEndpoint);
......
......@@ -103,7 +103,13 @@ std::auto_ptr<RequestImpl> ResponderImpl::receive() {
messageRequest.ParseFromArray((*message).data(), (*message).size());
// Create the request
result = auto_ptr<RequestImpl>(new RequestImpl(m_application, messageRequest.endpoint(), messageRequest.message(), messageRequest.applicationid()));
result = auto_ptr<RequestImpl>(new RequestImpl(m_application,
messageRequest.applicationname(),
messageRequest.applicationid(),
messageRequest.message(),
messageRequest.serverurl(),
messageRequest.serverport(),
messageRequest.requesterport()));
// Set message 2 if it exists.
if (messageRequest.has_message2()) {
......
......@@ -208,10 +208,13 @@ message PublisherResponse {
}
message Request {
required int32 applicationId = 1;
required bytes message = 2;
optional bytes message2 = 3;
required string endpoint = 4;
required string applicationName = 1;
required int32 applicationId = 2;
required bytes message = 3;
optional bytes message2 = 4;
required string serverUrl = 5;
required int32 serverPort = 6;
required int32 requesterPort = 7;
}
message StartedUnmanagedCommand {
......
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