zeus/tools/gateway-front-end/appl/GateWay.cpp

212 lines
5.3 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-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;
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());
ZEUS_VERBOSE("New connection");
2016-05-23 21:18:37 +02:00
if (m_service == true) {
m_gateway->newClientGateWayBackEnd(std::move(data));
2016-05-23 21:18:37 +02:00
} else {
m_gateway->newClient(std::move(data));
}
}
}
};
}
void appl::GateWay::newClientGateWayBackEnd(enet::Tcp _connection) {
ZEUS_WARNING("New TCP connection (service)");
ememory::SharedPtr<appl::GateWayInterface> tmp = ememory::makeShared<appl::GateWayInterface>(std::move(_connection), this);
2016-05-23 21:18:37 +02:00
tmp->start();
m_gatewayBackEndList.push_back(tmp);
2016-05-23 21:18:37 +02:00
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::newClient(enet::Tcp _connection) {
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);
tmp->start(m_clientUID++, m_clientUID++);
2016-05-23 21:18:37 +02:00
m_clientList.push_back(tmp);
}
2016-06-23 22:08:11 +02:00
appl::GateWay::GateWay() :
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),
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);
m_interfaceGatewayBackEndServer = ememory::makeShared<appl::TcpServerInput>(this, true);
}
2016-06-23 22:08:11 +02:00
appl::GateWay::~GateWay() {
}
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);
m_interfaceGatewayBackEndServer->start(*propertyGatewayBackEndIp, *propertyGatewayBackEndPort);
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::stop() {
// TODO : Stop all server ...
}
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) {
if (it == nullptr) {
continue;
}
if (it->getName() != _userName) {
continue;
}
return it;
}
return nullptr;
}
std::vector<std::string> appl::GateWay::getAllUserName() {
std::vector<std::string> out;
/*
for (auto &it : m_gatewayBackEndList) {
if (it == nullptr) {
continue;
}
out.push_back(it->getName());
}
*/
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;
}
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() {
auto it = m_gatewayBackEndList.begin();
while (it != m_gatewayBackEndList.end()) {
if (*it != nullptr) {
if ((*it)->isAlive() == false) {
it = m_gatewayBackEndList.erase(it);
continue;
}
} else {
it = m_gatewayBackEndList.erase(it);
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) {
ZEUS_TODO("Client connection: " << _value);
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::onServiceConnect(const bool& _value) {
ZEUS_TODO("Service connection: " << _value);
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::onPropertyChangeClientIp() {
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::onPropertyChangeClientPort() {
}
2016-06-23 22:08:11 +02:00
void appl::GateWay::onPropertyChangeClientMax() {
}
void appl::GateWay::onPropertyChangeGateWayIp() {
}
void appl::GateWay::onPropertyChangeGateWayPort() {
}
void appl::GateWay::onPropertyChangeGateWayMax() {
}