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: ...@@ -138,7 +138,7 @@ public:
static int getId(); static int getId();
static void setTimeout(int timeout); static void setTimeout(int timeout);
static int getTimeout(); static int getTimeout();
static const std::string& getEndpoint(); static const Endpoint& getEndpoint();
static Server& getServer(); static Server& getServer();
static const Com& getCom(); static const Com& getCom();
...@@ -235,7 +235,7 @@ public: ...@@ -235,7 +235,7 @@ public:
const std::string& getName() const; const std::string& getName() const;
int getId() const; int getId() const;
const std::string& getUrl() const; const std::string& getUrl() const;
const std::string& getEndpoint() const; const Endpoint& getEndpoint() const;
std::string getNameId() const; std::string getNameId() const;
const Com& getCom() const; const Com& getCom() const;
......
...@@ -48,13 +48,14 @@ class Server : private Services { ...@@ -48,13 +48,14 @@ class Server : private Services {
public: public:
typedef std::function<void (bool)> ConnectionCheckerType; typedef std::function<void (bool)> ConnectionCheckerType;
Server(const Endpoint& endpoint, int timeoutMs = 0);
Server(const std::string& endpoint, int timeoutMs = 0); Server(const std::string& endpoint, int timeoutMs = 0);
~Server(); ~Server();
void setTimeout(int timeoutMs); void setTimeout(int timeoutMs);
int getTimeout() const; int getTimeout() const;
const std::string& getEndpoint() const; const Endpoint& getEndpoint() const;
const std::string& getUrl() const; const std::string& getUrl() const;
std::array<int, 3> getVersion() const; std::array<int, 3> getVersion() const;
int getPort() const; int getPort() const;
...@@ -132,6 +133,7 @@ public: ...@@ -132,6 +133,7 @@ public:
void unregisterEventListener(EventListener * listener); void unregisterEventListener(EventListener * listener);
private: private:
void initServer(const Endpoint& endpoint, int timeoutMs);
std::unique_ptr<application::Instance> makeInstance(); std::unique_ptr<application::Instance> makeInstance();
bool isAlive(int id) const; bool isAlive(int id) const;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "Strings.h"
#include "EventStreamSocket.h" #include "EventStreamSocket.h"
#include "OutputStreamSocket.h" #include "OutputStreamSocket.h"
...@@ -41,7 +42,7 @@ public: ...@@ -41,7 +42,7 @@ public:
void setTimeout(int timeout); void setTimeout(int timeout);
int getTimeout() const; int getTimeout() const;
const std::string& getEndpoint() const; const Endpoint& getEndpoint() const;
const std::string& getUrl() const; const std::string& getUrl() const;
std::array<int, 3> getVersion() const; std::array<int, 3> getVersion() const;
int getPort() const; int getPort() const;
...@@ -56,7 +57,7 @@ public: ...@@ -56,7 +57,7 @@ public:
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint); std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint);
std::unique_ptr<RequestSocketImpl> createRequestSocket(const std::string& endpoint, int timeout); 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::array<int, 3> m_serverVersion;
std::string m_url; std::string m_url;
int m_port; int m_port;
......
...@@ -176,15 +176,10 @@ void This::initApplication(int argc, char *argv[]) { ...@@ -176,15 +176,10 @@ void This::initApplication(int argc, char *argv[]) {
json::Object infoObject; json::Object infoObject;
json::parse(infoObject, info); 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_url = m_serverEndpoint.getProtocol() + "://" + m_serverEndpoint.getAddress();
m_port = endpoint.getPort(); m_port = m_serverEndpoint.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);
// Create the request socket. The server endpoint has been defined. // Create the request socket. The server endpoint has been defined.
Services::initRequestSocket(); Services::initRequestSocket();
...@@ -289,11 +284,11 @@ int This::getTimeout() { ...@@ -289,11 +284,11 @@ int This::getTimeout() {
return m_instance.Services::getTimeout(); return m_instance.Services::getTimeout();
} }
const std::string& This::getEndpoint() { const Endpoint& This::getEndpoint() {
if (m_instance.m_impl != nullptr) { if (m_instance.m_impl != nullptr) {
return m_instance.m_serverEndpoint; return m_instance.m_serverEndpoint;
} }
static string result; static Endpoint result;
return result; return result;
} }
...@@ -550,7 +545,7 @@ const std::string& Instance::getUrl() const { ...@@ -550,7 +545,7 @@ const std::string& Instance::getUrl() const {
return m_server->getUrl(); return m_server->getUrl();
} }
const std::string& Instance::getEndpoint() const { const Endpoint& Instance::getEndpoint() const {
return m_server->getEndpoint(); return m_server->getEndpoint();
} }
...@@ -1119,7 +1114,7 @@ std::unique_ptr<Requester> Requester::create(Instance & instance, const std::str ...@@ -1119,7 +1114,7 @@ std::unique_ptr<Requester> Requester::create(Instance & instance, const std::str
int responderId = instance.getId(); int responderId = instance.getId();
string responderUrl = instance.getUrl(); string responderUrl = instance.getUrl();
string responderEndpoint = instance.getEndpoint(); string responderEndpoint = instance.getEndpoint().toString();
// Create a request socket to the server of the instance. // Create a request socket to the server of the instance.
unique_ptr<RequestSocketImpl> instanceRequestSocket = This::m_instance.createRequestSocket(responderEndpoint); unique_ptr<RequestSocketImpl> instanceRequestSocket = This::m_instance.createRequestSocket(responderEndpoint);
......
...@@ -32,22 +32,13 @@ using namespace std; ...@@ -32,22 +32,13 @@ using namespace std;
namespace cameo { namespace cameo {
Server::Server(const std::string& endpoint, int timeoutMs) : void Server::initServer(const Endpoint& endpoint, int timeoutMs) {
Services() {
Services::init(); Services::init();
vector<string> tokens = split(endpoint); m_serverEndpoint = endpoint;
m_url = endpoint.getProtocol() + "://" + endpoint.getAddress();
if (tokens.size() < 3) { m_port = endpoint.getPort();
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;
// Set the timeout. // Set the timeout.
Services::setTimeout(timeoutMs); Services::setTimeout(timeoutMs);
...@@ -70,6 +61,27 @@ Server::Server(const std::string& endpoint, int 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() { Server::~Server() {
// Stop the event thread. // Stop the event thread.
if (m_eventThread.get() != nullptr) { if (m_eventThread.get() != nullptr) {
...@@ -85,7 +97,7 @@ int Server::getTimeout() const { ...@@ -85,7 +97,7 @@ int Server::getTimeout() const {
return Services::getTimeout(); return Services::getTimeout();
} }
const std::string& Server::getEndpoint() const { const Endpoint& Server::getEndpoint() const {
return Services::getEndpoint(); return Services::getEndpoint();
} }
...@@ -537,7 +549,7 @@ std::unique_ptr<application::Subscriber> Server::createSubscriber(int id, const ...@@ -537,7 +549,7 @@ std::unique_ptr<application::Subscriber> Server::createSubscriber(int id, const
int synchronizerPort = response[message::PublisherResponse::SYNCHRONIZER_PORT].GetInt(); int synchronizerPort = response[message::PublisherResponse::SYNCHRONIZER_PORT].GetInt();
int numberOfSubscribers = response[message::PublisherResponse::NUMBER_OF_SUBSCRIBERS].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(); subscriber->init();
return subscriber; return subscriber;
...@@ -667,7 +679,7 @@ void Server::unregisterEventListener(EventListener * listener) { ...@@ -667,7 +679,7 @@ void Server::unregisterEventListener(EventListener * listener) {
std::ostream& operator<<(std::ostream& os, const cameo::Server& server) { std::ostream& operator<<(std::ostream& os, const cameo::Server& server) {
os << "server@" << server.m_serverEndpoint; os << "server@" << server.m_serverEndpoint.toString();
return os; return os;
} }
......
...@@ -60,7 +60,7 @@ void Services::init() { ...@@ -60,7 +60,7 @@ void Services::init() {
void Services::initRequestSocket() { void Services::initRequestSocket() {
// Create the request socket. The server endpoint must have been initialized. // 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) { std::vector<std::string> Services::split(const std::string& info) {
...@@ -91,7 +91,7 @@ int Services::getTimeout() const { ...@@ -91,7 +91,7 @@ int Services::getTimeout() const {
return m_impl->getTimeout(); return m_impl->getTimeout();
} }
const std::string& Services::getEndpoint() const { const Endpoint& Services::getEndpoint() const {
return m_serverEndpoint; return m_serverEndpoint;
} }
......
...@@ -60,7 +60,7 @@ int PublisherImpl::getApplicationId() const { ...@@ -60,7 +60,7 @@ int PublisherImpl::getApplicationId() const {
} }
const std::string& PublisherImpl::getApplicationEndpoint() const { const std::string& PublisherImpl::getApplicationEndpoint() const {
return m_application->getEndpoint(); return m_application->getEndpoint().toString();
} }
bool PublisherImpl::waitForSubscribers() { 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