|
|
#include "mojoRemoteClient.h"
#include "mojoKernel.h"
#define LOG( X ) m_pLog->Log( "RPC", this, X )
#ifdef DEBUG
#define LOG_ERROR( X ) {\
char _tBfrErrMSG234[256];\ sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\ m_pLog->Log( "RPC", this, _LOG_ERROR, _tBfrErrMSG234 );\ }#else
#define LOG_ERROR( X ) m_pLog->Log( "RPC", this, _LOG_ERROR, X );
#endif
mojoRemoteClient::mojoRemoteClient( mojoKernel * ptr ):protocolParser(){ m_pLog = basicLogCollection::getInstance(); m_pPort = NULL; m_pMaster = ptr; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false;};
mojoRemoteClient::~mojoRemoteClient(){ waitThreadEnd( true ); if( m_bRegisteredWithRemotePeer ) { //Unregister Mojo
}; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; if ( m_pPort ) delete( m_pPort );};
bool mojoRemoteClient::create(){ if( m_pMaster == NULL ) return false; if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true; m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT; return init();};
int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){ if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) { ARGUS_DATA_HEADER *ptr = (ARGUS_DATA_HEADER*) Buffer; if ( ( ptr->SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) { uint16_t tCRC = checksum16( Buffer, ptr->SIZE + ARGUS_HEADER_SIZE ); uint16_t *cCRC = (uint16_t*)( Buffer + ptr->SIZE + ARGUS_HEADER_SIZE ); if ( tCRC == *cCRC ) return 0; } else return 1; }; return -1;};
protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){ protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage)); if ( tPack ) { tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE]; tPack->m_nSize = Buffer[1]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[2]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[3]; tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[4]; tPack->m_nSize = tPack->m_nSize - ARGUS_HEADER_SIZE; tPack->m_pData = (U8 *) malloc( tPack->m_nSize ); if ( tPack->m_pData ) { memcpy( tPack->m_pData, Buffer+ARGUS_HEADER_SIZE, tPack->m_nSize ); } else { free( tPack ); tPack = NULL; }; }; return tPack;};
void mojoRemoteClient::m_fThreadProc( void ){ time_t tTimeTSeconds = 0; time_t cTimeTSeconds = 0; uint8_t bTO_COUNTER = 0;
m_bThreadStatus = TH_WORKING; LOG("Connector Thread Started"); while ( m_bThreadStatus < TH_ENDED ) { // seconds Counter (for internal purposes)
cTimeTSeconds = time( NULL ); if ( cTimeTSeconds!=tTimeTSeconds ) { tTimeTSeconds = cTimeTSeconds; //Process each second
if ( bTO_COUNTER ) { bTO_COUNTER--; } else { switch ( m_bAutomationStage ) { case ARGUS_RPC_STAGE_WAIT_RECONNECT: m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT; break; }; }; }; switch ( m_bAutomationStage ) { case ARGUS_RPC_STAGE_CONNECT:{#ifdef DEBUG
char tB[128]; sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); LOG( tB );#endif
if( m_pMaster->m_sSiteData.remote_port ) { m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); if( m_pPort ) { if( m_pPort->create() ) { LOG("Connection Sucessfull"); m_bShutdownCommunication = false; m_bCommunicationEstablished = true; m_bRegisteredWithRemotePeer = false; setTransport( m_pPort ); m_bAutomationStage = ARGUS_RPC_STAGE_UPDATE_INFO; } else { delete( m_pPort ); m_pPort = NULL; m_bShutdownCommunication = true; m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; bTO_COUNTER = 30; //30 sec to wait
m_bAutomationStage = ARGUS_RPC_STAGE_WAIT_RECONNECT; }; }; }; break; }; case ARGUS_RPC_STAGE_WAIT_RECONNECT: break; case ARGUS_RPC_STAGE_UPDATE_INFO: case ARGUS_RPC_STAGE_IDLE:{ break; }; }; SLEEP(DEFAULT_THREAD_SLEEP_TIME); }; LOG("Connector Thread Ended!");};
bool mojoRemoteClient::packetHandler( protocolPackage *Package ){ uint32_t tQueryMessage; m_mQueryProtector.lock(); tQueryMessage = m_nQueryMessage; m_mQueryProtector.unlock(); switch( Package->m_nCode ){ case ARGUS_DATA_MSG_REGISTER:{ if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) { m_mQueryProtector.lock(); m_nQueryMessage = 0; m_mQueryProtector.unlock(); }; break; }; }; return true;};
/*
Register With Server and make Time Sync With Server*/bool mojoRemoteClient::registerMojo( CSTR identity ){ if( !m_bCommunicationEstablished ) return false; if( m_bRegisteredWithRemotePeer ) return true; uint8_t tBuffer[256]; uint32_t tPackSize = ARGUS_HEADER_SIZE+33; tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER; memcpy( tBuffer+1+ARGUS_HEADER_SIZE, identity, 32 ); if( m_pPort->sendData( tBuffer, argus_rpc_finishupPackage( tBuffer, tPackSize ) ) ) { m_mQueryProtector.lock(); m_nQueryMessage = ARGUS_DATA_MSG_REGISTER; m_mQueryProtector.unlock(); //Waiting For Responce
while( 1 ){ if( m_bShutdownCommunication ) break; m_mQueryProtector.lock(); if( m_nQueryMessage == 0 ) { m_mQueryProtector.unlock(); break; }; m_mQueryProtector.unlock(); SLEEP(1); }; } return m_bRegisteredWithRemotePeer;};
bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){ return true;};
bool mojoRemoteClient::validate(){ LOG("VAL"); /*
if( !m_bCommunicationEstablished ){ if( m_pPort ) { setTransport( NULL, false ); delete( m_pPort ); }; //Try To connect
m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port ); if( m_pPort ) { if( m_pPort->create() ) { m_bShutdownCommunication = false; m_bCommunicationEstablished = true; m_bRegisteredWithRemotePeer = false; setTransport( m_pPort ); } else { delete( m_pPort ); m_pPort = NULL; }; }; } else { if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ) { // Cancel all ongoing Queries
m_bShutdownCommunication = true; // Make Reconect Stage
m_bCommunicationEstablished = false; m_bRegisteredWithRemotePeer = false; setTransport( NULL, false ); delete( m_pPort ); m_pPort = NULL; } else { if( !registerMojo( m_pMaster->m_sIdentity ) ){ //Try Reconnect - in case of already registered -> skipping
// Cancel all ongoing Queries
m_bShutdownCommunication = true; // Make Reconect Stage
m_bCommunicationEstablished = false; setTransport( NULL, false ); delete( m_pPort ); m_pPort = NULL; }; }; }; */ return true;};
#undef LOG
#undef LOG_ERROR
|