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.

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