From fe4ba85a05d533c2d2cd637aec43f57ac2167bf3 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 8 Dec 2016 21:38:28 +0100 Subject: [PATCH] [DEV/DEBUG] corect the interface of connection/deiconection --- .../appl/{main.cpp => main-test-client.cpp} | 1 + test/client/lutin_zeus-test-client.py | 2 +- .../appl/{main.cpp => main-test-service.cpp} | 0 test/service1/lutin_zeus-test-service1.py | 2 +- tools/gateway/appl/GateWay.cpp | 14 +- tools/gateway/appl/RouterInterface.cpp | 17 ++- tools/gateway/appl/RouterInterface.hpp | 6 +- tools/gateway/appl/ServiceInterface.cpp | 126 ------------------ tools/gateway/appl/ServiceInterface.hpp | 39 ------ .../appl/{main.cpp => main-gateway.cpp} | 2 +- tools/gateway/lutin_zeus-gateway.py | 2 +- .../appl/{main.cpp => main-launcher.cpp} | 0 tools/launcher/lutin_zeus-launcher.py | 2 +- .../router/appl/{main.cpp => main-router.cpp} | 0 tools/router/lutin_zeus-router.py | 2 +- .../{main.cpp => main-service-picture.cpp} | 0 .../lutin_zeus-service-picture-impl.py | 2 +- .../appl/{main.cpp => main-service-user.cpp} | 0 .../lutin_zeus-service-user-impl.py | 2 +- .../appl/{main.cpp => main-service-video.cpp} | 0 .../lutin_zeus-service-video-impl.py | 2 +- 21 files changed, 36 insertions(+), 185 deletions(-) rename test/client/appl/{main.cpp => main-test-client.cpp} (99%) rename test/service1/appl/{main.cpp => main-test-service.cpp} (100%) delete mode 100644 tools/gateway/appl/ServiceInterface.cpp delete mode 100644 tools/gateway/appl/ServiceInterface.hpp rename tools/gateway/appl/{main.cpp => main-gateway.cpp} (97%) rename tools/launcher/appl/{main.cpp => main-launcher.cpp} (100%) rename tools/router/appl/{main.cpp => main-router.cpp} (100%) rename tools/service-picture/appl/{main.cpp => main-service-picture.cpp} (100%) rename tools/service-user/appl/{main.cpp => main-service-user.cpp} (100%) rename tools/service-video/appl/{main.cpp => main-service-video.cpp} (100%) diff --git a/test/client/appl/main.cpp b/test/client/appl/main-test-client.cpp similarity index 99% rename from test/client/appl/main.cpp rename to test/client/appl/main-test-client.cpp index 362672b..2c04d96 100644 --- a/test/client/appl/main.cpp +++ b/test/client/appl/main-test-client.cpp @@ -206,6 +206,7 @@ int main(int _argc, const char *_argv[]) { APPL_INFO("Appl in waiting ... " << iii << "/3"); iii++; } + APPL_INFO("Request disconnect"); client1.disconnect(); APPL_INFO("=================================="); APPL_INFO("== ZEUS test client stop"); diff --git a/test/client/lutin_zeus-test-client.py b/test/client/lutin_zeus-test-client.py index 061179f..7aa72e5 100644 --- a/test/client/lutin_zeus-test-client.py +++ b/test/client/lutin_zeus-test-client.py @@ -33,7 +33,7 @@ def configure(target, my_module): ]) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-test-client.cpp' ]) return True diff --git a/test/service1/appl/main.cpp b/test/service1/appl/main-test-service.cpp similarity index 100% rename from test/service1/appl/main.cpp rename to test/service1/appl/main-test-service.cpp diff --git a/test/service1/lutin_zeus-test-service1.py b/test/service1/lutin_zeus-test-service1.py index 53ee530..a1efeca 100644 --- a/test/service1/lutin_zeus-test-service1.py +++ b/test/service1/lutin_zeus-test-service1.py @@ -29,7 +29,7 @@ def configure(target, my_module): my_module.add_depend(['zeus']) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-test-service.cpp' ]) return True diff --git a/tools/gateway/appl/GateWay.cpp b/tools/gateway/appl/GateWay.cpp index 5dd1d9d..62a6d0d 100644 --- a/tools/gateway/appl/GateWay.cpp +++ b/tools/gateway/appl/GateWay.cpp @@ -136,6 +136,9 @@ bool appl::GateWay::serviceExist(const std::string& _service) { if (it == nullptr) { continue; } + if (it->isConnected() == false) { + continue; + } for (auto &srvIt : it->getServiceList()) { if (srvIt == _service) { return true; @@ -150,6 +153,9 @@ uint16_t appl::GateWay::serviceClientIdGet(const std::string& _service) { if (it == nullptr) { continue; } + if (it->isConnected() == false) { + continue; + } for (auto &srvIt : it->getServiceList()) { if (srvIt == _service) { return it->getId(); @@ -167,6 +173,9 @@ std::vector appl::GateWay::getAllServiceName() { if (it == nullptr) { continue; } + if (it->isConnected() == false) { + continue; + } for (auto &srvIt : it->getServiceList()) { out.push_back(srvIt); } @@ -182,6 +191,9 @@ bool appl::GateWay::send(ememory::SharedPtr _data) { if (*it == nullptr) { continue; } + if ((*it)->isConnected() == false) { + continue; + } if ((*it)->getId() == id) { (*it)->send(_data); return true; @@ -192,7 +204,7 @@ bool appl::GateWay::send(ememory::SharedPtr _data) { } void appl::GateWay::cleanIO() { - APPL_INFO("Check if something need to be clean ..."); + APPL_VERBOSE("Check if something need to be clean ..."); std::vector tmpIDToRemove; // Clean all IOs... { diff --git a/tools/gateway/appl/RouterInterface.cpp b/tools/gateway/appl/RouterInterface.cpp index fa45892..3cfba22 100644 --- a/tools/gateway/appl/RouterInterface.cpp +++ b/tools/gateway/appl/RouterInterface.cpp @@ -28,13 +28,18 @@ appl::clientSpecificInterface::~clientSpecificInterface() { APPL_INFO("-- DELETE Client --"); APPL_INFO("-------------------"); } -/* -void appl::clientSpecificInterface::answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp) { - //m_interfaceWeb->answerError(_transactionId, m_routeurUID, ZEUS_ID_SERVICE_ROOT, protocolError, _errorHelp); - //m_interfaceWeb->sendCtrl(m_routeurUID, ZEUS_ID_SERVICE_ROOT, "DISCONNECT"); - m_state = appl::clientState::disconnect; + +zeus::WebServer* appl::clientSpecificInterface::getInterface() { + return m_interfaceWeb; } -*/ + +bool appl::clientSpecificInterface::isConnected() { + if (m_interfaceWeb == nullptr) { + return false; + } + return m_interfaceWeb->isActive(); +}; + bool appl::clientSpecificInterface::start(appl::GateWay* _gateway, zeus::WebServer* _interfaceWeb, uint16_t _id) { appl::IOInterface::start(_gateway, _id); m_interfaceWeb = _interfaceWeb; diff --git a/tools/gateway/appl/RouterInterface.hpp b/tools/gateway/appl/RouterInterface.hpp index 40f686d..b8bf22f 100644 --- a/tools/gateway/appl/RouterInterface.hpp +++ b/tools/gateway/appl/RouterInterface.hpp @@ -21,9 +21,8 @@ namespace appl { bool start(appl::GateWay* _gateway, zeus::WebServer* _interfaceWeb, uint16_t _id); void send(ememory::SharedPtr _data); //void answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp); - zeus::WebServer* getInterface() { - return m_interfaceWeb; - } + zeus::WebServer* getInterface(); + bool isConnected(); }; class RouterInterface { @@ -41,7 +40,6 @@ namespace appl { bool isAlive(); void send(const ememory::SharedPtr& _data); void clean(); - bool isConnected() { return m_interfaceWeb.isActive(); }; }; } diff --git a/tools/gateway/appl/ServiceInterface.cpp b/tools/gateway/appl/ServiceInterface.cpp deleted file mode 100644 index 01bd276..0000000 --- a/tools/gateway/appl/ServiceInterface.cpp +++ /dev/null @@ -1,126 +0,0 @@ -/** @file - * @author Edouard DUPIN - * @copyright 2016, Edouard DUPIN, all right reserved - * @license APACHE v2.0 (see license file) - */ - -#include -#include -#include -#include - -// todo : cHANGE THIS ... -static const std::string protocolError = "PROTOCOL-ERROR"; - - -bool appl::ServiceInterface::requestURI(const std::string& _uri) { - APPL_WARNING("request connect on SERVICE: '" << _uri << "'"); - if(m_gatewayInterface == nullptr) { - APPL_ERROR("Can not access to the main GateWay interface (nullptr)"); - return false; - } - std::string tmpURI = _uri; - if (tmpURI.size() == 0) { - APPL_ERROR("Empty URI ... not supported ..."); - return false; - } - if (tmpURI[0] == '/') { - tmpURI = std::string(tmpURI.begin() + 1, tmpURI.end()); - } - std::vector listValue = etk::split(tmpURI, '?'); - if (listValue.size() == 0) { - APPL_ERROR("can not parse URI ..."); - return false; - } - tmpURI = listValue[0]; - if (etk::start_with(tmpURI, "service:") == false ) { - APPL_ERROR("Missing 'service:' at the start of the URI ..."); - return false; - } - m_name = &tmpURI[8]; - APPL_INFO("Connect service name : '" << m_name << "'"); - return true; -} - - -appl::ServiceInterface::ServiceInterface(enet::Tcp _connection, appl::GateWay* _gatewayInterface) : - m_gatewayInterface(_gatewayInterface), - m_interfaceClient(std::move(_connection), true) { - APPL_INFO("-----------------"); - APPL_INFO("-- NEW Service --"); - APPL_INFO("-----------------"); -} - -appl::ServiceInterface::~ServiceInterface() { - APPL_INFO("--------------------"); - APPL_INFO("-- DELETE Service --"); - APPL_INFO("--------------------"); -} - -bool appl::ServiceInterface::isAlive() { - return m_interfaceClient.isActive(); -} - -void appl::ServiceInterface::start() { - m_interfaceClient.connect(this, &appl::ServiceInterface::onServiceData); - m_interfaceClient.connectUri(this, &appl::ServiceInterface::requestURI); - m_interfaceClient.connect(); - m_interfaceClient.setInterfaceName("srv-?"); -} - -void appl::ServiceInterface::stop() { - m_interfaceClient.disconnect(); -} - - -void appl::ServiceInterface::SendData(uint64_t _userSessionId, ememory::SharedPtr _data) { - _data->setClientId(_userSessionId); - m_interfaceClient.writeBinary(_data); -} - -void appl::ServiceInterface::onServiceData(ememory::SharedPtr _value) { - if (_value == nullptr) { - return; - } - uint32_t transactionId = _value->getTransactionId(); - //data.add("from-service", ejson::String(m_name)); - if (_value->getType() == zeus::Message::type::event) { - /* - if (data.valueExist("event") == true) { - // No need to have a user ID ... - if (data["event"].toString().get() == "IS-ALIVE") { - APPL_VERBOSE("Service Alive ..."); - if (std::chrono::steady_clock::now() - m_interfaceClient.getLastTimeSend() >= std::chrono::seconds(20)) { - ejson::Object tmpp; - tmpp.add("event", ejson::String("IS-ALIVE")); - m_interfaceClient.writeJson(tmpp); - } - } else { - APPL_INFO("Unknow service event: '" << data["event"].toString().get() << "'"); - } - return; - } - */ - return; - } - if (_value->getType() == zeus::Message::type::call) { - ememory::SharedPtr callObj = ememory::staticPointerCast(_value); - std::string callFunction = callObj->getCall(); - if (callFunction == "getUserName") { - m_interfaceClient.answerValue(transactionId, _value->getClientId(), _value->getServiceId(), *m_gatewayInterface->propertyUserName); - return; - } - answerProtocolError(transactionId, "unknow function"); - } - if (_value->getClientId() == 0) { - APPL_ERROR("Service interface ==> wrong service answer ==> missing 'client-id'"); - return; - } - m_gatewayInterface->answer(_value->getClientId(), _value); -} - - -void appl::ServiceInterface::answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp) { - m_interfaceClient.answerError(_transactionId, 0, 0, protocolError, _errorHelp); - m_interfaceClient.disconnect(true); -} \ No newline at end of file diff --git a/tools/gateway/appl/ServiceInterface.hpp b/tools/gateway/appl/ServiceInterface.hpp deleted file mode 100644 index 8f7967f..0000000 --- a/tools/gateway/appl/ServiceInterface.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/** @file - * @author Edouard DUPIN - * @copyright 2016, Edouard DUPIN, all right reserved - * @license APACHE v2.0 (see license file) - */ -#pragma once - -#include -#include -#error remove ... -namespace appl { - class GateWay; - class RouterInterface; - class userSpecificInterface; - class ServiceInterface { - friend class appl::RouterInterface; - friend class appl::userSpecificInterface; - private: - appl::GateWay* m_gatewayInterface; - zeus::WebServer m_interfaceClient; - std::string m_name; - public: - ServiceInterface(enet::Tcp _connection, appl::GateWay* _gatewayInterface); - virtual ~ServiceInterface(); - void start(); - void stop(); - void onServiceData(ememory::SharedPtr _value); - bool requestURI(const std::string& _uri); - public: - void SendData(uint64_t _userSessionId, ememory::SharedPtr _data); - const std::string& getName() { - return m_name; - } - bool isAlive(); - protected: - void answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp); - }; -} - diff --git a/tools/gateway/appl/main.cpp b/tools/gateway/appl/main-gateway.cpp similarity index 97% rename from tools/gateway/appl/main.cpp rename to tools/gateway/appl/main-gateway.cpp index 4c782ca..c3e1855 100644 --- a/tools/gateway/appl/main.cpp +++ b/tools/gateway/appl/main-gateway.cpp @@ -51,7 +51,7 @@ int main(int _argc, const char *_argv[]) { APPL_INFO("=================================="); basicGateway.start(); while (true) { - std::this_thread::sleep_for(std::chrono::milliseconds(10000)); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); basicGateway.cleanIO(); } basicGateway.stop(); diff --git a/tools/gateway/lutin_zeus-gateway.py b/tools/gateway/lutin_zeus-gateway.py index f30c890..6540848 100644 --- a/tools/gateway/lutin_zeus-gateway.py +++ b/tools/gateway/lutin_zeus-gateway.py @@ -34,7 +34,7 @@ def configure(target, my_module): #'appl/ServiceInterface.cpp', 'appl/DirectInterface.cpp', 'appl/GateWay.cpp', - 'appl/main.cpp' + 'appl/main-gateway.cpp' ]) return True diff --git a/tools/launcher/appl/main.cpp b/tools/launcher/appl/main-launcher.cpp similarity index 100% rename from tools/launcher/appl/main.cpp rename to tools/launcher/appl/main-launcher.cpp diff --git a/tools/launcher/lutin_zeus-launcher.py b/tools/launcher/lutin_zeus-launcher.py index 2df21fe..1c9c6bf 100644 --- a/tools/launcher/lutin_zeus-launcher.py +++ b/tools/launcher/lutin_zeus-launcher.py @@ -33,7 +33,7 @@ def configure(target, my_module): ]) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-launcher.cpp' ]) return True diff --git a/tools/router/appl/main.cpp b/tools/router/appl/main-router.cpp similarity index 100% rename from tools/router/appl/main.cpp rename to tools/router/appl/main-router.cpp diff --git a/tools/router/lutin_zeus-router.py b/tools/router/lutin_zeus-router.py index e4d9da7..c9fffcf 100644 --- a/tools/router/lutin_zeus-router.py +++ b/tools/router/lutin_zeus-router.py @@ -32,7 +32,7 @@ def configure(target, my_module): 'appl/ClientInterface.cpp', 'appl/GateWayInterface.cpp', 'appl/Router.cpp', - 'appl/main.cpp' + 'appl/main-router.cpp' ]) return True diff --git a/tools/service-picture/appl/main.cpp b/tools/service-picture/appl/main-service-picture.cpp similarity index 100% rename from tools/service-picture/appl/main.cpp rename to tools/service-picture/appl/main-service-picture.cpp diff --git a/tools/service-picture/lutin_zeus-service-picture-impl.py b/tools/service-picture/lutin_zeus-service-picture-impl.py index efca548..70afcaf 100644 --- a/tools/service-picture/lutin_zeus-service-picture-impl.py +++ b/tools/service-picture/lutin_zeus-service-picture-impl.py @@ -35,7 +35,7 @@ def configure(target, my_module): ]) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-service-picture.cpp' ]) my_module.add_flag('c++', "-DSERVICE_NAME=\"\\\"picture\\\"\"") return True diff --git a/tools/service-user/appl/main.cpp b/tools/service-user/appl/main-service-user.cpp similarity index 100% rename from tools/service-user/appl/main.cpp rename to tools/service-user/appl/main-service-user.cpp diff --git a/tools/service-user/lutin_zeus-service-user-impl.py b/tools/service-user/lutin_zeus-service-user-impl.py index bb33102..d1642ba 100644 --- a/tools/service-user/lutin_zeus-service-user-impl.py +++ b/tools/service-user/lutin_zeus-service-user-impl.py @@ -36,7 +36,7 @@ def configure(target, my_module): ]) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-service-user.cpp' ]) my_module.add_flag('c++', "-DSERVICE_NAME=\"\\\"user\\\"\"") diff --git a/tools/service-video/appl/main.cpp b/tools/service-video/appl/main-service-video.cpp similarity index 100% rename from tools/service-video/appl/main.cpp rename to tools/service-video/appl/main-service-video.cpp diff --git a/tools/service-video/lutin_zeus-service-video-impl.py b/tools/service-video/lutin_zeus-service-video-impl.py index 64f21ad..23ef3e2 100644 --- a/tools/service-video/lutin_zeus-service-video-impl.py +++ b/tools/service-video/lutin_zeus-service-video-impl.py @@ -35,7 +35,7 @@ def configure(target, my_module): ]) my_module.add_src_file([ 'appl/debug.cpp', - 'appl/main.cpp' + 'appl/main-service-video.cpp' ]) my_module.add_flag('c++', "-DSERVICE_NAME=\"\\\"video\\\"\"") return True