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.

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