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.

1043 lines
43 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
​
​
​
​
​
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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
12 months 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
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. case ARGUS_DATA_MSG_FREEZE_BUBBLES:{
  294. // Freeze Current Active Bubbles
  295. break;
  296. };
  297. case ARGUS_DATA_MSG_CLEAN_BUBBLES:{
  298. // Clear All Frozen Bubbles!
  299. break;
  300. };
  301. };
  302. return true;
  303. };
  304. uint8_t mojoRemoteClient::getAutomationStage( ){
  305. m_mAutomationStageProtector.lock();
  306. uint8_t retVal = m_bAutomationStage;
  307. m_mAutomationStageProtector.unlock();
  308. return retVal;
  309. };
  310. void mojoRemoteClient::setAutomationStage( uint8_t stg ){
  311. m_mAutomationStageProtector.lock();
  312. m_bAutomationStage = stg;
  313. m_mAutomationStageProtector.unlock();
  314. };
  315. void mojoRemoteClient::finishTransaction(){
  316. m_mCurrentCMDProtector.lock();
  317. if ( m_pCurrentCMD ) {
  318. if ( m_pCurrentCMD->DATA ) free( m_pCurrentCMD->DATA );
  319. free( m_pCurrentCMD );
  320. m_pCurrentCMD = NULL;
  321. };
  322. m_mCurrentCMDProtector.unlock();
  323. setAutomationStage( ARGUS_RPC_STAGE_IDLE );
  324. };
  325. bool mojoRemoteClient::validateTransaction(){
  326. if ( !m_bRegisteredWithRemotePeer ) return false;
  327. if ( m_bShutdownCommunication ) return false;
  328. m_mCurrentCMDProtector.lock();
  329. if ( m_pCurrentCMD == NULL ) {
  330. m_mCurrentCMDProtector.unlock();
  331. return true;
  332. };
  333. m_mCurrentCMDProtector.unlock();
  334. return false;
  335. };
  336. bool mojoRemoteClient::pushNewTransaction( RPC_CMD_RECORD *ptr ){
  337. if ( ptr == NULL ) return false;
  338. m_mCurrentCMDProtector.lock();
  339. if ( m_qCMD->push( ptr ) != -1 ){
  340. m_mCurrentCMDProtector.unlock();
  341. return true;
  342. };
  343. m_mCurrentCMDProtector.unlock();
  344. return false;
  345. };
  346. bool mojoRemoteClient::fetchNewTransaction(){
  347. if ( m_bShutdownCommunication ) return false;
  348. m_mCurrentCMDProtector.lock();
  349. if ( m_pCurrentCMD ) {
  350. m_mCurrentCMDProtector.unlock();
  351. return false;
  352. };
  353. m_pCurrentCMD = m_qCMD->pop();
  354. if ( m_pCurrentCMD ) {
  355. m_mCurrentCMDProtector.unlock();
  356. return true;
  357. };
  358. m_mCurrentCMDProtector.unlock();
  359. return false;
  360. };
  361. void mojoRemoteClient::enqueueDisconnect( uint8_t reason ){
  362. static uint8_t pBuffer[400];
  363. if( !m_bCommunicationEstablished ) return;
  364. pBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_DISCONNECT;
  365. pBuffer[ARGUS_HEADER_SIZE+1] = reason;
  366. m_nOutbondSerial++;
  367. m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 2 ) );
  368. };
  369. void mojoRemoteClient::enqueueInfoMsg(){
  370. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  371. if ( tCMD ) {
  372. tCMD->CMD = ARGUS_DATA_MSG_INFO;
  373. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  374. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  375. if (tCMD->DATA) {
  376. memcpy( tCMD->DATA, &m_sINFO, sizeof( ARGUS_DATA_MOJO_INFO ) );
  377. LOG("ARGUS_DATA_MOJO_INFO");
  378. if ( pushNewTransaction( tCMD ) ) return;
  379. free( tCMD->DATA );
  380. };
  381. free( tCMD );
  382. };
  383. };
  384. #include "../common/deviceNodeCamera.h"
  385. #include "../common/deviceNodeSensor.h"
  386. #include "../common/deviceNodeFactory.h"
  387. void mojoRemoteClient::enqueueStatMsg(){
  388. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  389. if ( tCMD ) {
  390. tCMD->CMD = ARGUS_DATA_MSG_STAT;
  391. tCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_STATUS );
  392. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  393. if( tCMD->DATA ){
  394. memset( tCMD->DATA, 0, tCMD->DATA_SIZE );
  395. ARGUS_DATA_MOJO_STATUS *cSTATUS = (ARGUS_DATA_MOJO_STATUS*)tCMD->DATA;
  396. DBC_SYS_LOAD_INFO cSYSSTAT;
  397. m_pMaster->getSystemUtilization( &cSYSSTAT );
  398. cSTATUS->CPU_LOAD = round( cSYSSTAT.cpu15 );
  399. cSTATUS->CPU_TEMP = round( cSYSSTAT.temperature );
  400. //if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0;
  401. //if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0;
  402. cSTATUS->DISK_UTI = cSYSSTAT.disk;
  403. cSTATUS->MEMO_UTI = cSYSSTAT.mem;
  404. deviceNode **tA = NULL;
  405. uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ALS_SENSOR, tA );
  406. if( !tCNT ){
  407. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_LFS_SENSOR, tA );
  408. if( !tCNT ){
  409. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_200_SENSOR, tA );
  410. if( !tCNT ){
  411. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_PLS_360_SENSOR, tA );
  412. if( !tCNT ){
  413. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_3D_SENSOR, tA );
  414. if( !tCNT ){
  415. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_NET_PLS_200, tA );
  416. };
  417. };
  418. };
  419. };
  420. };
  421. if( tCNT ){
  422. for ( int i = 0; i < tCNT; i++) {
  423. deviceNodeSensor *tSENS = (deviceNodeSensor*) tA[i];
  424. if (tSENS) {
  425. uint16_t ttt = tSENS->getSensStatus();
  426. if ( ttt > 2 ) {
  427. ttt = 1;
  428. } else {
  429. ttt = 2;
  430. };
  431. cSTATUS->STATUSES = cSTATUS->STATUSES | ( ttt <<(i*3) );
  432. } else LOG("No Sensor PTR!!");
  433. };
  434. free( tA );
  435. tA = NULL;
  436. #ifdef DEBUG_EXTRA
  437. } else {
  438. LOG("No Sensors Defined/Enabled In the system!");
  439. #endif
  440. };
  441. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_STATIC_CAMERA, tA );
  442. if( !tCNT ){
  443. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PTZ_CAMERA, tA );
  444. if( !tCNT ){
  445. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_STATIC_CAMERA, tA );
  446. if( !tCNT ){
  447. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PTZ_CAMERA, tA );
  448. if( !tCNT ){
  449. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_IP_PRESET_CAMERA, tA );
  450. if( !tCNT ){
  451. tCNT = deviceNodeFactory::getInstance()->fetchNodes( ARGUS_ANALOG_PRESET_CAMERA, tA );
  452. };
  453. };
  454. };
  455. };
  456. };
  457. if( tCNT ){
  458. for ( int i = 0; i < tCNT; i++) {
  459. deviceNodeCamera *tCAM = (deviceNodeCamera*) tA[i];
  460. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getCamStatus() > 0)?(ARGUS_RPC_STATUS_CAM_OK<<(i*3)):0);
  461. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getStreamStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+1)));
  462. cSTATUS->STATUSES = cSTATUS->STATUSES | ( (tCAM->getPtzStatus() > 0)?0:(ARGUS_RPC_STATUS_CAM_OK<<(i*3+2)));
  463. };
  464. free( tA );
  465. #ifdef DEBUG_EXTRA
  466. } else {
  467. LOG("No Camera Defined/Enabled In the system!");
  468. #endif
  469. };
  470. #ifdef DEBUG_EXTRA
  471. char tB[128];
  472. sprintf( tB, "Final Status: 0x%04X", cSTATUS->STATUSES );
  473. LOG( tB );
  474. #endif
  475. if ( pushNewTransaction( tCMD ) ) return;
  476. free( tCMD->DATA );
  477. };
  478. free( tCMD );
  479. };
  480. };
  481. void mojoRemoteClient::enqueueTimeReq(){
  482. RPC_CMD_RECORD *tCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  483. if ( tCMD ) {
  484. tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC;
  485. tCMD->DATA_SIZE = 0;
  486. tCMD->DATA = NULL;
  487. if ( pushNewTransaction( tCMD ) ) return;
  488. free( tCMD );
  489. };
  490. };
  491. void mojoRemoteClient::flushTransactions(){
  492. m_mCurrentCMDProtector.lock();
  493. if ( m_pCurrentCMD ) {
  494. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  495. free( m_pCurrentCMD );
  496. };
  497. while( m_pCurrentCMD = m_qCMD->pop() ){
  498. if (m_pCurrentCMD->DATA) free( m_pCurrentCMD->DATA );
  499. free( m_pCurrentCMD );
  500. };
  501. m_pCurrentCMD = NULL;
  502. m_mCurrentCMDProtector.unlock();
  503. };
  504. void mojoRemoteClient::setTO( uint8_t seconds ){
  505. m_mTOProtoctor.lock();
  506. m_bTOCounter = seconds;
  507. m_mTOProtoctor.unlock();
  508. };
  509. bool mojoRemoteClient::decTO(){
  510. m_mTOProtoctor.lock();
  511. if ( m_bTOCounter ) {
  512. m_bTOCounter--;
  513. m_mTOProtoctor.unlock();
  514. return true;
  515. };
  516. m_mTOProtoctor.unlock();
  517. return false;
  518. };
  519. void mojoRemoteClient::m_fThreadProc( void ){
  520. time_t tTimeTSeconds = 0;
  521. time_t cTimeTSeconds = 0;
  522. uint32_t tSecondsCounter = 0;
  523. m_bThreadStatus = TH_WORKING;
  524. #ifdef DEBUG
  525. LOG("Connector Thread Started");
  526. #endif
  527. while ( m_bThreadStatus < TH_ENDED ) {
  528. // seconds Counter (for internal purposes)
  529. cTimeTSeconds = time( NULL );
  530. //Process each second
  531. if ( cTimeTSeconds != tTimeTSeconds ) {
  532. tSecondsCounter++;
  533. tTimeTSeconds = cTimeTSeconds;
  534. switch ( getAutomationStage() ) {
  535. case ARGUS_RPC_STAGE_CONNECT:{
  536. flushTransactions();
  537. if ( m_bUseBackupServer ) {
  538. if ( m_bActiveServer == 0 ) {
  539. #ifdef DEBUG
  540. char tB[128];
  541. sprintf(tB,"Creating Remote connection to Main Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  542. LOG( tB );
  543. #endif
  544. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  545. } else {
  546. #ifdef DEBUG
  547. char tB[128];
  548. sprintf(tB,"Creating Remote connection to Backup Server [%s:%d]", m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  549. LOG( tB );
  550. #endif
  551. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port );
  552. }
  553. if( m_pPort ) {
  554. if( m_pPort->create() ) {
  555. #ifdef DEBUG
  556. LOG("Connection Sucessfull");
  557. #endif
  558. m_bShutdownCommunication = false;
  559. m_bCommunicationEstablished = true;
  560. m_bRegisteredWithRemotePeer = false;
  561. setTransport( m_pPort, false );
  562. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  563. } else {
  564. LOG_ERROR("Connection to Remote Server Failed!");
  565. setConnectionStatus( CS_DISCONNECTED );
  566. setTransport( NULL, false );
  567. delete( m_pPort );
  568. m_pPort = NULL;
  569. m_bShutdownCommunication = true;
  570. m_bCommunicationEstablished = false;
  571. m_bRegisteredWithRemotePeer = false;
  572. m_bActiveServer = 1 - m_bActiveServer;
  573. setTO( 30 ); //30 sec to wait
  574. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  575. };
  576. };
  577. } else {
  578. if (m_pMaster->m_sSiteData.remote_port) {
  579. #ifdef DEBUG
  580. char tB[128];
  581. sprintf(tB,"Creating Remote connection to Alone Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  582. LOG( tB );
  583. #endif
  584. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  585. if( m_pPort ) {
  586. if( m_pPort->create() ) {
  587. #ifdef DEBUG
  588. LOG("Connection Sucessfull");
  589. #endif
  590. m_bShutdownCommunication = false;
  591. m_bCommunicationEstablished = true;
  592. m_bRegisteredWithRemotePeer = false;
  593. setTransport( m_pPort, false );
  594. setAutomationStage( ARGUS_RPC_STAGE_REGISTER );
  595. } else {
  596. LOG_ERROR("Connection to Remote Server Failed!");
  597. setConnectionStatus( CS_DISCONNECTED );
  598. setTransport( NULL, false );
  599. delete( m_pPort );
  600. m_pPort = NULL;
  601. m_bShutdownCommunication = true;
  602. m_bCommunicationEstablished = false;
  603. m_bRegisteredWithRemotePeer = false;
  604. setTO( 30 ); //30 sec to wait
  605. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  606. };
  607. };
  608. };
  609. };
  610. break;
  611. };
  612. case ARGUS_RPC_STAGE_WAIT_RECONNECT:{
  613. #ifdef DEBUG_EXTRA
  614. LOG("Wait Reconnect!");
  615. #endif
  616. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CONNECT );
  617. break;
  618. };
  619. case ARGUS_RPC_STAGE_REGISTER:{
  620. #ifdef DEBUG
  621. LOG("Stage Register!");
  622. #endif
  623. CSTR tIP = m_pPort->getLocalIp4();
  624. if( tIP ) {
  625. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  626. if ( m_pCurrentCMD ) {
  627. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  628. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_REGISTRATION );//strlen( m_pMaster->m_sIdentity );
  629. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  630. if ( m_pCurrentCMD->DATA ) {
  631. memset( m_pCurrentCMD->DATA, 0, m_pCurrentCMD->DATA_SIZE );
  632. strncpy( ((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->identity, m_pMaster->m_sIdentity, 32 );
  633. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *)m_pCurrentCMD->DATA)->peer, tIP, 22);
  634. if( peer_send_rpc_pack( m_pCurrentCMD ) ){
  635. setTO( 3 ); // Wait for 3 seconds to be registered with server
  636. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  637. free( tIP );
  638. break;
  639. };
  640. free( m_pCurrentCMD->DATA );
  641. };
  642. free( m_pCurrentCMD );
  643. };
  644. free( tIP );
  645. };
  646. m_pCurrentCMD = NULL;
  647. break;
  648. };
  649. case ARGUS_RPC_STAGE_SEND_HELLO:{
  650. #ifdef DEBUG
  651. LOG("Stage HELLO!");
  652. #endif
  653. m_pCurrentCMD = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  654. if ( m_pCurrentCMD ) {
  655. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  656. m_pCurrentCMD->DATA_SIZE = sizeof( ARGUS_DATA_MOJO_INFO );
  657. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  658. if (m_pCurrentCMD->DATA) {
  659. LOG("Send Client Info!");
  660. m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  661. LOG("ARGUS_DATA_MOJO_INFO");
  662. if ( peer_send_rpc_pack( m_pCurrentCMD ) ){
  663. setTO( 1 ); // Wait for 3 seconds
  664. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  665. };
  666. free( m_pCurrentCMD->DATA );
  667. };
  668. free( m_pCurrentCMD );
  669. };
  670. m_pCurrentCMD = NULL;
  671. break;
  672. };
  673. case ARGUS_RPC_STAGE_WAIT_ACK:{
  674. //LOG("Wait ACK!");
  675. if ( !decTO() ) setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  676. break;
  677. };
  678. case ARGUS_RPC_STAGE_IDLE: {
  679. if( ( tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME ) == 15 ) enqueueTimeReq(); else enqueueStatMsg(); break;
  680. };
  681. case ARGUS_RPC_STAGE_DISCONNECT:{
  682. LOG("Stage Disconnect!");
  683. // Gracefully Disconnect
  684. //if( peer_send_disconnect() ){
  685. // bTO_COUNTER = 1;
  686. // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  687. //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  688. //break; ---> Go to reconnect to other server
  689. };
  690. case ARGUS_RPC_STAGE_CLOSE:{
  691. // Just Disconnect
  692. LOG("Stage Close!");
  693. flushTransactions();
  694. setTransport( NULL, false );
  695. delete( m_pPort );
  696. m_pPort = NULL;
  697. m_bShutdownCommunication = true;
  698. m_bCommunicationEstablished = false;
  699. m_bRegisteredWithRemotePeer = false;
  700. setTO( 30 ); //30 sec to wait
  701. m_bActiveServer = 1 - m_bActiveServer;
  702. setAutomationStage( ARGUS_RPC_STAGE_WAIT_RECONNECT );
  703. break;
  704. };
  705. };
  706. m_mCurrentVTRProtector.lock();
  707. if ( m_vVTRHelpers.size() ) {
  708. //Handle New Video Transfer
  709. LOG("CHK HLPR");
  710. if( m_vVTRHelpers.at(0)->state == 0 ){
  711. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  712. if ( tPack ) {
  713. tPack->DATA_SIZE = sizeof( ARGUS_DATA_VIDEO_INFO );
  714. tPack->DATA = malloc( tPack->DATA_SIZE );
  715. if ( tPack->DATA ) {
  716. memcpy( tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE );
  717. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  718. if( m_qCMD->push( tPack ) ){
  719. m_vVTRHelpers.at(0)->state = 1;
  720. m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
  721. } else {
  722. free( tPack->DATA );
  723. free( tPack );
  724. }
  725. } else {
  726. free( tPack );
  727. };
  728. };
  729. };
  730. /// Validate Stalled/ Finished Videos (Try To Send next time
  731. for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); ) {
  732. if ( (*idx)->state == 2 ) {
  733. if ((cTimeTSeconds - (*idx)->ts) > 5) {
  734. // The transfer is staled
  735. (*idx)->state = 0;
  736. }
  737. };
  738. if ( (*idx)->state == 3 ){
  739. LOG("Del HLPR");
  740. //Finished Transfer
  741. delete( (*idx)->ptr );
  742. free( (*idx) );
  743. m_vVTRHelpers.erase(idx);
  744. LOG("Erase HLPR done");
  745. break; // must revalidate Beg-End conditions for FOR loop
  746. } else idx++;
  747. };
  748. };
  749. m_mCurrentVTRProtector.unlock();
  750. };
  751. if (validateTransaction()) {
  752. if ( fetchNewTransaction() ) {
  753. if( peer_send_rpc_pack( m_pCurrentCMD ) ) {
  754. setTO( 1 );
  755. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  756. } else LOG_ERROR("Failure!");
  757. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  758. } else SLEEP( DEFAULT_THREAD_SLEEP_TIME );
  759. };
  760. LOG("Connector Thread Ended!");
  761. };
  762. /*
  763. Register With Server and make Time Sync With Server
  764. */
  765. bool mojoRemoteClient::peer_send_rpc_pack( RPC_CMD_RECORD *ptr ){
  766. bool retVal;
  767. static uint8_t pBuffer[4194304]; //4Mb
  768. if ( ptr == NULL ) return false;
  769. if( !m_bCommunicationEstablished ) return false;
  770. //if( !m_bRegisteredWithRemotePeer ) return false;
  771. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  772. //uint16_t i = 0;
  773. //for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  774. memcpy( pBuffer+ARGUS_HEADER_SIZE+1, ptr->DATA, ptr->DATA_SIZE );
  775. m_nOutbondSerial++;
  776. //retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  777. retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+ptr->DATA_SIZE));
  778. return retVal;
  779. };
  780. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  781. if( ptr == NULL ) return false;
  782. if( !m_bCommunicationEstablished ) return false;
  783. if( !m_bRegisteredWithRemotePeer ) return false;
  784. uint8_t tBuffer[1024];
  785. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  786. uint16_t i = 0;
  787. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  788. m_nOutbondSerial++;
  789. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  790. };
  791. */
  792. bool mojoRemoteClient::validate(){
  793. if( m_bCommunicationEstablished ){
  794. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  795. m_bCommunicationEstablished = false;
  796. if( m_pPort ) {
  797. // Cancel all ongoing Queries
  798. m_bShutdownCommunication = true;
  799. setTransport( NULL, false );
  800. delete( m_pPort );
  801. };
  802. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  803. // Revalidate Data!!!
  804. //m_bActiveServer = 0;
  805. //m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  806. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  807. if( m_pPort ) {
  808. if( m_pPort->create() ) {
  809. m_bShutdownCommunication = false;
  810. m_bCommunicationEstablished = true;
  811. m_bRegisteredWithRemotePeer = false;
  812. setTransport( m_pPort, false );
  813. } else {
  814. delete( m_pPort );
  815. m_pPort = NULL;
  816. };
  817. };
  818. */
  819. };
  820. #ifdef DEBUG_EXTRA
  821. } else {
  822. LOG("Not Connected!");
  823. #endif
  824. /*
  825. if( m_pPort ) {
  826. // Cancel all ongoing Queries
  827. m_bShutdownCommunication = true;
  828. setTransport( NULL, false );
  829. delete( m_pPort );
  830. };
  831. //Try To connect
  832. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  833. if( m_pPort ) {
  834. if( m_pPort->create() ) {
  835. m_bShutdownCommunication = false;
  836. m_bCommunicationEstablished = true;
  837. m_bRegisteredWithRemotePeer = false;
  838. setTransport( m_pPort, false );
  839. } else {
  840. delete( m_pPort );
  841. m_pPort = NULL;
  842. };
  843. };
  844. */
  845. };
  846. return true;
  847. };
  848. VTR_HELPER *mojoRemoteClient::getHelperByDBID( uint64_t dbid ){
  849. m_mCurrentVTRProtector.lock();
  850. if( m_vVTRHelpers.size() ){
  851. for ( V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++ ) {
  852. if ( (*idx)->ptr->getRemoteReferenceID() == dbid ) {
  853. m_mCurrentVTRProtector.unlock();
  854. return (*idx);
  855. };
  856. };
  857. };
  858. m_mCurrentVTRProtector.unlock();
  859. return NULL;
  860. };
  861. bool mojoRemoteClient::addEvent( EVENT_CONTAINER *Evt ){
  862. #ifdef DEBUG_EXTRA
  863. LOG("Add Evt!");
  864. #endif
  865. if ( Evt == NULL ) return false;
  866. RPC_CMD_RECORD *tPack = (RPC_CMD_RECORD *)malloc(sizeof(RPC_CMD_RECORD));
  867. if ( tPack ) {
  868. #ifdef DEBUG_EXTRA
  869. char tB[128];
  870. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000 );
  871. LOG( tB );
  872. #endif
  873. tPack->DATA = (void*) argus_rpc_convert_evt( Evt );
  874. if ( tPack->DATA ) {
  875. tPack->CMD = ARGUS_DATA_MSG_EVT;
  876. tPack->DATA_SIZE = sizeof( ARGUS_DATA_EVENT );
  877. if( m_qCMD->push( tPack ) ) return true;
  878. free( tPack->DATA );
  879. };
  880. free( tPack );
  881. };
  882. return false;
  883. };
  884. bool mojoRemoteClient::addVideo( VIDEO_SEGMENT_INFO *ptr ){
  885. #ifdef DEBUG_EXTRA
  886. LOG("Add Video!");
  887. #endif
  888. if ( ptr == NULL ) return false;
  889. VTR_HELPER *tHLPR = (VTR_HELPER*) malloc( sizeof(VTR_HELPER));
  890. if ( tHLPR ) {
  891. tHLPR->state = 0;
  892. tHLPR->ptr = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
  893. LOG("NW HLPR");
  894. if ( tHLPR->ptr ) {
  895. LOG("HLPR OK");
  896. if ( tHLPR->ptr->init( ptr )) {
  897. LOG("HLPR INIT OK");
  898. m_mCurrentVTRProtector.lock();
  899. m_vVTRHelpers.push_back( tHLPR );
  900. m_mCurrentVTRProtector.unlock();
  901. LOG("HLPR PUSH OK");
  902. return true;
  903. };
  904. delete( tHLPR->ptr );
  905. };
  906. free (tHLPR);
  907. };
  908. return false;
  909. };
  910. mojoVideoTransferHelper::mojoVideoTransferHelper( uint32_t buffer_size ){
  911. m_pLog = basicLogCollection::getInstance();
  912. m_bVideoExists = false;
  913. m_bVideoOpened = false;
  914. m_nBufferSize = buffer_size;
  915. m_pBuffer = (uint8_t *) malloc( m_nBufferSize );
  916. m_nCTSMS = 0;
  917. m_nRemoteRefID = 0;
  918. m_pFD = NULL;
  919. memset( m_cCurrentFile, 0, MAX_PATH );
  920. };
  921. mojoVideoTransferHelper::~mojoVideoTransferHelper(){
  922. LOG("HDESTRC");
  923. abort();
  924. LOG("HDESTRC DONE");
  925. };
  926. #include <sys/stat.h>
  927. bool mojoVideoTransferHelper::init( VIDEO_SEGMENT_INFO *ptr ){
  928. if ( m_pBuffer == NULL ) return false;
  929. if ( m_bVideoExists ) {
  930. if ( m_bVideoOpened ) fclose( m_pFD );
  931. unlink(m_cCurrentFile);
  932. };
  933. if( !( m_bVideoExists = fileExist(ptr->scratchPath) ) ) return false;
  934. m_nRemoteRefID = 0;
  935. strncpy( m_cCurrentFile, ptr->scratchPath, MAX_PATH );
  936. m_cCurrentFile[MAX_PATH-1] = (char)0;
  937. struct stat st;
  938. stat( m_cCurrentFile, &st );
  939. m_sIn.DATA_SIZE = st.st_size;
  940. m_sIn.DTS = ptr->streamTSS;
  941. m_sIn.DMS = ptr->streamTMS;
  942. m_sIn.FPS = ptr->streamFPS;
  943. m_sIn.REF_ID = m_nRemoteRefID;
  944. strncpy( m_sIn.FMT, ptr->recordEXT, 5 );
  945. m_sIn.FMT[5]=(char)0;
  946. m_sIn.WIDTH = ptr->streamWidth;
  947. m_sIn.HEIGHT = ptr->streamHeight;
  948. #ifdef DEBUG_EXTRA
  949. char tB[129];
  950. 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 );
  951. LOG(tB);
  952. #endif
  953. m_pFD = fopen( m_cCurrentFile, "r" );
  954. if( m_bVideoOpened = m_pFD != NULL ) return true;
  955. return false;
  956. };
  957. uint8_t *mojoVideoTransferHelper::getNextChunk( uint32_t req_chunk_size, uint32_t *ack_size ){
  958. uint8_t *retVal = NULL;
  959. if ( m_bVideoOpened ) {
  960. retVal = (uint8_t *) malloc( req_chunk_size + 12 );//+DB_SOURCE_ID_x_64+Size_32
  961. if ( retVal ) {
  962. int32_t rv = fread( retVal+12, 1, req_chunk_size, m_pFD );
  963. if ( rv < 0 ) {
  964. free( retVal );
  965. retVal = NULL;
  966. abort();
  967. } else {
  968. *ack_size = rv+12;
  969. retVal[0] = ( m_nRemoteRefID >> 56 ) & 0xFF;
  970. retVal[1] = ( m_nRemoteRefID >> 48 ) & 0xFF;
  971. retVal[2] = ( m_nRemoteRefID >> 40 ) & 0xFF;
  972. retVal[3] = ( m_nRemoteRefID >> 32 ) & 0xFF;
  973. retVal[4] = ( m_nRemoteRefID >> 24 ) & 0xFF;
  974. retVal[5] = ( m_nRemoteRefID >> 16 ) & 0xFF;
  975. retVal[6] = ( m_nRemoteRefID >> 8 ) & 0xFF;
  976. retVal[7] = m_nRemoteRefID & 0xFF;
  977. retVal[8] = ( rv >> 24 ) & 0xFF;
  978. retVal[9] = ( rv >> 16 ) & 0xFF;
  979. retVal[10] = ( rv >> 8 ) & 0xFF;
  980. retVal[11] = rv & 0xFF;
  981. if ( feof( m_pFD )){
  982. fclose( m_pFD );
  983. m_bVideoOpened = false;
  984. };
  985. };
  986. };
  987. };
  988. return retVal;
  989. };
  990. ARGUS_DATA_VIDEO_INFO *mojoVideoTransferHelper::getVideoInfo(){
  991. return &m_sIn;
  992. };
  993. void mojoVideoTransferHelper::abort(){
  994. };
  995. #undef LOG
  996. #undef LOG_ERROR