diff --git a/tools/service-user/appl/main.cpp b/tools/service-user/appl/main.cpp index 8fe0416..213373d 100644 --- a/tools/service-user/appl/main.cpp +++ b/tools/service-user/appl/main.cpp @@ -13,61 +13,63 @@ #include #include + + +static std::mutex g_mutex; +static std::string g_userName; +static ejson::Document g_database; + namespace appl { - class User { + + class SystemService { private: - std::mutex m_mutex; - std::string m_userName; - ejson::Document m_database; + ememory::SharedPtr m_client; public: - User(const std::string& _userName) : - m_userName(_userName) { - std::unique_lock lock(m_mutex); - APPL_WARNING("new USER: " << m_userName); - bool ret = m_database.load(std::string("USERDATA:") + m_userName + ".json"); - if (ret == false) { - APPL_WARNING(" ==> LOAD error"); - } + SystemService(ememory::SharedPtr _client) : + m_client(_client) { + APPL_WARNING("New SystemService ... for user: "); } - ~User() { - std::unique_lock lock(m_mutex); - APPL_WARNING("delete USER [START]"); - APPL_DEBUG("Store User Info:"); - bool ret = m_database.storeSafe(std::string("USERDATA:") + m_userName + ".json"); - if (ret == false) { - APPL_WARNING(" ==> Store error"); - } - APPL_WARNING("delete USER [STOP]"); + ~SystemService() { + APPL_WARNING("Delete service-user interface."); } - const std::string& getName() { - return m_userName; - } - std::vector getGroups(const std::string& _clientName) { - std::unique_lock lock(m_mutex); + public: + std::vector getGroups(std::string _clientName) { std::vector out; - ejson::Object clients = m_database["client"].toObject(); - if (clients.exist() == false) { - // Section never created + if (m_client == nullptr) { return out; } - ejson::Object client = clients[_clientName].toObject(); - if (clients.exist() == false) { - // No specificity for this client (in case it have no special right) - return out; + // TODO: check if basished ... + if (m_client->getName() != "") { + std::unique_lock lock(g_mutex); + std::vector out; + ejson::Object clients = g_database["client"].toObject(); + if (clients.exist() == false) { + // Section never created + return out; + } + ejson::Object client = clients[m_client->getName()].toObject(); + if (clients.exist() == false) { + // No specificity for this client (in case it have no special right) + return out; + } + if (client["tocken"].toString().get() != "") { + out.push_back("connected"); + } + // TODO: check banishing ... + ejson::Array groups = client["group"].toArray(); + for (auto it : groups) { + out.push_back(it.toString().get()); + } } - if (client["tocken"].toString().get() != "") { - out.push_back("connected"); - } - // TODO: check banishing ... - ejson::Array groups = client["group"].toArray(); - for (auto it : groups) { - out.push_back(it.toString().get()); + // TODO: Check default visibility ... (if user want to have default visibility at Noone ==> then public must be removed... + if (true) { + out.push_back("public"); } return out; } - bool checkTocken(const std::string& _clientName, const std::string& _tocken) { - std::unique_lock lock(m_mutex); - ejson::Object clients = m_database["client"].toObject(); + bool checkTocken(std::string _clientName, std::string _tocken) { + std::unique_lock lock(g_mutex); + ejson::Object clients = g_database["client"].toObject(); if (clients.exist() == false) { // Section never created return false; @@ -85,9 +87,9 @@ namespace appl { } return false; } - bool checkAuth(const std::string& _password) { - std::unique_lock lock(m_mutex); - std::string pass = m_database["password"].toString().get(); + bool checkAuth(std::string _password) { + std::unique_lock lock(g_mutex); + std::string pass = g_database["password"].toString().get(); if (pass == "") { // pb password return false; @@ -97,82 +99,17 @@ namespace appl { } return false; } - std::vector filterServices(const std::string& _clientName, std::vector _inputList) { - std::unique_lock lock(m_mutex); + std::vector filterServices(std::string _clientName, std::vector _currentList) { + std::unique_lock lock(g_mutex); // When connected to our session ==> we have no control access ... - if (_clientName == m_userName) { - return _inputList; + if (_clientName == g_userName) { + return _currentList; } std::vector out; APPL_TODO("Filter service list ==> not implemented..."); return out; } }; - - class UserManager { - private: - std::mutex m_mutex; - std::map> m_listLoaded; - public: - UserManager() { - - } - ememory::SharedPtr getUser(const std::string& _userName) { - std::unique_lock lock(m_mutex); - auto it = m_listLoaded.find(_userName); - if (it != m_listLoaded.end()) { - // User already loaded: - return it->second; - } - // load New User: - ememory::SharedPtr tmp(new appl::User(_userName)); - m_listLoaded.insert(std::make_pair(_userName, tmp)); - return tmp; - } - }; - class SystemService { - private: - ememory::SharedPtr m_user; - private: - ememory::SharedPtr m_client; - public: - SystemService() { - APPL_WARNING("New SystemService ..."); - } - SystemService(ememory::SharedPtr _user, ememory::SharedPtr _client) : - m_user(_user), - m_client(_client) { - APPL_WARNING("New SystemService ... for user: "); - } - ~SystemService() { - APPL_WARNING("delete SystemService ..."); - } - public: - std::vector getGroups(std::string _clientName) { - std::vector out; - if (m_client == nullptr) { - return out; - } - // TODO: check if basished ... - if (m_client->getName() != "") { - out = m_user->getGroups(m_client->getName()); - } - // TODO: Check default visibility ... (if user want to have default visibility at Noone ==> then public must be removed... - if (true) { - out.push_back("public"); - } - return out; - } - bool checkTocken(std::string _clientName, std::string _tocken) { - return m_user->checkTocken(_clientName, _tocken); - } - bool checkAuth(std::string _password) { - return m_user->checkAuth(_password); - } - std::vector filterServices(std::string _clientName, std::vector _currentList) { - return m_user->filterServices(_clientName, _currentList); - } - }; } @@ -187,21 +124,39 @@ int main(int _argc, const char *_argv[]) { ip = std::string(&data[5]); } else if (etk::start_with(data, "--port=") == true) { port = etk::string_to_uint16_t(std::string(&data[7])); + } else if (etk::start_with(data, "--name=") == true) { + g_userName = std::string(&data[7]); } else if ( data == "-h" || data == "--help") { APPL_PRINT(etk::getApplicationName() << " - help : "); APPL_PRINT(" " << _argv[0] << " [options]"); - APPL_PRINT(" --ip=XXX Server connection IP (default: 1.7.0.0.1)"); - APPL_PRINT(" --port=XXX Server connection PORT (default: 1983)"); + APPL_PRINT(" --name=XXX User name of the service"); + APPL_PRINT(" --ip=XXX Server connection IP (default: 1.7.0.0.1)"); + APPL_PRINT(" --port=XXX Server connection PORT (default: 1983)"); return -1; } } + if (g_userName.size() == 0) { + APPL_ERROR("Missing User name when runnig service"); + exit(-1); + } + { + std::unique_lock lock(g_mutex); + APPL_WARNING("Load USER: " << g_userName); + bool ret = g_database.load(std::string("USERDATA:") + g_userName + ".json"); + if (ret == false) { + APPL_WARNING(" ==> LOAD error"); + } + } + // TODO: Remove the While true, ==> sevice must be spown by a user call, if a service die, the wall system will die ... while (true) { APPL_INFO("==========================================================="); APPL_INFO("== ZEUS instanciate service: " << SERVICE_NAME << " [START]"); APPL_INFO("==========================================================="); - ememory::SharedPtr userMng = ememory::makeShared(); - zeus::ServiceType serviceInterface(userMng); + + zeus::ServiceType serviceInterface([](ememory::SharedPtr _client){ + return ememory::makeShared(_client); + }); if (ip != "") { serviceInterface.propertyIp.set(ip); } @@ -259,5 +214,18 @@ int main(int _argc, const char *_argv[]) { APPL_INFO("== ZEUS service: " << *serviceInterface.propertyNameService << " [STOP] GateWay Stop"); APPL_INFO("==========================================================="); } + APPL_INFO("Stop service ==> flush internal datas ..."); + { + std::unique_lock lock(g_mutex); + APPL_DEBUG("Store User Info:"); + bool ret = g_database.storeSafe(std::string("USERDATA:") + g_userName + ".json"); + if (ret == false) { + APPL_WARNING(" ==> Store error"); + } + APPL_WARNING("delete USER [STOP]"); + } + APPL_INFO("==========================================================="); + APPL_INFO("== ZEUS service: " << SERVICE_NAME << " [END-APPLICATION]"); + APPL_INFO("==========================================================="); return 0; } diff --git a/zeus/Service.cpp b/zeus/Service.cpp index 7c7c7d1..9fc2c76 100644 --- a/zeus/Service.cpp +++ b/zeus/Service.cpp @@ -95,16 +95,16 @@ void zeus::Service::connect(uint32_t _numberRetry){ return; } m_interfaceClient->connect(this, &zeus::Service::onClientData); - m_interfaceClient->setInterface(std::move(connection), false); + m_interfaceClient->setInterface(std::move(connection), false, propertyNameService.get()); m_interfaceClient->connect(); - zeus::Future ret = m_interfaceClient->call("connect-service", propertyNameService.get()); - ret.wait(); + // TODO : Check error ... + /* if (ret.get() == false) { ZEUS_ERROR("Can not configure the interface for the service with the current name ..."); m_interfaceClient->disconnect(); return; } - + */ ZEUS_DEBUG("connect [STOP]"); } diff --git a/zeus/Service.hpp b/zeus/Service.hpp index 85d7540..1f46283 100644 --- a/zeus/Service.hpp +++ b/zeus/Service.hpp @@ -93,6 +93,7 @@ namespace zeus { } }; } + namespace zeus { /** * @brief @@ -239,11 +240,17 @@ namespace zeus { m_listFunction.push_back(tmp); return tmp; } + }; - template + template class ServiceType : public zeus::Service { private: - ememory::SharedPtr m_getUserInterface; + std::function(ememory::SharedPtr)> m_factory; + public: + ServiceType(std::function(ememory::SharedPtr)> _factory) { + m_factory = _factory; + } + private: // no need of shared_ptr or unique_ptr (if service die all is lost and is client die, the gateway notify us...) std::map, ememory::SharedPtr>> m_interface; public: @@ -280,15 +287,6 @@ namespace zeus { m_listFunction.push_back(tmp); return tmp; } - /** - * @brief - * @param[in] - * @return - */ - ServiceType(ememory::SharedPtr _interface): - m_getUserInterface(_interface) { - - } /** * @brief * @param[in] @@ -313,10 +311,10 @@ namespace zeus { ZEUS_DEBUG(" groups=" << etk::to_string(_groups)); ememory::SharedPtr tmpProperty = ememory::makeShared(_clientName, _groups); ememory::SharedPtr tmpSrv; - if (m_getUserInterface == nullptr) { - tmpSrv = ememory::makeShared(nullptr, tmpProperty); + if (m_factory != nullptr) { + tmpSrv = m_factory(tmpProperty); } else { - tmpSrv = ememory::makeShared(m_getUserInterface->getUser(_userName), tmpProperty); + ZEUS_ERROR("Create service with no factory"); } m_interface.insert(std::make_pair(_clientId, std::make_pair(tmpProperty, tmpSrv))); // enable list of function availlable: