2016-05-22 22:40:42 +02:00
|
|
|
/** @file
|
|
|
|
* @author Edouard DUPIN
|
|
|
|
* @copyright 2016, Edouard DUPIN, all right reserved
|
|
|
|
* @license APACHE v2.0 (see license file)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <jus/debug.h>
|
|
|
|
#include <jus/GateWayService.h>
|
2016-05-23 21:18:37 +02:00
|
|
|
#include <jus/GateWay.h>
|
2016-05-22 22:40:42 +02:00
|
|
|
#include <ejson/ejson.h>
|
|
|
|
|
2016-05-23 21:18:37 +02:00
|
|
|
jus::GateWayService::GateWayService(enet::Tcp _connection, jus::GateWay* _gatewayInterface) :
|
|
|
|
m_gatewayInterface(_gatewayInterface),
|
|
|
|
m_interfaceClient(std::move(_connection)) {
|
2016-05-24 22:29:03 +02:00
|
|
|
JUS_INFO("-----------------");
|
|
|
|
JUS_INFO("-- NEW Service --");
|
|
|
|
JUS_INFO("-----------------");
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
jus::GateWayService::~GateWayService() {
|
|
|
|
|
2016-05-24 22:29:03 +02:00
|
|
|
JUS_INFO("--------------------");
|
|
|
|
JUS_INFO("-- DELETE Service --");
|
|
|
|
JUS_INFO("--------------------");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool jus::GateWayService::isAlive() {
|
|
|
|
return m_interfaceClient.isActive();
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-05-23 21:18:37 +02:00
|
|
|
void jus::GateWayService::start() {
|
2016-05-24 23:00:56 +02:00
|
|
|
m_interfaceClient.connect(this, &jus::GateWayService::onServiceData);
|
2016-05-23 21:18:37 +02:00
|
|
|
m_interfaceClient.connect();
|
2016-05-22 22:40:42 +02:00
|
|
|
m_interfaceClient.setInterfaceName("srv-?");
|
|
|
|
}
|
|
|
|
|
|
|
|
void jus::GateWayService::stop() {
|
|
|
|
m_interfaceClient.disconnect();
|
|
|
|
}
|
|
|
|
|
2016-05-23 21:18:37 +02:00
|
|
|
void jus::GateWayService::SendData(size_t _userSessionId, ejson::Object _data, const std::string& _action) {
|
2016-05-22 22:40:42 +02:00
|
|
|
_data.add("client-id", ejson::String(etk::to_string(_userSessionId)));
|
2016-05-23 21:18:37 +02:00
|
|
|
_data.add("action", ejson::String(_action));
|
2016-05-24 22:29:03 +02:00
|
|
|
JUS_DEBUG("Send Service: " << _data.generateHumanString());
|
|
|
|
m_interfaceClient.write(_data.generateMachineString());
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 23:00:56 +02:00
|
|
|
void jus::GateWayService::onServiceData(std::string _value) {
|
2016-05-22 22:40:42 +02:00
|
|
|
JUS_DEBUG("On service data: " << _value);
|
|
|
|
ejson::Object data(_value);
|
2016-05-24 22:29:03 +02:00
|
|
|
if (data.valueExist("event") == true) {
|
|
|
|
if (data["event"].toString().get() == "IS-ALIVE") {
|
|
|
|
JUS_INFO("Service Alive ...");
|
|
|
|
} else {
|
|
|
|
JUS_INFO("Unknow service event: '" << data["event"].toString().get() << "'");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-05-22 22:40:42 +02:00
|
|
|
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());
|
2016-05-22 23:15:09 +02:00
|
|
|
// TODO : Return something ...
|
2016-05-22 22:40:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_name = data["connect-service"].toString().get();
|
|
|
|
m_interfaceClient.setInterfaceName("srv-" + m_name);
|
|
|
|
JUS_WARNING("Service name configured");
|
2016-05-22 23:15:09 +02:00
|
|
|
// TODO : Return something ...
|
2016-05-22 22:40:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (data.valueExist("client-id") == false) {
|
2016-05-23 21:18:37 +02:00
|
|
|
JUS_ERROR("Service interface ==> wrong service answer ==> missing 'client-id'");
|
2016-05-22 22:40:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-05-23 21:18:37 +02:00
|
|
|
uint64_t userSessionId = etk::string_to_uint64_t(data["client-id"].toString().get());
|
|
|
|
data.remove("client-id");
|
|
|
|
data.remove("action");
|
|
|
|
m_gatewayInterface->answer(userSessionId, data);
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|