You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
  1. #include "mojoRemoteClient.h"
  2. #include "mojoKernel.h"
  3. #define LOG( X ) m_pLog->Log( "RPC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG234[256];\
  7. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "RPC", this, _LOG_ERROR, _tBfrErrMSG234 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "RPC", this, _LOG_ERROR, X );
  12. #endif
  13. mojoRemoteClient::mojoRemoteClient( mojoKernel * ptr ):protocolParser(){
  14. m_pLog = basicLogCollection::getInstance();
  15. m_pPort = NULL;
  16. m_pMaster = ptr;
  17. m_bShutdownCommunication = true;
  18. m_bCommunicationEstablished = false;
  19. m_bRegisteredWithRemotePeer = false;
  20. };
  21. mojoRemoteClient::~mojoRemoteClient(){
  22. waitThreadEnd( true );
  23. if( m_bRegisteredWithRemotePeer ) {
  24. //Unregister Mojo
  25. };
  26. m_bShutdownCommunication = true;
  27. m_bCommunicationEstablished = false;
  28. m_bRegisteredWithRemotePeer = false;
  29. if ( m_pPort ) delete( m_pPort );
  30. };
  31. bool mojoRemoteClient::create(){
  32. if( m_pMaster == NULL ) return false;
  33. if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true;
  34. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  35. return init();
  36. };
  37. int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){
  38. if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) {
  39. ARGUS_DATA_HEADER *ptr = (ARGUS_DATA_HEADER*) Buffer;
  40. if ( ( ptr->SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) {
  41. uint16_t tCRC = checksum16( Buffer, ptr->SIZE + ARGUS_HEADER_SIZE );
  42. uint16_t *cCRC = (uint16_t*)( Buffer + ptr->SIZE + ARGUS_HEADER_SIZE );
  43. if ( tCRC == *cCRC ) return 0;
  44. } else return 1;
  45. };
  46. return -1;
  47. };
  48. protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){
  49. protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage));
  50. if ( tPack ) {
  51. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  52. tPack->m_nSize = Buffer[1];
  53. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[2];
  54. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[3];
  55. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[4];
  56. tPack->m_nSize = tPack->m_nSize - ARGUS_HEADER_SIZE;
  57. tPack->m_pData = (U8 *) malloc( tPack->m_nSize );
  58. if ( tPack->m_pData ) {
  59. memcpy( tPack->m_pData, Buffer+ARGUS_HEADER_SIZE, tPack->m_nSize );
  60. } else {
  61. free( tPack );
  62. tPack = NULL;
  63. };
  64. };
  65. return tPack;
  66. };
  67. void mojoRemoteClient::m_fThreadProc( void ){
  68. time_t tTimeTSeconds = 0;
  69. time_t cTimeTSeconds = 0;
  70. uint8_t bTO_COUNTER = 0;
  71. m_bThreadStatus = TH_WORKING;
  72. LOG("Connector Thread Started");
  73. while ( m_bThreadStatus < TH_ENDED ) {
  74. // seconds Counter (for internal purposes)
  75. cTimeTSeconds = time( NULL );
  76. if ( cTimeTSeconds!=tTimeTSeconds ) {
  77. tTimeTSeconds = cTimeTSeconds;
  78. //Process each second
  79. if ( bTO_COUNTER ) {
  80. bTO_COUNTER--;
  81. } else {
  82. switch ( m_bAutomationStage ) {
  83. case ARGUS_RPC_STAGE_WAIT_RECONNECT: m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT; break;
  84. };
  85. };
  86. };
  87. switch ( m_bAutomationStage ) {
  88. case ARGUS_RPC_STAGE_CONNECT:{
  89. #ifdef DEBUG
  90. char tB[128];
  91. sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  92. LOG( tB );
  93. #endif
  94. if( m_pMaster->m_sSiteData.remote_port ) {
  95. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  96. if( m_pPort ) {
  97. if( m_pPort->create() ) {
  98. LOG("Connection Sucessfull");
  99. m_bShutdownCommunication = false;
  100. m_bCommunicationEstablished = true;
  101. m_bRegisteredWithRemotePeer = false;
  102. setTransport( m_pPort );
  103. m_bAutomationStage = ARGUS_RPC_STAGE_UPDATE_INFO;
  104. } else {
  105. delete( m_pPort );
  106. m_pPort = NULL;
  107. m_bShutdownCommunication = true;
  108. m_bCommunicationEstablished = false;
  109. m_bRegisteredWithRemotePeer = false;
  110. bTO_COUNTER = 30; //30 sec to wait
  111. m_bAutomationStage = ARGUS_RPC_STAGE_WAIT_RECONNECT;
  112. };
  113. };
  114. };
  115. break;
  116. };
  117. case ARGUS_RPC_STAGE_WAIT_RECONNECT: break;
  118. case ARGUS_RPC_STAGE_UPDATE_INFO:
  119. case ARGUS_RPC_STAGE_IDLE:{
  120. break;
  121. };
  122. };
  123. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  124. };
  125. LOG("Connector Thread Ended!");
  126. };
  127. bool mojoRemoteClient::packetHandler( protocolPackage *Package ){
  128. uint32_t tQueryMessage;
  129. m_mQueryProtector.lock();
  130. tQueryMessage = m_nQueryMessage;
  131. m_mQueryProtector.unlock();
  132. switch( Package->m_nCode ){
  133. case ARGUS_DATA_MSG_REGISTER:{
  134. if ( tQueryMessage == ARGUS_DATA_MSG_REGISTER ) {
  135. m_mQueryProtector.lock();
  136. m_nQueryMessage = 0;
  137. m_mQueryProtector.unlock();
  138. };
  139. break;
  140. };
  141. };
  142. return true;
  143. };
  144. /*
  145. Register With Server and make Time Sync With Server
  146. */
  147. bool mojoRemoteClient::registerMojo( CSTR identity ){
  148. if( !m_bCommunicationEstablished ) return false;
  149. if( m_bRegisteredWithRemotePeer ) return true;
  150. uint8_t tBuffer[256];
  151. uint32_t tPackSize = ARGUS_HEADER_SIZE+33;
  152. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_REGISTER;
  153. memcpy( tBuffer+1+ARGUS_HEADER_SIZE, identity, 32 );
  154. if( m_pPort->sendData( tBuffer, argus_rpc_finishupPackage( tBuffer, tPackSize ) ) ) {
  155. m_mQueryProtector.lock();
  156. m_nQueryMessage = ARGUS_DATA_MSG_REGISTER;
  157. m_mQueryProtector.unlock();
  158. //Waiting For Responce
  159. while( 1 ){
  160. if( m_bShutdownCommunication ) break;
  161. m_mQueryProtector.lock();
  162. if( m_nQueryMessage == 0 ) {
  163. m_mQueryProtector.unlock();
  164. break;
  165. };
  166. m_mQueryProtector.unlock();
  167. SLEEP(1);
  168. };
  169. }
  170. return m_bRegisteredWithRemotePeer;
  171. };
  172. bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  173. return true;
  174. };
  175. bool mojoRemoteClient::validate(){
  176. LOG("VAL");
  177. /*
  178. if( !m_bCommunicationEstablished ){
  179. if( m_pPort ) {
  180. setTransport( NULL, false );
  181. delete( m_pPort );
  182. };
  183. //Try To connect
  184. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  185. if( m_pPort ) {
  186. if( m_pPort->create() ) {
  187. m_bShutdownCommunication = false;
  188. m_bCommunicationEstablished = true;
  189. m_bRegisteredWithRemotePeer = false;
  190. setTransport( m_pPort );
  191. } else {
  192. delete( m_pPort );
  193. m_pPort = NULL;
  194. };
  195. };
  196. } else {
  197. if( m_pPort->getConnectionStatus() == CS_DISCONNECTED ) {
  198. // Cancel all ongoing Queries
  199. m_bShutdownCommunication = true;
  200. // Make Reconect Stage
  201. m_bCommunicationEstablished = false;
  202. m_bRegisteredWithRemotePeer = false;
  203. setTransport( NULL, false );
  204. delete( m_pPort );
  205. m_pPort = NULL;
  206. } else {
  207. if( !registerMojo( m_pMaster->m_sIdentity ) ){
  208. //Try Reconnect - in case of already registered -> skipping
  209. // Cancel all ongoing Queries
  210. m_bShutdownCommunication = true;
  211. // Make Reconect Stage
  212. m_bCommunicationEstablished = false;
  213. setTransport( NULL, false );
  214. delete( m_pPort );
  215. m_pPort = NULL;
  216. };
  217. };
  218. };
  219. */
  220. return true;
  221. };
  222. #undef LOG
  223. #undef LOG_ERROR