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.

1347 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. pthread_setname_np( pthread_self(), "remoteClient" );
  687. while (m_bThreadStatus < TH_ENDED)
  688. {
  689. // seconds Counter (for internal purposes)
  690. cTimeTSeconds = time(NULL);
  691. // Process each second
  692. if (cTimeTSeconds != tTimeTSeconds)
  693. {
  694. tSecondsCounter++;
  695. tTimeTSeconds = cTimeTSeconds;
  696. switch (getAutomationStage())
  697. {
  698. case ARGUS_RPC_STAGE_CONNECT:
  699. {
  700. flushTransactions();
  701. if (m_bUseBackupServer)
  702. {
  703. if (m_bActiveServer == 0)
  704. {
  705. #ifdef DEBUG
  706. char tB[128];
  707. sprintf(tB, "Creating Remote connection to Main Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  708. LOG(tB);
  709. #endif
  710. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  711. }
  712. else
  713. {
  714. #ifdef DEBUG
  715. char tB[128];
  716. sprintf(tB, "Creating Remote connection to Backup Server [%s:%d]", m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port);
  717. LOG(tB);
  718. #endif
  719. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_backup_ip, m_pMaster->m_sSiteData.remote_backup_port);
  720. }
  721. if (m_pPort)
  722. {
  723. if (m_pPort->create())
  724. {
  725. #ifdef DEBUG
  726. LOG("Connection Sucessfull");
  727. #endif
  728. m_bShutdownCommunication = false;
  729. m_bCommunicationEstablished = true;
  730. m_bRegisteredWithRemotePeer = false;
  731. setTransport(m_pPort, false);
  732. setAutomationStage(ARGUS_RPC_STAGE_REGISTER);
  733. }
  734. else
  735. {
  736. LOG_ERROR("Connection to Remote Server Failed!");
  737. setConnectionStatus(CS_DISCONNECTED);
  738. setTransport(NULL, false);
  739. delete(m_pPort);
  740. m_pPort = NULL;
  741. m_bShutdownCommunication = true;
  742. m_bCommunicationEstablished = false;
  743. m_bRegisteredWithRemotePeer = false;
  744. m_bActiveServer = 1 - m_bActiveServer;
  745. setTO(30); // 30 sec to wait
  746. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  747. };
  748. };
  749. }
  750. else
  751. {
  752. if (m_pMaster->m_sSiteData.remote_port)
  753. {
  754. #ifdef DEBUG
  755. char tB[128];
  756. sprintf(tB, "Creating Remote connection to Alone Server [%s:%d]", m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  757. LOG(tB);
  758. #endif
  759. m_pPort = new clientPort(m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port);
  760. if (m_pPort)
  761. {
  762. if (m_pPort->create())
  763. {
  764. #ifdef DEBUG
  765. LOG("Connection Sucessfull");
  766. #endif
  767. m_bShutdownCommunication = false;
  768. m_bCommunicationEstablished = true;
  769. m_bRegisteredWithRemotePeer = false;
  770. setTransport(m_pPort, false);
  771. setAutomationStage(ARGUS_RPC_STAGE_REGISTER);
  772. }
  773. else
  774. {
  775. LOG_ERROR("Connection to Remote Server Failed!");
  776. setConnectionStatus(CS_DISCONNECTED);
  777. setTransport(NULL, false);
  778. delete(m_pPort);
  779. m_pPort = NULL;
  780. m_bShutdownCommunication = true;
  781. m_bCommunicationEstablished = false;
  782. m_bRegisteredWithRemotePeer = false;
  783. setTO(30); // 30 sec to wait
  784. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  785. };
  786. };
  787. };
  788. };
  789. break;
  790. };
  791. case ARGUS_RPC_STAGE_WAIT_RECONNECT:
  792. {
  793. #ifdef DEBUG_EXTRA
  794. LOG("Wait Reconnect!");
  795. #endif
  796. if (! decTO())
  797. {
  798. setAutomationStage(ARGUS_RPC_STAGE_CONNECT);
  799. }
  800. break;
  801. };
  802. case ARGUS_RPC_STAGE_REGISTER:
  803. {
  804. #ifdef DEBUG
  805. LOG("Stage Register!");
  806. #endif
  807. CSTR tIP = m_pPort->getLocalIp4();
  808. if (tIP)
  809. {
  810. m_pCurrentCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  811. if (m_pCurrentCMD)
  812. {
  813. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_REGISTER;
  814. m_pCurrentCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_REGISTRATION); // strlen( m_pMaster->m_sIdentity );
  815. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  816. if (m_pCurrentCMD->DATA)
  817. {
  818. memset(m_pCurrentCMD->DATA, 0, m_pCurrentCMD->DATA_SIZE);
  819. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *) m_pCurrentCMD->DATA)->identity, m_pMaster->m_sIdentity, 32);
  820. strncpy(((ARGUS_DATA_MOJO_REGISTRATION *) m_pCurrentCMD->DATA)->peer, tIP, 22);
  821. if (peer_send_rpc_pack(m_pCurrentCMD))
  822. {
  823. setTO(3); // Wait for 3 seconds to be registered with server
  824. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  825. free(tIP);
  826. break;
  827. };
  828. free(m_pCurrentCMD->DATA);
  829. };
  830. free(m_pCurrentCMD);
  831. };
  832. free(tIP);
  833. };
  834. m_pCurrentCMD = NULL;
  835. break;
  836. };
  837. case ARGUS_RPC_STAGE_SEND_HELLO:
  838. {
  839. #ifdef DEBUG_EXTRA
  840. LOG("Stage HELLO!");
  841. #endif
  842. m_pCurrentCMD = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  843. if (m_pCurrentCMD)
  844. {
  845. m_pCurrentCMD->CMD = ARGUS_DATA_MSG_INFO;
  846. m_pCurrentCMD->DATA_SIZE = sizeof(ARGUS_DATA_MOJO_INFO);
  847. m_pCurrentCMD->DATA = malloc(m_pCurrentCMD->DATA_SIZE);
  848. if (m_pCurrentCMD->DATA)
  849. {
  850. #ifdef DEBUG_EXTRA
  851. LOG("Send Client Info!");
  852. #endif
  853. // Have that info on RC's Create()
  854. // m_pMaster->fillMojoInfo( (ARGUS_DATA_MOJO_INFO*)m_pCurrentCMD->DATA );
  855. memcpy(m_pCurrentCMD->DATA, &m_sINFO, sizeof(ARGUS_DATA_MOJO_INFO));
  856. #ifdef DEBUG_EXTRA
  857. LOG("ARGUS_DATA_MOJO_INFO");
  858. #endif
  859. if (peer_send_rpc_pack(m_pCurrentCMD))
  860. {
  861. setTO(1); // Wait for 3 seconds
  862. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  863. };
  864. free(m_pCurrentCMD->DATA);
  865. };
  866. free(m_pCurrentCMD);
  867. };
  868. m_pCurrentCMD = NULL;
  869. break;
  870. };
  871. case ARGUS_RPC_STAGE_WAIT_ACK:
  872. {
  873. // LOG("Wait ACK!");
  874. if (! decTO())
  875. {
  876. setAutomationStage(ARGUS_RPC_STAGE_CLOSE);
  877. }
  878. break;
  879. };
  880. case ARGUS_RPC_STAGE_IDLE:
  881. {
  882. if ((tSecondsCounter % ARGUS_SECONDS_TO_SYNC_TIME) == 15)
  883. {
  884. enqueueTimeReq();
  885. }
  886. else
  887. {
  888. enqueueStatMsg();
  889. }
  890. break;
  891. };
  892. case ARGUS_RPC_STAGE_DISCONNECT:
  893. {
  894. LOG("Stage Disconnect!");
  895. // Gracefully Disconnect
  896. // if( peer_send_disconnect() ){
  897. // bTO_COUNTER = 1;
  898. // setAutomationStage( ARGUS_RPC_STAGE_WAIT_ACK );
  899. //} else setAutomationStage( ARGUS_RPC_STAGE_CLOSE );
  900. // break; ---> Go to reconnect to other server
  901. };
  902. /* fallthrough */
  903. case ARGUS_RPC_STAGE_CLOSE:
  904. {
  905. // Just Disconnect
  906. LOG("Stage Close!");
  907. flushTransactions();
  908. setTransport(NULL, false);
  909. delete(m_pPort);
  910. m_pPort = NULL;
  911. m_bShutdownCommunication = true;
  912. m_bCommunicationEstablished = false;
  913. m_bRegisteredWithRemotePeer = false;
  914. setTO(30); // 30 sec to wait
  915. m_bActiveServer = 1 - m_bActiveServer;
  916. setAutomationStage(ARGUS_RPC_STAGE_WAIT_RECONNECT);
  917. break;
  918. };
  919. };
  920. m_mCurrentVTRProtector.lock();
  921. if (m_vVTRHelpers.size())
  922. {
  923. // Handle New Video Transfer
  924. LOG("CHK HLPR");
  925. if (m_vVTRHelpers.at(0)->state == 0)
  926. {
  927. RPC_CMD_RECORD * tPack = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  928. if (tPack)
  929. {
  930. tPack->DATA_SIZE = sizeof(ARGUS_DATA_VIDEO_INFO);
  931. tPack->DATA = malloc(tPack->DATA_SIZE);
  932. if (tPack->DATA)
  933. {
  934. memcpy(tPack->DATA, m_vVTRHelpers.at(0)->ptr->getVideoInfo(), tPack->DATA_SIZE);
  935. tPack->CMD = ARGUS_DATA_MSG_VIDEO_INFO;
  936. if (m_qCMD->push(tPack))
  937. {
  938. m_vVTRHelpers.at(0)->state = 1;
  939. m_vVTRHelpers.at(0)->ts = cTimeTSeconds;
  940. }
  941. else
  942. {
  943. free(tPack->DATA);
  944. free(tPack);
  945. }
  946. }
  947. else
  948. {
  949. free(tPack);
  950. };
  951. };
  952. };
  953. /// Validate Stalled/ Finished Videos (Try To Send next time
  954. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end();)
  955. {
  956. if ((*idx)->state == 2)
  957. {
  958. if ((cTimeTSeconds - (*idx)->ts) > 5)
  959. {
  960. // The transfer is staled
  961. (*idx)->state = 0;
  962. }
  963. };
  964. if ((*idx)->state == 3)
  965. {
  966. LOG("Del HLPR");
  967. // Finished Transfer
  968. delete((*idx)->ptr);
  969. free(*idx);
  970. m_vVTRHelpers.erase(idx);
  971. LOG("Erase HLPR done");
  972. break; // must revalidate Beg-End conditions for FOR loop
  973. }
  974. else
  975. {
  976. idx++;
  977. }
  978. };
  979. };
  980. m_mCurrentVTRProtector.unlock();
  981. };
  982. if (validateTransaction())
  983. {
  984. if (fetchNewTransaction())
  985. {
  986. if (peer_send_rpc_pack(m_pCurrentCMD))
  987. {
  988. setTO(1);
  989. setAutomationStage(ARGUS_RPC_STAGE_WAIT_ACK);
  990. }
  991. else
  992. {
  993. LOG_ERROR("Failure!");
  994. }
  995. }
  996. else
  997. {
  998. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  999. }
  1000. }
  1001. else
  1002. {
  1003. SLEEP(DEFAULT_THREAD_SLEEP_TIME);
  1004. }
  1005. };
  1006. LOG("Connector Thread Ended!");
  1007. }
  1008. /*
  1009. Register With Server and make Time Sync With Server
  1010. */
  1011. bool mojoRemoteClient::peer_send_rpc_pack(RPC_CMD_RECORD * ptr)
  1012. {
  1013. bool retVal;
  1014. static uint8_t pBuffer[4194304]; // 4Mb
  1015. if (ptr == NULL)
  1016. {
  1017. return false;
  1018. }
  1019. if (! m_bCommunicationEstablished)
  1020. {
  1021. return false;
  1022. }
  1023. // if( !m_bRegisteredWithRemotePeer ) return false;
  1024. pBuffer[ARGUS_HEADER_SIZE] = ptr->CMD;
  1025. // uint16_t i = 0;
  1026. // for (; i < ptr->DATA_SIZE; i++) pBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t *)ptr->DATA)[i];
  1027. memcpy(pBuffer + ARGUS_HEADER_SIZE + 1, ptr->DATA, ptr->DATA_SIZE);
  1028. m_nOutbondSerial++;
  1029. // retVal = m_pPort->sendData( pBuffer, argus_rpc_finishupPackage( pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  1030. retVal = m_pPort->sendData(pBuffer, argus_rpc_finishupPackage(pBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE + 1 + ptr->DATA_SIZE));
  1031. return retVal;
  1032. }
  1033. /*bool mojoRemoteClient::createVideoTransferTask( ARGUS_DATA_VIDEO_SEGMENT *ptr ){
  1034. if( ptr == NULL ) return false;
  1035. if( !m_bCommunicationEstablished ) return false;
  1036. if( !m_bRegisteredWithRemotePeer ) return false;
  1037. uint8_t tBuffer[1024];
  1038. tBuffer[ARGUS_HEADER_SIZE] = ARGUS_DATA_MSG_VIDEO_INFO;
  1039. uint16_t i = 0;
  1040. for(; i < sizeof(ARGUS_DATA_VIDEO_SEGMENT); i++) tBuffer[ARGUS_HEADER_SIZE+1+i] = ((uint8_t*)ptr)[i];
  1041. m_nOutbondSerial++;
  1042. return m_pPort->sendData(tBuffer, argus_rpc_finishupPackage(tBuffer, m_nOutbondSerial, ARGUS_HEADER_SIZE+1+i ));
  1043. };
  1044. */
  1045. bool mojoRemoteClient::validate()
  1046. {
  1047. if (m_bCommunicationEstablished)
  1048. {
  1049. #if 0
  1050. lk;sdf
  1051. if ( getConnectionStatus() == CS_DISCONNECTED ) {
  1052. m_bCommunicationEstablished = false;
  1053. if( m_pPort ) {
  1054. // Cancel all ongoing Queries
  1055. m_bShutdownCommunication = true;
  1056. setTransport( NULL, false );
  1057. delete( m_pPort );
  1058. };
  1059. m_bAutomationStage = ARGUS_RPC_STAGE_CONNECT;
  1060. // Revalidate Data!!!
  1061. //m_bActiveServer = 0;
  1062. m_bUseBackupServer = ( ( m_pMaster->m_sSiteData.remote_backup_port > 0 ) && strlen( ( m_pMaster->m_sSiteData.remote_backup_ip ) > 7 ) );
  1063. /*m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  1064. if( m_pPort ) {
  1065. if( m_pPort->create() ) {
  1066. m_bShutdownCommunication = false;
  1067. m_bCommunicationEstablished = true;
  1068. m_bRegisteredWithRemotePeer = false;
  1069. setTransport( m_pPort, false );
  1070. } else {
  1071. delete( m_pPort );
  1072. m_pPort = NULL;
  1073. };
  1074. };
  1075. */
  1076. };
  1077. #endif
  1078. #ifdef DEBUG_EXTRA
  1079. }
  1080. else
  1081. {
  1082. LOG("Not Connected!");
  1083. #endif
  1084. /*
  1085. if( m_pPort ) {
  1086. // Cancel all ongoing Queries
  1087. m_bShutdownCommunication = true;
  1088. setTransport( NULL, false );
  1089. delete( m_pPort );
  1090. };
  1091. //Try To connect
  1092. m_pPort = new clientPort( m_pMaster->m_sSiteData.remote_ip, m_pMaster->m_sSiteData.remote_port );
  1093. if( m_pPort ) {
  1094. if( m_pPort->create() ) {
  1095. m_bShutdownCommunication = false;
  1096. m_bCommunicationEstablished = true;
  1097. m_bRegisteredWithRemotePeer = false;
  1098. setTransport( m_pPort, false );
  1099. } else {
  1100. delete( m_pPort );
  1101. m_pPort = NULL;
  1102. };
  1103. };
  1104. */
  1105. };
  1106. return true;
  1107. }
  1108. VTR_HELPER * mojoRemoteClient::getHelperByDBID(uint64_t dbid)
  1109. {
  1110. m_mCurrentVTRProtector.lock();
  1111. if (m_vVTRHelpers.size())
  1112. {
  1113. for (V_VIDEO_TRANSFER_HELPERS::iterator idx = m_vVTRHelpers.begin(); idx != m_vVTRHelpers.end(); idx++)
  1114. {
  1115. if ((*idx)->ptr->getRemoteReferenceID() == dbid)
  1116. {
  1117. m_mCurrentVTRProtector.unlock();
  1118. return (*idx);
  1119. };
  1120. };
  1121. };
  1122. m_mCurrentVTRProtector.unlock();
  1123. return NULL;
  1124. }
  1125. bool mojoRemoteClient::addEvent(EVENT_CONTAINER * Evt)
  1126. {
  1127. #ifdef DEBUG_EXTRA
  1128. LOG("Add Evt!");
  1129. #endif
  1130. if (Evt == NULL)
  1131. {
  1132. return false;
  1133. }
  1134. RPC_CMD_RECORD * tPack = (RPC_CMD_RECORD *) malloc(sizeof(RPC_CMD_RECORD));
  1135. if (tPack)
  1136. {
  1137. #ifdef DEBUG_EXTRA
  1138. char tB[128];
  1139. sprintf(tB, "TS: %d.%d", Evt->m_UMSTS / 1000, Evt->m_UMSTS % 1000);
  1140. LOG(tB);
  1141. #endif
  1142. tPack->DATA = (void *) argus_rpc_convert_evt(Evt);
  1143. if (tPack->DATA)
  1144. {
  1145. tPack->CMD = ARGUS_DATA_MSG_EVT;
  1146. tPack->DATA_SIZE = sizeof(ARGUS_DATA_EVENT);
  1147. if (m_qCMD->push(tPack))
  1148. {
  1149. return true;
  1150. }
  1151. free(tPack->DATA);
  1152. };
  1153. free(tPack);
  1154. };
  1155. return false;
  1156. }
  1157. bool mojoRemoteClient::addVideo(VIDEO_SEGMENT_INFO * ptr)
  1158. {
  1159. #ifdef DEBUG_EXTRA
  1160. LOG("Add Video!");
  1161. #endif
  1162. if (ptr == NULL)
  1163. {
  1164. return false;
  1165. }
  1166. VTR_HELPER * tHLPR = (VTR_HELPER *) malloc(sizeof(VTR_HELPER));
  1167. if (tHLPR)
  1168. {
  1169. tHLPR->state = 0;
  1170. tHLPR->ptr = new mojoVideoTransferHelper(ARGUS_RPC_DEFAULT_MEDIA_CHUNK);
  1171. LOG("NW HLPR");
  1172. if (tHLPR->ptr)
  1173. {
  1174. LOG("HLPR OK");
  1175. if (tHLPR->ptr->init(ptr))
  1176. {
  1177. LOG("HLPR INIT OK");
  1178. m_mCurrentVTRProtector.lock();
  1179. m_vVTRHelpers.push_back(tHLPR);
  1180. m_mCurrentVTRProtector.unlock();
  1181. LOG("HLPR PUSH OK");
  1182. return true;
  1183. };
  1184. delete(tHLPR->ptr);
  1185. };
  1186. free(tHLPR);
  1187. };
  1188. return false;
  1189. }
  1190. mojoVideoTransferHelper::mojoVideoTransferHelper(uint32_t buffer_size)
  1191. {
  1192. m_pLog = basicLogCollection::getInstance();
  1193. m_bVideoExists = false;
  1194. m_bVideoOpened = false;
  1195. m_nBufferSize = buffer_size;
  1196. m_pBuffer = (uint8_t *) malloc(m_nBufferSize);
  1197. m_nCTSMS = 0;
  1198. m_nRemoteRefID = 0;
  1199. m_pFD = NULL;
  1200. memset(m_cCurrentFile, 0, MAX_PATH);
  1201. }
  1202. mojoVideoTransferHelper::~mojoVideoTransferHelper()
  1203. {
  1204. LOG("HDESTRC");
  1205. abort();
  1206. LOG("HDESTRC DONE");
  1207. }
  1208. #include <sys/stat.h>
  1209. bool mojoVideoTransferHelper::init(VIDEO_SEGMENT_INFO * ptr)
  1210. {
  1211. if (m_pBuffer == NULL)
  1212. {
  1213. return false;
  1214. }
  1215. if (m_bVideoExists)
  1216. {
  1217. if (m_bVideoOpened)
  1218. {
  1219. fclose(m_pFD);
  1220. }
  1221. unlink(m_cCurrentFile);
  1222. };
  1223. if (! (m_bVideoExists = fileExist(ptr->scratchPath)))
  1224. {
  1225. return false;
  1226. }
  1227. m_nRemoteRefID = 0;
  1228. strncpy(m_cCurrentFile, ptr->scratchPath, MAX_PATH);
  1229. m_cCurrentFile[MAX_PATH - 1] = (char) 0;
  1230. struct stat st;
  1231. stat(m_cCurrentFile, &st);
  1232. m_sIn.DATA_SIZE = st.st_size;
  1233. m_sIn.DTS = ptr->streamTSS;
  1234. m_sIn.DMS = ptr->streamTMS;
  1235. m_sIn.FPS = ptr->streamFPS;
  1236. m_sIn.REF_ID = m_nRemoteRefID;
  1237. strncpy(m_sIn.FMT, ptr->recordEXT, 5);
  1238. m_sIn.FMT[5] = (char) 0;
  1239. m_sIn.WIDTH = ptr->streamWidth;
  1240. m_sIn.HEIGHT = ptr->streamHeight;
  1241. #ifdef DEBUG_EXTRA
  1242. char tB[129];
  1243. 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);
  1244. LOG(tB);
  1245. #endif
  1246. m_pFD = fopen(m_cCurrentFile, "r");
  1247. if ((m_bVideoOpened = m_pFD != NULL))
  1248. {
  1249. return true;
  1250. }
  1251. return false;
  1252. }
  1253. uint8_t * mojoVideoTransferHelper::getNextChunk(uint32_t req_chunk_size, uint32_t * ack_size)
  1254. {
  1255. uint8_t * retVal = NULL;
  1256. if (m_bVideoOpened)
  1257. {
  1258. retVal = (uint8_t *) malloc(req_chunk_size + 12); //+DB_SOURCE_ID_x_64+Size_32
  1259. if (retVal)
  1260. {
  1261. int32_t rv = fread(retVal + 12, 1, req_chunk_size, m_pFD);
  1262. if (rv < 0)
  1263. {
  1264. free(retVal);
  1265. retVal = NULL;
  1266. abort();
  1267. }
  1268. else
  1269. {
  1270. *ack_size = rv + 12;
  1271. retVal[0] = (m_nRemoteRefID >> 56) & 0xFF;
  1272. retVal[1] = (m_nRemoteRefID >> 48) & 0xFF;
  1273. retVal[2] = (m_nRemoteRefID >> 40) & 0xFF;
  1274. retVal[3] = (m_nRemoteRefID >> 32) & 0xFF;
  1275. retVal[4] = (m_nRemoteRefID >> 24) & 0xFF;
  1276. retVal[5] = (m_nRemoteRefID >> 16) & 0xFF;
  1277. retVal[6] = (m_nRemoteRefID >> 8) & 0xFF;
  1278. retVal[7] = m_nRemoteRefID & 0xFF;
  1279. retVal[8] = (rv >> 24) & 0xFF;
  1280. retVal[9] = (rv >> 16) & 0xFF;
  1281. retVal[10] = (rv >> 8) & 0xFF;
  1282. retVal[11] = rv & 0xFF;
  1283. if (feof(m_pFD))
  1284. {
  1285. fclose(m_pFD);
  1286. m_bVideoOpened = false;
  1287. };
  1288. };
  1289. };
  1290. };
  1291. return retVal;
  1292. }
  1293. ARGUS_DATA_VIDEO_INFO * mojoVideoTransferHelper::getVideoInfo()
  1294. {
  1295. return &m_sIn;
  1296. }
  1297. void mojoVideoTransferHelper::abort() {}
  1298. #undef LOG
  1299. #undef LOG_ERROR