Commit 895e4d18 authored by legoc's avatar legoc
Browse files

(split) Defined the Com class for Communication operations manager

parent 186976ca
...@@ -101,6 +101,25 @@ class This : private Services, private EventListener { ...@@ -101,6 +101,25 @@ class This : private Services, private EventListener {
typedef std::function<void ()> StopFunctionType; typedef std::function<void ()> StopFunctionType;
public: public:
/**
* Class defining the Communication Operations Manager (COM).
*/
class Com {
friend class This;
public:
void storeKeyValue(const std::string& key, const std::string& value) const;
std::string getKeyValue(const std::string& key) const;
void removeKey(const std::string& key) const;
private:
Com(Server * server, int applicationId);
Server * m_server;
int m_applicationId;
};
This(); This();
~This(); ~This();
...@@ -117,6 +136,7 @@ public: ...@@ -117,6 +136,7 @@ public:
static int getTimeout(); static int getTimeout();
static const std::string& getEndpoint(); static const std::string& getEndpoint();
static Server& getServer(); static Server& getServer();
static const Com& getCom();
/** /**
* throws StarterServerException. * throws StarterServerException.
...@@ -146,10 +166,6 @@ public: ...@@ -146,10 +166,6 @@ public:
*/ */
static std::unique_ptr<Instance> connectToStarter(); static std::unique_ptr<Instance> connectToStarter();
static void storeKeyValue(const std::string& key, const std::string& value);
static std::string getKeyValue(const std::string& key);
static void removeKey(const std::string& key);
private: private:
void initApplication(int argc, char *argv[]); void initApplication(int argc, char *argv[]);
...@@ -177,6 +193,7 @@ private: ...@@ -177,6 +193,7 @@ private:
std::unique_ptr<Server> m_server; std::unique_ptr<Server> m_server;
std::unique_ptr<Server> m_starterServer; std::unique_ptr<Server> m_starterServer;
std::unique_ptr<Com> m_com;
std::unique_ptr<WaitingImplSet> m_waitingSet; std::unique_ptr<WaitingImplSet> m_waitingSet;
std::unique_ptr<HandlerImpl> m_stopHandler; std::unique_ptr<HandlerImpl> m_stopHandler;
...@@ -195,6 +212,20 @@ class Instance : private EventListener { ...@@ -195,6 +212,20 @@ class Instance : private EventListener {
public: public:
typedef std::function<void (State)> StateHandlerType; typedef std::function<void (State)> StateHandlerType;
class Com {
friend class Instance;
public:
std::string getKeyValue(const std::string& key) const;
private:
Com(Server * server);
Server * m_server;
int m_applicationId;
};
~Instance(); ~Instance();
const std::string& getName() const; const std::string& getName() const;
...@@ -202,6 +233,8 @@ public: ...@@ -202,6 +233,8 @@ public:
const std::string& getUrl() const; const std::string& getUrl() const;
const std::string& getEndpoint() const; const std::string& getEndpoint() const;
std::string getNameId() const; std::string getNameId() const;
const Com& getCom() const;
bool hasResult() const; bool hasResult() const;
bool exists() const; bool exists() const;
const std::string& getErrorMessage() const; const std::string& getErrorMessage() const;
...@@ -246,8 +279,6 @@ public: ...@@ -246,8 +279,6 @@ public:
std::shared_ptr<OutputStreamSocket> getOutputStreamSocket(); std::shared_ptr<OutputStreamSocket> getOutputStreamSocket();
std::string getKeyValue(const std::string& key);
private: private:
Instance(Server * server); Instance(Server * server);
...@@ -262,6 +293,8 @@ private: ...@@ -262,6 +293,8 @@ private:
std::shared_ptr<OutputStreamSocket> m_outputStreamSocket; std::shared_ptr<OutputStreamSocket> m_outputStreamSocket;
int m_id; int m_id;
std::string m_errorMessage; std::string m_errorMessage;
Com m_com;
int m_pastStates; int m_pastStates;
State m_initialState; State m_initialState;
State m_lastState; State m_lastState;
......
...@@ -53,6 +53,22 @@ namespace application { ...@@ -53,6 +53,22 @@ namespace application {
This This::m_instance; This This::m_instance;
const std::string This::RUNNING_STATE = "RUNNING"; const std::string This::RUNNING_STATE = "RUNNING";
This::Com::Com(Server * server, int applicationId) :
m_server(server),
m_applicationId(applicationId) {
}
void This::Com::storeKeyValue(const std::string& key, const std::string& value) const {
m_server->storeKeyValue(m_applicationId, key, value);
}
std::string This::Com::getKeyValue(const std::string& key) const {
return m_server->getKeyValue(m_applicationId, key);
}
void This::Com::removeKey(const std::string& key) const {
m_server->removeKey(m_applicationId, key);
}
State This::parseState(const std::string& value) { State This::parseState(const std::string& value) {
if (value == "UNKNOWN") { if (value == "UNKNOWN") {
...@@ -207,6 +223,10 @@ void This::initApplication(int argc, char *argv[]) { ...@@ -207,6 +223,10 @@ void This::initApplication(int argc, char *argv[]) {
// Init listener. // Init listener.
setName(m_name); setName(m_name);
m_server->registerEventListener(this); m_server->registerEventListener(this);
// Init com.
m_com = unique_ptr<Com>(new Com(m_server.get(), m_id));
} }
This::~This() { This::~This() {
...@@ -246,6 +266,10 @@ Server& This::getServer() { ...@@ -246,6 +266,10 @@ Server& This::getServer() {
return *m_instance.m_server; return *m_instance.m_server;
} }
const This::Com& This::getCom() {
return *m_instance.m_com;
}
Server& This::getStarterServer() { Server& This::getStarterServer() {
if (m_instance.m_starterServer.get() == nullptr) { if (m_instance.m_starterServer.get() == nullptr) {
...@@ -405,18 +429,6 @@ std::unique_ptr<Instance> This::connectToStarter() { ...@@ -405,18 +429,6 @@ std::unique_ptr<Instance> This::connectToStarter() {
return unique_ptr<Instance>(nullptr); return unique_ptr<Instance>(nullptr);
} }
void This::storeKeyValue(const std::string& key, const std::string& value) {
m_instance.m_server->storeKeyValue(m_instance.m_id, key, value);
}
std::string This::getKeyValue(const std::string& key) {
return m_instance.m_server->getKeyValue(m_instance.m_id, key);
}
void This::removeKey(const std::string& key) {
m_instance.m_server->removeKey(m_instance.m_id, key);
}
void This::stoppingFunction(StopFunctionType stop) { void This::stoppingFunction(StopFunctionType stop) {
application::State state = waitForStop(); application::State state = waitForStop();
...@@ -434,9 +446,20 @@ void This::handleStopImpl(StopFunctionType function) { ...@@ -434,9 +446,20 @@ void This::handleStopImpl(StopFunctionType function) {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Instance // Instance
Instance::Com::Com(Server * server) :
m_server(server),
m_applicationId(-1) {
}
std::string Instance::Com::getKeyValue(const std::string& key) const {
// TODO catch exceptions and rethrow an exception: TerminatedException?
return m_server->getKeyValue(m_applicationId, key);
}
Instance::Instance(Server * server) : Instance::Instance(Server * server) :
m_server(server), m_server(server),
m_id(-1), m_id(-1),
m_com(server),
m_pastStates(0), m_pastStates(0),
m_initialState(UNKNOWN), m_initialState(UNKNOWN),
m_lastState(UNKNOWN), m_lastState(UNKNOWN),
...@@ -455,6 +478,7 @@ Instance::~Instance() { ...@@ -455,6 +478,7 @@ Instance::~Instance() {
void Instance::setId(int id) { void Instance::setId(int id) {
m_id = id; m_id = id;
m_com.m_applicationId = id;
} }
const std::string& Instance::getName() const { const std::string& Instance::getName() const {
...@@ -502,6 +526,10 @@ std::string Instance::getNameId() const { ...@@ -502,6 +526,10 @@ std::string Instance::getNameId() const {
return os.str(); return os.str();
} }
const Instance::Com& Instance::getCom() const {
return m_com;
}
bool Instance::hasResult() const { bool Instance::hasResult() const {
return m_hasResult; return m_hasResult;
} }
...@@ -706,11 +734,6 @@ std::shared_ptr<OutputStreamSocket> Instance::getOutputStreamSocket() { ...@@ -706,11 +734,6 @@ std::shared_ptr<OutputStreamSocket> Instance::getOutputStreamSocket() {
return m_outputStreamSocket; return m_outputStreamSocket;
} }
std::string Instance::getKeyValue(const std::string& key) {
// TODO catch exceptions and rethrow an exception: TerminatedException?
return m_server->getKeyValue(m_id, key);
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// InstanceArray // InstanceArray
......
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