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.

792 lines
31 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. ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA;
  304. DBC_SYS_LOAD_INFO cSYSSTAT;
  305. uint8_t tS = m_pMaster->m_pDBC->getLRFDataStatus( 1 );
  306. cSTATUS->STATUSES = 0xff00ff00 | tS;
  307. m_pMaster->getSystemUtilization( &cSYSSTAT );
  308. cSTATUS->CPU_LOAD = round( cSYSSTAT.cpu15 );
  309. cSTATUS->CPU_TEMP = round( cSYSSTAT.temperature );
  310. //if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0;
  311. //if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0;
  312. cSTATUS->DISK_UTI = cSYSSTAT.disk;
  313. cSTATUS->MEMO_UTI = cSYSSTAT.mem;
  314. if ( pushNewTransaction( tCMD ) ) return;
  315. free( tCMD->DATA );
  316. };
  317. free( tCMD );
  318. };
  319. };
  320. void mojoRemoteClient::enqueueTimeReq(){
  321. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  322. if ( tCMD ) {
  323. tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC;
  324. tCMD->DATA_SIZE = 0;
  325. tCMD->DATA = NULL;
  326. if ( pushNewTransaction( tCMD ) ) return;
  327. free( tCMD );
  328. };
  329. };
  330. void mojoRemoteClient::flushTransactions(){
  331. m_mCurrentCMDProtector.lock();
  332. if ( m_pCurrentCMD ) {
  333. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  334. free( m_pCurrentCMD );
  335. };
  336. while( m_pCurrentCMD = m_qCMD->pop() ){
  337. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  338. free( m_pCurrentCMD );
  339. };
  340. m_pCurrentCMD = NULL;
  341. m_mCurrentCMDProtector.unlock();
  342. };
  343. void mojoRemoteClient::setTO( uint8_t seconds ){
  344. m_mTOProtoctor.lock();
  345. m_bTOCounter = seconds;
  346. m_mTOProtoctor.unlock();
  347. };
  348. bool mojoRemoteClient::decTO(){
  349. m_mTOProtoctor.lock();
  350. if ( m_bTOCounter ) {
  351. m_bTOCounter--;
  352. m_mTOProtoctor.unlock();
  353. return true;
  354. };
  355. m_mTOProtoctor.unlock();
  356. return false;
  357. };
  358. void mojoRemoteClient::m_fThreadProc( void ){
  359. time_t tTimeTSeconds = 0;
  360. time_t cTimeTSeconds = 0;
  361. uint32_t tSecondsCounter = 0;
  362. m_bThreadStatus = TH_WORKING;
  363. #ifdef DEBUG
  364. LOG("Connector Thread Started");
  365. #endif
  366. while ( m_bThreadStatus < TH_ENDED ) {
  367. // seconds Counter (for internal purposes)
  368. cTimeTSeconds = time( NULL );
  369. //Process each second
  370. if ( cTimeTSeconds != tTimeTSeconds ) {
  371. tSecondsCounter++;
  372. tTimeTSeconds = cTimeTSeconds;
  373. switch ( getAutomationStage() ) {
  374. case ARGUS_RPC_STAGE_CONNECT:{
  375. flushTransactions();
  376. #ifdef DEBUG
  377. char tB[128];
  378. sprintf(tB,"Creating Remote connection to [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  379. LOG( tB );
  380. #endif
  381. if ( m_bUseBackupServer ) {
  382. if ( m_bActiveServer == 0 ) {
  383. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  384. } else {
  385. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  386. }
  387. if( m_pPort ) {
  388. if( m_pPort->create() ) {
  389. #ifdef DEBUG
  390. LOG("Connection Sucessfull");
  391. #endif
  392. m_bShutdownCommunication = false;
  393. m_bCommunicationEstablished = true;
  394. m_bRegisteredWithRemotePeer = false;
  395. setTransport( m_pPort, false );
  396. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  397. } else {
  398. setConnectionStatus( CS_DISCONNECTED );
  399. setTransport( NULL, false );
  400. delete( m_pPort );
  401. m_pPort = NULL;
  402. m_bShutdownCommunication = true;
  403. m_bCommunicationEstablished = false;
  404. m_bRegisteredWithRemotePeer = false;
  405. m_bActiveServer = 1 - m_bActiveServer;
  406. setTO( 30 ); //30 sec to wait
  407. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  408. };
  409. };
  410. } else {
  411. if (m_pMaster->m_sSiteData.remote_port) {
  412. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  413. if( m_pPort ) {
  414. if( m_pPort->create() ) {
  415. #ifdef DEBUG
  416. LOG("Connection Sucessfull");
  417. #endif
  418. m_bShutdownCommunication = false;
  419. m_bCommunicationEstablished = true;
  420. m_bRegisteredWithRemotePeer = false;
  421. setTransport( m_pPort, false );
  422. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  423. } else {
  424. setConnectionStatus( CS_DISCONNECTED );
  425. setTransport( NULL, false );
  426. delete( m_pPort );
  427. m_pPort = NULL;
  428. m_bShutdownCommunication = true;
  429. m_bCommunicationEstablished = false;
  430. m_bRegisteredWithRemotePeer = false;
  431. setTO( 30 ); //30 sec to wait
  432. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  433. };
  434. };
  435. };
  436. };
  437. break;
  438. };
  439. case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
  440. #ifdef DEBUG_EXTRA
  441. LOG("Wait Reconnect!");
  442. #endif
  443. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT );
  444. break;
  445. };
  446. case ARGUS_RPC_STAGE_REGISTER:{
  447. #ifdef DEBUG
  448. LOG("Stage Register!");
  449. #endif
  450. m_pCurrentCMD = (RPC_CMD_RECORD*) malloc(sizeof(RPC_CMD_RECORD));
  451. if ( m_pCurrentCMD ) {
  452. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  453. m_pCurrentCMD->DATA_SIZE = strlen( m_pMaster->m_sIdentity );
  454. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  455. if ( m_pCurrentCMD->DATA ) {
  456. strncpy( (char *)m_pCurrentCMD->DATA, m_pMaster->m_sIdentity, m_pCurrentCMD->DATA_SIZE );
  457. if( peer_send_rpc_pack( m_pCurrentCMD ) ){
  458. setTO( 3 ); // Wait for 3 seconds to be registered with server
  459. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  460. break;
  461. };
  462. free( m_pCurrentCMD->DATA );
  463. };
  464. free( m_pCurrentCMD );
  465. };
  466. m_pCurrentCMD = NULL;
  467. break;
  468. };
  469. case ARGUS_RPC_STAGE_SEND_HELLO:{
  470. #ifdef DEBUG
  471. LOG("Stage HELLO!");
  472. #endif
  473. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  474. if ( m_pCurrentCMD ) {
  475. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  476. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  477. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  478. if (m_pCurrentCMD->DATA) {
  479. LOG("Send Client Info!");
  480. m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  481. LOG("ARGUS_DATA_MOJO_INFO");
  482. if ( peer_send_rpc_pack( m_pCurrentCMD ) ){
  483. setTO( 1 ); // Wait for 3 seconds
  484. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  485. };
  486. free( m_pCurrentCMD->DATA );
  487. };
  488. free( m_pCurrentCMD );
  489. };
  490. m_pCurrentCMD = NULL;
  491. break;
  492. };
  493. case ARGUS_RPC_STAGE_WAIT_ACK:{
  494. //LOG("Wait ACK!");
  495. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  496. break;
  497. };
  498. case ARGUS_RPC_STAGE_IDLE: if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) enqueueTimeReq(); else enqueueStatMsg(); break;
  499. case ARGUS_RPC_STAGE_DISCONNECT:{
  500. LOG("Stage Disconnect!");
  501. // Gracefully Disconnect
  502. //if( peer_send_disconnect() ){
  503. // bTO_COUNTER = 1;
  504. // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  505. //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  506. //break; ---> Go to reconnect to other server
  507. };
  508. case ARGUS_RPC_STAGE_CLOSE:{
  509. // Just Disconnect
  510. LOG("Stage Close!");
  511. flushTransactions();
  512. setTransport( NULL, false );
  513. delete( m_pPort );
  514. m_pPort = NULL;
  515. m_bShutdownCommunication = true;
  516. m_bCommunicationEstablished = false;
  517. m_bRegisteredWithRemotePeer = false;
  518. setTO( 30 ); //30 sec to wait
  519. m_bActiveServer = 1 - m_bActiveServer;
  520. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  521. break;
  522. };
  523. };
  524. };
  525. if ( validateTransaction() ) {
  526. if ( fetchNewTransaction() ) {
  527. if( peer_send_rpc_pack( m_pCurrentCMD ) ) {
  528. setTO( 1 );
  529. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  530. } else LOG_ERROR("Failure!");
  531. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  532. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  533. };
  534. LOG("Connector Thread Ended!");
  535. };
  536. /*
  537. Register With Server and make Time Sync With Server
  538. */
  539. bool mojoRemoteClient::peer_send_rpc_pack( RPC_CMD_RECORD *ptr ){
  540. bool retVal;
  541. static uint8_t pBuffer[4194304]; //4Mb
  542. if ( ptr == NULL ) return false;
  543. if( !m_bCommunicationEstablished ) return false;
  544. //if( !m_bRegisteredWithRemotePeer ) return false;
  545. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  546. //uint16_t i = 0;
  547. //for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  548. memcpy( pBuffer+ARGUS_HEADER_SIZE+1, ptr->DATA, ptr->DATA_SIZE );
  549. m_nOutbondSerial++;
  550. //retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  551. retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+ptr->DATA_SIZE));
  552. return retVal;
  553. };
  554. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  555. if( ptr == NULL ) return false;
  556. if( !m_bCommunicationEstablished ) return false;
  557. if( !m_bRegisteredWithRemotePeer ) return false;
  558. uint8_t tBuffer[1024];
  559. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  560. uint16_t i = 0;
  561. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  562. m_nOutbondSerial++;
  563. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  564. };
  565. */
  566. bool mojoRemoteClient::validate(){
  567. if( m_bCommunicationEstablished ){
  568. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  569. m_bCommunicationEstablished = false;
  570. if( m_pPort ) {
  571. // Cancel all ongoing Queries
  572. m_bShutdownCommunication = true;
  573. setTransport( NULL, false );
  574. delete( m_pPort );
  575. };
  576. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  577. // Revalidate Data!!!
  578. //m_bActiveServer = 0;
  579. //m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  580. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  581. if( m_pPort ) {
  582. if( m_pPort->create() ) {
  583. m_bShutdownCommunication = false;
  584. m_bCommunicationEstablished = true;
  585. m_bRegisteredWithRemotePeer = false;
  586. setTransport( m_pPort, false );
  587. } else {
  588. delete( m_pPort );
  589. m_pPort = NULL;
  590. };
  591. };
  592. */
  593. };
  594. #ifdef DEBUG_EXTRA
  595. } else {
  596. LOG("Not Connected!");
  597. #endif
  598. /*
  599. if( m_pPort ) {
  600. // Cancel all ongoing Queries
  601. m_bShutdownCommunication = true;
  602. setTransport( NULL, false );
  603. delete( m_pPort );
  604. };
  605. //Try To connect
  606. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  607. if( m_pPort ) {
  608. if( m_pPort->create() ) {
  609. m_bShutdownCommunication = false;
  610. m_bCommunicationEstablished = true;
  611. m_bRegisteredWithRemotePeer = false;
  612. setTransport( m_pPort, false );
  613. } else {
  614. delete( m_pPort );
  615. m_pPort = NULL;
  616. };
  617. };
  618. */
  619. };
  620. return true;
  621. };
  622. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  623. #ifdef DEBUG_EXTRA
  624. LOG("Add Evt!");
  625. #endif
  626. if ( Evt == NULL ) return false;
  627. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  628. if ( tPack ) {
  629. #ifdef DEBUG_EXTRA
  630. char tB[128];
  631. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000 );
  632. LOG( tB );
  633. #endif
  634. tPack->DATA = (void*) argus_rpc_convert_evt( Evt );
  635. if ( tPack->DATA ) {
  636. tPack->CMD = ARGUS_DATA_MSG_EVT;
  637. tPack->DATA_SIZE = sizeof( ARGUS_DATA_EVENT );
  638. if( m_qCMD->push( tPack ) ) return true;
  639. free( tPack->DATA );
  640. };
  641. free( tPack );
  642. };
  643. return false;
  644. };
  645. bool mojoRemoteClient::addVideo( VIDEO_SEGMENT_INFO *ptr ){
  646. #ifdef DEBUG_EXTRA
  647. LOG("Add Video!");
  648. #endif
  649. if ( ptr == NULL ) return false;
  650. if ( m_pVideoHelper == NULL ) return false;
  651. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  652. if ( tPack ) {
  653. if( m_pVideoHelper->init( ptr ) ) {
  654. tPack->DATA_SIZE = sizeof( ARGUS_DATA_VIDEO_INFO );
  655. tPack->DATA = malloc( tPack->DATA_SIZE );
  656. if ( tPack->DATA ) {
  657. memcpy( tPack->DATA, m_pVideoHelper->getVideoInfo(), tPack->DATA_SIZE );
  658. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  659. if( m_qCMD->push( tPack ) ) return true;
  660. free( tPack->DATA );
  661. };
  662. m_pVideoHelper->abort();
  663. };
  664. free( tPack );
  665. };
  666. return false;
  667. };
  668. mojoVideoTransferHelper::mojoVideoTransferHelper( uint32_t buffer_size ){
  669. m_pLog = basicLogCollection::getInstance();
  670. m_bVideoExists = false;
  671. m_bVideoOpened = false;
  672. m_nBufferSize = buffer_size;
  673. m_pBuffer = (uint8_t *) malloc( m_nBufferSize );
  674. m_nCTSMS = 0;
  675. m_nRemoteRefID = 0;
  676. m_pFD = NULL;
  677. memset( m_cCurrentFile, 0, MAX_PATH );
  678. };
  679. mojoVideoTransferHelper::~mojoVideoTransferHelper(){
  680. abort();
  681. };
  682. #include <sys/stat.h>
  683. bool mojoVideoTransferHelper::init( VIDEO_SEGMENT_INFO *ptr ){
  684. if ( m_pBuffer == NULL ) return false;
  685. if ( m_bVideoExists ) {
  686. if ( m_bVideoOpened ) fclose( m_pFD );
  687. unlink(m_cCurrentFile);
  688. };
  689. if( !( m_bVideoExists = fileExist(ptr->scratchPath) ) ) return false;
  690. m_nRemoteRefID = 0;
  691. strncpy( m_cCurrentFile, ptr->scratchPath, MAX_PATH );
  692. m_cCurrentFile[MAX_PATH-1] = (char)0;
  693. struct stat st;
  694. stat( m_cCurrentFile, &st );
  695. m_sIn.DATA_SIZE = st.st_size;
  696. m_sIn.DTS = ptr->streamTSS;
  697. m_sIn.DMS = ptr->streamTMS;
  698. m_sIn.FPS = ptr->streamFPS;
  699. strncpy( m_sIn.FMT, ptr->recordEXT, 5 );
  700. m_sIn.FMT[5]=(char)0;
  701. m_sIn.WIDTH = ptr->streamWidth;
  702. m_sIn.HEIGHT = ptr->streamHeight;
  703. #ifdef DEBUG_EXTRA
  704. char tB[129];
  705. 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 );
  706. LOG(tB);
  707. #endif
  708. m_pFD = fopen( m_cCurrentFile, "r" );
  709. if( m_bVideoOpened = m_pFD != NULL ) return true;
  710. return false;
  711. };
  712. uint8_t *mojoVideoTransferHelper::getNextChunk( uint32_t req_chunk_size, uint32_t *ack_size ){
  713. uint8_t *retVal = NULL;
  714. if ( m_bVideoOpened ) {
  715. retVal = (uint8_t *) malloc( req_chunk_size + 12 );//+DB_SOURCE_ID_x_64+Size_32
  716. if ( retVal ) {
  717. int32_t rv = fread( retVal+12, 1, req_chunk_size, m_pFD );
  718. if ( rv < 0 ) {
  719. free( retVal );
  720. retVal = NULL;
  721. abort();
  722. } else {
  723. *ack_size = rv+12;
  724. retVal[0] = ( m_nRemoteRefID >> 56 ) & 0xFF;
  725. retVal[1] = ( m_nRemoteRefID >> 48 ) & 0xFF;
  726. retVal[2] = ( m_nRemoteRefID >> 40 ) & 0xFF;
  727. retVal[3] = ( m_nRemoteRefID >> 32 ) & 0xFF;
  728. retVal[4] = ( m_nRemoteRefID >> 24 ) & 0xFF;
  729. retVal[5] = ( m_nRemoteRefID >> 16 ) & 0xFF;
  730. retVal[6] = ( m_nRemoteRefID >> 8 ) & 0xFF;
  731. retVal[7] = m_nRemoteRefID & 0xFF;
  732. retVal[8] = ( rv >> 24 ) & 0xFF;
  733. retVal[9] = ( rv >> 16 ) & 0xFF;
  734. retVal[10] = ( rv >> 8 ) & 0xFF;
  735. retVal[11] = rv & 0xFF;
  736. if ( feof( m_pFD )){
  737. fclose( m_pFD );
  738. m_bVideoOpened = false;
  739. };
  740. };
  741. };
  742. };
  743. return retVal;
  744. };
  745. ARGUS_DATA_VIDEO_INFO *mojoVideoTransferHelper::getVideoInfo(){
  746. return &m_sIn;
  747. };
  748. void mojoVideoTransferHelper::abort(){
  749. };
  750. #undef LOG
  751. #undef LOG_ERROR