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>
|
|
|
|
|
2016-05-18 22:10:21 +02:00
|
|
|
|
|
|
|
|
2016-05-17 22:14:02 +02:00
|
|
|
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-17 22:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jus::Service::~Service() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-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) {
|
|
|
|
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-22 22:40:42 +02:00
|
|
|
void jus::Service::connect(const std::string& _serviceName){
|
2016-05-23 21:18:37 +02:00
|
|
|
disconnect();
|
2016-05-17 22:14:02 +02:00
|
|
|
JUS_DEBUG("connect [START]");
|
2016-05-23 21:18:37 +02:00
|
|
|
enet::Tcp connection = std::move(enet::connectTcpClient(*propertyIp, *propertyPort));
|
|
|
|
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-24 22:29:03 +02:00
|
|
|
|
|
|
|
void jus::Service::pingIsAlive() {
|
|
|
|
m_interfaceClient.write("{\"event\":\"IS-ALIVE\"}");
|
|
|
|
}
|
|
|
|
|
|
|
|
ejson::Value jus::Service::callJson(const ejson::Object& _obj) {
|
2016-05-22 22:40:42 +02:00
|
|
|
std::string action = _obj["action"].toString().get();
|
2016-05-23 21:18:37 +02:00
|
|
|
if (action == "new") {
|
2016-05-22 22:40:42 +02:00
|
|
|
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
|
2016-05-23 21:18:37 +02:00
|
|
|
std::string userName = _obj["user"].toString().get();
|
|
|
|
clientConnect(clientId, userName);
|
2016-05-24 22:29:03 +02:00
|
|
|
/*
|
2016-05-23 21:18:37 +02:00
|
|
|
ejson::Object tmpp;
|
|
|
|
tmpp.add("client-id", ejson::String(etk::to_string(clientId)));
|
|
|
|
tmpp.add("return", ejson::String("OK"));
|
2016-05-24 22:29:03 +02:00
|
|
|
return tmpp
|
|
|
|
*/
|
|
|
|
return ejson::Null();
|
|
|
|
}
|
|
|
|
if (action == "delete") {
|
2016-05-23 21:18:37 +02:00
|
|
|
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
|
|
|
|
clientDisconnect(clientId);
|
2016-05-24 22:29:03 +02:00
|
|
|
/*
|
2016-05-23 21:18:37 +02:00
|
|
|
ejson::Object tmpp;
|
|
|
|
tmpp.add("client-id", ejson::String(etk::to_string(clientId)));
|
|
|
|
tmpp.add("return", ejson::String("OK"));
|
|
|
|
return tmpp;
|
2016-05-24 22:29:03 +02:00
|
|
|
*/
|
|
|
|
return ejson::Null();
|
|
|
|
}
|
|
|
|
if ( action == "call"
|
|
|
|
|| action == "") {
|
2016-05-23 21:18:37 +02:00
|
|
|
uint64_t clientId = etk::string_to_uint64_t(_obj["client-id"].toString().get());
|
|
|
|
ejson::Object tmpp = callJson2(clientId, _obj);
|
|
|
|
tmpp.add("client-id", ejson::String(etk::to_string(clientId)));
|
|
|
|
return tmpp;
|
|
|
|
}
|
2016-05-24 22:29:03 +02:00
|
|
|
ejson::Object tmpp;
|
|
|
|
tmpp.add("error", ejson::String("NOT-IMPLEMENTED-ACTION"));
|
|
|
|
return tmpp;
|
2016-05-17 22:14:02 +02:00
|
|
|
}
|