JobThreadF.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include "VMSCommLibF.h"
  4. #pragma hdrstop
  5. #include "JobThreadF.h"
  6. #include "ClientSessionF.h"
  7. #include "ClientSessionManagerF.h"
  8. #include "CDSCtlrF.h"
  9. #include "VmsProtocolF.h"
  10. //---------------------------------------------------------------------------
  11. #pragma package(smart_init)
  12. TJobThread *JobThread = NULL;
  13. //---------------------------------------------------------------------------
  14. TJobThread::TJobThread()
  15. {
  16. FPnlState = NULL;
  17. FTmrDb = NULL;
  18. }
  19. //---------------------------------------------------------------------------
  20. TJobThread::~TJobThread()
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TJobThread::updateLedColor(TColor AColor)
  25. {
  26. try {
  27. if (FPnlState) {
  28. PostMessage((HWND)g_AppCfg.lMainWinHandle, WM_PANEL_REFRESH, 3, (LPARAM)AColor);
  29. }
  30. } catch(Exception &e) {}
  31. }
  32. //---------------------------------------------------------------------------
  33. DWORD __fastcall TJobThread::Process(LPVOID AParam)
  34. {
  35. TClientSession *pSession;
  36. IPC_JOB_MESSAGE *pMsg;
  37. DWORD dwJob;
  38. int nRcvSize;
  39. char *pRcvBuff;
  40. OverlappedIO overlappedIO;
  41. while(!IsTerm() && !g_AppCfg.bThrExit)
  42. {
  43. updateLedColor(clGreen);
  44. if (!g_jobQ.PopBlocking(dwJob))
  45. {
  46. continue;
  47. }
  48. if (dwJob == Q_CLOSE)
  49. {
  50. break;
  51. }
  52. else
  53. if (dwJob == Q_TICK)
  54. {
  55. updateLedColor(clRed);
  56. ClientSessionManager->SendFlushAllClient();
  57. ClientSessionManager->CheckCommandTimeoutSessions();
  58. #if USE_POOL
  59. #else
  60. ClientSessionManager->CollectGarbageSessions();
  61. #endif
  62. continue;
  63. }
  64. else
  65. if (dwJob == eTcpCloseAll)
  66. {
  67. updateLedColor(clLime);
  68. ClientSessionManager->CloseAll();
  69. continue;
  70. }
  71. else
  72. if (dwJob == eVmsFormDownload)
  73. {
  74. // 정주기 시나리오 다운로드
  75. SendVmsFormDownload();
  76. continue;
  77. }
  78. else
  79. if (dwJob == eVmsStatusReqAll)
  80. {
  81. // 정주기 상태정보 요청
  82. SendVmsStatusReq();
  83. continue;
  84. }
  85. updateLedColor(clLime);
  86. pMsg = (IPC_JOB_MESSAGE*)dwJob;
  87. try
  88. {
  89. pSession = (TClientSession*)pMsg->ObjPtr;
  90. if (pSession == NULL)
  91. {
  92. MERROR("JOBT Unknown Event Null session: %d", pMsg->Type);
  93. }
  94. else
  95. {
  96. switch(pMsg->Type)
  97. {
  98. case eTcpConnect:
  99. if (pSession->OnConnect())
  100. {
  101. pSession->SendFlush();
  102. }
  103. break;
  104. case eTcpErrorEvent:
  105. case eTcpErrorRecv:
  106. case eTcpErrorSend:
  107. case eTcpClose:
  108. pSession->Disconnect();
  109. break;
  110. case eTcpRecv:
  111. nRcvSize = pSession->GetFreeSpaceSize();
  112. pRcvBuff = pSession->GetBuffer();
  113. if (nRcvSize >= pMsg->Len && pRcvBuff)
  114. {
  115. memcpy(pRcvBuff, pMsg->Buff, pMsg->Len);
  116. overlappedIO.FSession = pSession;
  117. RecvCompletion(0, pMsg->Len, (LPWSAOVERLAPPED)&overlappedIO, 0);
  118. }
  119. break;
  120. //TODO: 20241220, FTP DOWNLOAD
  121. case eVmsFtpFileDownload:
  122. if (pSession->download_ftp_file() > 0)
  123. {
  124. pSession->SendFlush();
  125. }
  126. break;
  127. // 폼 다운로드 시작
  128. case eVmsScenarioDownload: // 개별 명령에 의한 시나리오 다운로드
  129. if (pSession->download_form_schedule(false) > 0)
  130. {
  131. pSession->SendFlush();
  132. }
  133. break;
  134. case eVmsStatusReq:
  135. if (pSession->ReqStauts())
  136. {
  137. pSession->SendFlush();
  138. }
  139. break;
  140. case eVmsPowerModuleStautsReq:
  141. if (pSession->ReqPowerModuleStatus())
  142. {
  143. pSession->SendFlush();
  144. }
  145. break;
  146. case eVmsDisplayModuleStatusReq:
  147. if (pSession->ReqDisplayModuelStatus())
  148. {
  149. pSession->SendFlush();
  150. }
  151. break;
  152. case eVmsParamReq:
  153. if (pSession->ReqParameter() > 0)
  154. {
  155. pSession->SendFlush();
  156. }
  157. break;
  158. case eVmsPixelImage:
  159. if (pSession->ReqPixelImage(pMsg->Buff) > 0)
  160. {
  161. pSession->SendFlush();
  162. }
  163. break;
  164. case eVmsUploadCurrentForm:
  165. if (pSession->ReqUploadCurrentForm() > 0)
  166. {
  167. pSession->SendFlush();
  168. }
  169. break;
  170. case eVmsDispalyDefaultForm:
  171. if (pSession->ReqDisplayDefaultForm() > 0)
  172. {
  173. pSession->SendFlush();
  174. }
  175. break;
  176. case eVmsUploadScheduleForm:
  177. if (pSession->ReqUploadScheduleForm() > 0)
  178. {
  179. pSession->SendFlush();
  180. }
  181. break;
  182. case eVmsDisplayFormId:
  183. if (pSession->ReqDisplayFormId(pMsg->Buff) > 0)
  184. {
  185. pSession->SendFlush();
  186. }
  187. break;
  188. case eVmsStatusControl:
  189. if (pSession->ReqStatusControl(pMsg->Buff, pMsg->Len) > 0)
  190. {
  191. pSession->SendFlush();
  192. }
  193. break;
  194. case eVmsSendPacket:
  195. if (pSession->ReqSendPacket(pMsg->Buff, pMsg->Len) > 0)
  196. {
  197. pSession->SendFlush();
  198. }
  199. break;
  200. }
  201. if (pSession->IsConnected)
  202. {
  203. pSession->SendFlush();
  204. }
  205. }
  206. }
  207. __finally
  208. {
  209. //SAFE_DELETE(pMsg);
  210. }
  211. }
  212. Term();
  213. return 0;
  214. }
  215. //---------------------------------------------------------------------------
  216. int __fastcall TJobThread::SendVmsFormDownload()
  217. {
  218. CtlrItr it;
  219. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  220. {
  221. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  222. if (!pObj->Used) continue;
  223. if (!pObj->FProvide) continue;
  224. try
  225. {
  226. TClientSession *pSession = pObj->FSession;
  227. if (pSession)
  228. {
  229. if (pSession->download_form_schedule(true) > 0)
  230. {
  231. pSession->SendFlush();
  232. }
  233. }
  234. // 제어기로 전송 시퀀스를 호출한 후에 타임아웃을 체크하도록 한다.
  235. pObj->CTRLMODE->SaveFlag = false;
  236. pObj->CTRLMODE->pTimer = Now();
  237. }
  238. catch(Exception &e)
  239. {
  240. }
  241. }
  242. return 0;
  243. }
  244. //---------------------------------------------------------------------------
  245. int __fastcall TJobThread::SendVmsStatusReq()
  246. {
  247. CtlrItr it;
  248. for(it=CDSCtlrManager->FLists.FObjects.begin(); it != CDSCtlrManager->FLists.FObjects.end(); ++it)
  249. {
  250. TCDSCtlr *pObj = (TCDSCtlr*)it->second;
  251. if (!pObj->Used) continue;
  252. try
  253. {
  254. TClientSession *pSession = pObj->FSession;
  255. if (pSession)
  256. {
  257. if (pSession->ReqStauts() > 0)
  258. {
  259. pSession->SendFlush();
  260. }
  261. }
  262. }
  263. catch(Exception &e)
  264. {
  265. }
  266. }
  267. return 0;
  268. }
  269. //---------------------------------------------------------------------------