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.

1344 lines
34 KiB

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