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.

1034 lines
42 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
​
​
​
​
​
​
​
​
​
​
​
​
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_bCloseCommunicationReason = 0;
  26. //m_pVideoHelper = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
  27. memset(&m_sINFO, 0, sizeof(ARGUS_DATA_MOJO_INFO));
  28. m_qCMD = new RPC_CMD_QUEUE();
  29. };
  30. mojoRemoteClient::~mojoRemoteClient(){
  31. LOG("Destructor");
  32. if( m_bRegisteredWithRemotePeer ) {
  33. LOG("Flushing");
  34. while ( m_bRegisteredWithRemotePeer ) {
  35. m_mCurrentVTRProtector.lock();
  36. if ( m_vVTRHelpers.size() == 0 ){
  37. m_mCurrentVTRProtector.unlock();
  38. break;
  39. };
  40. m_mCurrentVTRProtector.unlock();
  41. SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  42. };
  43. m_mCurrentVTRProtector.lock();
  44. if ( m_vVTRHelpers.size() ) {
  45. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) delete( *idx );
  46. m_vVTRHelpers.clear();
  47. } else {
  48. //Send Disconmnect
  49. enqueueDisconnect( m_bCloseCommunicationReason );
  50. };
  51. m_mCurrentVTRProtector.unlock();
  52. };
  53. LOG("Flushing Done!");
  54. waitThreadEnd( true );
  55. flushTransactions();
  56. m_bShutdownCommunication = true;
  57. m_bCommunicationEstablished = false;
  58. m_bRegisteredWithRemotePeer = false;
  59. LOG("Delete Port");
  60. if ( m_pPort ) delete( m_pPort );
  61. LOG("Delete Ring");
  62. delete( m_qCMD );
  63. LOG("Done");
  64. };
  65. bool mojoRemoteClient::create(){
  66. if( m_pMaster == NULL ) return false;
  67. m_pMaster->fillMojoInfo( &m_sINFO );
  68. if( m_bCommunicationEstablished && ( m_bThreadStatus == TH_WORKING ) ) return true;
  69. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  70. m_pCurrentCMD = NULL;
  71. m_bActiveServer = 0;
  72. m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && ( strlen( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  73. return init();
  74. };
  75. int mojoRemoteClient::packetChecker(U8 *Buffer, size_t BufferSize){
  76. if ( Buffer[0] == ARGUS_START_SERVER_FLAG ) {
  77. if( BufferSize > ARGUS_DATA_MSG_MIN_SIZE ){
  78. uint32_t SIZE = Buffer[5];
  79. SIZE = ( SIZE << 8 ) | Buffer[6];
  80. SIZE = ( SIZE << 8 ) | Buffer[7];
  81. SIZE = ( SIZE << 8 ) | Buffer[8];
  82. if ( ( SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE ) <= BufferSize ) {
  83. uint16_t tCRC = checksum16( Buffer, SIZE + ARGUS_HEADER_SIZE );
  84. uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE];
  85. cCRC = ( cCRC << 8 ) | Buffer[SIZE + ARGUS_HEADER_SIZE+1];
  86. if ( tCRC == cCRC ) return 0;
  87. } else return 1; // to grou
  88. } else return 1; //to grow
  89. };
  90. return -1; // to skip
  91. };
  92. protocolPackage *mojoRemoteClient::packetExtractor(U8 *Buffer, size_t BufferSize){
  93. protocolPackage *tPack = (protocolPackage*) malloc( sizeof(protocolPackage));
  94. if ( tPack ) {
  95. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  96. tPack->m_nSize = Buffer[5];
  97. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[6];
  98. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[7];
  99. tPack->m_nSize = ( tPack->m_nSize << 8 ) | Buffer[8];
  100. tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
  101. tPack->m_pData = (U8 *) malloc( tPack->m_nSize );
  102. if ( tPack->m_pData ) {
  103. memcpy( tPack->m_pData, Buffer, tPack->m_nSize );
  104. } else {
  105. free( tPack );
  106. tPack = NULL;
  107. };
  108. };
  109. return tPack;
  110. };
  111. bool mojoRemoteClient::packetHandler( protocolPackage *Package ){
  112. m_nInbondSerial = Package->m_pData[1];
  113. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[2];
  114. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[3];
  115. m_nInbondSerial = ( m_nInbondSerial << 8 ) | Package->m_pData[4];
  116. #ifdef DEBUG_EXTRA
  117. char bB[129];
  118. sprintf( bB, "Code: %02X", Package->m_nCode );
  119. LOG( bB );
  120. #endif
  121. switch( Package->m_nCode ){
  122. case ARGUS_DATA_MSG_REGISTER:
  123. #ifdef DEBUG
  124. LOG("Reg From Server");
  125. #endif
  126. m_bRegisteredWithRemotePeer = true;
  127. setAutomationStage( ARGUS_RPC_STAGE_SEND_HELLO );
  128. //finishTransaction();
  129. break;
  130. case ARGUS_DATA_MSG_ACCESS_DENIED: m_bRegisteredWithRemotePeer = false; LOG("ACC DENIED");
  131. case ARGUS_DATA_MSG_DISABLED: finishTransaction(); LOG("MSG disabled"); break;
  132. case ARGUS_DATA_MSG_STAT: finishTransaction(); break;
  133. case ARGUS_DATA_MSG_INFO: finishTransaction(); break;
  134. case ARGUS_DATA_MSG_GET_INFO: enqueueInfoMsg(); break;
  135. case ARGUS_DATA_MSG_REQ_TIME_SYNC:{
  136. #ifdef DEBUG_EXTRA
  137. LOG("TSYNC REQ");
  138. #endif
  139. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  140. if ( tCMD ) {
  141. tCMD->CMD = ARGUS_DATA_MSG_TIME_SYNC;
  142. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_TIME_SYNC_REQ );
  143. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  144. if (tCMD->DATA) {
  145. ARGUS_DATA_TIME_SYNC_REQ *tDATA = (ARGUS_DATA_TIME_SYNC_REQ*)tCMD->DATA;
  146. tDATA->STS = getMilliseconds();
  147. if ( peer_send_rpc_pack( tCMD ) ) {
  148. setTO( 3 );
  149. free( tCMD->DATA );
  150. free( tCMD );
  151. break;
  152. };
  153. free( tCMD->DATA );
  154. };
  155. free( tCMD );
  156. };
  157. //Only On failure
  158. finishTransaction();
  159. break;
  160. };
  161. case ARGUS_DATA_MSG_EVT: finishTransaction(); break;
  162. case ARGUS_DATA_MSG_VIDEO_INFO:{
  163. //get DATABASE ID
  164. uint64_t tDBID = Package->m_pData[ARGUS_HEADER_SIZE+1];
  165. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+2];
  166. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+3];
  167. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+4];
  168. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+5];
  169. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+6];
  170. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+7];
  171. tDBID = ( tDBID << 8 ) | Package->m_pData[ARGUS_HEADER_SIZE+8];
  172. #ifdef DEBUG
  173. char tZ[128];
  174. sprintf( tZ, "Video Ack %lld ID", tDBID );
  175. LOG( tZ );
  176. #endif
  177. m_mCurrentVTRProtector.lock();
  178. if ( tDBID ) {
  179. VTR_HELPER *tHELP = m_vVTRHelpers.at(0);
  180. tHELP->ptr->setRemoteReferenceID( tDBID );
  181. tHELP->state = 2;
  182. tHELP->ts = time( NULL );
  183. } else {
  184. //Abort Transaction
  185. m_vVTRHelpers.at(0)->state = 3;
  186. m_mCurrentVTRProtector.unlock();
  187. break;
  188. };
  189. m_mCurrentVTRProtector.unlock();
  190. };
  191. case ARGUS_DATA_MSG_VIDEO_DATA:{
  192. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  193. if ( tCMD ) {
  194. uint32_t tSize;
  195. tCMD->CMD = ARGUS_DATA_MSG_VIDEO_DATA;
  196. m_mCurrentVTRProtector.lock();
  197. if ( m_vVTRHelpers.size() == 0 ) {
  198. tCMD->DATA = NULL;
  199. tCMD->DATA_SIZE = 0;
  200. if( !m_qCMD->push( tCMD ) ){
  201. free( tCMD );
  202. };
  203. m_mCurrentVTRProtector.unlock();
  204. finishTransaction(); //?? Do we need it
  205. break;
  206. };
  207. tCMD->DATA = (void *)m_vVTRHelpers.at(0)->ptr->getNextChunk(ARGUS_RPC_DEFAULT_MEDIA_CHUNK, &tSize);
  208. m_mCurrentVTRProtector.unlock();
  209. tCMD->DATA_SIZE = tSize;
  210. #ifdef DEBUG_EXTRA
  211. char tC[128];
  212. sprintf( tC, "Sending Video Chunk %d bytes", tSize );
  213. LOG( tC );
  214. #endif
  215. if ( tCMD->DATA ) {
  216. /*
  217. //Direct Send to peer data
  218. if ( peer_send_rpc_pack( tCMD ) ) {
  219. setTO( 1 );
  220. free( tCMD->DATA );
  221. free( tCMD );
  222. if ( tSize < ARGUS_RPC_DEFAULT_MEDIA_CHUNK ) {
  223. //Last Chunk
  224. m_mCurrentVTRProtector.lock();
  225. mojoVideoTransferHelper tPtr = m_qVTRHelpers.first();
  226. delte( tPtr );
  227. m_qVTRHelpers.pop();
  228. if ( !m_qVTRHelpers.empty() ) {
  229. };
  230. m_mCurrentVTRProtector.unlock();
  231. };
  232. break;
  233. };
  234. free( tCMD->DATA );
  235. */
  236. // Send Via Queue
  237. if( m_qCMD->push( tCMD ) ){
  238. m_mCurrentVTRProtector.lock();
  239. m_vVTRHelpers.at(0)->ts = time(NULL);
  240. m_mCurrentVTRProtector.unlock();
  241. finishTransaction(); //?? Do we need it
  242. break;
  243. };
  244. free( tCMD->DATA );
  245. } else {
  246. LOG("Finish");
  247. // Finish Send That File
  248. m_mCurrentVTRProtector.lock();
  249. m_vVTRHelpers.at(0)->state = 3;
  250. m_mCurrentVTRProtector.unlock();
  251. };
  252. free( tCMD );
  253. };
  254. //Only On failure
  255. finishTransaction();
  256. break;
  257. };
  258. case ARGUS_DATA_MSG_TIME_SYNC:{
  259. struct timespec tS = { 0 };
  260. uint64_t cT = getMilliseconds();
  261. ARGUS_DATA_TIME_SYNC_ACK *tPTR = (ARGUS_DATA_TIME_SYNC_ACK *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
  262. uint64_t eTime = tPTR->ATS + ( (cT - tPTR->STS) >> 1 );
  263. tS.tv_sec = eTime / 1000;
  264. tS.tv_nsec = ( eTime % 1000 ) * 1000000;
  265. if( clock_settime( CLOCK_REALTIME, &tS ) != 0){
  266. LOG_ERROR("Time Correction Failed!");
  267. switch ( errno ) {
  268. case EACCES: LOG_ERROR("Not have write permission for the dynamic POSIX clock device indicated!"); break;
  269. case EFAULT: LOG_ERROR("tS points outside the accessible address space!"); break;
  270. case EINVAL: LOG_ERROR("Parameter specified is invalid!"); break;
  271. 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;
  272. case ENOTSUP: LOG_ERROR("The operation is not supported by the dynamic POSIX clock device specified!"); break;
  273. case EPERM: LOG_ERROR("clock_settime() does not have permission to set the clock indicated!"); break;
  274. };
  275. };
  276. #ifdef DEBUG_EXTRA
  277. LOG("Time Sync ACK!");
  278. char tB[256];
  279. sprintf( tB,"RT:%lld ST:%lld CT:%lld", tPTR->STS, tPTR->ATS, cT );
  280. LOG( tB );
  281. sprintf( tB,"tv_s:%lld tv_ns:%lld", tS.tv_sec, tS.tv_nsec );
  282. LOG( tB );
  283. #endif
  284. finishTransaction();
  285. break;
  286. };
  287. case ARGUS_DATA_MSG_RPC:{
  288. ARGUS_DATA_RPC *tPTR = (ARGUS_DATA_RPC *)(Package->m_pData+ARGUS_HEADER_SIZE+1);
  289. m_pMaster->m_pDBC->pushMemCMD( tPTR->NODE_ID, tPTR->CMD, tPTR->DATA );
  290. break;
  291. };
  292. };
  293. return true;
  294. };
  295. uint8_t mojoRemoteClient::getAutomationStage( ){
  296. m_mAutomationStageProtector.lock();
  297. uint8_t retVal = m_bAutomationStage;
  298. m_mAutomationStageProtector.unlock();
  299. return retVal;
  300. };
  301. void mojoRemoteClient::setAutomationStage( uint8_t stg ){
  302. m_mAutomationStageProtector.lock();
  303. m_bAutomationStage = stg;
  304. m_mAutomationStageProtector.unlock();
  305. };
  306. void mojoRemoteClient::finishTransaction(){
  307. m_mCurrentCMDProtector.lock();
  308. if ( m_pCurrentCMD ) {
  309. if ( m_pCurrentCMD->DATA ) free( m_pCurrentCMD->DATA );
  310. free( m_pCurrentCMD );
  311. m_pCurrentCMD = NULL;
  312. };
  313. m_mCurrentCMDProtector.unlock();
  314. setAutomationStage( ARGUS_RPC_STAGE_IDLE );
  315. };
  316. bool mojoRemoteClient::validateTransaction(){
  317. if ( !m_bRegisteredWithRemotePeer ) return false;
  318. if ( m_bShutdownCommunication ) return false;
  319. m_mCurrentCMDProtector.lock();
  320. if ( m_pCurrentCMD == NULL ) {
  321. m_mCurrentCMDProtector.unlock();
  322. return true;
  323. };
  324. m_mCurrentCMDProtector.unlock();
  325. return false;
  326. };
  327. bool mojoRemoteClient::pushNewTransaction( RPC_CMD_RECORD *ptr ){
  328. if ( ptr == NULL ) return false;
  329. m_mCurrentCMDProtector.lock();
  330. if ( m_qCMD->push( ptr ) != -1 ){
  331. m_mCurrentCMDProtector.unlock();
  332. return true;
  333. };
  334. m_mCurrentCMDProtector.unlock();
  335. return false;
  336. };
  337. bool mojoRemoteClient::fetchNewTransaction(){
  338. if ( m_bShutdownCommunication ) return false;
  339. m_mCurrentCMDProtector.lock();
  340. if ( m_pCurrentCMD ) {
  341. m_mCurrentCMDProtector.unlock();
  342. return false;
  343. };
  344. m_pCurrentCMD = m_qCMD->pop();
  345. if ( m_pCurrentCMD ) {
  346. m_mCurrentCMDProtector.unlock();
  347. return true;
  348. };
  349. m_mCurrentCMDProtector.unlock();
  350. return false;
  351. };
  352. void mojoRemoteClient::enqueueDisconnect( uint8_t reason ){
  353. static uint8_t pBuffer[400];
  354. if( !m_bCommunicationEstablished ) return;
  355. pBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_DISCONNECT;
  356. pBuffer[ARGUS_HEADER_SIZE+1] = reason;
  357. m_nOutbondSerial++;
  358. m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 2 ) );
  359. };
  360. void mojoRemoteClient::enqueueInfoMsg(){
  361. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  362. if ( tCMD ) {
  363. tCMD->CMD = ARGUS_DATA_MSG_INFO;
  364. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  365. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  366. if (tCMD->DATA) {
  367. memcpy( tCMD->DATA, &m_sINFO, sizeof( ARGUS_DATA_MOJO_INFO ) );
  368. LOG("ARGUS_DATA_MOJO_INFO");
  369. if ( pushNewTransaction( tCMD ) ) return;
  370. free( tCMD->DATA );
  371. };
  372. free( tCMD );
  373. };
  374. };
  375. #include "../common/deviceNodeCamera.h"
  376. #include "../common/deviceNodeSensor.h"
  377. #include "../common/deviceNodeFactory.h"
  378. void mojoRemoteClient::enqueueStatMsg(){
  379. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  380. if ( tCMD ) {
  381. tCMD->CMD = ARGUS_DATA_MSG_STAT;
  382. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_STATUS );
  383. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  384. if( tCMD->DATA ){
  385. memset( tCMD->DATA, 0, tCMD->DATA_SIZE );
  386. ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA;
  387. DBC_SYS_LOAD_INFO cSYSSTAT;
  388. m_pMaster->getSystemUtilization( &cSYSSTAT );
  389. cSTATUS->CPU_LOAD = round( cSYSSTAT.cpu15 );
  390. cSTATUS->CPU_TEMP = round( cSYSSTAT.temperature );
  391. //if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0;
  392. //if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0;
  393. cSTATUS->DISK_UTI = cSYSSTAT.disk;
  394. cSTATUS->MEMO_UTI = cSYSSTAT.mem;
  395. deviceNode **tA = NULL;
  396. uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ALS_SENSOR, tA );
  397. if( !tCNT ){
  398. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_LFS_SENSOR, tA );
  399. if( !tCNT ){
  400. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_200_SENSOR, tA );
  401. if( !tCNT ){
  402. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_360_SENSOR, tA );
  403. if( !tCNT ){
  404. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_3D_SENSOR, tA );
  405. if( !tCNT ){
  406. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_NET_PLS_200, tA );
  407. };
  408. };
  409. };
  410. };
  411. };
  412. if( tCNT ){
  413. for ( int i = 0; i < tCNT; i++) {
  414. deviceNodeSensor *tSENS = (deviceNodeSensor*) tA[i];
  415. if (tSENS) {
  416. uint16_t ttt = tSENS->getSensStatus();
  417. if ( ttt > 2 ) {
  418. ttt = 1;
  419. } else {
  420. ttt = 2;
  421. };
  422. cSTATUS->STATUSES = cSTATUS->STATUSES | ( ttt <<(i*3) );
  423. } else LOG("No Sensor PTR!!");
  424. };
  425. free( tA );
  426. tA = NULL;
  427. #ifdef DEBUG_EXTRA
  428. } else {
  429. LOG("No Sensors Defined/Enabled In the system!");
  430. #endif
  431. };
  432. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_STATIC_CAMERA, tA );
  433. if( !tCNT ){
  434. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PTZ_CAMERA, tA );
  435. if( !tCNT ){
  436. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_STATIC_CAMERA, tA );
  437. if( !tCNT ){
  438. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
  439. if( !tCNT ){
  440. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PRESET_CAMERA, tA );
  441. if( !tCNT ){
  442. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PRESET_CAMERA, tA );
  443. };
  444. };
  445. };
  446. };
  447. };
  448. if( tCNT ){
  449. for ( int i = 0; i < tCNT; i++) {
  450. deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[i];
  451. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getCamStatus() > 0)?(ARGUS_RPC_STATUS_CAM_OK<<(i*3)):0);
  452. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getStreamStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+1)));
  453. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getPtzStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+2)));
  454. };
  455. free( tA );
  456. #ifdef DEBUG_EXTRA
  457. } else {
  458. LOG("No Camera Defined/Enabled In the system!");
  459. #endif
  460. };
  461. #ifdef DEBUG_EXTRA
  462. char tB[128];
  463. sprintf( tB, "Final Status: 0x%04X", cSTATUS->STATUSES );
  464. LOG( tB );
  465. #endif
  466. if ( pushNewTransaction( tCMD ) ) return;
  467. free( tCMD->DATA );
  468. };
  469. free( tCMD );
  470. };
  471. };
  472. void mojoRemoteClient::enqueueTimeReq(){
  473. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  474. if ( tCMD ) {
  475. tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC;
  476. tCMD->DATA_SIZE = 0;
  477. tCMD->DATA = NULL;
  478. if ( pushNewTransaction( tCMD ) ) return;
  479. free( tCMD );
  480. };
  481. };
  482. void mojoRemoteClient::flushTransactions(){
  483. m_mCurrentCMDProtector.lock();
  484. if ( m_pCurrentCMD ) {
  485. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  486. free( m_pCurrentCMD );
  487. };
  488. while( m_pCurrentCMD = m_qCMD->pop() ){
  489. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  490. free( m_pCurrentCMD );
  491. };
  492. m_pCurrentCMD = NULL;
  493. m_mCurrentCMDProtector.unlock();
  494. };
  495. void mojoRemoteClient::setTO( uint8_t seconds ){
  496. m_mTOProtoctor.lock();
  497. m_bTOCounter = seconds;
  498. m_mTOProtoctor.unlock();
  499. };
  500. bool mojoRemoteClient::decTO(){
  501. m_mTOProtoctor.lock();
  502. if ( m_bTOCounter ) {
  503. m_bTOCounter--;
  504. m_mTOProtoctor.unlock();
  505. return true;
  506. };
  507. m_mTOProtoctor.unlock();
  508. return false;
  509. };
  510. void mojoRemoteClient::m_fThreadProc( void ){
  511. time_t tTimeTSeconds = 0;
  512. time_t cTimeTSeconds = 0;
  513. uint32_t tSecondsCounter = 0;
  514. m_bThreadStatus = TH_WORKING;
  515. #ifdef DEBUG
  516. LOG("Connector Thread Started");
  517. #endif
  518. while ( m_bThreadStatus < TH_ENDED ) {
  519. // seconds Counter (for internal purposes)
  520. cTimeTSeconds = time( NULL );
  521. //Process each second
  522. if ( cTimeTSeconds != tTimeTSeconds ) {
  523. tSecondsCounter++;
  524. tTimeTSeconds = cTimeTSeconds;
  525. switch ( getAutomationStage() ) {
  526. case ARGUS_RPC_STAGE_CONNECT:{
  527. flushTransactions();
  528. if ( m_bUseBackupServer ) {
  529. if ( m_bActiveServer == 0 ) {
  530. #ifdef DEBUG
  531. char tB[128];
  532. sprintf(tB,"Creating Remote connection to Main Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  533. LOG( tB );
  534. #endif
  535. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  536. } else {
  537. #ifdef DEBUG
  538. char tB[128];
  539. sprintf(tB,"Creating Remote connection to Backup Server [%s:%d]", m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  540. LOG( tB );
  541. #endif
  542. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  543. }
  544. if( m_pPort ) {
  545. if( m_pPort->create() ) {
  546. #ifdef DEBUG
  547. LOG("Connection Sucessfull");
  548. #endif
  549. m_bShutdownCommunication = false;
  550. m_bCommunicationEstablished = true;
  551. m_bRegisteredWithRemotePeer = false;
  552. setTransport( m_pPort, false );
  553. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  554. } else {
  555. LOG_ERROR("Connection to Remote Server Failed!");
  556. setConnectionStatus( CS_DISCONNECTED );
  557. setTransport( NULL, false );
  558. delete( m_pPort );
  559. m_pPort = NULL;
  560. m_bShutdownCommunication = true;
  561. m_bCommunicationEstablished = false;
  562. m_bRegisteredWithRemotePeer = false;
  563. m_bActiveServer = 1 - m_bActiveServer;
  564. setTO( 30 ); //30 sec to wait
  565. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  566. };
  567. };
  568. } else {
  569. if (m_pMaster->m_sSiteData.remote_port) {
  570. #ifdef DEBUG
  571. char tB[128];
  572. sprintf(tB,"Creating Remote connection to Alone Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  573. LOG( tB );
  574. #endif
  575. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  576. if( m_pPort ) {
  577. if( m_pPort->create() ) {
  578. #ifdef DEBUG
  579. LOG("Connection Sucessfull");
  580. #endif
  581. m_bShutdownCommunication = false;
  582. m_bCommunicationEstablished = true;
  583. m_bRegisteredWithRemotePeer = false;
  584. setTransport( m_pPort, false );
  585. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  586. } else {
  587. LOG_ERROR("Connection to Remote Server Failed!");
  588. setConnectionStatus( CS_DISCONNECTED );
  589. setTransport( NULL, false );
  590. delete( m_pPort );
  591. m_pPort = NULL;
  592. m_bShutdownCommunication = true;
  593. m_bCommunicationEstablished = false;
  594. m_bRegisteredWithRemotePeer = false;
  595. setTO( 30 ); //30 sec to wait
  596. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  597. };
  598. };
  599. };
  600. };
  601. break;
  602. };
  603. case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
  604. #ifdef DEBUG_EXTRA
  605. LOG("Wait Reconnect!");
  606. #endif
  607. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT );
  608. break;
  609. };
  610. case ARGUS_RPC_STAGE_REGISTER:{
  611. #ifdef DEBUG
  612. LOG("Stage Register!");
  613. #endif
  614. CSTR tIP = m_pPort->getLocalIp4();
  615. if( tIP ) {
  616. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  617. if ( m_pCurrentCMD ) {
  618. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  619. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_REGISTRATION );//strlen( m_pMaster->m_sIdentity );
  620. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  621. if ( m_pCurrentCMD->DATA ) {
  622. memset( m_pCurrentCMD->DATA, 0, m_pCurrentCMD->DATA_SIZE );
  623. strncpy( ((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->identity, m_pMaster->m_sIdentity, 32 );
  624. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->peer, tIP, 22);
  625. if( peer_send_rpc_pack( m_pCurrentCMD ) ){
  626. setTO( 3 ); // Wait for 3 seconds to be registered with server
  627. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  628. free( tIP );
  629. break;
  630. };
  631. free( m_pCurrentCMD->DATA );
  632. };
  633. free( m_pCurrentCMD );
  634. };
  635. free( tIP );
  636. };
  637. m_pCurrentCMD = NULL;
  638. break;
  639. };
  640. case ARGUS_RPC_STAGE_SEND_HELLO:{
  641. #ifdef DEBUG
  642. LOG("Stage HELLO!");
  643. #endif
  644. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  645. if ( m_pCurrentCMD ) {
  646. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  647. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  648. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  649. if (m_pCurrentCMD->DATA) {
  650. LOG("Send Client Info!");
  651. m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  652. LOG("ARGUS_DATA_MOJO_INFO");
  653. if ( peer_send_rpc_pack( m_pCurrentCMD ) ){
  654. setTO( 1 ); // Wait for 3 seconds
  655. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  656. };
  657. free( m_pCurrentCMD->DATA );
  658. };
  659. free( m_pCurrentCMD );
  660. };
  661. m_pCurrentCMD = NULL;
  662. break;
  663. };
  664. case ARGUS_RPC_STAGE_WAIT_ACK:{
  665. //LOG("Wait ACK!");
  666. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  667. break;
  668. };
  669. case ARGUS_RPC_STAGE_IDLE: {
  670. if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) enqueueTimeReq(); else enqueueStatMsg(); break;
  671. };
  672. case ARGUS_RPC_STAGE_DISCONNECT:{
  673. LOG("Stage Disconnect!");
  674. // Gracefully Disconnect
  675. //if( peer_send_disconnect() ){
  676. // bTO_COUNTER = 1;
  677. // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  678. //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  679. //break; ---> Go to reconnect to other server
  680. };
  681. case ARGUS_RPC_STAGE_CLOSE:{
  682. // Just Disconnect
  683. LOG("Stage Close!");
  684. flushTransactions();
  685. setTransport( NULL, false );
  686. delete( m_pPort );
  687. m_pPort = NULL;
  688. m_bShutdownCommunication = true;
  689. m_bCommunicationEstablished = false;
  690. m_bRegisteredWithRemotePeer = false;
  691. setTO( 30 ); //30 sec to wait
  692. m_bActiveServer = 1 - m_bActiveServer;
  693. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  694. break;
  695. };
  696. };
  697. m_mCurrentVTRProtector.lock();
  698. if ( m_vVTRHelpers.size() ) {
  699. //Handle New Video Transfer
  700. LOG("CHK HLPR");
  701. if( m_vVTRHelpers.at(0)->state == 0 ){
  702. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  703. if ( tPack ) {
  704. tPack->DATA_SIZE = sizeof( ARGUS_DATA_VIDEO_INFO );
  705. tPack->DATA = malloc( tPack->DATA_SIZE );
  706. if ( tPack->DATA ) {
  707. memcpy( tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE );
  708. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  709. if( m_qCMD->push( tPack ) ){
  710. m_vVTRHelpers.at(0)->state = 1;
  711. m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
  712. } else {
  713. free( tPack->DATA );
  714. free( tPack );
  715. }
  716. } else {
  717. free( tPack );
  718. };
  719. };
  720. };
  721. /// Validate Stalled/ Finished Videos (Try To Send next time
  722. for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) {
  723. if ( (*idx)->state == 2 ) {
  724. if ((cTimeTSeconds - (*idx)->ts) > 5) {
  725. // The transfer is staled
  726. (*idx)->state = 0;
  727. }
  728. };
  729. if ( (*idx)->state == 3 ){
  730. LOG("Del HLPR");
  731. //Finished Transfer
  732. delete( (*idx)->ptr );
  733. free( (*idx) );
  734. m_vVTRHelpers.erase(idx);
  735. LOG("Erase HLPR done");
  736. break; // must revalidate Beg-End conditions for FOR loop
  737. };
  738. };
  739. };
  740. m_mCurrentVTRProtector.unlock();
  741. };
  742. if (validateTransaction()) {
  743. if ( fetchNewTransaction() ) {
  744. if( peer_send_rpc_pack( m_pCurrentCMD ) ) {
  745. setTO( 1 );
  746. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  747. } else LOG_ERROR("Failure!");
  748. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  749. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  750. };
  751. LOG("Connector Thread Ended!");
  752. };
  753. /*
  754. Register With Server and make Time Sync With Server
  755. */
  756. bool mojoRemoteClient::peer_send_rpc_pack( RPC_CMD_RECORD *ptr ){
  757. bool retVal;
  758. static uint8_t pBuffer[4194304]; //4Mb
  759. if ( ptr == NULL ) return false;
  760. if( !m_bCommunicationEstablished ) return false;
  761. //if( !m_bRegisteredWithRemotePeer ) return false;
  762. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  763. //uint16_t i = 0;
  764. //for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  765. memcpy( pBuffer+ARGUS_HEADER_SIZE+1, ptr->DATA, ptr->DATA_SIZE );
  766. m_nOutbondSerial++;
  767. //retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  768. retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+ptr->DATA_SIZE));
  769. return retVal;
  770. };
  771. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  772. if( ptr == NULL ) return false;
  773. if( !m_bCommunicationEstablished ) return false;
  774. if( !m_bRegisteredWithRemotePeer ) return false;
  775. uint8_t tBuffer[1024];
  776. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  777. uint16_t i = 0;
  778. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  779. m_nOutbondSerial++;
  780. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  781. };
  782. */
  783. bool mojoRemoteClient::validate(){
  784. if( m_bCommunicationEstablished ){
  785. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  786. m_bCommunicationEstablished = false;
  787. if( m_pPort ) {
  788. // Cancel all ongoing Queries
  789. m_bShutdownCommunication = true;
  790. setTransport( NULL, false );
  791. delete( m_pPort );
  792. };
  793. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  794. // Revalidate Data!!!
  795. //m_bActiveServer = 0;
  796. //m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  797. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  798. if( m_pPort ) {
  799. if( m_pPort->create() ) {
  800. m_bShutdownCommunication = false;
  801. m_bCommunicationEstablished = true;
  802. m_bRegisteredWithRemotePeer = false;
  803. setTransport( m_pPort, false );
  804. } else {
  805. delete( m_pPort );
  806. m_pPort = NULL;
  807. };
  808. };
  809. */
  810. };
  811. #ifdef DEBUG_EXTRA
  812. } else {
  813. LOG("Not Connected!");
  814. #endif
  815. /*
  816. if( m_pPort ) {
  817. // Cancel all ongoing Queries
  818. m_bShutdownCommunication = true;
  819. setTransport( NULL, false );
  820. delete( m_pPort );
  821. };
  822. //Try To connect
  823. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  824. if( m_pPort ) {
  825. if( m_pPort->create() ) {
  826. m_bShutdownCommunication = false;
  827. m_bCommunicationEstablished = true;
  828. m_bRegisteredWithRemotePeer = false;
  829. setTransport( m_pPort, false );
  830. } else {
  831. delete( m_pPort );
  832. m_pPort = NULL;
  833. };
  834. };
  835. */
  836. };
  837. return true;
  838. };
  839. VTR_HELPER *mojoRemoteClient::getHelperByDBID( uint64_t dbid ){
  840. m_mCurrentVTRProtector.lock();
  841. if( m_vVTRHelpers.size() ){
  842. for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) {
  843. if ( (*idx)->ptr->getRemoteReferenceID() == dbid ) {
  844. m_mCurrentVTRProtector.unlock();
  845. return (*idx);
  846. };
  847. };
  848. };
  849. m_mCurrentVTRProtector.unlock();
  850. return NULL;
  851. };
  852. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  853. #ifdef DEBUG_EXTRA
  854. LOG("Add Evt!");
  855. #endif
  856. if ( Evt == NULL ) return false;
  857. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  858. if ( tPack ) {
  859. #ifdef DEBUG_EXTRA
  860. char tB[128];
  861. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000 );
  862. LOG( tB );
  863. #endif
  864. tPack->DATA = (void*) argus_rpc_convert_evt( Evt );
  865. if ( tPack->DATA ) {
  866. tPack->CMD = ARGUS_DATA_MSG_EVT;
  867. tPack->DATA_SIZE = sizeof( ARGUS_DATA_EVENT );
  868. if( m_qCMD->push( tPack ) ) return true;
  869. free( tPack->DATA );
  870. };
  871. free( tPack );
  872. };
  873. return false;
  874. };
  875. bool mojoRemoteClient::addVideo( VIDEO_SEGMENT_INFO *ptr ){
  876. #ifdef DEBUG_EXTRA
  877. LOG("Add Video!");
  878. #endif
  879. if ( ptr == NULL ) return false;
  880. VTR_HELPER *tHLPR = (VTR_HELPER*) malloc( sizeof(VTR_HELPER));
  881. if ( tHLPR ) {
  882. tHLPR->state = 0;
  883. tHLPR->ptr = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
  884. LOG("NW HLPR");
  885. if ( tHLPR->ptr ) {
  886. LOG("HLPR OK");
  887. if ( tHLPR->ptr->init( ptr )) {
  888. LOG("HLPR INIT OK");
  889. m_mCurrentVTRProtector.lock();
  890. m_vVTRHelpers.push_back( tHLPR );
  891. m_mCurrentVTRProtector.unlock();
  892. LOG("HLPR PUSH OK");
  893. return true;
  894. };
  895. delete( tHLPR->ptr );
  896. };
  897. free (tHLPR);
  898. };
  899. return false;
  900. };
  901. mojoVideoTransferHelper::mojoVideoTransferHelper( uint32_t buffer_size ){
  902. m_pLog = basicLogCollection::getInstance();
  903. m_bVideoExists = false;
  904. m_bVideoOpened = false;
  905. m_nBufferSize = buffer_size;
  906. m_pBuffer = (uint8_t *) malloc( m_nBufferSize );
  907. m_nCTSMS = 0;
  908. m_nRemoteRefID = 0;
  909. m_pFD = NULL;
  910. memset( m_cCurrentFile, 0, MAX_PATH );
  911. };
  912. mojoVideoTransferHelper::~mojoVideoTransferHelper(){
  913. LOG("HDESTRC");
  914. abort();
  915. LOG("HDESTRC DONE");
  916. };
  917. #include <sys/stat.h>
  918. bool mojoVideoTransferHelper::init( VIDEO_SEGMENT_INFO *ptr ){
  919. if ( m_pBuffer == NULL ) return false;
  920. if ( m_bVideoExists ) {
  921. if ( m_bVideoOpened ) fclose( m_pFD );
  922. unlink(m_cCurrentFile);
  923. };
  924. if( !( m_bVideoExists = fileExist(ptr->scratchPath) ) ) return false;
  925. m_nRemoteRefID = 0;
  926. strncpy( m_cCurrentFile, ptr->scratchPath, MAX_PATH );
  927. m_cCurrentFile[MAX_PATH-1] = (char)0;
  928. struct stat st;
  929. stat( m_cCurrentFile, &st );
  930. m_sIn.DATA_SIZE = st.st_size;
  931. m_sIn.DTS = ptr->streamTSS;
  932. m_sIn.DMS = ptr->streamTMS;
  933. m_sIn.FPS = ptr->streamFPS;
  934. m_sIn.REF_ID = m_nRemoteRefID;
  935. strncpy( m_sIn.FMT, ptr->recordEXT, 5 );
  936. m_sIn.FMT[5]=(char)0;
  937. m_sIn.WIDTH = ptr->streamWidth;
  938. m_sIn.HEIGHT = ptr->streamHeight;
  939. #ifdef DEBUG_EXTRA
  940. char tB[129];
  941. 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 );
  942. LOG(tB);
  943. #endif
  944. m_pFD = fopen( m_cCurrentFile, "r" );
  945. if( m_bVideoOpened = m_pFD != NULL ) return true;
  946. return false;
  947. };
  948. uint8_t *mojoVideoTransferHelper::getNextChunk( uint32_t req_chunk_size, uint32_t *ack_size ){
  949. uint8_t *retVal = NULL;
  950. if ( m_bVideoOpened ) {
  951. retVal = (uint8_t *) malloc( req_chunk_size + 12 );//+DB_SOURCE_ID_x_64+Size_32
  952. if ( retVal ) {
  953. int32_t rv = fread( retVal+12, 1, req_chunk_size, m_pFD );
  954. if ( rv < 0 ) {
  955. free( retVal );
  956. retVal = NULL;
  957. abort();
  958. } else {
  959. *ack_size = rv+12;
  960. retVal[0] = ( m_nRemoteRefID >> 56 ) & 0xFF;
  961. retVal[1] = ( m_nRemoteRefID >> 48 ) & 0xFF;
  962. retVal[2] = ( m_nRemoteRefID >> 40 ) & 0xFF;
  963. retVal[3] = ( m_nRemoteRefID >> 32 ) & 0xFF;
  964. retVal[4] = ( m_nRemoteRefID >> 24 ) & 0xFF;
  965. retVal[5] = ( m_nRemoteRefID >> 16 ) & 0xFF;
  966. retVal[6] = ( m_nRemoteRefID >> 8 ) & 0xFF;
  967. retVal[7] = m_nRemoteRefID & 0xFF;
  968. retVal[8] = ( rv >> 24 ) & 0xFF;
  969. retVal[9] = ( rv >> 16 ) & 0xFF;
  970. retVal[10] = ( rv >> 8 ) & 0xFF;
  971. retVal[11] = rv & 0xFF;
  972. if ( feof( m_pFD )){
  973. fclose( m_pFD );
  974. m_bVideoOpened = false;
  975. };
  976. };
  977. };
  978. };
  979. return retVal;
  980. };
  981. ARGUS_DATA_VIDEO_INFO *mojoVideoTransferHelper::getVideoInfo(){
  982. return &m_sIn;
  983. };
  984. void mojoVideoTransferHelper::abort(){
  985. };
  986. #undef LOG
  987. #undef LOG_ERROR