2016-05-22 22:40:42 +02:00
/** @file
* @ author Edouard DUPIN
* @ copyright 2016 , Edouard DUPIN , all right reserved
2017-01-05 21:28:23 +01:00
* @ license MPL v2 .0 ( see license file )
2016-05-22 22:40:42 +02:00
*/
2016-10-02 17:01:22 +02:00
# include <appl/debug.hpp>
2016-11-21 22:15:46 +01:00
# include <appl/GateWayInterface.hpp>
2016-10-02 17:01:22 +02:00
# include <appl/ClientInterface.hpp>
2016-11-23 22:03:04 +01:00
# include <appl/Router.hpp>
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-11-23 22:03:04 +01:00
appl : : GateWayInterface : : GateWayInterface ( enet : : Tcp _connection , appl : : Router * _routerInterface ) :
m_routerInterface ( _routerInterface ) ,
2016-11-29 21:39:39 +01:00
m_interfaceClient ( std : : move ( _connection ) , true ) ,
m_lastSourceID ( 0x8000 ) {
2016-11-30 22:09:16 +01:00
ZEUS_INFO ( " ----------------- " ) ;
ZEUS_INFO ( " -- NEW GateWay -- " ) ;
ZEUS_INFO ( " ----------------- " ) ;
2016-11-21 22:15:46 +01:00
2016-05-22 22:40:42 +02:00
}
2016-11-21 22:15:46 +01:00
appl : : GateWayInterface : : ~ GateWayInterface ( ) {
2016-05-22 22:40:42 +02:00
2016-11-30 22:09:16 +01:00
ZEUS_INFO ( " --------------------- " ) ;
ZEUS_INFO ( " -- DELETE GateWay -- " ) ;
ZEUS_INFO ( " --------------------- " ) ;
2016-05-24 22:29:03 +02:00
}
2016-11-21 22:15:46 +01:00
bool appl : : GateWayInterface : : isAlive ( ) {
2016-05-24 22:29:03 +02:00
return m_interfaceClient . isActive ( ) ;
2016-05-22 22:40:42 +02:00
}
2016-11-21 22:15:46 +01:00
bool appl : : GateWayInterface : : requestURI ( const std : : string & _uri ) {
ZEUS_INFO ( " request connect on User - GateWay: ' " < < _uri < < " ' " ) ;
2016-11-23 22:03:04 +01:00
if ( m_routerInterface = = nullptr ) {
2016-11-21 22:15:46 +01:00
ZEUS_ERROR ( " Can not access to the main GateWay interface (nullptr) " ) ;
return false ;
}
std : : string tmpURI = _uri ;
if ( tmpURI . size ( ) = = 0 ) {
ZEUS_ERROR ( " Empty URI ... not supported ... " ) ;
return false ;
}
if ( tmpURI [ 0 ] = = ' / ' ) {
tmpURI = std : : string ( tmpURI . begin ( ) + 1 , tmpURI . end ( ) ) ;
}
// TODO : Remove subParameters xxx?YYY
// check if the USER is already connected:
2017-01-04 00:02:22 +01:00
bool tmp = m_routerInterface - > userIsConnected ( tmpURI ) ;
if ( tmp = = true ) {
2016-11-21 22:15:46 +01:00
ZEUS_ERROR ( " User is already connected ==> this is a big error ... " ) ;
return false ;
}
m_name = tmpURI ;
ZEUS_WARNING ( " Connection of user : ' " < < tmpURI < < " ' " ) ;
return true ;
}
void appl : : GateWayInterface : : start ( ) {
m_interfaceClient . connect ( this , & appl : : GateWayInterface : : onServiceData ) ;
m_interfaceClient . connectUri ( this , & appl : : GateWayInterface : : requestURI ) ;
2016-05-23 21:18:37 +02:00
m_interfaceClient . connect ( ) ;
2016-11-29 21:39:39 +01:00
m_interfaceClient . setInterfaceName ( " GW-? " ) ;
2016-05-22 22:40:42 +02:00
}
2016-11-21 22:15:46 +01:00
void appl : : GateWayInterface : : stop ( ) {
2016-05-22 22:40:42 +02:00
m_interfaceClient . disconnect ( ) ;
}
2016-06-14 21:28:54 +02:00
2016-12-08 00:16:40 +01:00
void appl : : GateWayInterface : : send ( ememory : : SharedPtr < zeus : : Message > _data ) {
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-11-29 21:39:39 +01:00
uint16_t appl : : GateWayInterface : : addClient ( ememory : : SharedPtr < appl : : ClientInterface > _value ) {
m_clientConnected . push_back ( _value ) ;
return m_lastSourceID + + ;
}
void appl : : GateWayInterface : : rmClient ( ememory : : SharedPtr < appl : : ClientInterface > _value ) {
2016-12-08 22:13:16 +01:00
if ( _value = = nullptr ) {
return ;
}
uint16_t id = _value - > getId ( ) ;
2016-11-29 21:39:39 +01:00
auto it = m_clientConnected . begin ( ) ;
while ( it ! = m_clientConnected . end ( ) ) {
if ( * it = = _value ) {
it = m_clientConnected . erase ( it ) ;
} else {
+ + it ;
}
}
2016-12-08 22:13:16 +01:00
m_interfaceClient . event ( uint32_t ( id ) < < 16 , ZEUS_ID_GATEWAY , " removeRouterClient " , id ) ;
2016-11-29 21:39:39 +01:00
}
2016-12-08 00:16:40 +01:00
void appl : : GateWayInterface : : onServiceData ( ememory : : SharedPtr < zeus : : Message > _value ) {
2016-06-22 22:52:18 +02:00
if ( _value = = nullptr ) {
return ;
}
2016-11-29 21:39:39 +01:00
if ( m_name = = " " ) {
uint32_t transactionId = _value - > getTransactionId ( ) ;
2016-12-08 00:16:40 +01:00
if ( _value - > getType ( ) = = zeus : : message : : type : : call ) {
ememory : : SharedPtr < zeus : : message : : Call > callObj = ememory : : staticPointerCast < zeus : : message : : Call > ( _value ) ;
2016-11-29 21:39:39 +01:00
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 , _value - > getDestination ( ) , _value - > getSource ( ) , false ) ;
return ;
2016-06-14 21:28:54 +02:00
}
2016-11-29 21:39:39 +01:00
m_name = callObj - > getParameter < std : : string > ( 0 ) ;
m_interfaceClient . setInterfaceName ( " srv- " + m_name ) ;
m_interfaceClient . answerValue ( transactionId , _value - > getDestination ( ) , _value - > getSource ( ) , true ) ;
return ;
2016-05-25 21:25:33 +02:00
}
2016-11-29 21:39:39 +01:00
answerProtocolError ( transactionId , " unknow function " ) ;
2016-05-24 22:29:03 +02:00
}
return ;
}
2016-11-29 21:39:39 +01:00
uint16_t destinationId = _value - > getDestinationId ( ) ;
for ( auto & it : m_clientConnected ) {
if ( it - > checkId ( destinationId ) = = true ) {
it - > send ( _value ) ;
2016-05-22 22:40:42 +02:00
return ;
}
}
2016-11-29 21:39:39 +01:00
m_interfaceClient . answerError ( _value - > getTransactionId ( ) , _value - > getDestination ( ) , _value - > getSource ( ) , " UNKNOW-DESTINATION " , " the Id= " + etk : : to_string ( destinationId ) + " is unknow " ) ;
2016-05-22 22:40:42 +02:00
}
2016-06-14 21:28:54 +02:00
2016-11-21 22:15:46 +01:00
void appl : : GateWayInterface : : answerProtocolError ( uint32_t _transactionId , const std : : string & _errorHelp ) {
2016-11-28 21:28:26 +01:00
m_interfaceClient . answerError ( _transactionId , 0 , 0 , protocolError , _errorHelp ) ;
2016-06-14 21:28:54 +02:00
m_interfaceClient . disconnect ( true ) ;
}