[DEV] better interface of the connection process
This commit is contained in:
parent
5028352541
commit
5efffd975f
@ -83,7 +83,10 @@ void jus::Client::connect(const std::string& _remoteUserToConnect){
|
|||||||
enet::Tcp connection = std::move(enet::connectTcpClient(*propertyIp, *propertyPort));
|
enet::Tcp connection = std::move(enet::connectTcpClient(*propertyIp, *propertyPort));
|
||||||
m_interfaceClient.setInterface(std::move(connection));
|
m_interfaceClient.setInterface(std::move(connection));
|
||||||
m_interfaceClient.connect();
|
m_interfaceClient.connect();
|
||||||
m_interfaceClient.write(std::string("{\"connect-to-user\":\"") + _remoteUserToConnect + "\", \"client-type:\":\"jus-client\"}");
|
bool ret = call_b("connectToUser", _remoteUserToConnect, "jus-client");
|
||||||
|
if (ret == false) {
|
||||||
|
JUS_ERROR("Connection error");
|
||||||
|
}
|
||||||
JUS_DEBUG("connect [STOP]");
|
JUS_DEBUG("connect [STOP]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
jus::GateWayClient::GateWayClient(enet::Tcp _connection, jus::GateWay* _gatewayInterface) :
|
jus::GateWayClient::GateWayClient(enet::Tcp _connection, jus::GateWay* _gatewayInterface) :
|
||||||
|
m_state(jus::GateWayClient::state::unconnect),
|
||||||
m_gatewayInterface(_gatewayInterface),
|
m_gatewayInterface(_gatewayInterface),
|
||||||
m_interfaceClient(std::move(_connection)),
|
m_interfaceClient(std::move(_connection)),
|
||||||
m_transactionLocalId(-1) {
|
m_transactionLocalId(-1) {
|
||||||
@ -29,6 +30,7 @@ jus::GateWayClient::~GateWayClient() {
|
|||||||
|
|
||||||
void jus::GateWayClient::start(size_t _uid) {
|
void jus::GateWayClient::start(size_t _uid) {
|
||||||
m_uid = _uid;
|
m_uid = _uid;
|
||||||
|
m_state = jus::GateWayClient::state::connect;
|
||||||
m_interfaceClient.connect(this, &jus::GateWayClient::onClientData);
|
m_interfaceClient.connect(this, &jus::GateWayClient::onClientData);
|
||||||
m_interfaceClient.connect(true);
|
m_interfaceClient.connect(true);
|
||||||
m_interfaceClient.setInterfaceName("cli-" + etk::to_string(m_uid));
|
m_interfaceClient.setInterfaceName("cli-" + etk::to_string(m_uid));
|
||||||
@ -57,28 +59,119 @@ bool jus::GateWayClient::isAlive() {
|
|||||||
return m_interfaceClient.isActive();
|
return m_interfaceClient.isActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void jus::GateWayClient::protocolError(const std::string& _errorHelp) {
|
||||||
|
ejson::Object answer;
|
||||||
|
answer.add("error", ejson::String("PROTOCOL-ERROR"));
|
||||||
|
answer.add("error-help", ejson::String(_errorHelp));
|
||||||
|
JUS_DEBUG("answer: " << answer.generateHumanString());
|
||||||
|
m_interfaceClient.write(answer.generateMachineString());
|
||||||
|
m_state = jus::GateWayClient::state::disconnect;
|
||||||
|
m_interfaceClient.disconnect(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void jus::GateWayClient::returnBool(int32_t _transactionId, bool _value) {
|
||||||
|
ejson::Object answer;
|
||||||
|
answer.add("id", ejson::Number(_transactionId));
|
||||||
|
answer.add("return", ejson::Boolean(_value));
|
||||||
|
JUS_DEBUG("answer: " << answer.generateHumanString());
|
||||||
|
m_interfaceClient.write(answer.generateMachineString());
|
||||||
|
}
|
||||||
|
|
||||||
void jus::GateWayClient::onClientData(std::string _value) {
|
void jus::GateWayClient::onClientData(std::string _value) {
|
||||||
JUS_DEBUG("On data: " << _value);
|
JUS_DEBUG("On data: " << _value);
|
||||||
ejson::Object data(_value);
|
ejson::Object data(_value);
|
||||||
int32_t transactionId = data["id"].toNumber().get();
|
int32_t transactionId = data["id"].toNumber().get();
|
||||||
if (m_userConnectionName == "") {
|
if (transactionId == 0) {
|
||||||
if (data.valueExist("connect-to-user") == true) {
|
JUS_ERROR("Protocol error ==>missing id");
|
||||||
|
protocolError("missing parameter: 'id'");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
switch (m_state) {
|
||||||
|
case jus::GateWayClient::state::unconnect:
|
||||||
|
{
|
||||||
|
JUS_ERROR("Must never appear");
|
||||||
|
protocolError("Gateway internal error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
case jus::GateWayClient::state::connect:
|
||||||
|
{
|
||||||
|
if (m_userConnectionName != "") {
|
||||||
|
protocolError("Gateway internal error 2");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string call = data["call"].toString().get();
|
||||||
|
if (call == "connectToUser") {
|
||||||
m_userConnectionName = data["connect-to-user"].toString().get();
|
m_userConnectionName = data["connect-to-user"].toString().get();
|
||||||
JUS_WARNING("[" << m_uid << "] Set client connect to user : '" << m_userConnectionName << "'");
|
JUS_WARNING("[" << m_uid << "] Set client connect to user : '" << m_userConnectionName << "'");
|
||||||
|
|
||||||
m_userService = m_gatewayInterface->get("system-user");
|
m_userService = m_gatewayInterface->get("system-user");
|
||||||
|
if (m_userService == nullptr) {
|
||||||
|
protocolError("Gateway internal error 'No user interface'");
|
||||||
|
} else {
|
||||||
ejson::Object linkService;
|
ejson::Object linkService;
|
||||||
linkService.add("event", ejson::String("new"));
|
linkService.add("event", ejson::String("new"));
|
||||||
linkService.add("user", ejson::String(m_userConnectionName));
|
linkService.add("user", ejson::String(m_userConnectionName));
|
||||||
m_userService->SendData(-m_uid, linkService);
|
m_userService->SendData(-m_uid, linkService);
|
||||||
|
m_state = jus::GateWayClient::state::userIdentify;
|
||||||
// TODO : Return something ...
|
returnBool(transactionId, true);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
JUS_WARNING("[" << m_uid << "] Client must send conection to user name ...");
|
JUS_WARNING("[" << m_uid << "] Client must send conection to user name ...");
|
||||||
// TODO : Return something ...
|
protocolError("Missing call of connectToUser");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
case jus::GateWayClient::state::userIdentify:
|
||||||
|
{
|
||||||
|
std::string call = data["call"].toString().get();
|
||||||
|
if (call == "identify") {
|
||||||
|
std::string clientName = data["param"].toArray()[0].toString().get();
|
||||||
|
std::string clientTocken = data["param"].toArray()[1].toString().get();
|
||||||
|
ejson::Object gwCall;
|
||||||
|
int32_t tmpID = m_transactionLocalId--;
|
||||||
|
gwCall.add("id", ejson::Number(tmpID));
|
||||||
|
gwCall.add("call", ejson::String("checkTocken"));
|
||||||
|
ejson::Array gwParam;
|
||||||
|
gwParam.add(ejson::String(clientName));
|
||||||
|
gwParam.add(ejson::String(clientTocken));
|
||||||
|
gwCall.add("param", gwParam);
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
m_actions.push_back(std::make_pair(tmpID,
|
||||||
|
[=](ejson::Object& _data) {
|
||||||
|
JUS_ERROR(" ==> Tocken ckeck return ...");
|
||||||
|
if (_data["return"].toBoolean().get() == true) {
|
||||||
|
m_clientName = clientName;
|
||||||
|
m_clientgroups.clear();
|
||||||
|
// TODO : Update all service name and group ...
|
||||||
|
returnBool(transactionId, true);
|
||||||
|
} else {
|
||||||
|
returnBool(transactionId, false);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
if (m_userService != nullptr) {
|
||||||
|
m_userService->SendData(-m_uid, gwCall);
|
||||||
|
} else {
|
||||||
|
protocolError("gateWay internal error 3");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (call == "auth") {
|
||||||
|
std::string password = data["param"].toArray()[0].toString().get();
|
||||||
|
protocolError("Not implemented");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (call == "anonymous") {
|
||||||
|
protocolError("Not implemented");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
protocolError("Client must call: identify/auth/anonymous");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case jus::GateWayClient::state::clientIdentify:
|
||||||
|
{
|
||||||
std::string service = data["service"].toString().get();
|
std::string service = data["service"].toString().get();
|
||||||
// Thsi is 2 default service for the cient interface that manage the authorisation of view:
|
// Thsi is 2 default service for the cient interface that manage the authorisation of view:
|
||||||
if (service == "") {
|
if (service == "") {
|
||||||
@ -102,50 +195,6 @@ void jus::GateWayClient::onClientData(std::string _value) {
|
|||||||
m_interfaceClient.write(answer.generateMachineString());
|
m_interfaceClient.write(answer.generateMachineString());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (call == "identify") {
|
|
||||||
// Identify Client has an extern user ...
|
|
||||||
std::string clientName = data["param"].toArray()[0].toString().get();
|
|
||||||
std::string clientTocken = data["param"].toArray()[1].toString().get();
|
|
||||||
ejson::Object gwCall;
|
|
||||||
int32_t tmpID = m_transactionLocalId--;
|
|
||||||
gwCall.add("id", ejson::Number(tmpID));
|
|
||||||
gwCall.add("call", ejson::String("checkTocken"));
|
|
||||||
ejson::Array gwParam;
|
|
||||||
gwParam.add(ejson::String(clientName));
|
|
||||||
gwParam.add(ejson::String(clientTocken));
|
|
||||||
gwCall.add("param", gwParam);
|
|
||||||
{
|
|
||||||
std::unique_lock<std::mutex> lock(m_mutex);
|
|
||||||
m_actions.push_back(std::make_pair(tmpID,
|
|
||||||
[=](ejson::Object& _data) {
|
|
||||||
ejson::Object tmpAnswer;
|
|
||||||
tmpAnswer.add("id", ejson::Number(transactionId));
|
|
||||||
JUS_ERROR(" ==> Tocken ckeck return ...");
|
|
||||||
if (_data["return"].toBoolean().get() == true) {
|
|
||||||
m_clientName = clientName;
|
|
||||||
m_clientgroups.clear();
|
|
||||||
// TODO : Update all service name and group ...
|
|
||||||
tmpAnswer.add("return", ejson::Boolean(true));
|
|
||||||
} else {
|
|
||||||
tmpAnswer.add("return", ejson::Boolean(false));
|
|
||||||
}
|
|
||||||
JUS_DEBUG("answer: " << tmpAnswer.generateHumanString());
|
|
||||||
m_interfaceClient.write(tmpAnswer.generateMachineString());
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
if (m_userService != nullptr) {
|
|
||||||
m_userService->SendData(-m_uid, gwCall);
|
|
||||||
} else {
|
|
||||||
// TODO ...
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (call == "authentify") {
|
|
||||||
// Identify Client has an local user ... (connection to is the same ...)
|
|
||||||
std::string password = data["param"].toArray()[0].toString().get();
|
|
||||||
// TODO: ...
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (call == "link") {
|
if (call == "link") {
|
||||||
// first param:
|
// first param:
|
||||||
std::string serviceName = data["param"].toArray()[0].toString().get();
|
std::string serviceName = data["param"].toArray()[0].toString().get();
|
||||||
@ -253,6 +302,8 @@ void jus::GateWayClient::onClientData(std::string _value) {
|
|||||||
(*it)->SendData(m_uid, data);
|
(*it)->SendData(m_uid, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void jus::GateWayClient::returnMessage(ejson::Object _data) {
|
void jus::GateWayClient::returnMessage(ejson::Object _data) {
|
||||||
JUS_DEBUG("answer: " << _data.generateHumanString());
|
JUS_DEBUG("answer: " << _data.generateHumanString());
|
||||||
|
@ -15,10 +15,21 @@
|
|||||||
namespace jus {
|
namespace jus {
|
||||||
class GateWay;
|
class GateWay;
|
||||||
class GateWayClient {
|
class GateWayClient {
|
||||||
|
private:
|
||||||
using Observer = std::function<void(ejson::Object& _data)>;
|
using Observer = std::function<void(ejson::Object& _data)>;
|
||||||
|
enum class state {
|
||||||
|
unconnect, // starting sate
|
||||||
|
connect, // just get a TCP connection
|
||||||
|
userIdentify, // client set the user it want to access
|
||||||
|
clientIdentify, // client defien the mode of the acces (anonymous,client/user)
|
||||||
|
disconnect // client is dead or loal disconnection
|
||||||
|
};
|
||||||
|
enum state m_state; // state machine ...
|
||||||
private:
|
private:
|
||||||
jus::GateWay* m_gatewayInterface;
|
jus::GateWay* m_gatewayInterface;
|
||||||
jus::TcpString m_interfaceClient;
|
jus::TcpString m_interfaceClient;
|
||||||
|
void protocolError(const std::string& _errorHelp);
|
||||||
|
void returnBool(int32_t _transactionId, bool _value);
|
||||||
public:
|
public:
|
||||||
esignal::Signal<bool> signalIsConnected;
|
esignal::Signal<bool> signalIsConnected;
|
||||||
ememory::SharedPtr<jus::GateWayService> m_userService;
|
ememory::SharedPtr<jus::GateWayService> m_userService;
|
||||||
|
@ -79,7 +79,7 @@ void jus::TcpString::connect(bool _async){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void jus::TcpString::disconnect(){
|
void jus::TcpString::disconnect(bool _inThreadStop){
|
||||||
JUS_DEBUG("disconnect [START]");
|
JUS_DEBUG("disconnect [START]");
|
||||||
if (m_thread != nullptr) {
|
if (m_thread != nullptr) {
|
||||||
m_threadRunning = false;
|
m_threadRunning = false;
|
||||||
@ -91,11 +91,13 @@ void jus::TcpString::disconnect(){
|
|||||||
if (m_connection.getConnectionStatus() != enet::Tcp::status::unlink) {
|
if (m_connection.getConnectionStatus() != enet::Tcp::status::unlink) {
|
||||||
m_connection.unlink();
|
m_connection.unlink();
|
||||||
}
|
}
|
||||||
|
if (_inThreadStop == false) {
|
||||||
if (m_thread != nullptr) {
|
if (m_thread != nullptr) {
|
||||||
m_thread->join();
|
m_thread->join();
|
||||||
delete m_thread;
|
delete m_thread;
|
||||||
m_thread = nullptr;
|
m_thread = nullptr;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
JUS_DEBUG("disconnect [STOP]");
|
JUS_DEBUG("disconnect [STOP]");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ namespace jus {
|
|||||||
virtual ~TcpString();
|
virtual ~TcpString();
|
||||||
void setInterface(enet::Tcp _connection);
|
void setInterface(enet::Tcp _connection);
|
||||||
void connect(bool _async = false);
|
void connect(bool _async = false);
|
||||||
void disconnect();
|
void disconnect(bool _inThreadStop = false);
|
||||||
bool isActive() const;
|
bool isActive() const;
|
||||||
void setInterfaceName(const std::string& _name);
|
void setInterfaceName(const std::string& _name);
|
||||||
int32_t write(const std::string& _data);
|
int32_t write(const std::string& _data);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user