123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #include "StdAfx.h"
- #include "AsnycAction.h"
- #include "PacketAnalizer.h"
- #define SeqAction "Action"
- #define SeqEvent "Event"
- #define SpotMaxNum 10
- #define ACTIONTIMEOUT 60000
- CAsnycAction::CAsnycAction()
- {
- }
- CAsnycAction::~CAsnycAction()
- {
- }
- RET_STATUS CAsnycAction::OpenAllClient(const char *workpath[], int count)
- {
- for (int i = 0; i < count; i++)
- {
- CommonLogicClient* pClient = new CommonLogicClient();
- if (RET_SUCCEED == pClient->Open(workpath[i], ACTION | NOTIFY_UPDATE))
- {
- m_ClientList[workpath[i]] = pClient;
- m_NotifyHandleList[pClient->GetNotifyHandle()] = pClient;
- }
- else
- {
- CloseAllClient(workpath, i);
- m_ClientList.clear();
- return RET_FAILED;
- }
- }
- GetClientSeqResouce();
- return RET_SUCCEED;
- }
- RET_STATUS CAsnycAction::CloseAllClient(const char *workpath[], int count )
- {
- if (workpath == NULL&&count == 0)
- {
- //m_SeqList.clear();
- m_EventList.clear();
- m_NotifyHandleList.clear();
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
-
- while (iter != m_ClientList.end())
- {
- /*map<CommonLogicClient*, ResDataObject>::iterator iter2 = m_SeqList.find(iter->second);
- if (iter2 != m_SeqList.end())
- {
- m_SeqList.erase(iter2);
- }*/
- iter->second->Lock();
- iter->second->UnLock();
- iter->second->Close();
- delete iter->second;
- iter->second = NULL;
- iter++;
- }
- m_ClientList.clear();
- }
- else
- {
- //m_SeqList.clear();
- m_EventList.clear();
- m_NotifyHandleList.clear();
- for (int i = 0; i < count; i++)
- {
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.find(workpath[i]);
- if (iter != m_ClientList.end())
- {
- iter->second->Lock();
- iter->second->UnLock();
- iter->second->Close();
- delete iter->second;
- iter->second = NULL;
- m_ClientList.erase(iter);
- }
- }
- }
- if (m_ClientList.size() == 0)
- {
-
- }
- return RET_SUCCEED;
- }
- RET_STATUS CAsnycAction::GetClientSeqResouce()
- {
- ResDataObject Request, ResSEQ;
- /*m_SeqList.clear();
- m_EventList.clear();
- m_NotifyHandleList.clear();*/
- m_SeqList.clear();
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
- while (iter != m_ClientList.end())
- {
- if (RET_SUCCEED == iter->second->Action("GetSEQResource", Request, ResSEQ, 1000))
- {
- ResDataObject SeqResource, EventResource;
- if (ResSEQ.GetFirstOf(SeqAction) >= 0)
- {
- SeqResource = ResSEQ[SeqAction];
- }
- m_SeqList[iter->second] = SeqResource;
- if (ResSEQ.GetFirstOf(SeqEvent) >= 0)
- {
- EventResource = ResSEQ[SeqEvent];
- }
- m_EventList[iter->second] = EventResource;
- }
- else
- {
- CloseAllClient();
- return RET_FAILED;
- }
- iter++;
- }
- return RET_SUCCEED;
- }
- RET_STATUS CAsnycAction::SetAsnycAction(const char *ActionName, DWORD timeout)
- {
- timeout = ACTIONTIMEOUT;
- ResDataObject Request, ResSEQ;
- HANDLE HandleList[SpotMaxNum];
- CommonLogicClient* ClientList[SpotMaxNum];
- int result = RET_SUCCEED;
- int nHandIndex = 0;
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
- ResDataObject DeviceResource;
- int idx = -1;
- while (iter != m_ClientList.end())
- {
- map<CommonLogicClient*, ResDataObject>::iterator iterseq = m_SeqList.find(iter->second);
- if (iterseq == m_SeqList.end() || iterseq->second.GetFirstOf(ActionName) < 0)//如果没有订阅该消息,则不用查找该单元的资源属性值
- {
- iter++;
- continue;
- }
- iter->second->Lock();
- result = iter->second->Action_Req(ActionName, Request, timeout);
- TPRINTA_DEBUG("ActionName:%s, Unit: %s", ActionName, iter->first.c_str());
- HandleList[nHandIndex] = iter->second->GetResponseHandle();
- ClientList[nHandIndex] = iter->second;
- nHandIndex++;
- if (RET_SUCCEED != result)
- {
- TPRINTA_DEBUG("ActionName:%s, result: %d ,return", ActionName, result);
- break;
- }
- iter++;
- }
- if (RET_SUCCEED == result)
- {
- int count = 0;
- while (count != nHandIndex)
- {
- DWORD eventindex = WaitForMultipleObjects(nHandIndex, HandleList, false, timeout);
- if (eventindex == WAIT_TIMEOUT)
- {
- TPRINTA_DEBUG("ActionName:%s, Timeout, return", ActionName);
- result = RET_TIMEOUT;
- break;
- }
- else
- {
- /*map<HANDLE, CommonLogicClient*>::iterator iterclient = m_NotifyHandleList.find(HandleList[eventindex]);
- if (iterclient != m_NotifyHandleList.end())
- {
- iterclient->second->Action_Res(ActionName, ResSEQ, timeout);
- count++;
- }*/
- ClientList[eventindex]->Action_Res(ActionName, ResSEQ, timeout);
- count++;
-
- }
- }
-
- }
- iter = m_ClientList.begin();
- while (iter != m_ClientList.end())
- {
- map<CommonLogicClient*, ResDataObject>::iterator iterseq = m_SeqList.find(iter->second);
- if (iterseq == m_SeqList.end() || iterseq->second.GetFirstOf(ActionName) < 0)//如果没有订阅该消息,则不用查找该单元的资源属性值
- {
- iter++;
- continue;
- }
- iter->second->UnLock();
- iter++;
- }
- return (RET_STATUS)result;
- }
- int CAsnycAction::WaitforEventNotify(HANDLE &EventHandle, const char *EvtName[], int count, ResDataObject &ResNotify, DWORD timeout)
- {
- ResDataObject Request, ResSEQ, ReadNotify;
- ResNotify.clear();
- HANDLE HandleList[SpotMaxNum];
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
- ResDataObject DeviceResource;
- int idx = -1;
- DWORD NotifyHandleCount = 1;
- while (iter != m_ClientList.end())
- {
- map<CommonLogicClient*, ResDataObject>::iterator iterevent = m_EventList.find(iter->second);
- if (iterevent != m_EventList.end())
- {
- for (int i = 0; i < count; i++)
- {
- if (iterevent->second.GetFirstOf(EvtName[i]) >= 0)
- {
- HandleList[NotifyHandleCount] = iterevent->first->GetNotifyHandle();
- NotifyHandleCount++;
- }
- }
-
- }
- iter++;
- }
- HandleList[0] = EventHandle;
- int result = -1;
- #ifdef _DEBUG
- SYSTEMTIME st = { 0 };
- GetLocalTime(&st);
- printf("========time-%02d:%02d:%02d:%03d, WaitForMultipleObjects begin count : %d==============\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, NotifyHandleCount);
- TPRINTA_DEBUG("WaitForMultipleObjects begin count : %d", NotifyHandleCount);
- //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());
- #endif
-
- DWORD nHandleIndex = WaitForMultipleObjects(NotifyHandleCount, HandleList, false, timeout);
- #ifdef _DEBUG
- GetLocalTime(&st);
- printf("========time-%02d:%02d:%02d:%03d, WaitForMultipleObjects: %d==============\n", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, nHandleIndex);
- //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());
- #endif
- //TPRINTA_DEBUG("========WaitForMultipleObjects : %d ==============\n", nHandleIndex);
- if (nHandleIndex == WAIT_OBJECT_0)
- {
- result = nHandleIndex;
- }
- else if (nHandleIndex > WAIT_OBJECT_0 && nHandleIndex - WAIT_OBJECT_0 < NotifyHandleCount)
- {
- map<HANDLE, CommonLogicClient*>::iterator iterclient = m_NotifyHandleList.find(HandleList[nHandleIndex - WAIT_OBJECT_0]);
- if (iterclient == m_NotifyHandleList.end())
- {
- return result;
- }
- iterclient->second->Lock();
- while (iterclient->second->IsDataArrived())
- {
- iterclient->second->ReadCmd(ReadNotify);
-
- string strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
- PacketAnalizer::GetPacketContext(&ReadNotify, ResNotify);
- //printf("========WaitForNotify Key is : %s ==============\n", strKey.c_str());
- TPRINTA_DEBUG("========WaitForNotify Key is : %s ==============\n", strKey.c_str());
- for (int i = 0; i < count; i++)
- {
- if (EvtName[i] == strKey)
- {
- //if (strKey == "XWINDOWSTATUS")
- {
- //printf("========WaitForNotify vaule is : %d ==============\n", (int)ResNotify);
- TPRINTA_DEBUG("========WaitForNotify vaule is : %d ==============\n", (int)ResNotify);
- }
- result = i+1;
- iterclient->second->UnLock();
- return result;
- }
- }
- }
- iterclient->second->UnLock();
- }
- return result;
- }
- int CAsnycAction::GetResourceValue(const char *AttributeName, ResDataObject &ResValue)
- {
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
- ResDataObject DeviceResource;
- int idx = -1;
- while (iter != m_ClientList.end())
- {
-
- map<CommonLogicClient*, ResDataObject>::iterator iterevent = m_EventList.find(iter->second);
- if (iterevent == m_EventList.end() || iterevent->second.GetFirstOf(AttributeName)<0)//如果没有订阅该消息,则不用查找该单元的资源属性值
- {
- iter++;
- continue;
- }
- DeviceResource.clear();
- iter->second->GetDeviceResource(&DeviceResource);
- idx=DeviceResource["Attribute"].GetFirstOf(AttributeName);
- if (idx >= 0)
- {
- ResValue = DeviceResource["Attribute"][idx];
- break;
- }
- idx = DeviceResource["Attribute"].GetFirstOf("SubAttribute");
- if (idx >= 0)
- {
- int nsize= DeviceResource["Attribute"]["SubAttribute"].size();
- for (int i = 0; i < nsize; i++)
- {
- if (int subindx=DeviceResource["Attribute"]["SubAttribute"][i].GetFirstOf(AttributeName) >= 0)
- {
- ResValue = DeviceResource["Attribute"]["SubAttribute"][i][AttributeName];
- break;
- }
- }
- }
- iter++;
- }
- return idx;
- }
- RET_STATUS CAsnycAction::SetAction(const char *ActionName, ResDataObject &Req, ResDataObject &Resp)
- {
- map<string, CommonLogicClient*>::iterator iter = m_ClientList.begin();
- ResDataObject DeviceResource;
- int idx = -1;
- RET_STATUS ret = RET_NOSUPPORT;
- while (iter != m_ClientList.end())
- {
- iter->second->GetDeviceResource(&DeviceResource);
- if (DeviceResource.GetFirstOf("Action") >= 0)
- {
- idx = DeviceResource["Action"].GetFirstOf(ActionName);
- if (idx >= 0)
- {
- ret = (RET_STATUS) iter->second->Action(ActionName, Req, Resp, ACTIONTIMEOUT);
- if (ret != RET_SUCCEED)
- {
- TPRINTA_ERROR("Action %s return result is %d", ActionName, ret);
- }
- }
- }
- iter++;
- }
-
- return ret;
- }
- RET_STATUS CAsnycAction::ClearNotify()
- {
- ResDataObject ReadNotify;
- string strKey = "";
- map<HANDLE, CommonLogicClient*>::iterator iterNotify = m_NotifyHandleList.begin();
- while (iterNotify != m_NotifyHandleList.end())
- {
- //DWORD ret = WaitForSingleObject(iterNotify->first, 0);
- //if (ret == WAIT_OBJECT_0)
- //{
- while (iterNotify->second->IsDataArrived())
- {
- iterNotify->second->ReadCmd(ReadNotify);
- strKey = PacketAnalizer::GetPacketKey(&ReadNotify);
- TPRINTA_DEBUG("========ClearNotify Key is : %s ==============\n", strKey.c_str());
- }
- //}
- iterNotify++;
- }
- return RET_SUCCEED;
- }
|