[DEV] try something better to link and unlink

This commit is contained in:
Edouard DUPIN 2016-05-22 23:15:09 +02:00
parent a01f8e3e8d
commit 2e49776f86
8 changed files with 86 additions and 44 deletions

View File

@ -56,9 +56,10 @@ void jus::Client::onPropertyChangePort(){
}
void jus::Client::connect(){
void jus::Client::connect(const std::string& _remoteUserToConnect){
JUS_DEBUG("connect [START]");
m_interfaceClient.connect();
m_interfaceClient.write(std::string("{\"connect-to-user\":\"") + _remoteUserToConnect + "\", \"client-type:\":\"jus-client\"}");
JUS_DEBUG("connect [STOP]");
}

View File

@ -23,7 +23,7 @@ namespace jus {
public:
Client();
virtual ~Client();
void connect();
void connect(const std::string& _remoteUserToConnect);
void disconnect();
private:
void onClientData(const std::string& _value);

View File

@ -37,6 +37,18 @@ void jus::GateWayClient::stop() {
void jus::GateWayClient::onClientData(const std::string& _value) {
JUS_DEBUG("On data: " << _value);
ejson::Object data(_value);
if (m_userConnectionName == "") {
if (data.valueExist("connect-to-user") == true) {
m_userConnectionName = data["connect-to-user"].toString().get();
JUS_WARNING("[" << m_uid << "] Set client connect to user : '" << m_userConnectionName << "'");
// TODO : Return something ...
return;
}
JUS_WARNING("[" << m_uid << "] Client must send conection to user name ...");
// TODO : Return something ...
return;
}
if (data.valueExist("service") == false) {
// add default service
data.add("service", ejson::String("ServiceManager"));
@ -59,21 +71,6 @@ void jus::GateWayClient::onClientData(const std::string& _value) {
answer.add("return", listService);
} else if (call == "getServiceInformation") {
} else if (call == "link") {
// first param :
std::string serviceName = data["param"].toArray()[0].toString().get();
// TODO : check if already connected ...
//m_listConnectedService
// TODO : check if we have authorisation to connect service
ememory::SharedPtr<jus::GateWayService> srv = m_gatewayInterface->get(serviceName);
if (srv != nullptr) {
m_listConnectedService.push_back(srv);
answer.add("return", ejson::Boolean(true));
} else {
answer.add("return", ejson::Boolean(false));
}
} else if (call == "unlink") {
answer.add("return", ejson::Boolean(false));
} else {
JUS_ERROR("Function does not exist ... '" << call << "'");
answer.add("error", ejson::String("CALL-UNEXISTING"));
@ -85,14 +82,30 @@ void jus::GateWayClient::onClientData(const std::string& _value) {
std::string call = data["call"].toString().get();
} else {
for (auto &it : m_listConnectedService) {
if (it == nullptr) {
auto it = m_listConnectedService.begin();
while (it != m_listConnectedService.end()) {
if (*it == nullptr) {
++it;
continue;
}
if (it->getName() != service) {
if ((*it)->getName() != service) {
++it;
continue;
}
it->SendData(m_uid, data);
break;
}
if (it == m_listConnectedService.end()) {
ememory::SharedPtr<jus::GateWayService> srv = m_gatewayInterface->get(service);
if (srv != nullptr) {
m_listConnectedService.push_back(srv);
it = m_listConnectedService.end()-1;
} else {
// TODO: Return an error ...
}
}
if (it != m_listConnectedService.end()) {
JUS_CRITICAL("Add in link the name of the user in parameter ..."
(*it)->SendData(m_uid, data);
while (m_returnValueOk == false) {
JUS_DEBUG("wait Return Value");
usleep(20000);
@ -101,7 +114,6 @@ void jus::GateWayClient::onClientData(const std::string& _value) {
JUS_DEBUG("answer: " << valueReturn);
m_interfaceClient.write(valueReturn);
m_returnValueOk = false;
break;
}
}
}

View File

@ -23,6 +23,7 @@ namespace jus {
ejson::Object m_returnMessage;
std::vector<ememory::SharedPtr<jus::GateWayService>> m_listConnectedService;
size_t m_uid;
std::string m_userConnectionName;
public:
GateWayClient(jus::GateWay* _gatewayInterface);
virtual ~GateWayClient();

View File

@ -44,11 +44,13 @@ void jus::GateWayService::onClientData(const std::string& _value) {
if (data.valueExist("connect-service") == true) {
if (m_name != "") {
JUS_WARNING("Service interface ==> try change the servie name after init: '" << data["connect-service"].toString().get());
// TODO : Return something ...
return;
}
m_name = data["connect-service"].toString().get();
m_interfaceClient.setInterfaceName("srv-" + m_name);
JUS_WARNING("Service name configured");
// TODO : Return something ...
return;
}
if (data.valueExist("client-id") == false) {

View File

@ -57,25 +57,44 @@ void jus::Service::disconnect(){
ejson::Object jus::Service::callJson(const ejson::Object& _obj) {
std::string action = _obj["action"].toString().get();
if (action == "new") {
#if 0
if (action == "new") {
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
std::string userName = _obj["user"].toString().get();
clientConnect(clientId, userName);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else if (action == "delete") {
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
clientDisconnect(clientId);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else if ( action == "call"
|| action == "") {
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
return callJson2(clientId, _obj);
} else {
// TODO : ...
}
#else
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
std::string userName = _obj["user"].toString().get();
clientConnect(clientId, userName);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else if (action == "delete") {
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
clientDisconnect(clientId);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else if ( action == "call"
|| action == "") {
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
return callJson2(clientId, _obj);
} else {
// TODO : ...
}
std::string call = _obj["call"].toString().get();
if (call == "link") {
std::string userName = _obj["param"].toArray[0].toString().get();
clientConnect(clientId, userName);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else if (call == "unlink") {
clientDisconnect(clientId);
ejson::Object tmpp;
tmpp.add("return", ejson::String("OK"));
return tmpp;
} else {
return callJson2(clientId, _obj);
}
#endif
return ejson::Object();
}

View File

@ -32,7 +32,7 @@ int main(int _argc, const char *_argv[]) {
APPL_INFO("==================================");
APPL_INFO("== JUS test client start ==");
APPL_INFO("==================================");
client1.connect();
client1.connect("userName");
APPL_INFO(" ----------------------------------");
APPL_INFO(" -- Get service count --");
APPL_INFO(" ----------------------------------");
@ -50,13 +50,13 @@ int main(int _argc, const char *_argv[]) {
APPL_INFO(" - " << it);
}
// TODO: add return value
bool valConnect = client1.call_b("", "link", "serviceTest1");
bool valConnect = client1.call_b("serviceTest1", "link");
APPL_INFO("Link service 'serviceTest1' ret=" << valConnect);
bool retCall = client1.call_d("serviceTest1", "mul", 13.1, 2.0);
APPL_INFO("serviceTest1.mul = " << retCall);
valConnect = client1.call_b("", "unlink", "serviceTest1");
valConnect = client1.call_b("serviceTest1", "unlink");
APPL_INFO("un-Link service 'serviceTest1' ret=" << valConnect);
int32_t iii=0;

View File

@ -43,6 +43,13 @@ namespace appl {
}
};
class Calculator {
public:
Calculator() {
APPL_WARNING("New Calculator ...");
}
~Calculator() {
APPL_WARNING("delete Calculator ...");
}
private:
ememory::SharedPtr<appl::User> m_user;
public: