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.

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