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.

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