AsnycAction.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "StdAfx.h"
  2. #include "AsnycAction.h"
  3. #include "PacketAnalizer.h"
  4. #define SeqAction "Action"
  5. #define SeqEvent "Event"
  6. #define SpotMaxNum 10
  7. #define ACTIONTIMEOUT 60000
  8. CAsnycAction::CAsnycAction()
  9. {
  10. }
  11. CAsnycAction::~CAsnycAction()
  12. {
  13. }
  14. RET_STATUS CAsnycAction::OpenAllClient(const char *workpath[], int count)
  15. {
  16. for (int i = 0; i < count; i++)
  17. {
  18. CommonLogicClient* pClient = new CommonLogicClient();
  19. if (RET_SUCCEED == pClient->Open(workpath[i], ACTION | NOTIFY_UPDATE))
  20. {
  21. m_ClientList[workpath[i]] = pClient;
  22. m_NotifyHandleList[pClient->GetNotifyHandle()] = pClient;
  23. }
  24. else
  25. {
  26. CloseAllClient(workpath, i);
  27. m_ClientList.clear();
  28. return RET_FAILED;
  29. }
  30. }
  31. GetClientSeqResouce();
  32. return RET_SUCCEED;
  33. }
  34. RET_STATUS CAsnycAction::CloseAllClient(const char *workpath[], int count )
  35. {
  36. if (workpath == NULL&&count == 0)
  37. {
  38. //m_SeqList.clear();
  39. m_EventList.clear();
  40. m_NotifyHandleList.clear();
  41. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  42. while (iter != m_ClientList.end())
  43. {
  44. /*map<CommonLogicClient*, ResDataObject>::iterator iter2 = m_SeqList.find(iter->second);
  45. if (iter2 != m_SeqList.end())
  46. {
  47. m_SeqList.erase(iter2);
  48. }*/
  49. iter->second->Lock();
  50. iter->second->UnLock();
  51. iter->second->Close();
  52. delete iter->second;
  53. iter->second = NULL;
  54. iter++;
  55. }
  56. m_ClientList.clear();
  57. }
  58. else
  59. {
  60. //m_SeqList.clear();
  61. m_EventList.clear();
  62. m_NotifyHandleList.clear();
  63. for (int i = 0; i < count; i++)
  64. {
  65. map<string, CommonLogicClient*>::iterator iter = m_ClientList.find(workpath[i]);
  66. if (iter != m_ClientList.end())
  67. {
  68. iter->second->Lock();
  69. iter->second->UnLock();
  70. iter->second->Close();
  71. delete iter->second;
  72. iter->second = NULL;
  73. m_ClientList.erase(iter);
  74. }
  75. }
  76. }
  77. if (m_ClientList.size() == 0)
  78. {
  79. }
  80. return RET_SUCCEED;
  81. }
  82. RET_STATUS CAsnycAction::GetClientSeqResouce()
  83. {
  84. ResDataObject Request, ResSEQ;
  85. /*m_SeqList.clear();
  86. m_EventList.clear();
  87. m_NotifyHandleList.clear();*/
  88. m_SeqList.clear();
  89. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  90. while (iter != m_ClientList.end())
  91. {
  92. if (RET_SUCCEED == iter->second->Action("GetSEQResource", Request, ResSEQ, 1000))
  93. {
  94. ResDataObject SeqResource, EventResource;
  95. if (ResSEQ.GetFirstOf(SeqAction) >= 0)
  96. {
  97. SeqResource = ResSEQ[SeqAction];
  98. }
  99. m_SeqList[iter->second] = SeqResource;
  100. if (ResSEQ.GetFirstOf(SeqEvent) >= 0)
  101. {
  102. EventResource = ResSEQ[SeqEvent];
  103. }
  104. m_EventList[iter->second] = EventResource;
  105. }
  106. else
  107. {
  108. CloseAllClient();
  109. return RET_FAILED;
  110. }
  111. iter++;
  112. }
  113. return RET_SUCCEED;
  114. }
  115. RET_STATUS CAsnycAction::SetAsnycAction(const char *ActionName, DWORD timeout)
  116. {
  117. timeout = ACTIONTIMEOUT;
  118. ResDataObject Request, ResSEQ;
  119. HANDLE HandleList[SpotMaxNum];
  120. CommonLogicClient* ClientList[SpotMaxNum];
  121. int result = RET_SUCCEED;
  122. int nHandIndex = 0;
  123. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  124. ResDataObject DeviceResource;
  125. int idx = -1;
  126. while (iter != m_ClientList.end())
  127. {
  128. map<CommonLogicClient*, ResDataObject>::iterator iterseq = m_SeqList.find(iter->second);
  129. if (iterseq == m_SeqList.end() || iterseq->second.GetFirstOf(ActionName) < 0)//如果没有订阅该消息,则不用查找该单元的资源属性值
  130. {
  131. iter++;
  132. continue;
  133. }
  134. iter->second->Lock();
  135. result = iter->second->Action_Req(ActionName, Request, timeout);
  136. TPRINTA_DEBUG("ActionName:%s, Unit: %s", ActionName, iter->first.c_str());
  137. HandleList[nHandIndex] = iter->second->GetResponseHandle();
  138. ClientList[nHandIndex] = iter->second;
  139. nHandIndex++;
  140. if (RET_SUCCEED != result)
  141. {
  142. TPRINTA_DEBUG("ActionName:%s, result: %d ,return", ActionName, result);
  143. break;
  144. }
  145. iter++;
  146. }
  147. if (RET_SUCCEED == result)
  148. {
  149. int count = 0;
  150. while (count != nHandIndex)
  151. {
  152. DWORD eventindex = WaitForMultipleObjects(nHandIndex, HandleList, false, timeout);
  153. if (eventindex == WAIT_TIMEOUT)
  154. {
  155. TPRINTA_DEBUG("ActionName:%s, Timeout, return", ActionName);
  156. result = RET_TIMEOUT;
  157. break;
  158. }
  159. else
  160. {
  161. /*map<HANDLE, CommonLogicClient*>::iterator iterclient = m_NotifyHandleList.find(HandleList[eventindex]);
  162. if (iterclient != m_NotifyHandleList.end())
  163. {
  164. iterclient->second->Action_Res(ActionName, ResSEQ, timeout);
  165. count++;
  166. }*/
  167. ClientList[eventindex]->Action_Res(ActionName, ResSEQ, timeout);
  168. count++;
  169. }
  170. }
  171. }
  172. iter = m_ClientList.begin();
  173. while (iter != m_ClientList.end())
  174. {
  175. map<CommonLogicClient*, ResDataObject>::iterator iterseq = m_SeqList.find(iter->second);
  176. if (iterseq == m_SeqList.end() || iterseq->second.GetFirstOf(ActionName) < 0)//如果没有订阅该消息,则不用查找该单元的资源属性值
  177. {
  178. iter++;
  179. continue;
  180. }
  181. iter->second->UnLock();
  182. iter++;
  183. }
  184. return (RET_STATUS)result;
  185. }
  186. int CAsnycAction::WaitforEventNotify(HANDLE &EventHandle, const char *EvtName[], int count, ResDataObject &ResNotify, DWORD timeout)
  187. {
  188. ResDataObject Request, ResSEQ, ReadNotify;
  189. ResNotify.clear();
  190. HANDLE HandleList[SpotMaxNum];
  191. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  192. ResDataObject DeviceResource;
  193. int idx = -1;
  194. DWORD NotifyHandleCount = 1;
  195. while (iter != m_ClientList.end())
  196. {
  197. map<CommonLogicClient*, ResDataObject>::iterator iterevent = m_EventList.find(iter->second);
  198. if (iterevent != m_EventList.end())
  199. {
  200. for (int i = 0; i < count; i++)
  201. {
  202. if (iterevent->second.GetFirstOf(EvtName[i]) >= 0)
  203. {
  204. HandleList[NotifyHandleCount] = iterevent->first->GetNotifyHandle();
  205. NotifyHandleCount++;
  206. }
  207. }
  208. }
  209. iter++;
  210. }
  211. HandleList[0] = EventHandle;
  212. int result = -1;
  213. #ifdef _DEBUG
  214. SYSTEMTIME st = { 0 };
  215. GetLocalTime(&st);
  216. printf("========time-%02d:%02d:%02d:%03d, WaitForMultipleObjects begin count : %d==============\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, NotifyHandleCount);
  217. TPRINTA_DEBUG("WaitForMultipleObjects begin count : %d", NotifyHandleCount);
  218. //printf("time-%02d:%02d:%02d:%03d, Data Length = %d. Timeout While Receive data---\n ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, m_RawBuffer.GetPacketLen());
  219. #endif
  220. DWORD nHandleIndex = WaitForMultipleObjects(NotifyHandleCount, HandleList, false, timeout);
  221. #ifdef _DEBUG
  222. GetLocalTime(&st);
  223. printf("========time-%02d:%02d:%02d:%03d, WaitForMultipleObjects: %d==============\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, nHandleIndex);
  224. //printf("time-%02d:%02d:%02d:%03d, Data Length = %d. Timeout While Receive data---\n ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, m_RawBuffer.GetPacketLen());
  225. #endif
  226. //TPRINTA_DEBUG("========WaitForMultipleObjects : %d ==============\n", nHandleIndex);
  227. if (nHandleIndex == WAIT_OBJECT_0)
  228. {
  229. result = nHandleIndex;
  230. }
  231. else if (nHandleIndex > WAIT_OBJECT_0 && nHandleIndex - WAIT_OBJECT_0 < NotifyHandleCount)
  232. {
  233. map<HANDLE, CommonLogicClient*>::iterator iterclient = m_NotifyHandleList.find(HandleList[nHandleIndex - WAIT_OBJECT_0]);
  234. if (iterclient == m_NotifyHandleList.end())
  235. {
  236. return result;
  237. }
  238. iterclient->second->Lock();
  239. while (iterclient->second->IsDataArrived())
  240. {
  241. iterclient->second->ReadCmd(ReadNotify);
  242. string strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
  243. PacketAnalizer::GetPacketContext(&ReadNotify, ResNotify);
  244. //printf("========WaitForNotify Key is : %s ==============\n", strKey.c_str());
  245. TPRINTA_DEBUG("========WaitForNotify Key is : %s ==============\n", strKey.c_str());
  246. for (int i = 0; i < count; i++)
  247. {
  248. if (EvtName[i] == strKey)
  249. {
  250. //if (strKey == "XWINDOWSTATUS")
  251. {
  252. //printf("========WaitForNotify vaule is : %d ==============\n", (int)ResNotify);
  253. TPRINTA_DEBUG("========WaitForNotify vaule is : %d ==============\n", (int)ResNotify);
  254. }
  255. result = i+1;
  256. iterclient->second->UnLock();
  257. return result;
  258. }
  259. }
  260. }
  261. iterclient->second->UnLock();
  262. }
  263. return result;
  264. }
  265. int CAsnycAction::GetResourceValue(const char *AttributeName, ResDataObject &ResValue)
  266. {
  267. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  268. ResDataObject DeviceResource;
  269. int idx = -1;
  270. while (iter != m_ClientList.end())
  271. {
  272. map<CommonLogicClient*, ResDataObject>::iterator iterevent = m_EventList.find(iter->second);
  273. if (iterevent == m_EventList.end() || iterevent->second.GetFirstOf(AttributeName)<0)//如果没有订阅该消息,则不用查找该单元的资源属性值
  274. {
  275. iter++;
  276. continue;
  277. }
  278. DeviceResource.clear();
  279. iter->second->GetDeviceResource(&DeviceResource);
  280. idx=DeviceResource["Attribute"].GetFirstOf(AttributeName);
  281. if (idx >= 0)
  282. {
  283. ResValue = DeviceResource["Attribute"][idx];
  284. break;
  285. }
  286. idx = DeviceResource["Attribute"].GetFirstOf("SubAttribute");
  287. if (idx >= 0)
  288. {
  289. int nsize= DeviceResource["Attribute"]["SubAttribute"].size();
  290. for (int i = 0; i < nsize; i++)
  291. {
  292. if (int subindx=DeviceResource["Attribute"]["SubAttribute"][i].GetFirstOf(AttributeName) >= 0)
  293. {
  294. ResValue = DeviceResource["Attribute"]["SubAttribute"][i][AttributeName];
  295. break;
  296. }
  297. }
  298. }
  299. iter++;
  300. }
  301. return idx;
  302. }
  303. RET_STATUS CAsnycAction::SetAction(const char *ActionName, ResDataObject &Req, ResDataObject &Resp)
  304. {
  305. map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
  306. ResDataObject DeviceResource;
  307. int idx = -1;
  308. RET_STATUS ret = RET_NOSUPPORT;
  309. while (iter != m_ClientList.end())
  310. {
  311. iter->second->GetDeviceResource(&DeviceResource);
  312. if (DeviceResource.GetFirstOf("Action") >= 0)
  313. {
  314. idx = DeviceResource["Action"].GetFirstOf(ActionName);
  315. if (idx >= 0)
  316. {
  317. ret = (RET_STATUS) iter->second->Action(ActionName, Req, Resp, ACTIONTIMEOUT);
  318. if (ret != RET_SUCCEED)
  319. {
  320. TPRINTA_ERROR("Action %s return result is %d", ActionName, ret);
  321. }
  322. }
  323. }
  324. iter++;
  325. }
  326. return ret;
  327. }
  328. RET_STATUS CAsnycAction::ClearNotify()
  329. {
  330. ResDataObject ReadNotify;
  331. string strKey = "";
  332. map<HANDLE, CommonLogicClient*>::iterator iterNotify = m_NotifyHandleList.begin();
  333. while (iterNotify != m_NotifyHandleList.end())
  334. {
  335. //DWORD ret = WaitForSingleObject(iterNotify->first, 0);
  336. //if (ret == WAIT_OBJECT_0)
  337. //{
  338. while (iterNotify->second->IsDataArrived())
  339. {
  340. iterNotify->second->ReadCmd(ReadNotify);
  341. strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
  342. TPRINTA_DEBUG("========ClearNotify Key is : %s ==============\n", strKey.c_str());
  343. }
  344. //}
  345. iterNotify++;
  346. }
  347. return RET_SUCCEED;
  348. }