#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::iterator iter = m_ClientList.begin(); while (iter != m_ClientList.end()) { /*map::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::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::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::iterator iter = m_ClientList.begin(); ResDataObject DeviceResource; int idx = -1; while (iter != m_ClientList.end()) { map::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::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::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::iterator iter = m_ClientList.begin(); ResDataObject DeviceResource; int idx = -1; DWORD NotifyHandleCount = 1; while (iter != m_ClientList.end()) { map::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::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::iterator iter = m_ClientList.begin(); ResDataObject DeviceResource; int idx = -1; while (iter != m_ClientList.end()) { map::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::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::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; }