BOOL GetMonitorListByFile(CMonitorList &lstMonitor,const char *FileTitle) { CString strFileName; strFileName.Format("%s/MonitorManager/%s.txt",g_strRootPath.getText(),FileTitle); puts(strFileName); FILE *pf=fopen(strFileName.getText(),"r+"); char buf[256]={0}; if(pf) { CString strid=""; LoadConfig lc; lc.LoadAll(); while(!feof(pf)) { char *p=fgets(buf,255,pf); //puts(p); if(p!=NULL) { strid=p; strid.Replace("\r",""); strid.Replace("\n",""); Monitor *pM=new Monitor; try{ if(lc.CreateSingleMonitor(pM,strid.getText())) { puts("Create ok"); lstMonitor.push_back(pM); puts("push ok"); } else { puts("Create failed"); delete pM; } memset(buf,0,256); }catch(...) { delete pM; } }else break; } fclose(pf); }else throw MSException("Open file failed"); return (lstMonitor.size()>0); }
/*! \fn ThreadContrl::RefreshMonitors(CMonitorList &lstMonitor) */ void ThreadContrl::RefreshMonitors(CMonitorList &lstMonitor) { if(lstMonitor.size()==1) { puts("Enter"); Monitor *pm=lstMonitor.front(); ExecuteMonitor(pm); lstMonitor.pop_front(); delete pm; return ; } CMonitorList::iterator it; for(it=lstMonitor.begin();it!=lstMonitor.end();it++) { Monitor *pM=*it; pM->m_isRefresh=true; while(1) { MonitorThread *pThread=(MonitorThread*)GetIdleThread(); if(pThread) { pThread->ExecuteMonitor(pM); break; } else { ThreadEx::sleep(1000); continue; } } } //wait for monitor complete BOOL hasRuning; for(int n=0;n<300;n++) { hasRuning=TRUE; for(it=lstMonitor.begin();it!=lstMonitor.end();it++) { if((*it)->GetRunning()) { hasRuning=FALSE; break; } } if(hasRuning) break; ThreadEx::sleep(1000); } }
bool MakeMonitorListByBuf(CMonitorList &lstMonitor,const char *buf) { if(g_pOption==NULL) return false; const char *pt=buf; string str; LoadConfig lc; lc.m_pOption=g_pOption; lc.LoadAll(); do{ str=pt; if(str.empty()) break; Monitor *pM=new Monitor(); if(pM==NULL) break; try{ if(lc.CreateSingleMonitor(pM,str.c_str())) { puts("Create ok"); lstMonitor.push_back(pM); puts("push ok"); } else { puts("Create failed"); delete pM; } }catch(...) { puts("Create exception"); delete pM; } pt+=str.size()+1; }while(pt[0]!='\0'); return true; }
int main(int argc,char *argv[]) { //命令行参数说明 ? printf("\n Process arg %d: ",argc); for(int i=0; i<=argc-1; ++i) printf("%s ",argv[i]); printf("\n"); SetSvdbAddrByFile("mc.config"); if(argc>1) { putil=new Util(); if(argc>4) { puts("Parameter error"); //putil->ErrorLog("Parameter error"); return -1; } puts("Begin refresh"); BOOL isRefresh=FALSE; g_strRootPath=putil->GetRootPath(); try { ThreadContrl *pt=new ThreadContrl(); Option *popt=new Option; if(popt==NULL) { //putil->ErrorLog("Create Option object failed"); return 1; } //load 设置文件 mc.config popt->LoadOption(); g_ServerHost=popt->m_ServerAddress; pt->m_pOption=popt; g_pOption=popt; if(argc==2) { pt->InitRefresh(1); LoadConfig lc; // monitor device group file lc.m_pOption=popt; lc.LoadAll(); Monitor *pM=new Monitor(); if(!lc.CreateSingleMonitor(pM,argv[1])) { puts("Create Monitor failed"); delete pM; return -1; } pt->ExecuteMonitor(pM); delete pM; // return 1; } else if(argc==3) { if(strcmp(argv[1],"-e")==0) { Run_ProcessMonitor(argv[2]); return 1; } pt->InitRefresh(10); isRefresh=TRUE; // putil->InsertSVMQ("SiteviewRefresh","Start",argv[1]); // putil->g_strSession=argv[1]; CMonitorList lstMonitor; if(GetMonitorListByFile(lstMonitor,argv[2])) { printf("Monitor counts is :%d\n",lstMonitor.size()); pt->RefreshMonitors(lstMonitor); } else ; // putil->ErrorLog("Monitor list empty"); // putil->InsertSVMQ("SiteviewRefresh","End",argv[1]); } else if(argc==4) { char buf[256]={0}; pt->InitRefresh(30); printf("Parameter:%s,%s,%s\n",argv[1],argv[2],argv[3]); CMonitorList lstMonitor; if(GetMonitorListByPipe(lstMonitor,argv[1],argv[2],argv[3])) { printf("Monitor counts is :%d\n",lstMonitor.size()); g_RefreshQueueName=argv[2]; g_QueueAddr=argv[3]; pt->RefreshMonitors(lstMonitor); }else { // putil->ErrorLog("Refresh Monitors empty"); puts("eeeeeeee"); } sprintf(buf,"Refresh end"); ::PushMessage(argv[2],"Refresh_END",buf,strlen(buf)+1,"default",argv[3]); // ::Sleep(200000000); } } catch(MSException &e) { // if(isRefresh) // putil->InsertSVMQ("SiteviewRefresh","End",argv[1]); //putil->ErrorLog(e.GetDescription()); return -1; } if( Univ::enablemass && putil!=NULL ) { std::list<SingelRecord> listrcd; int count(0); if( (count=putil->AppendThenClearAllRecords(listrcd))>0 ) cout<<"AppendMass "<<count<<" records done,"<<" slept "<<Univ::msappend<<" ms."<<endl; else if(count<0) cout<<"AppendMassRecord failed!"<<endl; } Sleep(3000); return 1; } #ifdef WIN32 HANDLE hCommEvent=::CreateEvent(NULL,TRUE,FALSE,"Global\\Siteview-Communictions"); if(hCommEvent==NULL) { puts("Create event of communication failed"); return -1; } if(::GetLastError()!=ERROR_ALREADY_EXISTS) { puts("Parent process isn't exist"); //return -2; } ::SetLastError(0); #endif try { Run(); } catch(MSException &e) { //putil->ErrorLog(e.GetDescription()); WriteLog( e.GetDescription() ); return -3; } #ifdef WIN32 MSG msg; BOOL bRet=TRUE; while((bRet=::GetMessage(&msg,NULL,0,0))!=0) { // MemLeakCheckAndFree(); if(bRet==-1) { continue; } if(msg.message==WM_COMMUNICATION) { CTime curt=CTime::GetCurrentTimeEx(); svutil::TTimeSpan tsp=curt-g_LastSchTime; char text[256]={0}; sprintf(text,"@@@@ COMMUNICATION, min:%d ,%s -- %s @@@@\n",tsp.GetTotalMinutes(),g_LastSchTime.Format().c_str(),curt.Format().c_str()); puts(text); OutputDebugString(text); if(tsp.GetTotalMinutes()<2) ::ResetEvent(hCommEvent); else { string show= " Self-exit MonitorSchedule! "; show+=text; //putil->ErrorLog(show.c_str()); WriteLog( show.c_str() ); OutputDebugString(show.c_str()); ::Sleep(100); return 1; } } TranslateMessage(&msg); ::DispatchMessage(&msg); } if( Univ::enablemass && putil!=NULL ) { std::list<SingelRecord> listrcd; int count(0); if( (count=putil->AppendThenClearAllRecords(listrcd))>0 ) cout<<"AppendMass "<<count<<" records done,"<<" slept "<<Univ::msappend<<" ms."<<endl; else if(count<0) cout<<"AppendMassRecord failed!"<<endl; } if(pMain!=NULL) { if(pMain->m_bDataChange) pMain->SerialMonitorData(true); } #else while(true) { ThreadEx::sleep(2000000); } #endif return 1; }
void testmonitor(void) { putil=new Util; try{ putil->Init(); LoadConfig lc; lc.LoadAll(); CMonitorList Monitorlst; lc.CreateMonitors(Monitorlst); int count =Monitorlst.size(); printf("%d monitor be load\n",count); //Monitor *pM=NULL; //POSITION pos=Monitorlst.GetHeadPosition(); //POSITION pos2=NULL,pos3=NULL; CMonitorList::iterator it=Monitorlst.begin(); ReturnData* prd=NULL; int n=0; while(Monitorlst.size()>0) { // pM=Monitorlst.RemoveTail(); Monitor*pM=Monitorlst.front(); Monitorlst.pop_front(); // pM=Monitorlst.GetNext(pos); printf("\n------------MonitorID:%s----------------\n",pM->GetMonitorID()); printf("MonitorID:%s\nClass:%s\nMonitorType:%d\nSEID:%s\nParentID:%s\nFrequency:%d\n", \ (char*)pM->GetMonitorID(),(char*)pM->GetMonitorClass(),pM->GetMonitorType(),pM->GetSEID().c_str(),pM->GetParentID().c_str(), \ pM->GetFrequency()); puts("\n@@Parameter list@@"); CStringList &lst=pM->GetParameterList(); CStringList::iterator it2; it2=lst.begin(); while(it2!=lst.end()) printf("%s\n",*it2++); puts("\n+++ReturnList+++\n"); CReturnDataList&rdl=pM->GetReutrnList(); CReturnDataList::iterator it3; // pos3=rdl.GetHeadPosition(); it3=rdl.begin(); while(it3!=rdl.end()) { //prd=rdl.GetNext(pos3); prd=*it3++; printf("Label:%s\nName:%s\nUnit:%s\nType:%s\n",(char *)prd->m_Label,(char *)prd->m_Name,(char *)prd->m_Unit,(char *)prd->m_Type); } puts("\n===StateCondition===\n"); StateCondition ** sct=pM->GetStateCondition(); StateCondition *psct=sct[0]; printf("Expression:%s\nStateType:%d\n",(char *)psct->m_Expression,psct->m_Type); CStateConditionItemList&Item=psct->GetStateConditionList(); //pos2=Item.GetHeadPosition(); CStateConditionItemList::iterator it4; it4=Item.begin(); while(it4!=Item.end()) { // StateConditionItem *psti=Item.GetNext(pos2); StateConditionItem *psti=*it4++; printf("ItemID:%d\nOperator:%s\nName:%s\nValue:%s\n",psti->m_ItemID,(char*)psti->m_Operator,(char*)psti->m_ParamName,(char *)psti->m_ParamValue); } psct=sct[1]; printf("Expression:%s\nStateType:%d\n",(char *)psct->m_Expression,psct->m_Type); CStateConditionItemList&Item2=psct->GetStateConditionList(); // pos2=Item2.GetHeadPosition(); it4=Item2.begin(); while(it4!=Item2.end()) { // StateConditionItem *psti=Item2.GetNext(pos2); StateConditionItem *psti=*it4++; printf("ItemID:%d\nOperator:%s\nName:%s\nValue:%s\n",psti->m_ItemID,(char*)psti->m_Operator,(char*)psti->m_ParamName,(char *)psti->m_ParamValue); } psct=sct[2]; printf("Expression:%s\nStateType:%d\n",(char *)psct->m_Expression,psct->m_Type); CStateConditionItemList&Item3=psct->GetStateConditionList(); // pos2=Item3.GetHeadPosition(); it4=Item3.begin(); while(it4!=Item3.end()) { // StateConditionItem *psti=Item3.GetNext(pos2); StateConditionItem *psti=*it4++; printf("ItemID:%d\nOperator:%s\nName:%s\nValue:%s\n",psti->m_ItemID,(char*)psti->m_Operator,(char*)psti->m_ParamName,(char *)psti->m_ParamValue); } n++; delete pM; } printf("Total %d monitors\n",n); } catch(MSException &e) { printf("putil init exception:%s\n",e.Description); return ; } }
int SerialData::Serial(FILE *pf,CMonitorList &MonitorList,Groups *pGroups,Subsequent *pSubsequent,TASKMAP &TaskMap,bool out) { if(pf==NULL) return -2; STRINGMAP strmap; if(out) { fputs("//****SiteView ECC serial data file****\r\n",pf); fputs("//****Data version 7.01****\r\n",pf); fputs("[MonitorList_Begin]\r\n",pf); CMonitorList::iterator it; for(it=MonitorList.begin();it!=MonitorList.end();it++) { strmap.clear(); if(!(*it)->ToTextMap(strmap)) continue; fputs("[Monitor_Begin]\r\n",pf); SerialStringMap(strmap,pf); fputs("[Monitor_End]\r\n",pf); } fputs("[MonitorList_End]\r\n",pf); strmap.clear(); if(!pGroups->ToTextMap(strmap)) { fclose(pf); return -3; } fputs("[Groups_Begin]\r\n",pf); SerialStringMap(strmap,pf); fputs("[Groups_End]\r\n",pf); strmap.clear(); if(!pSubsequent->ToTextMap(strmap)) { fclose(pf); return -4; } fputs("[Subsequent_Begin]\r\n",pf); SerialStringMap(strmap,pf); fputs("[Subsequent_End]\r\n",pf); fputs("[TaskMap_Begin]\r\n",pf); TASKMAP::iterator tit; Task *ptask=NULL; while(TaskMap.findnext(tit)) { strmap.clear(); if((ptask=(*tit).getvalue())==NULL) continue; if(!ptask->ToTextMap(strmap)) continue; fputs("[Task_Begin]\r\n",pf); SerialStringMap(strmap,pf); fputs("[Task_End]\r\n",pf); } fputs("[TaskMap_End]\r\n",pf); fclose(pf); }else { string sline; char line[2048]={0}; char *pc=NULL; pc=fgets(line,2048,pf); if(pc==NULL) return -5; sline=line; int pos=0; if((pos=(int)sline.find("//"))!=0) return -6; if((pos=(int)sline.find("SiteView ECC serial data file"))<0) return -7; pc=fgets(line,2048,pf); if(pc==NULL) return -10; sline=line; if((pos=(int)sline.find("//"))!=0) return -8; if((pos=(int)sline.find("Data version 7.01"))<0) return -9; int i=0; bool flag=false; while(fgets(line,2048,pf)!=NULL) { sline=line; switch(i) { case 0: if((pos=(int)sline.find("[MonitorList_Begin]"))!=0) { continue; } else { flag=true; while(true) { bool mflag=false; strmap.clear(); while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Monitor_Begin]"))!=0) { if((pos=(int)sline.find("[MonitorList_End]"))==0) { flag=false; i++; break; } continue; } else break; } if(pc==NULL) return -11; if(!flag) break; while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Monitor_End]"))==0) { mflag=true; break; } CovertStringToMap(sline,strmap); } if(pc==NULL) return -12; if(mflag) { Monitor *pm=new Monitor(); if(Univ::seid!=1) pm->m_isRefresh= true; if(pm->FromTextMap(strmap)) MonitorList.push_back(pm); } } } break; case 1: if((pos=(int)sline.find("[Groups_Begin]"))!=0) { continue; }else { strmap.clear(); flag=false; while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Groups_End]"))==0) { i++; flag=true; break; } CovertStringToMap(sline,strmap); } if(pc==NULL) return -13; if(flag) pGroups->FromTextMap(strmap); } break; case 2: if((pos=(int)sline.find("[Subsequent_Begin]"))!=0) { continue; }else { strmap.clear(); flag=false; while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Subsequent_End]"))==0) { i++; flag=true; break; } CovertStringToMap(sline,strmap); } if(pc==NULL) return -13; if(flag) pSubsequent->FromTextMap(strmap); } break; case 3: if((pos=(int)sline.find("[TaskMap_Begin]"))!=0) { continue; }else { flag=true; while(true) { bool mflag=false; strmap.clear(); while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Task_Begin]"))!=0) { if((pos=(int)sline.find("[TaskMap_End]"))==0) { flag=false; i++; break; } continue; } else break; } if(pc==NULL) return -11; if(!flag) { return 0; } while((pc=fgets(line,2048,pf))!=NULL) { sline=line; if((pos=(int)sline.find("[Task_End]"))==0) { mflag=true; break; } CovertStringToMap(sline,strmap); } if(pc==NULL) return -12; if(mflag) { Task *pm=new Task(); if(pm->FromTextMap(strmap)) TaskMap[pm->m_taskname.c_str()]=pm; } } } break; case 4: return 0; default :break; } } } return 0; }