zeus/tools/system-gateway/appl/ServiceInterface.cpp

104 lines
3.2 KiB
C++
Raw Normal View History

/** @file
* @author Edouard DUPIN
* @copyright 2016, Edouard DUPIN, all right reserved
* @license APACHE v2.0 (see license file)
*/
2016-06-23 22:08:11 +02:00
#include <appl/debug.h>
#include <appl/ServiceInterface.h>
#include <appl/ClientInterface.h>
#include <appl/GateWay.h>
// todo : cHANGE THIS ...
static const std::string protocolError = "PROTOCOL-ERROR";
2016-06-23 22:08:11 +02:00
appl::ServiceInterface::ServiceInterface(enet::Tcp _connection, appl::GateWay* _gatewayInterface) :
2016-05-23 21:18:37 +02:00
m_gatewayInterface(_gatewayInterface),
m_interfaceClient(std::move(_connection), true) {
ZEUS_INFO("-----------------");
ZEUS_INFO("-- NEW Service --");
ZEUS_INFO("-----------------");
}
2016-06-23 22:08:11 +02:00
appl::ServiceInterface::~ServiceInterface() {
ZEUS_INFO("--------------------");
ZEUS_INFO("-- DELETE Service --");
ZEUS_INFO("--------------------");
}
2016-06-23 22:08:11 +02:00
bool appl::ServiceInterface::isAlive() {
return m_interfaceClient.isActive();
}
2016-06-23 22:08:11 +02:00
void appl::ServiceInterface::start() {
m_interfaceClient.connect(this, &appl::ServiceInterface::onServiceData);
2016-05-23 21:18:37 +02:00
m_interfaceClient.connect();
m_interfaceClient.setInterfaceName("srv-?");
}
2016-06-23 22:08:11 +02:00
void appl::ServiceInterface::stop() {
m_interfaceClient.disconnect();
}
2016-07-19 22:31:27 +02:00
void appl::ServiceInterface::SendData(uint64_t _userSessionId, ememory::SharedPtr<zeus::Buffer> _data) {
_data->setClientId(_userSessionId);
m_interfaceClient.writeBinary(_data);
2016-06-10 21:34:21 +02:00
}
2016-07-19 22:31:27 +02:00
void appl::ServiceInterface::onServiceData(ememory::SharedPtr<zeus::Buffer> _value) {
if (_value == nullptr) {
return;
}
uint32_t transactionId = _value->getTransactionId();
//data.add("from-service", ejson::String(m_name));
if (_value->getType() == zeus::Buffer::typeMessage::event) {
/*
if (data.valueExist("event") == true) {
// No need to have a user ID ...
if (data["event"].toString().get() == "IS-ALIVE") {
ZEUS_VERBOSE("Service Alive ...");
if (std::chrono::steady_clock::now() - m_interfaceClient.getLastTimeSend() >= std::chrono::seconds(20)) {
ejson::Object tmpp;
tmpp.add("event", ejson::String("IS-ALIVE"));
m_interfaceClient.writeJson(tmpp);
}
} else {
ZEUS_INFO("Unknow service event: '" << data["event"].toString().get() << "'");
2016-05-25 21:25:33 +02:00
}
return;
}
*/
return;
}
if (_value->getType() == zeus::Buffer::typeMessage::call) {
2016-07-15 21:22:11 +02:00
ememory::SharedPtr<zeus::BufferCall> callObj = ememory::staticPointerCast<zeus::BufferCall>(_value);
std::string callFunction = callObj->getCall();
if (callFunction == "connect-service") {
if (m_name != "") {
ZEUS_WARNING("Service interface ==> try change the servie name after init: '" << callObj->getParameter<std::string>(0));
m_interfaceClient.answerValue(transactionId, false);
return;
}
m_name = callObj->getParameter<std::string>(0);
m_interfaceClient.setInterfaceName("srv-" + m_name);
m_interfaceClient.answerValue(transactionId, true);
return;
}
answerProtocolError(transactionId, "unknow function");
}
if (_value->getClientId() == 0) {
ZEUS_ERROR("Service interface ==> wrong service answer ==> missing 'client-id'");
return;
}
m_gatewayInterface->answer(_value->getClientId(), _value);
}
2016-06-23 22:08:11 +02:00
void appl::ServiceInterface::answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp) {
m_interfaceClient.answerError(_transactionId, protocolError, _errorHelp);
m_interfaceClient.disconnect(true);
}