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.

1356 lines
35 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. { \
  7. char _tBfrErrMSG234[256]; \
  8. sprintf(_tBfrErrMSG234, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  9. m_pLog->Log("RPC", this, _LOG_ERROR, _tBfrErrMSG234); \
  10. }
  11. #else
  12. #define LOG_ERROR(X) m_pLog->Log("RPC", this, _LOG_ERROR, X);
  13. #endif
  14. mojoRemoteClient::mojoRemoteClient(mojoKernel * ptr) :
  15. protocolParser()
  16. {
  17. m_pLog = basicLogCollection::getInstance();
  18. m_pPort = NULL;
  19. m_pCurrentCMD = NULL;
  20. m_pMaster = ptr;
  21. m_nOutbondSerial = 0;
  22. m_nInbondSerial = 0;
  23. m_bShutdownCommunication = true;
  24. m_bCommunicationEstablished = false;
  25. m_bRegisteredWithRemotePeer = false;
  26. m_bUseBackupServer = false;
  27. m_bActiveServer = 0;
  28. m_bCloseCommunicationReason = 0;
  29. // m_pVideoHelper = new mojoVideoTransferHelper( ARGUS_RPC_DEFAULT_MEDIA_CHUNK );
  30. memset(&m_sINFO, 0, sizeof(ARGUS_DATA_MOJO_INFO));
  31. m_qCMD = new RPC_CMD_QUEUE();
  32. }
  33. mojoRemoteClient::~mojoRemoteClient()
  34. {
  35. if (m_bRegisteredWithRemotePeer)
  36. {
  37. while (m_bRegisteredWithRemotePeer)
  38. {
  39. m_mCurrentVTRProtector.lock();
  40. if (m_vVTRHelpers.size() == 0)
  41. {
  42. m_mCurrentVTRProtector.unlock();
  43. break;
  44. };
  45. m_mCurrentVTRProtector.unlock();
  46. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  47. };
  48. m_mCurrentVTRProtector.lock();
  49. if (m_vVTRHelpers.size())
  50. {
  51. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++)
  52. {
  53. delete(*idx);
  54. }
  55. m_vVTRHelpers.clear();
  56. };
  57. m_mCurrentVTRProtector.unlock();
  58. setAutomationStage( ARGUS_RPC_STAGE_DISCONNECT );
  59. //Waitiung to send to server graceful disconnection req
  60. SLEEP( 50 );
  61. };
  62. waitThreadEnd(true);
  63. flushTransactions();
  64. m_bShutdownCommunication = true;
  65. m_bCommunicationEstablished = false;
  66. m_bRegisteredWithRemotePeer = false;
  67. if (m_pPort)
  68. {
  69. delete(m_pPort);
  70. }
  71. delete(m_qCMD);
  72. }
  73. bool mojoRemoteClient::create()
  74. {
  75. if (m_pMaster == NULL)
  76. {
  77. return false;
  78. }
  79. m_pMaster->fillMojoInfo(&m_sINFO);
  80. if (m_bCommunicationEstablished && (m_bThreadStatus == TH_WORKING))
  81. {
  82. return true;
  83. }
  84. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  85. m_pCurrentCMD = NULL;
  86. m_bActiveServer = 0;
  87. m_bUseBackupServer = ((m_pMaster->m_sSiteData.remote_backup_port > 0) && (strlen(m_pMaster->m_sSiteData.remote_backup_ip) > 7));
  88. return init();
  89. }
  90. int mojoRemoteClient::packetChecker(U8 * Buffer, size_t BufferSize)
  91. {
  92. if (Buffer[0] == ARGUS_START_SERVER_FLAG)
  93. {
  94. if (BufferSize > ARGUS_DATA_MSG_MIN_SIZE)
  95. {
  96. uint32_t SIZE = Buffer[5];
  97. SIZE = (SIZE << 8) | Buffer[6];
  98. SIZE = (SIZE << 8) | Buffer[7];
  99. SIZE = (SIZE << 8) | Buffer[8];
  100. if ((SIZE + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE) <= BufferSize)
  101. {
  102. uint16_t tCRC = checksum16(Buffer, SIZE + ARGUS_HEADER_SIZE);
  103. uint16_t cCRC = Buffer[SIZE + ARGUS_HEADER_SIZE];
  104. cCRC = (cCRC << 8) | Buffer[SIZE + ARGUS_HEADER_SIZE + 1];
  105. if (tCRC == cCRC)
  106. {
  107. return 0;
  108. }
  109. }
  110. else
  111. {
  112. return 1; // to grou
  113. }
  114. }
  115. else
  116. {
  117. return 1; // to grow
  118. }
  119. };
  120. return -1; // to skip
  121. }
  122. protocolPackage * mojoRemoteClient::packetExtractor(U8 * Buffer, size_t BufferSize)
  123. {
  124. protocolPackage * tPack = (protocolPackage *) malloc(sizeof(protocolPackage));
  125. if (tPack)
  126. {
  127. tPack->m_nCode = Buffer[ARGUS_HEADER_SIZE];
  128. tPack->m_nSize = Buffer[5];
  129. tPack->m_nSize = (tPack->m_nSize << 8) | Buffer[6];
  130. tPack->m_nSize = (tPack->m_nSize << 8) | Buffer[7];
  131. tPack->m_nSize = (tPack->m_nSize << 8) | Buffer[8];
  132. tPack->m_nSize = tPack->m_nSize + ARGUS_HEADER_SIZE + ARGUS_CHECKSUM_SIZE;
  133. tPack->m_pData = (U8 *) malloc(tPack->m_nSize);
  134. if (tPack->m_pData && ( tPack->m_nSize <= BufferSize ) )
  135. {
  136. memcpy(tPack->m_pData, Buffer, tPack->m_nSize);
  137. }
  138. else
  139. {
  140. free(tPack);
  141. tPack = NULL;
  142. };
  143. };
  144. return tPack;
  145. }
  146. bool mojoRemoteClient::packetHandler(protocolPackage * Package)
  147. {
  148. m_nInbondSerial = Package->m_pData[1];
  149. m_nInbondSerial = (m_nInbondSerial << 8) | Package->m_pData[2];
  150. m_nInbondSerial = (m_nInbondSerial << 8) | Package->m_pData[3];
  151. m_nInbondSerial = (m_nInbondSerial << 8) | Package->m_pData[4];
  152. #ifdef DEBUG_EXTRA
  153. char bB[129];
  154. sprintf(bB, "Code: %02X", Package->m_nCode);
  155. LOG(bB);
  156. #endif
  157. switch (Package->m_nCode)
  158. {
  159. case ARGUS_DATA_MSG_REGISTER:
  160. #ifdef DEBUG
  161. LOG("Sucessfull Registration at Remote Server");
  162. #endif
  163. m_bRegisteredWithRemotePeer = true;
  164. setAutomationStage(ARGUS_RPC_STAGE_SEND_HELLO);
  165. //finishTransaction();
  166. break;
  167. case ARGUS_DATA_MSG_ACCESS_DENIED:
  168. //m_bRegisteredWithRemotePeer = false;
  169. LOG("Access denied!");
  170. /* fallthrough */
  171. case ARGUS_DATA_MSG_DISABLED:
  172. #ifdef DEBUG_EXTRA
  173. LOG("Communication is disabled!");
  174. #endif
  175. setAutomationStage(ARGUS_RPC_STAGE_CLOSE);
  176. //finishTransaction();
  177. break;
  178. case ARGUS_DATA_MSG_STAT : finishTransaction(); break;
  179. case ARGUS_DATA_MSG_INFO : finishTransaction(); break;
  180. case ARGUS_DATA_MSG_GET_INFO: enqueueInfoMsg(); break;
  181. case ARGUS_DATA_MSG_REQ_TIME_SYNC:
  182. {
  183. #ifdef DEBUG_EXTRA
  184. LOG("TSYNC REQ");
  185. #endif
  186. RPC_CMD_RECORD * tCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  187. if (tCMD)
  188. {
  189. tCMD->CMD = ARGUS_DATA_MSG_TIME_SYNC;
  190. tCMD->DATA_SIZE = sizeof(ARGUS_DATA_TIME_SYNC_REQ);
  191. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  192. if (tCMD->DATA)
  193. {
  194. ARGUS_DATA_TIME_SYNC_REQ * tDATA = (ARGUS_DATA_TIME_SYNC_REQ *) tCMD->DATA;
  195. tDATA->STS = getMilliseconds();
  196. if (peer_send_rpc_pack(tCMD))
  197. {
  198. setTO(3);
  199. free(tCMD->DATA);
  200. free(tCMD);
  201. break;
  202. };
  203. free(tCMD->DATA);
  204. };
  205. free(tCMD);
  206. };
  207. // Only On failure
  208. finishTransaction();
  209. break;
  210. };
  211. case ARGUS_DATA_MSG_EVT: finishTransaction(); break;
  212. case ARGUS_DATA_MSG_VIDEO_INFO:
  213. {
  214. // get DATABASE ID
  215. uint64_t tDBID = Package->m_pData[ARGUS_HEADER_SIZE + 1];
  216. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 2];
  217. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 3];
  218. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 4];
  219. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 5];
  220. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 6];
  221. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 7];
  222. tDBID = (tDBID << 8) | Package->m_pData[ARGUS_HEADER_SIZE + 8];
  223. #ifdef DEBUG
  224. char tZ[128];
  225. sprintf(tZ, "Video Ack %lu ID", tDBID);
  226. LOG(tZ);
  227. #endif
  228. m_mCurrentVTRProtector.lock();
  229. if (tDBID)
  230. {
  231. VTR_HELPER * tHELP = m_vVTRHelpers.at(0);
  232. tHELP->ptr->setRemoteReferenceID(tDBID);
  233. tHELP->state = 2;
  234. tHELP->ts = time(NULL);
  235. }
  236. else
  237. {
  238. // Abort Transaction
  239. m_vVTRHelpers.at(0)->state = 3;
  240. m_mCurrentVTRProtector.unlock();
  241. break;
  242. };
  243. m_mCurrentVTRProtector.unlock();
  244. };
  245. /* fallthrough */
  246. case ARGUS_DATA_MSG_VIDEO_DATA:
  247. {
  248. RPC_CMD_RECORD * tCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  249. if (tCMD)
  250. {
  251. uint32_t tSize;
  252. tCMD->CMD = ARGUS_DATA_MSG_VIDEO_DATA;
  253. m_mCurrentVTRProtector.lock();
  254. if (m_vVTRHelpers.size() == 0)
  255. {
  256. tCMD->DATA = NULL;
  257. tCMD->DATA_SIZE = 0;
  258. if (! m_qCMD->push(tCMD))
  259. {
  260. free(tCMD);
  261. };
  262. m_mCurrentVTRProtector.unlock();
  263. finishTransaction(); //?? Do we need it
  264. break;
  265. };
  266. tCMD->DATA = (void *) m_vVTRHelpers.at(0)->ptr->getNextChunk(ARGUS_RPC_DEFAULT_MEDIA_CHUNK, &tSize);
  267. m_mCurrentVTRProtector.unlock();
  268. tCMD->DATA_SIZE = tSize;
  269. #ifdef DEBUG_EXTRA
  270. char tC[128];
  271. sprintf(tC, "Sending Video Chunk %d bytes", tSize);
  272. LOG(tC);
  273. #endif
  274. if (tCMD->DATA)
  275. {
  276. /*
  277. //Direct Send to peer data
  278. if ( peer_send_rpc_pack( tCMD ) ) {
  279. setTO( 1 );
  280. free( tCMD->DATA );
  281. free( tCMD );
  282. if ( tSize < ARGUS_RPC_DEFAULT_MEDIA_CHUNK ) {
  283. //Last Chunk
  284. m_mCurrentVTRProtector.lock();
  285. mojoVideoTransferHelper tPtr = m_qVTRHelpers.first();
  286. delte( tPtr );
  287. m_qVTRHelpers.pop();
  288. if ( !m_qVTRHelpers.empty() ) {
  289. };
  290. m_mCurrentVTRProtector.unlock();
  291. };
  292. break;
  293. };
  294. free( tCMD->DATA );
  295. */
  296. // Send Via Queue
  297. if (m_qCMD->push(tCMD))
  298. {
  299. m_mCurrentVTRProtector.lock();
  300. m_vVTRHelpers.at(0)->ts = time(NULL);
  301. m_mCurrentVTRProtector.unlock();
  302. finishTransaction(); //?? Do we need it
  303. break;
  304. };
  305. free(tCMD->DATA);
  306. }
  307. else
  308. {
  309. LOG("Finish");
  310. // Finish Send That File
  311. m_mCurrentVTRProtector.lock();
  312. m_vVTRHelpers.at(0)->state = 3;
  313. m_mCurrentVTRProtector.unlock();
  314. };
  315. free(tCMD);
  316. };
  317. // Only On failure
  318. finishTransaction();
  319. break;
  320. };
  321. case ARGUS_DATA_MSG_TIME_SYNC:
  322. {
  323. struct timespec tS;// = {0}; initialized manually
  324. uint64_t cT = getMilliseconds();
  325. ARGUS_DATA_TIME_SYNC_ACK * tPTR = (ARGUS_DATA_TIME_SYNC_ACK *) (Package->m_pData + ARGUS_HEADER_SIZE + 1);
  326. uint64_t eTime = tPTR->ATS + ((cT - tPTR->STS) >> 1);
  327. tS.tv_sec = eTime / 1000;
  328. tS.tv_nsec = (eTime % 1000) * 1000000;
  329. if (clock_settime(CLOCK_REALTIME, &tS) != 0)
  330. {
  331. LOG_ERROR("Time Correction Failed!");
  332. switch (errno)
  333. {
  334. case EACCES : LOG_ERROR("Not have write permission for the dynamic POSIX clock device indicated!"); break;
  335. case EFAULT : LOG_ERROR("tS points outside the accessible address space!"); break;
  336. case EINVAL : LOG_ERROR("Parameter specified is invalid!"); break;
  337. 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;
  338. case ENOTSUP: LOG_ERROR("The operation is not supported by the dynamic POSIX clock device specified!"); break;
  339. case EPERM : LOG_ERROR("clock_settime() does not have permission to set the clock indicated!"); break;
  340. };
  341. };
  342. #ifdef DEBUG_EXTRA
  343. LOG("Time Sync ACK!");
  344. char tB[256];
  345. sprintf(tB, "RT:%lld ST:%lld CT:%lld", tPTR->STS, tPTR->ATS, cT);
  346. LOG(tB);
  347. sprintf(tB, "tv_s:%lld tv_ns:%lld", tS.tv_sec, tS.tv_nsec);
  348. LOG(tB);
  349. #endif
  350. finishTransaction();
  351. break;
  352. };
  353. case ARGUS_DATA_MSG_RPC:
  354. {
  355. ARGUS_DATA_RPC * tPTR = (ARGUS_DATA_RPC *) (Package->m_pData + ARGUS_HEADER_SIZE + 1);
  356. m_pMaster->m_pDBC->pushMemCMD(tPTR->NODE_ID, tPTR->CMD, tPTR->DATA);
  357. break;
  358. };
  359. case ARGUS_DATA_MSG_FREEZE_BUBBLES:
  360. {
  361. // Freeze Current Active Bubbles
  362. break;
  363. };
  364. case ARGUS_DATA_MSG_CLEAN_BUBBLES:
  365. {
  366. // Clear All Frozen Bubbles!
  367. break;
  368. };
  369. };
  370. return true;
  371. }
  372. uint8_t mojoRemoteClient::getAutomationStage()
  373. {
  374. m_mAutomationStageProtector.lock();
  375. uint8_t retVal = m_bAutomationStage;
  376. m_mAutomationStageProtector.unlock();
  377. return retVal;
  378. }
  379. void mojoRemoteClient::setAutomationStage(uint8_t stg)
  380. {
  381. m_mAutomationStageProtector.lock();
  382. m_bAutomationStage = stg;
  383. m_mAutomationStageProtector.unlock();
  384. }
  385. void mojoRemoteClient::finishTransaction()
  386. {
  387. m_mCurrentCMDProtector.lock();
  388. if (m_pCurrentCMD)
  389. {
  390. if (m_pCurrentCMD->DATA)
  391. {
  392. free(m_pCurrentCMD->DATA);
  393. }
  394. free(m_pCurrentCMD);
  395. m_pCurrentCMD = NULL;
  396. };
  397. m_mCurrentCMDProtector.unlock();
  398. setAutomationStage(ARGUS_RPC_STAGE_IDLE);
  399. }
  400. bool mojoRemoteClient::validateTransaction()
  401. {
  402. if (! m_bRegisteredWithRemotePeer)
  403. {
  404. return false;
  405. }
  406. if (m_bShutdownCommunication)
  407. {
  408. return false;
  409. }
  410. m_mCurrentCMDProtector.lock();
  411. if (m_pCurrentCMD == NULL)
  412. {
  413. m_mCurrentCMDProtector.unlock();
  414. return true;
  415. };
  416. m_mCurrentCMDProtector.unlock();
  417. return false;
  418. }
  419. bool mojoRemoteClient::pushNewTransaction(RPC_CMD_RECORD * ptr)
  420. {
  421. if (ptr == NULL)
  422. {
  423. return false;
  424. }
  425. m_mCurrentCMDProtector.lock();
  426. if (m_qCMD->push(ptr) != -1)
  427. {
  428. m_mCurrentCMDProtector.unlock();
  429. return true;
  430. };
  431. m_mCurrentCMDProtector.unlock();
  432. return false;
  433. }
  434. bool mojoRemoteClient::fetchNewTransaction()
  435. {
  436. if (m_bShutdownCommunication)
  437. {
  438. return false;
  439. }
  440. m_mCurrentCMDProtector.lock();
  441. if (m_pCurrentCMD)
  442. {
  443. m_mCurrentCMDProtector.unlock();
  444. return false;
  445. };
  446. m_pCurrentCMD = m_qCMD->pop();
  447. if (m_pCurrentCMD)
  448. {
  449. m_mCurrentCMDProtector.unlock();
  450. return true;
  451. };
  452. m_mCurrentCMDProtector.unlock();
  453. return false;
  454. }
  455. void mojoRemoteClient::enqueueDisconnect(uint8_t reason)
  456. {
  457. static uint8_t pBuffer[400];
  458. if (! m_bCommunicationEstablished)
  459. {
  460. return;
  461. }
  462. pBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_DISCONNECT;
  463. pBuffer[ARGUS_HEADER_SIZE + 1] = reason;
  464. m_nOutbondSerial++;
  465. m_pPort->sendData(pBuffer, argus_rpc_finishupPackage(pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 2));
  466. }
  467. void mojoRemoteClient::enqueueInfoMsg()
  468. {
  469. RPC_CMD_RECORD * tCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  470. if (tCMD)
  471. {
  472. tCMD->CMD = ARGUS_DATA_MSG_INFO;
  473. tCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_INFO);
  474. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  475. if (tCMD->DATA)
  476. {
  477. memcpy(tCMD->DATA, &m_sINFO, sizeof(ARGUS_DATA_MOJO_INFO));
  478. LOG("ARGUS_DATA_MOJO_INFO");
  479. if (pushNewTransaction(tCMD))
  480. {
  481. return;
  482. }
  483. free(tCMD->DATA);
  484. };
  485. free(tCMD);
  486. };
  487. }
  488. #include "../common/deviceNodeCamera.h"
  489. #include "../common/deviceNodeFactory.h"
  490. #include "../common/deviceNodeSensor.h"
  491. void mojoRemoteClient::enqueueStatMsg()
  492. {
  493. RPC_CMD_RECORD * tCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  494. if (tCMD)
  495. {
  496. tCMD->CMD = ARGUS_DATA_MSG_STAT;
  497. tCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_STATUS);
  498. tCMD->DATA = malloc(tCMD->DATA_SIZE);
  499. if (tCMD->DATA)
  500. {
  501. memset(tCMD->DATA, 0, tCMD->DATA_SIZE);
  502. ARGUS_DATA_MOJO_STATUS * cSTATUS = (ARGUS_DATA_MOJO_STATUS *) tCMD->DATA;
  503. DBC_SYS_LOAD_INFO cSYSSTAT;
  504. m_pMaster->getSystemUtilization(&cSYSSTAT);
  505. cSTATUS->CPU_LOAD = round(cSYSSTAT.cpu15);
  506. cSTATUS->CPU_TEMP = round(cSYSSTAT.temperature);
  507. // if( cSTATUS->CPU_LOAD == 0xFF ) cSTATUS->CPU_LOAD = 0;
  508. // if( cSTATUS->CPU_TEMP == 0xFF ) cSTATUS->CPU_TEMP = 0;
  509. cSTATUS->DISK_UTI = cSYSSTAT.disk;
  510. cSTATUS->MEMO_UTI = cSYSSTAT.mem;
  511. deviceNode ** tA = NULL;
  512. uint32_t tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ALS_SENSOR, tA);
  513. if (! tCNT)
  514. {
  515. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_LFS_SENSOR, tA);
  516. if (! tCNT)
  517. {
  518. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_PLS_200_SENSOR, tA);
  519. if (! tCNT)
  520. {
  521. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_PLS_360_SENSOR, tA);
  522. if (! tCNT)
  523. {
  524. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_3D_SENSOR, tA);
  525. if (! tCNT)
  526. {
  527. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_NET_PLS_200, tA);
  528. };
  529. };
  530. };
  531. };
  532. };
  533. cSTATUS->STATUSES.b_sensor = SENOR_STATUS_NA;
  534. cSTATUS->STATUSES.u_sensor = SENOR_STATUS_NA;
  535. if (tCNT)
  536. {
  537. for (uint32_t i = 0; i < tCNT; i++)
  538. {
  539. deviceNodeSensor * tSENS = (deviceNodeSensor *) tA[i];
  540. if (tSENS)
  541. {
  542. // Sensor's IDs are Sensor's NodeID's
  543. if (tSENS->getNodeID() == m_sINFO.BOTTOM_ID)
  544. {
  545. cSTATUS->STATUSES.b_sensor = tSENS->getSensStatus();
  546. };
  547. if (tSENS->getNodeID() == m_sINFO.UPPER_ID)
  548. {
  549. cSTATUS->STATUSES.u_sensor = tSENS->getSensStatus();
  550. };
  551. };
  552. };
  553. // Not Freeing tA[] -> those are pointers to static structs
  554. free(tA);
  555. tA = NULL;
  556. #ifdef DEBUG_EXTRA
  557. }
  558. else
  559. {
  560. LOG("No Sensors Defined/Enabled In the system!");
  561. #endif
  562. };
  563. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_STATIC_CAMERA, tA);
  564. if (! tCNT)
  565. {
  566. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PTZ_CAMERA, tA);
  567. if (! tCNT)
  568. {
  569. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_STATIC_CAMERA, tA);
  570. if (! tCNT)
  571. {
  572. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PTZ_CAMERA, tA);
  573. if (! tCNT)
  574. {
  575. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_IP_PRESET_CAMERA, tA);
  576. if (! tCNT)
  577. {
  578. tCNT = deviceNodeFactory::getInstance()->fetchNodes(ARGUS_ANALOG_PRESET_CAMERA, tA);
  579. };
  580. };
  581. };
  582. };
  583. };
  584. cSTATUS->STATUSES.camera = CAMERA_STATUS_NA;
  585. if (tCNT)
  586. {
  587. for (uint32_t i = 0; i < tCNT; i++)
  588. {
  589. deviceNodeCamera * tCAM = (deviceNodeCamera *) tA[i];
  590. cSTATUS->STATUSES.camera = cSTATUS->STATUSES.camera | ((tCAM->getCamStatus() > 0) ? (CAMERA_STATUS_OK << (i * 3)) : CAMERA_STATUS_NA);
  591. cSTATUS->STATUSES.camera = cSTATUS->STATUSES.camera | ((tCAM->getStreamStatus() > 0) ? (CAMERA_STATUS_STREAM_OK << (i * 3)) : CAMERA_STATUS_NA);
  592. cSTATUS->STATUSES.camera = cSTATUS->STATUSES.camera | ((tCAM->getPtzStatus() > 0) ? (CAMERA_STATUS_PTZ_OK << (i * 3)) : CAMERA_STATUS_NA);
  593. if (i == 0)
  594. {
  595. break; // Only first camera
  596. }
  597. };
  598. // Not Freeing tA[] -> those are pointers to static structs
  599. free(tA);
  600. tA = NULL;
  601. #ifdef DEBUG_EXTRA
  602. }
  603. else
  604. {
  605. LOG("No Camera Defined/Enabled In the system!");
  606. #endif
  607. };
  608. // All System Statuses goes here
  609. cSTATUS->STATUSES.system = cSTATUS->STATUSES.system | ((m_pMaster->getTrainPresence()) ? SYSTEM_STATUS_TRAIN : SYSTEM_STATUS_NA);
  610. if (pushNewTransaction(tCMD))
  611. {
  612. return;
  613. }
  614. free(tCMD->DATA);
  615. };
  616. free(tCMD);
  617. };
  618. }
  619. void mojoRemoteClient::enqueueTimeReq()
  620. {
  621. RPC_CMD_RECORD * tCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  622. if (tCMD)
  623. {
  624. tCMD->CMD = ARGUS_DATA_MSG_REQ_TIME_SYNC;
  625. tCMD->DATA_SIZE = 0;
  626. tCMD->DATA = NULL;
  627. if (pushNewTransaction(tCMD))
  628. {
  629. return;
  630. }
  631. free(tCMD);
  632. };
  633. }
  634. void mojoRemoteClient::flushTransactions()
  635. {
  636. m_mCurrentCMDProtector.lock();
  637. if (m_pCurrentCMD)
  638. {
  639. if (m_pCurrentCMD->DATA)
  640. {
  641. free(m_pCurrentCMD->DATA);
  642. }
  643. free(m_pCurrentCMD);
  644. };
  645. while ((m_pCurrentCMD = m_qCMD->pop()))
  646. {
  647. if (m_pCurrentCMD->DATA)
  648. {
  649. free(m_pCurrentCMD->DATA);
  650. }
  651. free(m_pCurrentCMD);
  652. };
  653. m_pCurrentCMD = NULL;
  654. m_mCurrentCMDProtector.unlock();
  655. }
  656. void mojoRemoteClient::setTO(uint8_t seconds)
  657. {
  658. m_mTOProtoctor.lock();
  659. m_bTOCounter = seconds;
  660. m_mTOProtoctor.unlock();
  661. }
  662. bool mojoRemoteClient::decTO()
  663. {
  664. m_mTOProtoctor.lock();
  665. if (m_bTOCounter)
  666. {
  667. m_bTOCounter--;
  668. m_mTOProtoctor.unlock();
  669. return true;
  670. };
  671. m_mTOProtoctor.unlock();
  672. return false;
  673. }
  674. void mojoRemoteClient::m_fThreadProc(void)
  675. {
  676. time_t tTimeTSeconds = 0;
  677. time_t cTimeTSeconds = 0;
  678. uint32_t tSecondsCounter = 0;
  679. m_bThreadStatus = TH_WORKING;
  680. pthread_setname_np( pthread_self(), "remoteClient" );
  681. while (m_bThreadStatus < TH_ENDED)
  682. {
  683. // seconds Counter (for internal purposes)
  684. cTimeTSeconds = time(NULL);
  685. // Process each second
  686. if (cTimeTSeconds != tTimeTSeconds)
  687. {
  688. tSecondsCounter++;
  689. tTimeTSeconds = cTimeTSeconds;
  690. switch (getAutomationStage())
  691. {
  692. case ARGUS_RPC_STAGE_CONNECT:
  693. {
  694. flushTransactions();
  695. if (m_bUseBackupServer)
  696. {
  697. if (m_bActiveServer == 0)
  698. {
  699. #ifdef DEBUG
  700. char tB[128];
  701. sprintf(tB, "Creating Remote connection to Main Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  702. LOG(tB);
  703. #endif
  704. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  705. }
  706. else
  707. {
  708. #ifdef DEBUG
  709. char tB[128];
  710. sprintf(tB, "Creating Remote connection to Backup Server [%s:%d]", m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port);
  711. LOG(tB);
  712. #endif
  713. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port);
  714. }
  715. if (m_pPort)
  716. {
  717. if (m_pPort->create())
  718. {
  719. #ifdef DEBUG
  720. LOG("Connection Sucessfull");
  721. #endif
  722. m_bShutdownCommunication = false;
  723. m_bCommunicationEstablished = true;
  724. m_bRegisteredWithRemotePeer = false;
  725. setTransport(m_pPort, false);
  726. setAutomationStage(ARGUS_RPC_STAGE_REGISTER);
  727. }
  728. else
  729. {
  730. LOG_ERROR("Connection to Remote Server Failed!");
  731. setConnectionStatus(CS_DISCONNECTED);
  732. setTransport(NULL, false);
  733. delete(m_pPort);
  734. m_pPort = NULL;
  735. m_bShutdownCommunication = true;
  736. m_bCommunicationEstablished = false;
  737. m_bRegisteredWithRemotePeer = false;
  738. m_bActiveServer = 1 - m_bActiveServer;
  739. setTO(30); // 30 sec to wait
  740. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  741. };
  742. };
  743. }
  744. else
  745. {
  746. if (m_pMaster->m_sSiteData.remote_port)
  747. {
  748. #ifdef DEBUG
  749. char tB[128];
  750. sprintf(tB, "Creating Remote connection to Alone Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  751. LOG(tB);
  752. #endif
  753. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  754. if (m_pPort)
  755. {
  756. if (m_pPort->create())
  757. {
  758. #ifdef DEBUG
  759. LOG("Connection Sucessfull");
  760. #endif
  761. m_bShutdownCommunication = false;
  762. m_bCommunicationEstablished = true;
  763. m_bRegisteredWithRemotePeer = false;
  764. setTransport(m_pPort, false);
  765. setAutomationStage(ARGUS_RPC_STAGE_REGISTER);
  766. }
  767. else
  768. {
  769. LOG_ERROR("Connection to Remote Server Failed!");
  770. setConnectionStatus(CS_DISCONNECTED);
  771. setTransport(NULL, false);
  772. delete(m_pPort);
  773. m_pPort = NULL;
  774. m_bShutdownCommunication = true;
  775. m_bCommunicationEstablished = false;
  776. m_bRegisteredWithRemotePeer = false;
  777. setTO(30); // 30 sec to wait
  778. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  779. };
  780. };
  781. };
  782. };
  783. break;
  784. };
  785. case ARGUS_RPC_STAGE_WAIT_RECONNECT:
  786. {
  787. #ifdef DEBUG_EXTRA
  788. LOG("Wait Reconnect!");
  789. #endif
  790. if (! decTO())
  791. {
  792. #ifdef DEBUG_EXTRA
  793. LOG("Go to Connect!");
  794. #endif
  795. setAutomationStage(ARGUS_RPC_STAGE_CONNECT);
  796. }
  797. break;
  798. };
  799. case ARGUS_RPC_STAGE_REGISTER:
  800. {
  801. #ifdef DEBUG_EXTRA
  802. LOG("Stage Register!");
  803. #endif
  804. CSTR tIP = m_pPort->getLocalIp4();
  805. if (tIP)
  806. {
  807. m_pCurrentCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  808. if (m_pCurrentCMD)
  809. {
  810. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  811. m_pCurrentCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_REGISTRATION); // strlen( m_pMaster->m_sIdentity );
  812. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  813. if (m_pCurrentCMD->DATA)
  814. {
  815. memset(m_pCurrentCMD->DATA, 0, m_pCurrentCMD->DATA_SIZE);
  816. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *) m_pCurrentCMD->DATA)->identity, m_pMaster->m_sIdentity, 32);
  817. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *) m_pCurrentCMD->DATA)->peer, tIP, 22);
  818. if (peer_send_rpc_pack(m_pCurrentCMD))
  819. {
  820. setTO(3); // Wait for 3 seconds to be registered with server
  821. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  822. free(tIP);
  823. break;
  824. };
  825. free(m_pCurrentCMD->DATA);
  826. };
  827. free(m_pCurrentCMD);
  828. };
  829. free(tIP);
  830. };
  831. m_pCurrentCMD = NULL;
  832. break;
  833. };
  834. case ARGUS_RPC_STAGE_SEND_HELLO:
  835. {
  836. #ifdef DEBUG_EXTRA
  837. LOG("Stage HELLO!");
  838. #endif
  839. m_pCurrentCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  840. if (m_pCurrentCMD)
  841. {
  842. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  843. m_pCurrentCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_INFO);
  844. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  845. if (m_pCurrentCMD->DATA)
  846. {
  847. #ifdef DEBUG_EXTRA
  848. LOG("Send Client Info!");
  849. #endif
  850. // Have that info on RC's Create()
  851. // m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  852. memcpy(m_pCurrentCMD->DATA, &m_sINFO, sizeof(ARGUS_DATA_MOJO_INFO));
  853. #ifdef DEBUG_EXTRA
  854. LOG("ARGUS_DATA_MOJO_INFO");
  855. #endif
  856. if (peer_send_rpc_pack(m_pCurrentCMD))
  857. {
  858. setTO(1); // Wait for 3 seconds
  859. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  860. };
  861. free(m_pCurrentCMD->DATA);
  862. };
  863. free(m_pCurrentCMD);
  864. };
  865. m_pCurrentCMD = NULL;
  866. break;
  867. };
  868. case ARGUS_RPC_STAGE_WAIT_ACK:
  869. {
  870. #ifdef DEBUG_EXTRA
  871. LOG("Wait ACK!");
  872. #endif
  873. if (! decTO())
  874. {
  875. setAutomationStage(ARGUS_RPC_STAGE_CLOSE);
  876. }
  877. break;
  878. };
  879. case ARGUS_RPC_STAGE_IDLE:
  880. {
  881. if ((tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME) == 15)
  882. {
  883. enqueueTimeReq();
  884. }
  885. else
  886. {
  887. enqueueStatMsg();
  888. }
  889. break;
  890. };
  891. case ARGUS_RPC_STAGE_DISCONNECT:
  892. {
  893. #ifdef DEBUG_EXTRA
  894. LOG("Stage Disconnect!");
  895. #endif
  896. // Gracefully Disconnect at Client Destructor
  897. if ( m_bRegisteredWithRemotePeer ){
  898. enqueueDisconnect(m_bCloseCommunicationReason);
  899. setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  900. break; // ---> Go to reconnect to other server
  901. };
  902. };
  903. /* fallthrough */
  904. case ARGUS_RPC_STAGE_CLOSE:
  905. {
  906. #ifdef DEBUG_EXTRA
  907. LOG("Stage Close!");
  908. #endif
  909. flushTransactions();
  910. setTransport(NULL, false);
  911. delete(m_pPort);
  912. m_pPort = NULL;
  913. m_bShutdownCommunication = true;
  914. m_bCommunicationEstablished = false;
  915. m_bRegisteredWithRemotePeer = false;
  916. setTO(30); // 30 sec to wait
  917. m_bActiveServer = 1 - m_bActiveServer;
  918. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  919. break;
  920. };
  921. };
  922. m_mCurrentVTRProtector.lock();
  923. if (m_vVTRHelpers.size())
  924. {
  925. // Handle New Video Transfer
  926. #ifdef DEBUG_EXTRA
  927. LOG("CHK HLPR");
  928. #endif
  929. if (m_vVTRHelpers.at(0)->state == 0)
  930. {
  931. RPC_CMD_RECORD * tPack = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  932. if (tPack)
  933. {
  934. tPack->DATA_SIZE = sizeof(ARGUS_DATA_VIDEO_INFO);
  935. tPack->DATA = malloc(tPack->DATA_SIZE);
  936. if (tPack->DATA)
  937. {
  938. memcpy(tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE);
  939. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  940. if (m_qCMD->push(tPack))
  941. {
  942. m_vVTRHelpers.at(0)->state = 1;
  943. m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
  944. }
  945. else
  946. {
  947. free(tPack->DATA);
  948. free(tPack);
  949. }
  950. }
  951. else
  952. {
  953. free(tPack);
  954. };
  955. };
  956. };
  957. /// Validate Stalled/ Finished Videos (Try To Send next time
  958. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end();)
  959. {
  960. if ((*idx)->state == 2)
  961. {
  962. if ((cTimeTSeconds - (*idx)->ts) > 5)
  963. {
  964. // The transfer is staled
  965. (*idx)->state = 0;
  966. }
  967. };
  968. if ((*idx)->state == 3)
  969. {
  970. #ifdef DEBUG_EXTRA
  971. LOG("Del HLPR");
  972. #endif
  973. // Finished Transfer
  974. delete((*idx)->ptr);
  975. free(*idx);
  976. m_vVTRHelpers.erase(idx);
  977. #ifdef DEBUG_EXTRA
  978. LOG("Erase HLPR done");
  979. #endif
  980. break; // must revalidate Beg-End conditions for FOR loop
  981. }
  982. else
  983. {
  984. idx++;
  985. }
  986. };
  987. };
  988. m_mCurrentVTRProtector.unlock();
  989. };
  990. if (validateTransaction())
  991. {
  992. if (fetchNewTransaction())
  993. {
  994. if (peer_send_rpc_pack(m_pCurrentCMD))
  995. {
  996. setTO(1);
  997. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  998. }
  999. else
  1000. {
  1001. LOG_ERROR("Failure!");
  1002. }
  1003. }
  1004. else
  1005. {
  1006. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  1007. }
  1008. }
  1009. else
  1010. {
  1011. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  1012. }
  1013. };
  1014. #ifdef DEBUG_EXTRA
  1015. LOG("Remote Connector Thread Ended!");
  1016. #endif
  1017. }
  1018. /*
  1019. Register With Server and make Time Sync With Server
  1020. */
  1021. bool mojoRemoteClient::peer_send_rpc_pack(RPC_CMD_RECORD * ptr)
  1022. {
  1023. bool retVal;
  1024. static uint8_t pBuffer[4194304]; // 4Mb
  1025. if (ptr == NULL)
  1026. {
  1027. return false;
  1028. }
  1029. if (! m_bCommunicationEstablished)
  1030. {
  1031. return false;
  1032. }
  1033. // if( !m_bRegisteredWithRemotePeer ) return false;
  1034. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  1035. // uint16_t i = 0;
  1036. // for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  1037. memcpy(pBuffer + ARGUS_HEADER_SIZE + 1, ptr->DATA, ptr->DATA_SIZE);
  1038. m_nOutbondSerial++;
  1039. // retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  1040. retVal = m_pPort->sendData(pBuffer, argus_rpc_finishupPackage(pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 1 + ptr->DATA_SIZE));
  1041. return retVal;
  1042. }
  1043. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  1044. if( ptr == NULL ) return false;
  1045. if( !m_bCommunicationEstablished ) return false;
  1046. if( !m_bRegisteredWithRemotePeer ) return false;
  1047. uint8_t tBuffer[1024];
  1048. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  1049. uint16_t i = 0;
  1050. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  1051. m_nOutbondSerial++;
  1052. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  1053. };
  1054. */
  1055. bool mojoRemoteClient::validate()
  1056. {
  1057. if (m_bCommunicationEstablished)
  1058. {
  1059. #if 0
  1060. lk;sdf
  1061. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  1062. m_bCommunicationEstablished = false;
  1063. if( m_pPort ) {
  1064. // Cancel all ongoing Queries
  1065. m_bShutdownCommunication = true;
  1066. setTransport( NULL, false );
  1067. delete( m_pPort );
  1068. };
  1069. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  1070. // Revalidate Data!!!
  1071. //m_bActiveServer = 0;
  1072. m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  1073. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  1074. if( m_pPort ) {
  1075. if( m_pPort->create() ) {
  1076. m_bShutdownCommunication = false;
  1077. m_bCommunicationEstablished = true;
  1078. m_bRegisteredWithRemotePeer = false;
  1079. setTransport( m_pPort, false );
  1080. } else {
  1081. delete( m_pPort );
  1082. m_pPort = NULL;
  1083. };
  1084. };
  1085. */
  1086. };
  1087. #endif
  1088. #ifdef DEBUG_EXTRA
  1089. }
  1090. else
  1091. {
  1092. LOG("Not Connected!");
  1093. #endif
  1094. /*
  1095. if( m_pPort ) {
  1096. // Cancel all ongoing Queries
  1097. m_bShutdownCommunication = true;
  1098. setTransport( NULL, false );
  1099. delete( m_pPort );
  1100. };
  1101. //Try To connect
  1102. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  1103. if( m_pPort ) {
  1104. if( m_pPort->create() ) {
  1105. m_bShutdownCommunication = false;
  1106. m_bCommunicationEstablished = true;
  1107. m_bRegisteredWithRemotePeer = false;
  1108. setTransport( m_pPort, false );
  1109. } else {
  1110. delete( m_pPort );
  1111. m_pPort = NULL;
  1112. };
  1113. };
  1114. */
  1115. };
  1116. return true;
  1117. }
  1118. VTR_HELPER * mojoRemoteClient::getHelperByDBID(uint64_t dbid)
  1119. {
  1120. m_mCurrentVTRProtector.lock();
  1121. if (m_vVTRHelpers.size())
  1122. {
  1123. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++)
  1124. {
  1125. if ((*idx)->ptr->getRemoteReferenceID() == dbid)
  1126. {
  1127. m_mCurrentVTRProtector.unlock();
  1128. return (*idx);
  1129. };
  1130. };
  1131. };
  1132. m_mCurrentVTRProtector.unlock();
  1133. return NULL;
  1134. }
  1135. bool mojoRemoteClient::addEvent(EVENT_CONTAINER * Evt)
  1136. {
  1137. #ifdef DEBUG_EXTRA
  1138. LOG("Add Evt!");
  1139. #endif
  1140. if (Evt == NULL)
  1141. {
  1142. return false;
  1143. }
  1144. RPC_CMD_RECORD * tPack = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  1145. if (tPack)
  1146. {
  1147. #ifdef DEBUG_EXTRA
  1148. char tB[128];
  1149. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000);
  1150. LOG(tB);
  1151. #endif
  1152. tPack->DATA = (void *) argus_rpc_convert_evt(Evt);
  1153. if (tPack->DATA)
  1154. {
  1155. tPack->CMD = ARGUS_DATA_MSG_EVT;
  1156. tPack->DATA_SIZE = sizeof(ARGUS_DATA_EVENT);
  1157. if (m_qCMD->push(tPack))
  1158. {
  1159. return true;
  1160. }
  1161. free(tPack->DATA);
  1162. };
  1163. free(tPack);
  1164. };
  1165. return false;
  1166. }
  1167. bool mojoRemoteClient::addVideo(VIDEO_SEGMENT_INFO * ptr)
  1168. {
  1169. #ifdef DEBUG_EXTRA
  1170. LOG("Add Video!");
  1171. #endif
  1172. if (ptr == NULL)
  1173. {
  1174. return false;
  1175. }
  1176. VTR_HELPER * tHLPR = (VTR_HELPER *) malloc(sizeof(VTR_HELPER));
  1177. if (tHLPR)
  1178. {
  1179. tHLPR->state = 0;
  1180. tHLPR->ptr = new mojoVideoTransferHelper(ARGUS_RPC_DEFAULT_MEDIA_CHUNK);
  1181. LOG("NW HLPR");
  1182. if (tHLPR->ptr)
  1183. {
  1184. LOG("HLPR OK");
  1185. if (tHLPR->ptr->init(ptr))
  1186. {
  1187. LOG("HLPR INIT OK");
  1188. m_mCurrentVTRProtector.lock();
  1189. m_vVTRHelpers.push_back(tHLPR);
  1190. m_mCurrentVTRProtector.unlock();
  1191. LOG("HLPR PUSH OK");
  1192. return true;
  1193. };
  1194. delete(tHLPR->ptr);
  1195. };
  1196. free(tHLPR);
  1197. };
  1198. return false;
  1199. }
  1200. mojoVideoTransferHelper::mojoVideoTransferHelper(uint32_t buffer_size)
  1201. {
  1202. m_pLog = basicLogCollection::getInstance();
  1203. m_bVideoExists = false;
  1204. m_bVideoOpened = false;
  1205. m_nBufferSize = buffer_size;
  1206. m_pBuffer = (uint8_t *) malloc(m_nBufferSize);
  1207. m_nCTSMS = 0;
  1208. m_nRemoteRefID = 0;
  1209. m_pFD = NULL;
  1210. memset(m_cCurrentFile, 0, MAX_PATH);
  1211. }
  1212. mojoVideoTransferHelper::~mojoVideoTransferHelper()
  1213. {
  1214. LOG("HDESTRC");
  1215. abort();
  1216. LOG("HDESTRC DONE");
  1217. }
  1218. #include <sys/stat.h>
  1219. bool mojoVideoTransferHelper::init(VIDEO_SEGMENT_INFO * ptr)
  1220. {
  1221. if (m_pBuffer == NULL)
  1222. {
  1223. return false;
  1224. }
  1225. if (m_bVideoExists)
  1226. {
  1227. if (m_bVideoOpened)
  1228. {
  1229. fclose(m_pFD);
  1230. }
  1231. unlink(m_cCurrentFile);
  1232. };
  1233. if (! (m_bVideoExists = fileExist(ptr->scratchPath)))
  1234. {
  1235. return false;
  1236. }
  1237. m_nRemoteRefID = 0;
  1238. strncpy(m_cCurrentFile, ptr->scratchPath, MAX_PATH);
  1239. m_cCurrentFile[MAX_PATH - 1] = (char) 0;
  1240. struct stat st;
  1241. stat(m_cCurrentFile, &st);
  1242. m_sIn.DATA_SIZE = st.st_size;
  1243. m_sIn.DTS = ptr->streamTSS;
  1244. m_sIn.DMS = ptr->streamTMS;
  1245. m_sIn.FPS = ptr->streamFPS;
  1246. m_sIn.REF_ID = m_nRemoteRefID;
  1247. strncpy(m_sIn.FMT, ptr->recordEXT, 5);
  1248. m_sIn.FMT[5] = (char) 0;
  1249. m_sIn.WIDTH = ptr->streamWidth;
  1250. m_sIn.HEIGHT = ptr->streamHeight;
  1251. #ifdef DEBUG_EXTRA
  1252. char tB[129];
  1253. 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);
  1254. LOG(tB);
  1255. #endif
  1256. m_pFD = fopen(m_cCurrentFile, "r");
  1257. if ((m_bVideoOpened = m_pFD != NULL))
  1258. {
  1259. return true;
  1260. }
  1261. return false;
  1262. }
  1263. uint8_t * mojoVideoTransferHelper::getNextChunk(uint32_t req_chunk_size, uint32_t * ack_size)
  1264. {
  1265. uint8_t * retVal = NULL;
  1266. if (m_bVideoOpened)
  1267. {
  1268. retVal = (uint8_t *) malloc(req_chunk_size + 12); //+DB_SOURCE_ID_x_64+Size_32
  1269. if (retVal)
  1270. {
  1271. int32_t rv = fread(retVal + 12, 1, req_chunk_size, m_pFD);
  1272. if (rv < 0)
  1273. {
  1274. free(retVal);
  1275. retVal = NULL;
  1276. abort();
  1277. }
  1278. else
  1279. {
  1280. *ack_size = rv + 12;
  1281. retVal[0] = (m_nRemoteRefID >> 56) & 0xFF;
  1282. retVal[1] = (m_nRemoteRefID >> 48) & 0xFF;
  1283. retVal[2] = (m_nRemoteRefID >> 40) & 0xFF;
  1284. retVal[3] = (m_nRemoteRefID >> 32) & 0xFF;
  1285. retVal[4] = (m_nRemoteRefID >> 24) & 0xFF;
  1286. retVal[5] = (m_nRemoteRefID >> 16) & 0xFF;
  1287. retVal[6] = (m_nRemoteRefID >> 8) & 0xFF;
  1288. retVal[7] = m_nRemoteRefID & 0xFF;
  1289. retVal[8] = (rv >> 24) & 0xFF;
  1290. retVal[9] = (rv >> 16) & 0xFF;
  1291. retVal[10] = (rv >> 8) & 0xFF;
  1292. retVal[11] = rv & 0xFF;
  1293. if (feof(m_pFD))
  1294. {
  1295. fclose(m_pFD);
  1296. m_bVideoOpened = false;
  1297. };
  1298. };
  1299. };
  1300. };
  1301. return retVal;
  1302. }
  1303. ARGUS_DATA_VIDEO_INFO * mojoVideoTransferHelper::getVideoInfo()
  1304. {
  1305. return &m_sIn;
  1306. }
  1307. void mojoVideoTransferHelper::abort() {}
  1308. #undef LOG
  1309. #undef LOG_ERROR