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.

821 lines
32 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
​
​
2 years ago
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
2 years ago
​
​
2 years ago
​
​
​
2 years ago
​
​
​
​
2 years ago
​
2 years ago
​
​
​
2 years ago
​
​
2 years ago
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
2 years ago
2 years ago
​
2 years ago
​
​
​
​
​
​
​
​
​
2 years ago
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_pCurrentCMD = NULL;
  17. m_pMaster = ptr;
  18. m_nOutbondSerial = 0;
  19. m_nInbondSerial = 0;
  20. m_bShutdownCommunication = true;
  21. m_bCommunicationEstablished = false;
  22. m_bRegisteredWithRemotePeer = false;
  23. m_bUseBackupServer = false;
  24. m_bActiveServer = 0;
  25. m_pVideoHelper = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
  26. m_qCMD = new RPC_CMD_QUEUE();
  27. };
  28. mojoRemoteClient::~mojoRemoteClient(){
  29. LOG("Destructor");
  30. waitThreadEnd( true );
  31. if( m_bRegisteredWithRemotePeer ) {
  32. //Unregister Mojo
  33. };
  34. LOG("Flushing");
  35. flushTransactions();
  36. if( m_pVideoHelper ) delete( m_pVideoHelper );
  37. m_bShutdownCommunication = true;
  38. m_bCommunicationEstablished = false;
  39. m_bRegisteredWithRemotePeer = false;
  40. LOG("Delete Port");
  41. if ( m_pPort ) delete( m_pPort );
  42. LOG("Delete Ring");
  43. delete( m_qCMD );
  44. LOG("Done");
  45. };
  46. bool mojoRemoteClient::create(){
  47. if( m_pMaster == NULL ) return false;
  48. if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true;
  49. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  50. m_pCurrentCMD = NULL;
  51. m_bActiveServer = 0;
  52. m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && ( strlen( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  53. return init();
  54. };
  55. int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){
  56. if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) {
  57. if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){
  58. uint32_t SIZE = Buffer[5];
  59. SIZE = ( SIZE << 8 ) | Buffer[6];
  60. SIZE = ( SIZE << 8 ) | Buffer[7];
  61. SIZE = ( SIZE << 8 ) | Buffer[8];
  62. if ( ( SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) {
  63. uint16_t tCRC = checksum16( Buffer, SIZE + ARGUS_HEADER_SIZE );
  64. uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE];
  65. cCRC = ( cCRC << 8 ) | Buffer[SIZE + ARGUS_HEADER_SIZE+1];
  66. if ( tCRC == cCRC ) return 0;
  67. } else return 1; // to grou
  68. } else return 1; //to grow
  69. };
  70. return -1; // to skip
  71. };
  72. protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){
  73. protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage));
  74. if ( tPack ) {
  75. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  76. tPack->m_nSize = Buffer[5];
  77. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[6];
  78. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[7];
  79. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[8];
  80. tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
  81. tPack->m_pData = (U8 *) malloc( tPack->m_nSize );
  82. if ( tPack->m_pData ) {
  83. memcpy( tPack->m_pData, Buffer, tPack->m_nSize );
  84. } else {
  85. free( tPack );
  86. tPack = NULL;
  87. };
  88. };
  89. return tPack;
  90. };
  91. bool mojoRemoteClient::packetHandler( protocolPackage *Package ){
  92. m_nInbondSerial = Package->m_pData[1];
  93. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[2];
  94. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[3];
  95. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[4];
  96. #ifdef DEBUG_EXTRA
  97. char bB[129];
  98. sprintf( bB, "Code: %02X", Package->m_nCode );
  99. LOG( bB );
  100. #endif
  101. switch( Package->m_nCode ){
  102. case ARGUS_DATA_MSG_REGISTER:
  103. #ifdef DEBUG
  104. LOG("Reg From Server");
  105. #endif
  106. m_bRegisteredWithRemotePeer = true;
  107. setAutomationStage( ARGUS_RPC_STAGE_SEND_HELLO );
  108. //finishTransaction();
  109. break;
  110. case ARGUS_DATA_MSG_ACCESS_DENIED: m_bRegisteredWithRemotePeer = false; LOG("ACC DENIED");
  111. case ARGUS_DATA_MSG_DISABLED: finishTransaction(); LOG("MSG disabled"); break;
  112. case ARGUS_DATA_MSG_STAT: finishTransaction(); break;
  113. case ARGUS_DATA_MSG_INFO: finishTransaction(); break;
  114. case ARGUS_DATA_MSG_GET_INFO: enqueueInfoMsg(); break;
  115. case ARGUS_DATA_MSG_REQ_TIME_SYNC:{
  116. #ifdef DEBUG_EXTRA
  117. LOG("TSYNC REQ");
  118. #endif
  119. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  120. if ( tCMD ) {
  121. tCMD->CMD = ARGUS_DATA_MSG_TIME_SYNC;
  122. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_TIME_SYNC_REQ );
  123. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  124. if (tCMD->DATA) {
  125. ARGUS_DATA_TIME_SYNC_REQ *tDATA = (ARGUS_DATA_TIME_SYNC_REQ*)tCMD->DATA;
  126. tDATA->STS = getMilliseconds();
  127. if ( peer_send_rpc_pack( tCMD ) ) {
  128. setTO( 3 );
  129. free( tCMD->DATA );
  130. free( tCMD );
  131. break;
  132. };
  133. free( tCMD->DATA );
  134. };
  135. free( tCMD );
  136. };
  137. //Only On failure
  138. finishTransaction();
  139. break;
  140. };
  141. case ARGUS_DATA_MSG_EVT: finishTransaction(); break;
  142. case ARGUS_DATA_MSG_VIDEO_INFO:{
  143. //get DATABASE ID
  144. uint64_t tDBID = Package->m_pData[ARGUS_HEADER_SIZE+1];
  145. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+2];
  146. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+3];
  147. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+4];
  148. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+5];
  149. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+6];
  150. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+7];
  151. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+8];
  152. #ifdef DEBUG_EXTRA
  153. char tZ[128];
  154. sprintf( tZ, "Video Ack %lld ID", tDBID );
  155. LOG( tZ );
  156. #endif
  157. m_pVideoHelper->setRemoteReferenceID( tDBID );
  158. };
  159. case ARGUS_DATA_MSG_VIDEO_DATA:{
  160. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  161. if ( tCMD ) {
  162. uint32_t tSize;
  163. tCMD->CMD = ARGUS_DATA_MSG_VIDEO_DATA;
  164. tCMD->DATA = (void *)m_pVideoHelper->getNextChunk( ARGUS_RPC_DEFAULT_MEDIA_CHUNK, &tSize );
  165. tCMD->DATA_SIZE = tSize;
  166. #ifdef DEBUG_EXTRA
  167. char tC[128];
  168. sprintf( tC, "Sending Video Chunk %d bytes", tSize );
  169. LOG( tC );
  170. #endif
  171. if ( tCMD->DATA ) {
  172. if ( peer_send_rpc_pack( tCMD ) ) {
  173. setTO( 1 );
  174. free( tCMD->DATA );
  175. free( tCMD );
  176. break;
  177. };
  178. free( tCMD->DATA );
  179. };
  180. free( tCMD );
  181. };
  182. //Only On failure
  183. finishTransaction();
  184. break;
  185. };
  186. case ARGUS_DATA_MSG_TIME_SYNC:{
  187. struct timespec tS = { 0 };
  188. uint64_t cT = getMilliseconds();
  189. ARGUS_DATA_TIME_SYNC_ACK *tPTR = (ARGUS_DATA_TIME_SYNC_ACK *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
  190. uint64_t eTime = tPTR->ATS + ( (cT - tPTR->STS) >> 1 );
  191. tS.tv_sec = eTime / 1000;
  192. tS.tv_nsec = ( eTime % 1000 ) * 1000000;
  193. if( clock_settime( CLOCK_REALTIME, &tS ) != 0){
  194. LOG_ERROR("Time Correction Failed!");
  195. switch ( errno ) {
  196. case EACCES: LOG_ERROR("Not have write permission for the dynamic POSIX clock device indicated!"); break;
  197. case EFAULT: LOG_ERROR("tS points outside the accessible address space!"); break;
  198. case EINVAL: LOG_ERROR("Parameter specified is invalid!"); break;
  199. case ENODEV: LOG_ERROR("The hot-pluggable device (like USB for example) represented by a dynamic clk_id has disappeared after its character device was opened!"); break;
  200. case ENOTSUP: LOG_ERROR("The operation is not supported by the dynamic POSIX clock device specified!"); break;
  201. case EPERM: LOG_ERROR("clock_settime() does not have permission to set the clock indicated!"); break;
  202. };
  203. };
  204. #ifdef DEBUG_EXTRA
  205. LOG("Time Sync ACK!");
  206. char tB[256];
  207. sprintf( tB,"RT:%lld ST:%lld CT:%lld", tPTR->STS, tPTR->ATS, cT );
  208. LOG( tB );
  209. sprintf( tB,"tv_s:%lld tv_ns:%lld", tS.tv_sec, tS.tv_nsec );
  210. LOG( tB );
  211. #endif
  212. finishTransaction();
  213. break;
  214. };
  215. case ARGUS_DATA_MSG_RPC:{
  216. ARGUS_DATA_RPC *tPTR = (ARGUS_DATA_RPC *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
  217. m_pMaster->m_pDBC->pushMemCMD( tPTR->NODE_ID, tPTR->CMD, tPTR->DATA );
  218. break;
  219. };
  220. };
  221. return true;
  222. };
  223. uint8_t mojoRemoteClient::getAutomationStage( ){
  224. m_mAutomationStageProtector.lock();
  225. uint8_t retVal = m_bAutomationStage;
  226. m_mAutomationStageProtector.unlock();
  227. return retVal;
  228. };
  229. void mojoRemoteClient::setAutomationStage( uint8_t stg ){
  230. m_mAutomationStageProtector.lock();
  231. m_bAutomationStage = stg;
  232. m_mAutomationStageProtector.unlock();
  233. };
  234. void mojoRemoteClient::finishTransaction(){
  235. m_mCurrentCMDProtector.lock();
  236. if ( m_pCurrentCMD ) {
  237. if ( m_pCurrentCMD->DATA ) free( m_pCurrentCMD->DATA );
  238. free( m_pCurrentCMD );
  239. m_pCurrentCMD = NULL;
  240. };
  241. m_mCurrentCMDProtector.unlock();
  242. setAutomationStage( ARGUS_RPC_STAGE_IDLE );
  243. };
  244. bool mojoRemoteClient::validateTransaction(){
  245. if ( !m_bRegisteredWithRemotePeer ) return false;
  246. if ( m_bShutdownCommunication ) return false;
  247. m_mCurrentCMDProtector.lock();
  248. if ( m_pCurrentCMD == NULL ) {
  249. m_mCurrentCMDProtector.unlock();
  250. return true;
  251. };
  252. m_mCurrentCMDProtector.unlock();
  253. return false;
  254. };
  255. bool mojoRemoteClient::pushNewTransaction( RPC_CMD_RECORD *ptr ){
  256. if ( ptr == NULL ) return false;
  257. m_mCurrentCMDProtector.lock();
  258. if ( m_qCMD->push( ptr ) != -1 ){
  259. m_mCurrentCMDProtector.unlock();
  260. return true;
  261. };
  262. m_mCurrentCMDProtector.unlock();
  263. return false;
  264. };
  265. bool mojoRemoteClient::fetchNewTransaction(){
  266. if ( m_bShutdownCommunication ) return false;
  267. m_mCurrentCMDProtector.lock();
  268. if ( m_pCurrentCMD ) {
  269. m_mCurrentCMDProtector.unlock();
  270. return false;
  271. };
  272. m_pCurrentCMD = m_qCMD->pop();
  273. if ( m_pCurrentCMD ) {
  274. m_mCurrentCMDProtector.unlock();
  275. return true;
  276. };
  277. m_mCurrentCMDProtector.unlock();
  278. return false;
  279. };
  280. void mojoRemoteClient::enqueueInfoMsg(){
  281. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  282. if ( tCMD ) {
  283. tCMD->CMD = ARGUS_DATA_MSG_INFO;
  284. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  285. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  286. if (tCMD->DATA) {
  287. LOG("Send Client Info!");
  288. m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)tCMD->DATA );
  289. LOG("ARGUS_DATA_MOJO_INFO");
  290. if ( pushNewTransaction( tCMD ) ) return;
  291. free( tCMD->DATA );
  292. };
  293. free( tCMD );
  294. };
  295. };
  296. void mojoRemoteClient::enqueueStatMsg(){
  297. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  298. if ( tCMD ) {
  299. tCMD->CMD = ARGUS_DATA_MSG_STAT;
  300. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_STATUS );
  301. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  302. if( tCMD->DATA ){
  303. memset( tCMD->DATA, 0, tCMD->DATA_SIZE );
  304. ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA;
  305. DBC_SYS_LOAD_INFO cSYSSTAT;
  306. m_pMaster->getSystemUtilization( &cSYSSTAT );
  307. cSTATUS->CPU_LOAD = round( cSYSSTAT.cpu15 );
  308. cSTATUS->CPU_TEMP = round( cSYSSTAT.temperature );
  309. //if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0;
  310. //if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0;
  311. cSTATUS->DISK_UTI = cSYSSTAT.disk;
  312. cSTATUS->MEMO_UTI = cSYSSTAT.mem;
  313. DBC_INT_LIST * tList;
  314. tList = m_pMaster->m_pDBC->listLRFSensors();
  315. if ( tList ) {
  316. for( uint8_t idx=0; idx<tList->size(); idx++ ) {
  317. uint8_t tS = m_pMaster->m_pDBC->getLRFDataStatus( tList->at(idx) );
  318. if ( tS > 1 ) {
  319. cSTATUS->STATUSES = cSTATUS->STATUSES | (1 << idx);
  320. };
  321. };
  322. tList->clear();
  323. delete( tList );
  324. };
  325. tList = m_pMaster->m_pDBC->listCameras();
  326. if ( tList ) {
  327. for( uint8_t idx=0; idx<tList->size(); idx++ ) {
  328. DBC_CAM_DATA *tCD = m_pMaster->m_pDBC->getCamData( tList->at(idx) );
  329. if ( tCD ) {
  330. if( tCD->online ) {
  331. cSTATUS->STATUSES = cSTATUS->STATUSES | (0x80000000 >> (idx<<1));
  332. };
  333. // check the stream
  334. if( tCD->online ) {
  335. cSTATUS->STATUSES = cSTATUS->STATUSES | (0x40000000 >> (idx<<1));
  336. };
  337. free( tCD );
  338. };
  339. };
  340. tList->clear();
  341. delete( tList );
  342. };
  343. if ( pushNewTransaction( tCMD ) ) return;
  344. free( tCMD->DATA );
  345. };
  346. free( tCMD );
  347. };
  348. };
  349. void mojoRemoteClient::enqueueTimeReq(){
  350. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  351. if ( tCMD ) {
  352. tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC;
  353. tCMD->DATA_SIZE = 0;
  354. tCMD->DATA = NULL;
  355. if ( pushNewTransaction( tCMD ) ) return;
  356. free( tCMD );
  357. };
  358. };
  359. void mojoRemoteClient::flushTransactions(){
  360. m_mCurrentCMDProtector.lock();
  361. if ( m_pCurrentCMD ) {
  362. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  363. free( m_pCurrentCMD );
  364. };
  365. while( m_pCurrentCMD = m_qCMD->pop() ){
  366. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  367. free( m_pCurrentCMD );
  368. };
  369. m_pCurrentCMD = NULL;
  370. m_mCurrentCMDProtector.unlock();
  371. };
  372. void mojoRemoteClient::setTO( uint8_t seconds ){
  373. m_mTOProtoctor.lock();
  374. m_bTOCounter = seconds;
  375. m_mTOProtoctor.unlock();
  376. };
  377. bool mojoRemoteClient::decTO(){
  378. m_mTOProtoctor.lock();
  379. if ( m_bTOCounter ) {
  380. m_bTOCounter--;
  381. m_mTOProtoctor.unlock();
  382. return true;
  383. };
  384. m_mTOProtoctor.unlock();
  385. return false;
  386. };
  387. void mojoRemoteClient::m_fThreadProc( void ){
  388. time_t tTimeTSeconds = 0;
  389. time_t cTimeTSeconds = 0;
  390. uint32_t tSecondsCounter = 0;
  391. m_bThreadStatus = TH_WORKING;
  392. #ifdef DEBUG
  393. LOG("Connector Thread Started");
  394. #endif
  395. while ( m_bThreadStatus < TH_ENDED ) {
  396. // seconds Counter (for internal purposes)
  397. cTimeTSeconds = time( NULL );
  398. //Process each second
  399. if ( cTimeTSeconds != tTimeTSeconds ) {
  400. tSecondsCounter++;
  401. tTimeTSeconds = cTimeTSeconds;
  402. switch ( getAutomationStage() ) {
  403. case ARGUS_RPC_STAGE_CONNECT:{
  404. flushTransactions();
  405. #ifdef DEBUG
  406. char tB[128];
  407. sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  408. LOG( tB );
  409. #endif
  410. if ( m_bUseBackupServer ) {
  411. if ( m_bActiveServer == 0 ) {
  412. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  413. } else {
  414. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  415. }
  416. if( m_pPort ) {
  417. if( m_pPort->create() ) {
  418. #ifdef DEBUG
  419. LOG("Connection Sucessfull");
  420. #endif
  421. m_bShutdownCommunication = false;
  422. m_bCommunicationEstablished = true;
  423. m_bRegisteredWithRemotePeer = false;
  424. setTransport( m_pPort, false );
  425. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  426. } else {
  427. setConnectionStatus( CS_DISCONNECTED );
  428. setTransport( NULL, false );
  429. delete( m_pPort );
  430. m_pPort = NULL;
  431. m_bShutdownCommunication = true;
  432. m_bCommunicationEstablished = false;
  433. m_bRegisteredWithRemotePeer = false;
  434. m_bActiveServer = 1 - m_bActiveServer;
  435. setTO( 30 ); //30 sec to wait
  436. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  437. };
  438. };
  439. } else {
  440. if (m_pMaster->m_sSiteData.remote_port) {
  441. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  442. if( m_pPort ) {
  443. if( m_pPort->create() ) {
  444. #ifdef DEBUG
  445. LOG("Connection Sucessfull");
  446. #endif
  447. m_bShutdownCommunication = false;
  448. m_bCommunicationEstablished = true;
  449. m_bRegisteredWithRemotePeer = false;
  450. setTransport( m_pPort, false );
  451. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  452. } else {
  453. setConnectionStatus( CS_DISCONNECTED );
  454. setTransport( NULL, false );
  455. delete( m_pPort );
  456. m_pPort = NULL;
  457. m_bShutdownCommunication = true;
  458. m_bCommunicationEstablished = false;
  459. m_bRegisteredWithRemotePeer = false;
  460. setTO( 30 ); //30 sec to wait
  461. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  462. };
  463. };
  464. };
  465. };
  466. break;
  467. };
  468. case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
  469. #ifdef DEBUG_EXTRA
  470. LOG("Wait Reconnect!");
  471. #endif
  472. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT );
  473. break;
  474. };
  475. case ARGUS_RPC_STAGE_REGISTER:{
  476. #ifdef DEBUG
  477. LOG("Stage Register!");
  478. #endif
  479. m_pCurrentCMD = (RPC_CMD_RECORD*) malloc(sizeof(RPC_CMD_RECORD));
  480. if ( m_pCurrentCMD ) {
  481. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  482. m_pCurrentCMD->DATA_SIZE = strlen( m_pMaster->m_sIdentity );
  483. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  484. if ( m_pCurrentCMD->DATA ) {
  485. strncpy( (char *)m_pCurrentCMD->DATA, m_pMaster->m_sIdentity, m_pCurrentCMD->DATA_SIZE );
  486. if( peer_send_rpc_pack( m_pCurrentCMD ) ){
  487. setTO( 3 ); // Wait for 3 seconds to be registered with server
  488. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  489. break;
  490. };
  491. free( m_pCurrentCMD->DATA );
  492. };
  493. free( m_pCurrentCMD );
  494. };
  495. m_pCurrentCMD = NULL;
  496. break;
  497. };
  498. case ARGUS_RPC_STAGE_SEND_HELLO:{
  499. #ifdef DEBUG
  500. LOG("Stage HELLO!");
  501. #endif
  502. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  503. if ( m_pCurrentCMD ) {
  504. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  505. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  506. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  507. if (m_pCurrentCMD->DATA) {
  508. LOG("Send Client Info!");
  509. m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  510. LOG("ARGUS_DATA_MOJO_INFO");
  511. if ( peer_send_rpc_pack( m_pCurrentCMD ) ){
  512. setTO( 1 ); // Wait for 3 seconds
  513. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  514. };
  515. free( m_pCurrentCMD->DATA );
  516. };
  517. free( m_pCurrentCMD );
  518. };
  519. m_pCurrentCMD = NULL;
  520. break;
  521. };
  522. case ARGUS_RPC_STAGE_WAIT_ACK:{
  523. //LOG("Wait ACK!");
  524. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  525. break;
  526. };
  527. case ARGUS_RPC_STAGE_IDLE: if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) enqueueTimeReq(); else enqueueStatMsg(); break;
  528. case ARGUS_RPC_STAGE_DISCONNECT:{
  529. LOG("Stage Disconnect!");
  530. // Gracefully Disconnect
  531. //if( peer_send_disconnect() ){
  532. // bTO_COUNTER = 1;
  533. // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  534. //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  535. //break; ---> Go to reconnect to other server
  536. };
  537. case ARGUS_RPC_STAGE_CLOSE:{
  538. // Just Disconnect
  539. LOG("Stage Close!");
  540. flushTransactions();
  541. setTransport( NULL, false );
  542. delete( m_pPort );
  543. m_pPort = NULL;
  544. m_bShutdownCommunication = true;
  545. m_bCommunicationEstablished = false;
  546. m_bRegisteredWithRemotePeer = false;
  547. setTO( 30 ); //30 sec to wait
  548. m_bActiveServer = 1 - m_bActiveServer;
  549. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  550. break;
  551. };
  552. };
  553. };
  554. if ( validateTransaction() ) {
  555. if ( fetchNewTransaction() ) {
  556. if( peer_send_rpc_pack( m_pCurrentCMD ) ) {
  557. setTO( 1 );
  558. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  559. } else LOG_ERROR("Failure!");
  560. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  561. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  562. };
  563. LOG("Connector Thread Ended!");
  564. };
  565. /*
  566. Register With Server and make Time Sync With Server
  567. */
  568. bool mojoRemoteClient::peer_send_rpc_pack( RPC_CMD_RECORD *ptr ){
  569. bool retVal;
  570. static uint8_t pBuffer[4194304]; //4Mb
  571. if ( ptr == NULL ) return false;
  572. if( !m_bCommunicationEstablished ) return false;
  573. //if( !m_bRegisteredWithRemotePeer ) return false;
  574. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  575. //uint16_t i = 0;
  576. //for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  577. memcpy( pBuffer+ARGUS_HEADER_SIZE+1, ptr->DATA, ptr->DATA_SIZE );
  578. m_nOutbondSerial++;
  579. //retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  580. retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+ptr->DATA_SIZE));
  581. return retVal;
  582. };
  583. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  584. if( ptr == NULL ) return false;
  585. if( !m_bCommunicationEstablished ) return false;
  586. if( !m_bRegisteredWithRemotePeer ) return false;
  587. uint8_t tBuffer[1024];
  588. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  589. uint16_t i = 0;
  590. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  591. m_nOutbondSerial++;
  592. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  593. };
  594. */
  595. bool mojoRemoteClient::validate(){
  596. if( m_bCommunicationEstablished ){
  597. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  598. m_bCommunicationEstablished = false;
  599. if( m_pPort ) {
  600. // Cancel all ongoing Queries
  601. m_bShutdownCommunication = true;
  602. setTransport( NULL, false );
  603. delete( m_pPort );
  604. };
  605. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  606. // Revalidate Data!!!
  607. //m_bActiveServer = 0;
  608. //m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  609. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  610. if( m_pPort ) {
  611. if( m_pPort->create() ) {
  612. m_bShutdownCommunication = false;
  613. m_bCommunicationEstablished = true;
  614. m_bRegisteredWithRemotePeer = false;
  615. setTransport( m_pPort, false );
  616. } else {
  617. delete( m_pPort );
  618. m_pPort = NULL;
  619. };
  620. };
  621. */
  622. };
  623. #ifdef DEBUG_EXTRA
  624. } else {
  625. LOG("Not Connected!");
  626. #endif
  627. /*
  628. if( m_pPort ) {
  629. // Cancel all ongoing Queries
  630. m_bShutdownCommunication = true;
  631. setTransport( NULL, false );
  632. delete( m_pPort );
  633. };
  634. //Try To connect
  635. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  636. if( m_pPort ) {
  637. if( m_pPort->create() ) {
  638. m_bShutdownCommunication = false;
  639. m_bCommunicationEstablished = true;
  640. m_bRegisteredWithRemotePeer = false;
  641. setTransport( m_pPort, false );
  642. } else {
  643. delete( m_pPort );
  644. m_pPort = NULL;
  645. };
  646. };
  647. */
  648. };
  649. return true;
  650. };
  651. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  652. #ifdef DEBUG_EXTRA
  653. LOG("Add Evt!");
  654. #endif
  655. if ( Evt == NULL ) return false;
  656. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  657. if ( tPack ) {
  658. #ifdef DEBUG_EXTRA
  659. char tB[128];
  660. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000 );
  661. LOG( tB );
  662. #endif
  663. tPack->DATA = (void*) argus_rpc_convert_evt( Evt );
  664. if ( tPack->DATA ) {
  665. tPack->CMD = ARGUS_DATA_MSG_EVT;
  666. tPack->DATA_SIZE = sizeof( ARGUS_DATA_EVENT );
  667. if( m_qCMD->push( tPack ) ) return true;
  668. free( tPack->DATA );
  669. };
  670. free( tPack );
  671. };
  672. return false;
  673. };
  674. bool mojoRemoteClient::addVideo( VIDEO_SEGMENT_INFO *ptr ){
  675. #ifdef DEBUG_EXTRA
  676. LOG("Add Video!");
  677. #endif
  678. if ( ptr == NULL ) return false;
  679. if ( m_pVideoHelper == NULL ) return false;
  680. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  681. if ( tPack ) {
  682. if( m_pVideoHelper->init( ptr ) ) {
  683. tPack->DATA_SIZE = sizeof( ARGUS_DATA_VIDEO_INFO );
  684. tPack->DATA = malloc( tPack->DATA_SIZE );
  685. if ( tPack->DATA ) {
  686. memcpy( tPack->DATA, m_pVideoHelper->getVideoInfo(), tPack->DATA_SIZE );
  687. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  688. if( m_qCMD->push( tPack ) ) return true;
  689. free( tPack->DATA );
  690. };
  691. m_pVideoHelper->abort();
  692. };
  693. free( tPack );
  694. };
  695. return false;
  696. };
  697. mojoVideoTransferHelper::mojoVideoTransferHelper( uint32_t buffer_size ){
  698. m_pLog = basicLogCollection::getInstance();
  699. m_bVideoExists = false;
  700. m_bVideoOpened = false;
  701. m_nBufferSize = buffer_size;
  702. m_pBuffer = (uint8_t *) malloc( m_nBufferSize );
  703. m_nCTSMS = 0;
  704. m_nRemoteRefID = 0;
  705. m_pFD = NULL;
  706. memset( m_cCurrentFile, 0, MAX_PATH );
  707. };
  708. mojoVideoTransferHelper::~mojoVideoTransferHelper(){
  709. abort();
  710. };
  711. #include <sys/stat.h>
  712. bool mojoVideoTransferHelper::init( VIDEO_SEGMENT_INFO *ptr ){
  713. if ( m_pBuffer == NULL ) return false;
  714. if ( m_bVideoExists ) {
  715. if ( m_bVideoOpened ) fclose( m_pFD );
  716. unlink(m_cCurrentFile);
  717. };
  718. if( !( m_bVideoExists = fileExist(ptr->scratchPath) ) ) return false;
  719. m_nRemoteRefID = 0;
  720. strncpy( m_cCurrentFile, ptr->scratchPath, MAX_PATH );
  721. m_cCurrentFile[MAX_PATH-1] = (char)0;
  722. struct stat st;
  723. stat( m_cCurrentFile, &st );
  724. m_sIn.DATA_SIZE = st.st_size;
  725. m_sIn.DTS = ptr->streamTSS;
  726. m_sIn.DMS = ptr->streamTMS;
  727. m_sIn.FPS = ptr->streamFPS;
  728. strncpy( m_sIn.FMT, ptr->recordEXT, 5 );
  729. m_sIn.FMT[5]=(char)0;
  730. m_sIn.WIDTH = ptr->streamWidth;
  731. m_sIn.HEIGHT = ptr->streamHeight;
  732. #ifdef DEBUG_EXTRA
  733. char tB[129];
  734. sprintf( tB, "VideoInfo Init: [%dx%d]@%d TS: %d.%d", m_sIn.WIDTH, m_sIn.HEIGHT, m_sIn.FPS, m_sIn.DTS, m_sIn.DMS );
  735. LOG(tB);
  736. #endif
  737. m_pFD = fopen( m_cCurrentFile, "r" );
  738. if( m_bVideoOpened = m_pFD != NULL ) return true;
  739. return false;
  740. };
  741. uint8_t *mojoVideoTransferHelper::getNextChunk( uint32_t req_chunk_size, uint32_t *ack_size ){
  742. uint8_t *retVal = NULL;
  743. if ( m_bVideoOpened ) {
  744. retVal = (uint8_t *) malloc( req_chunk_size + 12 );//+DB_SOURCE_ID_x_64+Size_32
  745. if ( retVal ) {
  746. int32_t rv = fread( retVal+12, 1, req_chunk_size, m_pFD );
  747. if ( rv < 0 ) {
  748. free( retVal );
  749. retVal = NULL;
  750. abort();
  751. } else {
  752. *ack_size = rv+12;
  753. retVal[0] = ( m_nRemoteRefID >> 56 ) & 0xFF;
  754. retVal[1] = ( m_nRemoteRefID >> 48 ) & 0xFF;
  755. retVal[2] = ( m_nRemoteRefID >> 40 ) & 0xFF;
  756. retVal[3] = ( m_nRemoteRefID >> 32 ) & 0xFF;
  757. retVal[4] = ( m_nRemoteRefID >> 24 ) & 0xFF;
  758. retVal[5] = ( m_nRemoteRefID >> 16 ) & 0xFF;
  759. retVal[6] = ( m_nRemoteRefID >> 8 ) & 0xFF;
  760. retVal[7] = m_nRemoteRefID & 0xFF;
  761. retVal[8] = ( rv >> 24 ) & 0xFF;
  762. retVal[9] = ( rv >> 16 ) & 0xFF;
  763. retVal[10] = ( rv >> 8 ) & 0xFF;
  764. retVal[11] = rv & 0xFF;
  765. if ( feof( m_pFD )){
  766. fclose( m_pFD );
  767. m_bVideoOpened = false;
  768. };
  769. };
  770. };
  771. };
  772. return retVal;
  773. };
  774. ARGUS_DATA_VIDEO_INFO *mojoVideoTransferHelper::getVideoInfo(){
  775. return &m_sIn;
  776. };
  777. void mojoVideoTransferHelper::abort(){
  778. };
  779. #undef LOG
  780. #undef LOG_ERROR