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.

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