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)
|
|
|
|
*/
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
#include <appl/debug.h>
|
|
|
|
#include <appl/ServiceInterface.h>
|
|
|
|
#include <appl/ClientInterface.h>
|
|
|
|
#include <appl/GateWay.h>
|
2016-05-22 22:40:42 +02:00
|
|
|
|
2016-06-14 21:28:54 +02:00
|
|
|
// 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),
|
2016-06-20 23:07:25 +02:00
|
|
|
m_interfaceClient(std::move(_connection), true) {
|
|
|
|
ZEUS_INFO("-----------------");
|
|
|
|
ZEUS_INFO("-- NEW Service --");
|
|
|
|
ZEUS_INFO("-----------------");
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
appl::ServiceInterface::~ServiceInterface() {
|
2016-05-22 22:40:42 +02:00
|
|
|
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_INFO("--------------------");
|
|
|
|
ZEUS_INFO("-- DELETE Service --");
|
|
|
|
ZEUS_INFO("--------------------");
|
2016-05-24 22:29:03 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
bool appl::ServiceInterface::isAlive() {
|
2016-05-24 22:29:03 +02:00
|
|
|
return m_interfaceClient.isActive();
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
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();
|
2016-05-22 22:40:42 +02:00
|
|
|
m_interfaceClient.setInterfaceName("srv-?");
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::ServiceInterface::stop() {
|
2016-05-22 22:40:42 +02:00
|
|
|
m_interfaceClient.disconnect();
|
|
|
|
}
|
|
|
|
|
2016-06-14 21:28:54 +02:00
|
|
|
|
2016-07-19 22:31:27 +02:00
|
|
|
void appl::ServiceInterface::SendData(uint64_t _userSessionId, ememory::SharedPtr<zeus::Buffer> _data) {
|
2016-06-22 22:52:18 +02:00
|
|
|
_data->setClientId(_userSessionId);
|
2016-06-16 21:15:53 +02:00
|
|
|
m_interfaceClient.writeBinary(_data);
|
2016-06-10 21:34:21 +02:00
|
|
|
}
|
2016-05-22 22:40:42 +02:00
|
|
|
|
2016-07-19 22:31:27 +02:00
|
|
|
void appl::ServiceInterface::onServiceData(ememory::SharedPtr<zeus::Buffer> _value) {
|
2016-06-22 22:52:18 +02:00
|
|
|
if (_value == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t transactionId = _value->getTransactionId();
|
2016-06-14 21:28:54 +02:00
|
|
|
//data.add("from-service", ejson::String(m_name));
|
2016-06-22 22:52:18 +02:00
|
|
|
if (_value->getType() == zeus::Buffer::typeMessage::event) {
|
2016-06-14 21:28:54 +02:00
|
|
|
/*
|
|
|
|
if (data.valueExist("event") == true) {
|
|
|
|
// No need to have a user ID ...
|
|
|
|
if (data["event"].toString().get() == "IS-ALIVE") {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_VERBOSE("Service Alive ...");
|
2016-06-14 21:28:54 +02:00
|
|
|
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 {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_INFO("Unknow service event: '" << data["event"].toString().get() << "'");
|
2016-05-25 21:25:33 +02:00
|
|
|
}
|
2016-06-14 21:28:54 +02:00
|
|
|
return;
|
2016-05-24 22:29:03 +02:00
|
|
|
}
|
2016-06-14 21:28:54 +02:00
|
|
|
*/
|
2016-05-24 22:29:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-22 22:52:18 +02:00
|
|
|
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);
|
2016-07-04 21:13:20 +02:00
|
|
|
std::string callFunction = callObj->getCall();
|
2016-06-16 21:15:53 +02:00
|
|
|
if (callFunction == "connect-service") {
|
2016-06-14 21:28:54 +02:00
|
|
|
if (m_name != "") {
|
2016-07-04 21:13:20 +02:00
|
|
|
ZEUS_WARNING("Service interface ==> try change the servie name after init: '" << callObj->getParameter<std::string>(0));
|
2016-06-14 21:28:54 +02:00
|
|
|
m_interfaceClient.answerValue(transactionId, false);
|
|
|
|
return;
|
|
|
|
}
|
2016-07-04 21:13:20 +02:00
|
|
|
m_name = callObj->getParameter<std::string>(0);
|
2016-06-14 21:28:54 +02:00
|
|
|
m_interfaceClient.setInterfaceName("srv-" + m_name);
|
2016-06-15 21:31:30 +02:00
|
|
|
m_interfaceClient.answerValue(transactionId, true);
|
2016-05-22 22:40:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-14 21:28:54 +02:00
|
|
|
answerProtocolError(transactionId, "unknow function");
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
2016-06-22 22:52:18 +02:00
|
|
|
if (_value->getClientId() == 0) {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_ERROR("Service interface ==> wrong service answer ==> missing 'client-id'");
|
2016-05-22 22:40:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-06-22 22:52:18 +02:00
|
|
|
m_gatewayInterface->answer(_value->getClientId(), _value);
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-14 21:28:54 +02:00
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::ServiceInterface::answerProtocolError(uint32_t _transactionId, const std::string& _errorHelp) {
|
2016-06-14 21:28:54 +02:00
|
|
|
m_interfaceClient.answerError(_transactionId, protocolError, _errorHelp);
|
|
|
|
m_interfaceClient.disconnect(true);
|
|
|
|
}
|