From 75de590480e2b9ecad0162ae56c2a51409538bf0 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 29 May 2016 23:59:19 +0200 Subject: [PATCH] [DEV] better integration of right and start dev of introspection of fuctions --- jus/FutureBase.cpp | 12 ++--- jus/FutureBase.h | 6 +-- jus/GateWayClient.cpp | 51 +++++++++++---------- jus/Service.cpp | 13 ++++-- jus/Service.h | 16 ++++--- jus/ServiceRemote.h | 4 +- test/client/appl/main.cpp | 79 +++++++++++++++++++++++++-------- tools/system-user/appl/main.cpp | 6 ++- 8 files changed, 126 insertions(+), 61 deletions(-) diff --git a/jus/FutureBase.cpp b/jus/FutureBase.cpp index b08ad49..e7270eb 100644 --- a/jus/FutureBase.cpp +++ b/jus/FutureBase.cpp @@ -104,29 +104,31 @@ bool jus::FutureBase::isFinished() { return m_data->m_isFinished; } -void jus::FutureBase::wait() { +jus::FutureBase& jus::FutureBase::wait() { while (isFinished() == false) { // TODO : Do it better ... like messaging/mutex_locked ... usleep(10000); } + return *this; } -bool jus::FutureBase::waitFor(std::chrono::microseconds _delta) { +jus::FutureBase& jus::FutureBase::waitFor(std::chrono::microseconds _delta) { std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now(); while ( std::chrono::steady_clock::now() - start < _delta && isFinished() == false) { // TODO : Do it better ... like messaging/mutex_locked ... usleep(10000); + start = std::chrono::steady_clock::now(); } - return isFinished(); + return *this; } -bool jus::FutureBase::waitUntil(std::chrono::steady_clock::time_point _endTime) { +jus::FutureBase& jus::FutureBase::waitUntil(std::chrono::steady_clock::time_point _endTime) { while ( std::chrono::steady_clock::now() < _endTime && isFinished() == false) { // TODO : Do it better ... like messaging/mutex_locked ... usleep(10000); } - return isFinished(); + return *this; } diff --git a/jus/FutureBase.h b/jus/FutureBase.h index 7442076..7d47c3c 100644 --- a/jus/FutureBase.h +++ b/jus/FutureBase.h @@ -24,9 +24,9 @@ namespace jus { std::string getErrorHelp(); bool isValid(); bool isFinished(); - void wait(); - bool waitFor(std::chrono::microseconds _delta); - bool waitUntil(std::chrono::steady_clock::time_point _endTime); + FutureBase& wait(); + FutureBase& waitFor(std::chrono::microseconds _delta = std::chrono::seconds(30)); + FutureBase& waitUntil(std::chrono::steady_clock::time_point _endTime); ejson::Object getRaw(); }; } diff --git a/jus/GateWayClient.cpp b/jus/GateWayClient.cpp index 9d18ec4..fe2beef 100644 --- a/jus/GateWayClient.cpp +++ b/jus/GateWayClient.cpp @@ -118,6 +118,8 @@ void jus::GateWayClient::onClientData(std::string _value) { ejson::Object linkService; linkService.add("event", ejson::String("new")); linkService.add("user", ejson::String(m_userConnectionName)); + linkService.add("client", ejson::String("**Gateway**")); + linkService.add("groups", ejson::Array()); m_userService->SendData(m_uid2, linkService); m_state = jus::GateWayClient::state::userIdentify; returnBool(transactionId, true); @@ -165,7 +167,7 @@ void jus::GateWayClient::onClientData(std::string _value) { } if (callFunction == "auth") { std::string password = data["param"].toArray()[0].toString().get(); - jus::Future fut = call(m_uid2, m_userService, "auth", password); + jus::Future fut = call(m_uid2, m_userService, "checkAuth", password); fut.wait(); // TODO: Set timeout ... if (fut.hasError() == true) { JUS_ERROR("Get error from the service ..."); @@ -219,24 +221,24 @@ void jus::GateWayClient::onClientData(std::string _value) { break; case jus::GateWayClient::state::clientIdentify: { - std::string service = data["service"].toString().get(); - // Thsi is 2 default service for the cient interface that manage the authorisation of view: - if (service == "") { + // This is 2 default service for the cient interface that manage the authorisation of view: + if (data.valueExist("service") == false) { std::string callFunction = data["call"].toString().get(); ejson::Object answer; //answer.add("from-service", ejson::String("")); answer.add("id", data["id"]); if (callFunction == "getServiceCount") { - // TODO : Do it better: - answer.add("return", ejson::Number(2)); + answer.add("return", ejson::Number(m_clientServices.size())); JUS_DEBUG("answer: " << answer.generateHumanString()); m_interfaceClient.write(answer.generateMachineString()); return; } if (callFunction == "getServiceList") { ejson::Array listService; - listService.add(ejson::String("ServiceManager/v0.1.0")); - listService.add(ejson::String("getServiceInformation/v0.1.0")); + for (auto &it : m_clientServices) { + listService.add(ejson::String(it)); + } + //listService.add(ejson::String("ServiceManager/v0.1.0")); answer.add("return", listService); JUS_DEBUG("answer: " << answer.generateHumanString()); m_interfaceClient.write(answer.generateMachineString()); @@ -252,14 +254,20 @@ void jus::GateWayClient::onClientData(std::string _value) { ++it; continue; } - if ((*it)->getName() != service) { + if ((*it)->getName() != serviceName) { ++it; continue; } break; } if (it == m_listConnectedService.end()) { - // TODO : check if we have authorisation to connect service + // check if service is connectable ... + if (std::find(m_clientServices.begin(), m_clientServices.end(), serviceName) == m_clientServices.end()) { + answer.add("return", ejson::Boolean(false)); + JUS_DEBUG("answer: (NOT authorized service) " << answer.generateHumanString()); + m_interfaceClient.write(answer.generateMachineString()); + return; + } ememory::SharedPtr srv = m_gatewayInterface->get(serviceName); if (srv != nullptr) { ejson::Object linkService; @@ -291,7 +299,7 @@ void jus::GateWayClient::onClientData(std::string _value) { ++it; continue; } - if ((*it)->getName() != service) { + if ((*it)->getName() != serviceName) { ++it; continue; } @@ -316,6 +324,11 @@ void jus::GateWayClient::onClientData(std::string _value) { m_interfaceClient.write(answer.generateMachineString()); return; } + std::string service = data["service"].toString().get(); + if (service == "") { + protocolError("call with \"service\"=\"\" ==> not permited"); + return; + } auto it = m_listConnectedService.begin(); while (it != m_listConnectedService.end()) { if (*it == nullptr) { @@ -347,20 +360,12 @@ void jus::GateWayClient::onClientData(std::string _value) { data["param"].toArray(), [=](jus::FutureBase _ret) { ejson::Object tmpp = _ret.getRaw(); - tmpp["id"] = data["id"]; - JUS_DEBUG(" ==> transmit"); + JUS_VERBOSE(" ==> transmit : " << tmpp["id"].toNumber().getU64() << " -> " << data["id"].toNumber().getU64()); + JUS_VERBOSE(" msg=" << tmpp.generateMachineString()); + tmpp["id"].toNumber().set(data["id"].toNumber().getU64()); + JUS_VERBOSE(" msg=" << tmpp.generateMachineString()); m_interfaceClient.write(tmpp.generateMachineString()); }); - /* - std::unique_lock lock(m_mutex); - m_actions.push_back(std::make_pair(transactionId, - [=](ejson::Object& _data) { - JUS_DEBUG(" ==> transmit"); - m_interfaceClient.write(_data.generateMachineString()); - })); - } - (*it)->SendData(m_uid, data); - */ } } } diff --git a/jus/Service.cpp b/jus/Service.cpp index 466c484..b7a0abc 100644 --- a/jus/Service.cpp +++ b/jus/Service.cpp @@ -86,7 +86,9 @@ ejson::Value jus::Service::callJson(const ejson::Object& _obj) { } else if (event == "new") { uint64_t clientId = _obj["client-id"].toNumber().getU64(); std::string userName = _obj["user"].toString().get(); - clientConnect(clientId, userName); + std::string clientName = _obj["client"].toString().get(); + std::vector clientGroup = convertJsonTo>(_obj["groups"]); + clientConnect(clientId, userName, clientName, clientGroup); } else if (event == "delete") { uint64_t clientId = _obj["client-id"].toNumber().getU64(); clientDisconnect(clientId); @@ -95,13 +97,18 @@ ejson::Value jus::Service::callJson(const ejson::Object& _obj) { } return ejson::Null(); } + ejson::Object tmpp; if (_obj.valueExist("call") == true) { uint64_t clientId = _obj["client-id"].toNumber().getU64(); - ejson::Object tmpp = callJson2(clientId, _obj); + std::string call = _obj["call"].toString().get(); + if (etk::start_with(call, "srv.") == true) { + tmpp.add("error", ejson::String("NOT-IMPLEMENTED-ACTION **")); + } else { + tmpp = callJson2(clientId, _obj); + } tmpp.add("client-id", ejson::Number(clientId)); return tmpp; } - ejson::Object tmpp; tmpp.add("error", ejson::String("NOT-IMPLEMENTED-ACTION")); return tmpp; } diff --git a/jus/Service.h b/jus/Service.h index df37549..498f546 100644 --- a/jus/Service.h +++ b/jus/Service.h @@ -16,7 +16,11 @@ namespace jus { class ClientProperty { public: - ClientProperty() {} + ClientProperty(const std::string& _clientName="", const std::vector& _groups = std::vector()) : + m_name(_clientName), + m_groups(_groups) { + + } private: std::string m_name; public: @@ -68,7 +72,7 @@ namespace jus { * @param[in] _userName User name of the client to connect * @todo Set a relur like ==> service not availlable / service close / service maintenance / service right reject */ - virtual void clientConnect(size_t _clientSessionID, const std::string& _userName) = 0; + virtual void clientConnect(size_t _clientSessionID, const std::string& _userName, const std::string& _clientName, const std::vector& _groups) = 0; virtual void clientDisconnect(size_t _clientSessionID) = 0; // Genenric function call: ejson::Value callJson(const ejson::Object& _obj); @@ -104,10 +108,12 @@ namespace jus { m_getUserInterface(_interface) { } - void clientConnect(uint64_t _clientSessionID, const std::string& _userName) { + void clientConnect(uint64_t _clientSessionID, const std::string& _userName, const std::string& _clientName, const std::vector& _groups) { std::unique_lock lock(m_mutex); - JUS_DEBUG("connect : " << _clientSessionID << " to '" << _userName << "'"); - ememory::SharedPtr tmpProperty = std::make_shared(); + JUS_DEBUG("connect: " << _clientSessionID << " to '" << _userName << "'"); + JUS_DEBUG(" client name='" << _clientName << "'"); + JUS_DEBUG(" groups=" << etk::to_string(_groups)); + ememory::SharedPtr tmpProperty = std::make_shared(_clientName, _groups); ememory::SharedPtr tmpSrv = std::make_shared(m_getUserInterface.getUser(_userName), tmpProperty); m_interface.insert(std::make_pair(_clientSessionID, std::make_pair(tmpProperty, tmpSrv))); } diff --git a/jus/ServiceRemote.h b/jus/ServiceRemote.h index 199c704..6807f59 100644 --- a/jus/ServiceRemote.h +++ b/jus/ServiceRemote.h @@ -31,13 +31,13 @@ namespace jus { template jus::FutureBase call(const std::string& _functionName, _ARGS&&... _args) { uint64_t id = getId(); - ejson::Object callElem = jus::createCall(id, _functionName, std::forward<_ARGS>(_args)...); + ejson::Object callElem = jus::createCallService(id, m_name, _functionName, std::forward<_ARGS>(_args)...); return callJson(id, callElem); } template jus::FutureBase callAction(const std::string& _functionName, _ARGS&&... _args, jus::FutureData::ObserverFinish _callback) { uint64_t id = getId(); - ejson::Object callElem = jus::createCall(id, _functionName, std::forward<_ARGS>(_args)...); + ejson::Object callElem = jus::createCallService(id, m_name, _functionName, std::forward<_ARGS>(_args)...); return callJson(id, callElem, _callback); } }; diff --git a/test/client/appl/main.cpp b/test/client/appl/main.cpp index 64aa7fb..4b36197 100644 --- a/test/client/appl/main.cpp +++ b/test/client/appl/main.cpp @@ -34,43 +34,84 @@ int main(int _argc, const char *_argv[]) { APPL_INFO("== JUS test client start =="); APPL_INFO("=================================="); client1.connect("test1#atria-soft.com"); - // Connect that is not us - //client1.identify("clientTest1#atria-soft.com", "QSDQSDGQSF54HSXWVCSQDJ654URTDJ654NBXCDFDGAEZ51968"); - jus::Future retIdentify = client1.call("identify", "clientTest1#atria-soft.com", "QSDQSDGQSF54HSXWVCSQDJ654URTDJ654NBXCDFDGAEZ51968"); - retIdentify.wait(); - //bool retIdentify = client1.call_b("identify", "clientTest1#atria-soft.com", "QSDQSDGQSF54HSXWVCSQDJ654URTDJ654NBXCDFDGAEZ51968"); - + if (false) { + jus::Future retIdentify = client1.call("identify", "clientTest1#atria-soft.com", "QSDQSDGQSF54HSXWVCSQDJ654URTDJ654NBXCDFDGAEZ51968"); + retIdentify.wait(); + if (retIdentify.get() == false) { + APPL_ERROR(" ==> NOT Connected with 'clientTest1#atria-soft.com'"); + return -1; + } else { + APPL_INFO(" ==> Connected with 'clientTest1#atria-soft.com'"); + } + } else if (true) { + jus::Future retIdentify = client1.call("auth", "coucou"); + retIdentify.wait(); + if (retIdentify.get() == false) { + APPL_ERROR(" ==> NOT Authentify with 'test1#atria-soft.com'"); + return -1; + } else { + APPL_INFO(" ==> Authentify with 'test1#atria-soft.com'"); + } + } else { + jus::Future retIdentify = client1.call("anonymous"); + retIdentify.wait(); + if (retIdentify.get() == false) { + APPL_ERROR(" ==> NOT Connected with 'anonymous'"); + return -1; + } else { + APPL_INFO(" ==> Connected with 'anonymous'"); + } + } // Connect to ourself: //client1.authentificate("coucou"); //bool retAuthentify = client1.call_b("authentify", "coucou"); APPL_INFO(" ----------------------------------"); - APPL_INFO(" -- Get service count --"); + APPL_INFO(" -- Get service count"); APPL_INFO(" ----------------------------------"); - /* - std::vector tmp; - tmp.push_back(1); - tmp.push_back(22); - tmp.push_back(333); - tmp.push_back(4444); - int32_t val = client1.call_i("getServiceCount", tmp, "coucou", false); - APPL_INFO("Nb services = " << val); - std::vector val2 = client1.call_vs("getServiceList"); + jus::Future retNbService = client1.call("getServiceCount"); + retNbService.wait(); + APPL_INFO("Nb services = " << retNbService.get()); + jus::Future> retServiceList = client1.call("getServiceList"); + retServiceList.wait(); APPL_INFO("List services:"); - for (auto &it: val2) { + for (auto &it: retServiceList.get()) { APPL_INFO(" - " << it); } + /* jus::ServiceRemote localService = client1.getService("serviceTest1"); if (localService.exist() == true) { double retCall = localService.call_d("mul", 13.1, 2.0); APPL_INFO("serviceTest1.mul = " << retCall); } */ + APPL_INFO(" ----------------------------------"); + APPL_INFO(" -- Get service system-user"); + APPL_INFO(" ----------------------------------"); jus::ServiceRemote remoteServiceUser = client1.getService("system-user"); if (remoteServiceUser.exist() == true) { - jus::Future> retCall = client1.call("getClientGroups", "clientTest1#atria-soft.com"); + jus::Future> retCall = remoteServiceUser.call("getGroups", "clientTest1#atria-soft.com"); retCall.wait(); + APPL_INFO("system-user.getGroups() = " << retCall.get()); + jus::Future retVersion = remoteServiceUser.call("srv.getVersion"); + jus::Future retType = remoteServiceUser.call("srv.getType"); + jus::Future> retExtention = remoteServiceUser.call("srv.getExtention"); + jus::Future> retMaintainer = remoteServiceUser.call("srv.getMaintainer"); + jus::Future> retFuctions = remoteServiceUser.call("srv.getFunctions"); + jus::Future> retFunctionSignature = remoteServiceUser.call("srv.getFunctionSignature", "filterServices"); + jus::Future retFunctionPrototype = remoteServiceUser.call("srv.getFunctionPrototype", "filterServices"); + jus::Future retFunctionHelp = remoteServiceUser.call("srv.getFunctionHelp", "filterServices"); + retVersion.wait(); + retType.wait(); + retExtention.wait(); + retMaintainer.wait(); + retFuctions.wait(); + retFunctionSignature.wait(); + retFunctionPrototype.wait(); + retFunctionHelp.wait(); + APPL_INFO("system-user.getClientGroups() = " << retCall.get()); + } int32_t iii=0; while (iii < 3) { @@ -80,7 +121,7 @@ int main(int _argc, const char *_argv[]) { } client1.disconnect(); APPL_INFO("=================================="); - APPL_INFO("== JUS test client stop =="); + APPL_INFO("== JUS test client stop"); APPL_INFO("=================================="); return 0; } diff --git a/tools/system-user/appl/main.cpp b/tools/system-user/appl/main.cpp index 2b8aa73..a69b9e3 100644 --- a/tools/system-user/appl/main.cpp +++ b/tools/system-user/appl/main.cpp @@ -98,8 +98,12 @@ namespace appl { } std::vector filterServices(const std::string& _clientName, std::vector _inputList) { std::unique_lock lock(m_mutex); + // When connected to our session ==> we have no control access ... + if (_clientName == m_userName) { + return _inputList; + } std::vector out; - // TODO: ... + APPL_TODO("Filter service list ==> not implemented..."); return out; } };