Commit 8853fa53 authored by legoc's avatar legoc
Browse files

(split) Replaced string endpoint by Endpoint into the API services classes

parent f1cc3517
......@@ -138,7 +138,7 @@ public:
static int getId();
static void setTimeout(int timeout);
static int getTimeout();
static const std::string& getEndpoint();
static const Endpoint& getEndpoint();
static Server& getServer();
static const Com& getCom();
......@@ -235,7 +235,7 @@ public:
const std::string& getName() const;
int getId() const;
const std::string& getUrl() const;
const std::string& getEndpoint() const;
const Endpoint& getEndpoint() const;
std::string getNameId() const;
const Com& getCom() const;
......
......@@ -48,13 +48,14 @@ class Server : private Services {
public:
typedef std::function<void (bool)> ConnectionCheckerType;
Server(const Endpoint& endpoint, int timeoutMs = 0);
Server(const std::string& endpoint, int timeoutMs = 0);
~Server();
void setTimeout(int timeoutMs);
int getTimeout() const;
const std::string& getEndpoint() const;
const Endpoint& getEndpoint() const;
const std::string& getUrl() const;
std::array<int, 3> getVersion() const;
int getPort() const;
......@@ -132,6 +133,7 @@ public:
void unregisterEventListener(EventListener * listener);
private:
void initServer(const Endpoint& endpoint, int timeoutMs);
std::unique_ptr<application::Instance> makeInstance();
bool isAlive(int id) const;
......
......@@ -19,6 +19,7 @@
#include <string>
#include <vector>
#include "Strings.h"
#include "EventStreamSocket.h"
#include "OutputStreamSocket.h"
......@@ -41,7 +42,7 @@ public:
void setTimeout(int timeout);
int getTimeout() const;
const std::string& getEndpoint() const;
const Endpoint& getEndpoint() const;
const std::string& getUrl() const;
std::array<int, 3> getVersion() const;
int getPort() const;
......@@ -56,7 +57,7 @@ public:
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint);
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint, int timeout);
std::string m_serverEndpoint;
Endpoint m_serverEndpoint;
std::array<int, 3> m_serverVersion;
std::string m_url;
int m_port;
......
......@@ -176,15 +176,10 @@ void This::initApplication(int argc, char *argv[]) {
json::Object infoObject;
json::parse(infoObject, info);
Endpoint endpoint = Endpoint::parse(infoObject[message::ApplicationIdentity::SERVER].GetString());
m_serverEndpoint = Endpoint::parse(infoObject[message::ApplicationIdentity::SERVER].GetString());
m_url = endpoint.getProtocol() + "://" + endpoint.getAddress();
m_port = endpoint.getPort();
// We separated host endpoint and server in the past (server being tcp://localhost)
// but that generates troubles when two applications communicate remotely.
// However leave the same value seems to be ok.
m_serverEndpoint = m_url + ":" + to_string(m_port);
m_url = m_serverEndpoint.getProtocol() + "://" + m_serverEndpoint.getAddress();
m_port = m_serverEndpoint.getPort();
// Create the request socket. The server endpoint has been defined.
Services::initRequestSocket();
......@@ -289,11 +284,11 @@ int This::getTimeout() {
return m_instance.Services::getTimeout();
}
const std::string& This::getEndpoint() {
const Endpoint& This::getEndpoint() {
if (m_instance.m_impl != nullptr) {
return m_instance.m_serverEndpoint;
}
static string result;
static Endpoint result;
return result;
}
......@@ -550,7 +545,7 @@ const std::string& Instance::getUrl() const {
return m_server->getUrl();
}
const std::string& Instance::getEndpoint() const {
const Endpoint& Instance::getEndpoint() const {
return m_server->getEndpoint();
}
......@@ -1119,7 +1114,7 @@ std::unique_ptr<Requester> Requester::create(Instance & instance, const std::str
int responderId = instance.getId();
string responderUrl = instance.getUrl();
string responderEndpoint = instance.getEndpoint();
string responderEndpoint = instance.getEndpoint().toString();
// Create a request socket to the server of the instance.
unique_ptr<RequestSocketImpl> instanceRequestSocket = This::m_instance.createRequestSocket(responderEndpoint);
......
......@@ -32,22 +32,13 @@ using namespace std;
namespace cameo {
Server::Server(const std::string& endpoint, int timeoutMs) :
Services() {
void Server::initServer(const Endpoint& endpoint, int timeoutMs) {
Services::init();
vector<string> tokens = split(endpoint);
if (tokens.size() < 3) {
throw InvalidArgumentException(endpoint + " is not a valid endpoint");
}
m_url = tokens[0] + ":" + tokens[1];
string port = tokens[2];
istringstream is(port);
is >> m_port;
m_serverEndpoint = m_url + ":" + port;
m_serverEndpoint = endpoint;
m_url = endpoint.getProtocol() + "://" + endpoint.getAddress();
m_port = endpoint.getPort();
// Set the timeout.
Services::setTimeout(timeoutMs);
......@@ -70,6 +61,27 @@ Server::Server(const std::string& endpoint, int timeoutMs) :
}
}
Server::Server(const Endpoint& endpoint, int timeoutMs) :
Services() {
Services::init();
initServer(endpoint, timeoutMs);
}
Server::Server(const std::string& endpoint, int timeoutMs) :
Services() {
Services::init();
try {
initServer(Endpoint::parse(endpoint), timeoutMs);
}
catch (...) {
throw InvalidArgumentException(endpoint + " is not a valid endpoint");
}
}
Server::~Server() {
// Stop the event thread.
if (m_eventThread.get() != nullptr) {
......@@ -85,7 +97,7 @@ int Server::getTimeout() const {
return Services::getTimeout();
}
const std::string& Server::getEndpoint() const {
const Endpoint& Server::getEndpoint() const {
return Services::getEndpoint();
}
......@@ -537,7 +549,7 @@ std::unique_ptr<application::Subscriber> Server::createSubscriber(int id, const
int synchronizerPort = response[message::PublisherResponse::SYNCHRONIZER_PORT].GetInt();
int numberOfSubscribers = response[message::PublisherResponse::NUMBER_OF_SUBSCRIBERS].GetInt();
unique_ptr<application::Subscriber> subscriber(new application::Subscriber(this, getUrl(), publisherPort, synchronizerPort, publisherName, numberOfSubscribers, instanceName, id, m_serverEndpoint, m_serverStatusEndpoint));
unique_ptr<application::Subscriber> subscriber(new application::Subscriber(this, getUrl(), publisherPort, synchronizerPort, publisherName, numberOfSubscribers, instanceName, id, m_serverEndpoint.toString(), m_serverStatusEndpoint));
subscriber->init();
return subscriber;
......@@ -667,7 +679,7 @@ void Server::unregisterEventListener(EventListener * listener) {
std::ostream& operator<<(std::ostream& os, const cameo::Server& server) {
os << "server@" << server.m_serverEndpoint;
os << "server@" << server.m_serverEndpoint.toString();
return os;
}
......
......@@ -60,7 +60,7 @@ void Services::init() {
void Services::initRequestSocket() {
// Create the request socket. The server endpoint must have been initialized.
m_requestSocket = std::move(createRequestSocket(m_serverEndpoint, m_impl->m_timeout));
m_requestSocket = std::move(createRequestSocket(m_serverEndpoint.toString(), m_impl->m_timeout));
}
std::vector<std::string> Services::split(const std::string& info) {
......@@ -91,7 +91,7 @@ int Services::getTimeout() const {
return m_impl->getTimeout();
}
const std::string& Services::getEndpoint() const {
const Endpoint& Services::getEndpoint() const {
return m_serverEndpoint;
}
......
......@@ -60,7 +60,7 @@ int PublisherImpl::getApplicationId() const {
}
const std::string& PublisherImpl::getApplicationEndpoint() const {
return m_application->getEndpoint();
return m_application->getEndpoint().toString();
}
bool PublisherImpl::waitForSubscribers() {
......
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