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.

1035 lines
42 KiB

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