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.

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