zeus/jus/Service.cpp

104 lines
2.8 KiB
C++
Raw Normal View History

/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
#include <jus/Service.h>
#include <jus/debug.h>
#include <etk/stdTools.h>
2016-05-23 21:18:37 +02:00
#include <enet/TcpClient.h>
#include <ejson/ejson.h>
#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) {
m_interfaceClient.connect(this, &jus::Service::onClientData);
}
jus::Service::~Service() {
}
void jus::Service::onClientData(std::string _value) {
ejson::Object request(_value);
2016-05-23 21:18:37 +02:00
JUS_INFO("Request: " << _value);
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());
}
}
void jus::Service::onPropertyChangeIp() {
2016-05-23 21:18:37 +02:00
disconnect();
}
void jus::Service::onPropertyChangePort(){
2016-05-23 21:18:37 +02:00
disconnect();
}
void jus::Service::connect(const std::string& _serviceName){
2016-05-23 21:18:37 +02:00
disconnect();
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));
m_interfaceClient.connect();
m_interfaceClient.write(std::string("{\"connect-service\":\"") + _serviceName + "\"}");
JUS_DEBUG("connect [STOP]");
}
void jus::Service::disconnect(){
JUS_DEBUG("disconnect [START]");
m_interfaceClient.disconnect();
JUS_DEBUG("disconnect [STOP]");
}
void jus::Service::pingIsAlive() {
m_interfaceClient.write("{\"event\":\"IS-ALIVE\"}");
}
ejson::Value jus::Service::callJson(const ejson::Object& _obj) {
std::string action = _obj["action"].toString().get();
2016-05-23 21:18:37 +02:00
if (action == "new") {
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-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
*/
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-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;
*/
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;
}
ejson::Object tmpp;
tmpp.add("error", ejson::String("NOT-IMPLEMENTED-ACTION"));
return tmpp;
}