2016-05-12 22:45:40 +02:00
|
|
|
/** @file
|
|
|
|
* @author Edouard DUPIN
|
|
|
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
|
|
|
* @license APACHE v2.0 (see license file)
|
|
|
|
*/
|
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
#include <jus/debug.h>
|
2016-05-12 22:45:40 +02:00
|
|
|
#include <jus/GateWayClient.h>
|
2016-05-17 22:14:02 +02:00
|
|
|
#include <ejson/ejson.h>
|
2016-05-22 22:40:42 +02:00
|
|
|
#include <jus/GateWay.h>
|
|
|
|
#include <unistd.h>
|
2016-05-12 22:45:40 +02:00
|
|
|
|
2016-05-22 22:40:42 +02:00
|
|
|
jus::GateWayClient::GateWayClient(jus::GateWay* _gatewayInterface) :
|
|
|
|
m_gatewayInterface(_gatewayInterface),
|
|
|
|
m_returnValueOk(false) {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
jus::GateWayClient::~GateWayClient() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-22 22:40:42 +02:00
|
|
|
void jus::GateWayClient::start(const std::string& _ip, uint16_t _port, size_t _uid) {
|
2016-05-12 22:45:40 +02:00
|
|
|
m_interfaceClient.propertyIp.set(_ip);
|
|
|
|
m_interfaceClient.propertyPort.set(_port);
|
2016-05-22 22:40:42 +02:00
|
|
|
m_uid = _uid;
|
2016-05-13 23:10:13 +02:00
|
|
|
m_interfaceClient.propertyServer.set(true);
|
2016-05-22 22:40:42 +02:00
|
|
|
m_interfaceClient.connect(true);
|
|
|
|
m_interfaceClient.setInterfaceName("cli-" + etk::to_string(m_uid));
|
2016-05-17 22:14:02 +02:00
|
|
|
m_dataCallback = m_interfaceClient.signalData.connect(this, &jus::GateWayClient::onClientData);
|
2016-05-12 22:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void jus::GateWayClient::stop() {
|
2016-05-13 23:10:13 +02:00
|
|
|
m_interfaceClient.disconnect();
|
2016-05-12 22:45:40 +02:00
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
void jus::GateWayClient::onClientData(const std::string& _value) {
|
|
|
|
JUS_DEBUG("On data: " << _value);
|
|
|
|
ejson::Object data(_value);
|
2016-05-22 23:15:09 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
if (data.valueExist("service") == false) {
|
|
|
|
// add default service
|
|
|
|
data.add("service", ejson::String("ServiceManager"));
|
|
|
|
JUS_WARNING("missing service name ==> set it by default at ServiceManager");
|
|
|
|
}
|
|
|
|
std::string service = data["service"].toString().get();
|
|
|
|
// Thsi is 2 default service for the cient interface that manage the authorisation of view:
|
|
|
|
if (service == "ServiceManager") {
|
|
|
|
std::string call = data["call"].toString().get();
|
|
|
|
ejson::Object answer;
|
|
|
|
answer.add("from-service", ejson::String("ServiceManager"));
|
2016-05-22 22:40:42 +02:00
|
|
|
answer.add("id", data["id"]);
|
2016-05-17 22:14:02 +02:00
|
|
|
if (call == "getServiceCount") {
|
|
|
|
// TODO : Do it better:
|
|
|
|
answer.add("return", ejson::Number(2));
|
|
|
|
} else if (call == "getServiceList") {
|
|
|
|
ejson::Array listService;
|
|
|
|
listService.add(ejson::String("ServiceManager/v0.1.0"));
|
|
|
|
listService.add(ejson::String("getServiceInformation/v0.1.0"));
|
|
|
|
answer.add("return", listService);
|
|
|
|
} else if (call == "getServiceInformation") {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
JUS_ERROR("Function does not exist ... '" << call << "'");
|
|
|
|
answer.add("error", ejson::String("CALL-UNEXISTING"));
|
|
|
|
}
|
|
|
|
std::string valueReturn = answer.generate();
|
|
|
|
JUS_DEBUG("answer: " << valueReturn);
|
|
|
|
m_interfaceClient.write(valueReturn);
|
|
|
|
} else if (service == "Authentification") {
|
|
|
|
std::string call = data["call"].toString().get();
|
|
|
|
|
|
|
|
} else {
|
2016-05-22 23:15:09 +02:00
|
|
|
auto it = m_listConnectedService.begin();
|
|
|
|
while (it != m_listConnectedService.end()) {
|
|
|
|
if (*it == nullptr) {
|
|
|
|
++it;
|
2016-05-22 22:40:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-22 23:15:09 +02:00
|
|
|
if ((*it)->getName() != service) {
|
|
|
|
++it;
|
2016-05-22 22:40:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2016-05-22 23:15:09 +02:00
|
|
|
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);
|
2016-05-22 22:40:42 +02:00
|
|
|
while (m_returnValueOk == false) {
|
|
|
|
JUS_DEBUG("wait Return Value");
|
|
|
|
usleep(20000);
|
|
|
|
}
|
|
|
|
std::string valueReturn = m_returnMessage.generate();
|
|
|
|
JUS_DEBUG("answer: " << valueReturn);
|
|
|
|
m_interfaceClient.write(valueReturn);
|
|
|
|
m_returnValueOk = false;
|
|
|
|
}
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-22 22:40:42 +02:00
|
|
|
void jus::GateWayClient::returnMessage(ejson::Object _data) {
|
|
|
|
m_returnMessage = _data;
|
|
|
|
m_returnValueOk = true;
|
|
|
|
}
|