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/Service.h>
|
|
|
|
#include <jus/debug.h>
|
2016-05-22 22:40:42 +02:00
|
|
|
#include <etk/stdTools.h>
|
2016-05-23 21:18:37 +02:00
|
|
|
#include <enet/TcpClient.h>
|
2016-05-22 22:40:42 +02:00
|
|
|
#include <ejson/ejson.h>
|
2016-05-12 22:45:40 +02:00
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
jus::Service::Service() :
|
|
|
|
propertyIp(this, "ip", "127.0.0.1", "Ip to connect server", &jus::Service::onPropertyChangeIp),
|
2016-05-23 22:38:46 +02:00
|
|
|
propertyPort(this, "port", 1982, "Port to connect server", &jus::Service::onPropertyChangePort) {
|
2016-05-24 23:00:56 +02:00
|
|
|
m_interfaceClient.connect(this, &jus::Service::onClientData);
|
2016-05-30 21:01:39 +02:00
|
|
|
|
|
|
|
advertise("getExtention", &jus::Service::getExtention);
|
|
|
|
setLastFuncDesc("Get List of availlable extention of this service");
|
|
|
|
addLastFuncReturn("A list of extention register in the service");
|
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jus::Service::~Service() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-30 21:01:39 +02:00
|
|
|
|
|
|
|
std::vector<std::string> jus::Service::getExtention() {
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-24 23:00:56 +02:00
|
|
|
void jus::Service::onClientData(std::string _value) {
|
2016-05-17 22:14:02 +02:00
|
|
|
ejson::Object request(_value);
|
2016-05-25 21:25:33 +02:00
|
|
|
ejson::Value tmpID = request["id"];
|
|
|
|
request.remove("id");
|
2016-05-23 21:18:37 +02:00
|
|
|
JUS_INFO("Request: " << _value);
|
2016-05-24 22:29:03 +02:00
|
|
|
ejson::Value answer = callJson(request);
|
|
|
|
// check if an answer is needed
|
|
|
|
if (answer.isNull() == false) {
|
2016-05-25 21:25:33 +02:00
|
|
|
answer.toObject().add("id", tmpID);
|
2016-05-24 22:29:03 +02:00
|
|
|
JUS_INFO("Answer: " << answer.generateHumanString());
|
|
|
|
m_interfaceClient.write(answer.generateMachineString());
|
|
|
|
}
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void jus::Service::onPropertyChangeIp() {
|
2016-05-23 21:18:37 +02:00
|
|
|
disconnect();
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void jus::Service::onPropertyChangePort(){
|
2016-05-23 21:18:37 +02:00
|
|
|
disconnect();
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-25 21:25:33 +02:00
|
|
|
void jus::Service::connect(const std::string& _serviceName, uint32_t _numberRetry){
|
2016-05-23 21:18:37 +02:00
|
|
|
disconnect();
|
2016-05-17 22:14:02 +02:00
|
|
|
JUS_DEBUG("connect [START]");
|
2016-05-25 21:25:33 +02:00
|
|
|
enet::Tcp connection = std::move(enet::connectTcpClient(*propertyIp, *propertyPort, _numberRetry));
|
|
|
|
if (connection.getConnectionStatus() != enet::Tcp::status::link) {
|
|
|
|
JUS_DEBUG("connect [STOP] ==> can not connect");
|
|
|
|
return;
|
|
|
|
}
|
2016-05-23 21:18:37 +02:00
|
|
|
m_interfaceClient.setInterface(std::move(connection));
|
2016-05-17 22:14:02 +02:00
|
|
|
m_interfaceClient.connect();
|
2016-05-22 22:40:42 +02:00
|
|
|
m_interfaceClient.write(std::string("{\"connect-service\":\"") + _serviceName + "\"}");
|
2016-05-17 22:14:02 +02:00
|
|
|
JUS_DEBUG("connect [STOP]");
|
|
|
|
}
|
|
|
|
|
|
|
|
void jus::Service::disconnect(){
|
|
|
|
JUS_DEBUG("disconnect [START]");
|
|
|
|
m_interfaceClient.disconnect();
|
|
|
|
JUS_DEBUG("disconnect [STOP]");
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:25:33 +02:00
|
|
|
bool jus::Service::GateWayAlive() {
|
|
|
|
return m_interfaceClient.isActive();
|
|
|
|
}
|
2016-05-24 22:29:03 +02:00
|
|
|
|
|
|
|
void jus::Service::pingIsAlive() {
|
2016-05-25 21:25:33 +02:00
|
|
|
if (std::chrono::steady_clock::now() - m_interfaceClient.getLastTimeSend() >= std::chrono::seconds(30)) {
|
|
|
|
m_interfaceClient.write("{\"event\":\"IS-ALIVE\"}");
|
|
|
|
}
|
2016-05-24 22:29:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ejson::Value jus::Service::callJson(const ejson::Object& _obj) {
|
2016-05-25 21:25:33 +02:00
|
|
|
if (_obj.valueExist("event") == true) {
|
|
|
|
std::string event = _obj["event"].toString().get();
|
|
|
|
if (event == "IS-ALIVE") {
|
|
|
|
// Gateway just aswer a keep alive information ...
|
|
|
|
// Nothing to do ...
|
|
|
|
} else if (event == "new") {
|
2016-05-27 22:21:32 +02:00
|
|
|
uint64_t clientId = _obj["client-id"].toNumber().getU64();
|
2016-05-25 21:25:33 +02:00
|
|
|
std::string userName = _obj["user"].toString().get();
|
2016-05-29 23:59:19 +02:00
|
|
|
std::string clientName = _obj["client"].toString().get();
|
|
|
|
std::vector<std::string> clientGroup = convertJsonTo<std::vector<std::string>>(_obj["groups"]);
|
|
|
|
clientConnect(clientId, userName, clientName, clientGroup);
|
2016-05-25 21:25:33 +02:00
|
|
|
} else if (event == "delete") {
|
2016-05-27 22:21:32 +02:00
|
|
|
uint64_t clientId = _obj["client-id"].toNumber().getU64();
|
2016-05-25 21:25:33 +02:00
|
|
|
clientDisconnect(clientId);
|
|
|
|
} else {
|
|
|
|
JUS_ERROR("Unknow event: '" << event << "'");
|
|
|
|
}
|
2016-05-24 22:29:03 +02:00
|
|
|
return ejson::Null();
|
|
|
|
}
|
2016-05-29 23:59:19 +02:00
|
|
|
ejson::Object tmpp;
|
2016-05-30 21:01:39 +02:00
|
|
|
uint64_t clientId = _obj["client-id"].toNumber().getU64();
|
2016-05-25 21:25:33 +02:00
|
|
|
if (_obj.valueExist("call") == true) {
|
2016-05-29 23:59:19 +02:00
|
|
|
std::string call = _obj["call"].toString().get();
|
2016-05-30 21:01:39 +02:00
|
|
|
if (isFunctionAuthorized(clientId, call) == true) {
|
2016-05-29 23:59:19 +02:00
|
|
|
tmpp = callJson2(clientId, _obj);
|
2016-05-30 21:01:39 +02:00
|
|
|
tmpp.add("client-id", ejson::Number(clientId));
|
|
|
|
return tmpp;
|
|
|
|
} else {
|
|
|
|
tmpp.add("error", ejson::String("NOT-AUTHORIZED-FUNCTION"));
|
2016-05-29 23:59:19 +02:00
|
|
|
}
|
2016-05-30 21:01:39 +02:00
|
|
|
} else {
|
|
|
|
tmpp.add("error", ejson::String("NOT-IMPLEMENTED-FUNCTION"));
|
2016-05-23 21:18:37 +02:00
|
|
|
}
|
2016-05-30 21:01:39 +02:00
|
|
|
tmpp.add("client-id", ejson::Number(clientId));
|
2016-05-24 22:29:03 +02:00
|
|
|
return tmpp;
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|