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-10-02 17:01:22 +02:00
|
|
|
#include <appl/GateWay.hpp>
|
|
|
|
#include <appl/debug.hpp>
|
|
|
|
#include <enet/TcpServer.hpp>
|
2016-05-23 21:18:37 +02:00
|
|
|
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
namespace appl {
|
2016-05-23 21:18:37 +02:00
|
|
|
class TcpServerInput {
|
|
|
|
private:
|
|
|
|
enet::TcpServer m_interface;
|
|
|
|
std::thread* m_thread;
|
|
|
|
bool m_threadRunning;
|
2016-06-23 22:08:11 +02:00
|
|
|
appl::GateWay* m_gateway;
|
2016-05-23 21:18:37 +02:00
|
|
|
bool m_service;
|
|
|
|
public:
|
2016-06-23 22:08:11 +02:00
|
|
|
TcpServerInput(appl::GateWay* _gateway, bool _service) :
|
2016-05-23 21:18:37 +02:00
|
|
|
m_thread(nullptr),
|
|
|
|
m_threadRunning(false),
|
|
|
|
m_gateway(_gateway),
|
|
|
|
m_service(_service) {
|
|
|
|
|
|
|
|
}
|
|
|
|
virtual ~TcpServerInput() {}
|
|
|
|
void start(const std::string& _host, uint16_t _port) {
|
|
|
|
m_interface.setHostNane(_host);
|
|
|
|
m_interface.setPort(_port);
|
|
|
|
m_interface.link();
|
|
|
|
m_threadRunning = true;
|
|
|
|
m_thread = new std::thread([&](void *){ this->threadCallback();}, nullptr);
|
|
|
|
if (m_thread == nullptr) {
|
|
|
|
m_threadRunning = false;
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_ERROR("creating callback thread!");
|
2016-05-23 21:18:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void stop() {
|
|
|
|
if (m_thread != nullptr) {
|
|
|
|
m_threadRunning = false;
|
|
|
|
}
|
|
|
|
m_interface.unlink();
|
|
|
|
if (m_thread != nullptr) {
|
|
|
|
m_thread->join();
|
|
|
|
delete m_thread;
|
|
|
|
m_thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void threadCallback() {
|
|
|
|
// get datas:
|
|
|
|
while (m_threadRunning == true) {
|
|
|
|
// READ section data:
|
|
|
|
enet::Tcp data = std::move(m_interface.waitNext());
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_VERBOSE("New connection");
|
2016-05-23 21:18:37 +02:00
|
|
|
if (m_service == true) {
|
2016-11-21 22:15:46 +01:00
|
|
|
m_gateway->newClientGateWayBackEnd(std::move(data));
|
2016-05-23 21:18:37 +02:00
|
|
|
} else {
|
|
|
|
m_gateway->newClient(std::move(data));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2016-05-12 22:45:40 +02:00
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
void appl::GateWay::newClientGateWayBackEnd(enet::Tcp _connection) {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_WARNING("New TCP connection (service)");
|
2016-11-21 22:15:46 +01:00
|
|
|
ememory::SharedPtr<appl::GateWayInterface> tmp = ememory::makeShared<appl::GateWayInterface>(std::move(_connection), this);
|
2016-05-23 21:18:37 +02:00
|
|
|
tmp->start();
|
2016-11-21 22:15:46 +01:00
|
|
|
m_gatewayBackEndList.push_back(tmp);
|
2016-05-23 21:18:37 +02:00
|
|
|
}
|
2016-05-12 22:45:40 +02:00
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::newClient(enet::Tcp _connection) {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_WARNING("New TCP connection (client)");
|
2016-07-15 21:22:11 +02:00
|
|
|
ememory::SharedPtr<appl::ClientInterface> tmp = ememory::makeShared<appl::ClientInterface>(std::move(_connection), this);
|
2016-05-26 22:12:37 +02:00
|
|
|
tmp->start(m_clientUID++, m_clientUID++);
|
2016-05-23 21:18:37 +02:00
|
|
|
m_clientList.push_back(tmp);
|
|
|
|
}
|
2016-05-12 22:45:40 +02:00
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
appl::GateWay::GateWay() :
|
2016-05-22 22:40:42 +02:00
|
|
|
m_clientUID(1),
|
2016-06-23 22:08:11 +02:00
|
|
|
propertyClientIp(this, "client-ip", "127.0.0.1", "Ip to listen client", &appl::GateWay::onPropertyChangeClientIp),
|
|
|
|
propertyClientPort(this, "client-port", 1983, "Port to listen client", &appl::GateWay::onPropertyChangeClientPort),
|
|
|
|
propertyClientMax(this, "client-max", 80, "Maximum of client at the same time", &appl::GateWay::onPropertyChangeClientMax),
|
2016-11-21 22:15:46 +01:00
|
|
|
propertyGatewayBackEndIp(this, "gw-ip", "127.0.0.1", "Ip to listen client", &appl::GateWay::onPropertyChangeGateWayIp),
|
|
|
|
propertyGatewayBackEndPort(this, "gw-port", 1984, "Port to listen client", &appl::GateWay::onPropertyChangeGateWayPort),
|
|
|
|
propertyGatewayBackEndMax(this, "gw-max", 80, "Maximum of client at the same time", &appl::GateWay::onPropertyChangeGateWayMax) {
|
2016-07-15 21:22:11 +02:00
|
|
|
m_interfaceClientServer = ememory::makeShared<appl::TcpServerInput>(this, false);
|
2016-11-21 22:15:46 +01:00
|
|
|
m_interfaceGatewayBackEndServer = ememory::makeShared<appl::TcpServerInput>(this, true);
|
2016-05-12 22:45:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
appl::GateWay::~GateWay() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::start() {
|
2016-05-23 21:18:37 +02:00
|
|
|
m_interfaceClientServer->start(*propertyClientIp, *propertyClientPort);
|
2016-11-21 22:15:46 +01:00
|
|
|
m_interfaceGatewayBackEndServer->start(*propertyGatewayBackEndIp, *propertyGatewayBackEndPort);
|
2016-05-12 22:45:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::stop() {
|
2016-11-21 22:15:46 +01:00
|
|
|
// TODO : Stop all server ...
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
ememory::SharedPtr<appl::GateWayInterface> appl::GateWay::get(const std::string& _userName) {
|
|
|
|
// TODO : Start USer only when needed, not get it all time started...
|
|
|
|
for (auto &it : m_gatewayBackEndList) {
|
2016-05-22 22:40:42 +02:00
|
|
|
if (it == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-11-21 22:15:46 +01:00
|
|
|
if (it->getName() != _userName) {
|
2016-05-22 22:40:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
|
|
|
|
std::vector<std::string> appl::GateWay::getAllUserName() {
|
2016-05-27 22:21:32 +02:00
|
|
|
std::vector<std::string> out;
|
2016-11-21 22:15:46 +01:00
|
|
|
/*
|
|
|
|
for (auto &it : m_gatewayBackEndList) {
|
2016-05-27 22:21:32 +02:00
|
|
|
if (it == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out.push_back(it->getName());
|
|
|
|
}
|
2016-11-21 22:15:46 +01:00
|
|
|
*/
|
2016-05-27 22:21:32 +02:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::answer(uint64_t _userSessionId, const ememory::SharedPtr<zeus::Buffer>& _data) {
|
2016-05-23 21:18:37 +02:00
|
|
|
for (auto &it : m_clientList) {
|
|
|
|
if (it == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-05-26 22:12:37 +02:00
|
|
|
if (it->checkId(_userSessionId) == false) {
|
2016-05-23 21:18:37 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
it->returnMessage(_data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::cleanIO() {
|
2016-05-24 22:29:03 +02:00
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
auto it = m_gatewayBackEndList.begin();
|
|
|
|
while (it != m_gatewayBackEndList.end()) {
|
2016-05-24 22:29:03 +02:00
|
|
|
if (*it != nullptr) {
|
|
|
|
if ((*it)->isAlive() == false) {
|
2016-11-21 22:15:46 +01:00
|
|
|
it = m_gatewayBackEndList.erase(it);
|
2016-05-24 22:29:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-21 22:15:46 +01:00
|
|
|
it = m_gatewayBackEndList.erase(it);
|
2016-05-24 22:29:03 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it2 = m_clientList.begin();
|
|
|
|
while (it2 != m_clientList.end()) {
|
|
|
|
if (*it2 != nullptr) {
|
|
|
|
if ((*it2)->isAlive() == false) {
|
|
|
|
it2 = m_clientList.erase(it2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
it2 = m_clientList.erase(it2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::onClientConnect(const bool& _value) {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_TODO("Client connection: " << _value);
|
2016-05-22 22:40:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::onServiceConnect(const bool& _value) {
|
2016-06-20 23:07:25 +02:00
|
|
|
ZEUS_TODO("Service connection: " << _value);
|
2016-05-12 22:45:40 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::onPropertyChangeClientIp() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::onPropertyChangeClientPort() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-06-23 22:08:11 +02:00
|
|
|
void appl::GateWay::onPropertyChangeClientMax() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
void appl::GateWay::onPropertyChangeGateWayIp() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
void appl::GateWay::onPropertyChangeGateWayPort() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-21 22:15:46 +01:00
|
|
|
void appl::GateWay::onPropertyChangeGateWayMax() {
|
2016-05-12 22:45:40 +02:00
|
|
|
|
|
|
|
}
|