/***********************************************************************//** * @brief Read Good Time Intervals from XML element * * @param[in] xml XML element. * * @exception GException::invalid_value * Invalid XML format encountered. * * Read Good Time Intervals from an XML element. The format of the Good Time * Intervals is * * <parameter name="GoodTimeIntervals" tmin="..." tmax="..."/> * * The units of the @a tmin and @a tmax parameters are seconds. * * Note that the method also expects that the time reference is provided * as parameter in the @p xml element. ***************************************************************************/ void GGti::read(const GXmlElement& xml) { // Clear energy boundaries clear(); // Read time reference m_reference.read(xml); // Get energy boundaries parameter const GXmlElement* par = gammalib::xml_get_par(G_READ_XML, xml, "GoodTimeIntervals"); // Extract tmin and tmax attributes if (par->has_attribute("tmin") && par->has_attribute("tmax")) { double tmin = gammalib::todouble(par->attribute("tmin")); double tmax = gammalib::todouble(par->attribute("tmax")); append(GTime(tmin, m_reference), GTime(tmax, m_reference)); } else { std::string msg = "Attributes \"tmin\" and/or \"tmax\" not found" " in XML parameter \"GoodTimeIntervals\"." " Please verify the XML format."; throw GException::invalid_value(G_READ_XML, msg); } // Return return; }
/***********************************************************************//** * @brief Test GTimes ***************************************************************************/ void TestGObservation::test_times(void) { // Test void constructor test_try("Void constructor"); try { GTimes times; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GTimes starting from an empty object GTimes times; test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Add a time times.append(GTime()); test_value(times.size(), 1, "GTimes should have 1 time."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Remove time times.remove(0); test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Append two times times.append(GTime()); times.append(GTime()); test_value(times.size(), 2, "GTimes should have 2 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Clear object times.clear(); test_value(times.size(), 0, "GTimes should have zero size."); test_assert(times.is_empty(), "GTimes should be empty."); // Insert two times times.insert(0, GTime()); times.insert(0, GTime()); test_value(times.size(), 2, "GTimes should have 2 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Extend times times.extend(times); test_value(times.size(), 4, "GTimes should have 4 times."); test_assert(!times.is_empty(), "GTimes should not be empty."); // Return return; }
int Info(const char* format,...){ char buff[8192]; va_list args; int count=0; va_start(args,format); count=vsnprintf(buff,sizeof(buff)/sizeof(char),format,args); va_end(args); fprintf( stdout , "[%d %d] - %s" , GDate() , GTime() , buff ); return count; }
int SafeWriteLog(int fd,const char *cate,const char *format,...){ char buff[8192]; va_list args; int count=0; va_start(args,format); //Add timestamp size=18 sprintf(buff,"[%d %d] ",GDate(),GTime()); //Info( "Buff=%s\n", buff ) ; count=vsnprintf(buff+18,8192-18,format,args); va_end(args); SafeWrite( fd , buff , strlen(buff)); return count; }
int FLog(int fd,const char *cate,const char *format,...){ char buff[8192]; char head[30]; va_list args; int count=0; if( fd <= 0 ) return 0; va_start(args,format); count=vsnprintf(buff,sizeof(buff)/sizeof(char),format,args); va_end(args); sprintf(head,"[%d %d] %s - " , GDate() , GTime() , cate ); write( fd , head , strlen(head)); write( fd , buff , strlen(buff)); return count; }
/***********************************************************************//** * @brief Test GGti ***************************************************************************/ void TestGObservation::test_gti(void) { // Test void constructor test_try("Void constructor"); try { GGti gti; test_try_success(); } catch (std::exception &e) { test_try_failure(e); } // Manipulate GTIs starting from an empty object GGti gti; test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Add empty interval gti.append(GTime(1.0), GTime(1.0)); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Add one interval gti.append(GTime(1.0), GTime(10.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 10.0, 1.0e-10, "Stop time should be 10."); // Remove interval gti.remove(0); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Append two overlapping intervals gti.append(GTime(1.0), GTime(100.0)); gti.append(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Clear object gti.clear(); test_value(gti.size(), 0, "GGti should have zero size."); test_assert(gti.is_empty(), "GGti should be empty."); test_value(gti.tstart().secs(), 0.0, 1.0e-10, "Start time should be 0."); test_value(gti.tstop().secs(), 0.0, 1.0e-10, "Stop time should be 0."); // Append two overlapping intervals in inverse order gti.clear(); gti.append(GTime(10.0), GTime(1000.0)); gti.append(GTime(1.0), GTime(100.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Insert two overlapping intervals gti.clear(); gti.insert(GTime(1.0), GTime(100.0)); gti.insert(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Insert two overlapping intervals in inverse order gti.clear(); gti.insert(GTime(10.0), GTime(1000.0)); gti.insert(GTime(1.0), GTime(100.0)); test_value(gti.size(), 2, "GGti should have 2 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Merge two overlapping intervals gti.clear(); gti.merge(GTime(1.0), GTime(100.0)); gti.merge(GTime(10.0), GTime(1000.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Merge two overlapping intervals in inverse order gti.clear(); gti.merge(GTime(10.0), GTime(1000.0)); gti.merge(GTime(1.0), GTime(100.0)); test_value(gti.size(), 1, "GGti should have 1 interval."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Check extension gti.clear(); gti.append(GTime(1.0), GTime(10.0)); gti.append(GTime(10.0), GTime(100.0)); GGti ext; ext.append(GTime(100.0), GTime(1000.0)); gti.extend(ext); test_value(gti.size(), 3, "GGti should have 3 intervals."); test_assert(!gti.is_empty(), "GGti should not be empty."); test_value(gti.tstart(0).secs(), 1.0, 1.0e-10, "Bin 0 start time should be 1."); test_value(gti.tstart(1).secs(), 10.0, 1.0e-10, "Bin 1 start time should be 10."); test_value(gti.tstart(2).secs(), 100.0, 1.0e-10, "Bin 2 start time should be 100."); test_value(gti.tstop(0).secs(), 10.0, 1.0e-10, "Bin 0 stop time should be 10."); test_value(gti.tstop(1).secs(), 100.0, 1.0e-10, "Bin 1 stop time should be 100."); test_value(gti.tstop(2).secs(), 1000.0, 1.0e-10, "Bin 2 stop time should be 1000."); test_value(gti.tstart().secs(), 1.0, 1.0e-10, "Start time should be 1."); test_value(gti.tstop().secs(), 1000.0, 1.0e-10, "Stop time should be 1000."); // Return return; }
/* *REG /DATA=表名 参数设置 位置未确定 *调用各个算法 */ bool TCManager::algorithmic(TCPacket &inP,string strName,AnaWord &aw, TCPacket &outP) { CTString spath; TCSystem::GetTempPath(spath); spath = spath + "Sys.dat"; //--UNIX PORT SYSTEMTIME st; GetLocalTime(&st); if ( GTime(st) >= Expire ) { if(_access(spath.GetData(),0))//-- file not existed { return SetResultState(false,outP,GetLanguage(UserIDIsNotFound)); } } /* #ifdef WINPLAT WIN32_FIND_DATA wfd; if (FindFirstFile(spath.GetData(),&wfd) != INVALID_HANDLE_VALUE) { return SetResultState(false,outP,GetLanguage(UserIDIsNotFound)); } #else #endif */ int nUserID = GetUserID(inP); if (nUserID==0) return SetResultState(false,outP,GetLanguage(UserIDIsNotFound)); bool bMeOpenTable = false; //是否为c++自己打开的 string sTableName = ""; int nCount = aw.GetWordCount(); for (int i=0;i<nCount;i++) { if (aw.GetAt(i) == "/DATA" && i< nCount-2 ) { sTableName = aw.GetAt(i+2); } } if (sTableName.size()==0) return SetResultState(false,outP,GetLanguage(FormatIsError)); CDataInterface *pDI = g_system.GetCurData(nUserID,sTableName); if (pDI ==NULL) { if (g_system.OpenTable(nUserID,sTableName,sTableName)==false) return SetResultState(false,outP,GetLanguage(OperationFail)); pDI = g_system.GetCurData(nUserID,sTableName); if (pDI ==NULL) return SetResultState(false,outP,GetLanguage(UserTableIsNotFound)); bMeOpenTable = true; } CTString str = aw.GetAt(0); CTString sDllName, sProcName; int nProcType; if (TCSystem::GetModuleInfo(str,sDllName, sProcName,nProcType)==true) { //--UNIX PORT //#ifdef WINPLAT HINSTANCE hmod = LoadLibrary(sDllName.GetData()); if (hmod==NULL) //没有找到对应的DLL { if (bMeOpenTable) //自己释放 g_system.CloseTable(nUserID, sTableName); SetResultState(false,outP,GetLanguage(DLLFileNotFind));//"没有找到对应的DLL" return false; } if (nProcType ==2) { typedef int (*pTrain)(CDataInterface *pData, CEnvParam *pEnv, Interaction *itt, string VO, string &ppOutStruct,CResult **ppRst); pTrain proc; proc = (pTrain)GetProcAddress(hmod,sProcName.GetData()); if (proc==NULL) //"没有找到对应的过程名" { if (bMeOpenTable) //自己释放 g_system.CloseTable(nUserID, sTableName); SetResultState(false,outP,GetLanguage(ProcNameNotFind)); return false; } Interaction itTemp; string VO = strName; string ppOutStruct; CResult *pRst = NULL; int rst =(*proc)(pDI,g_system.GetCurEnv(0),&itTemp,VO,ppOutStruct,&pRst); if (pRst) { int nBuflen = pRst->GetBufLen(); BYTE *pOut = new BYTE[nBuflen]; BYTE *pTemp = pOut; pRst->Serialize(pTemp); outP.SetPacketName("Result"); delete pRst; outP.AddItem("Out",( char*)pOut,nBuflen); delete []pOut; } //////////////////释放资源/////////////////// if (bMeOpenTable) g_system.CloseTable(nUserID, sTableName); ::FreeLibrary((HMODULE)hmod); //////////////////释放资源/////////////////// if (rst != 0) return SetResultState(false,outP,""); else return SetResultState(true,outP); } else {//===temp } //#else // int rc; /* return codes */ // void *FunctionLib =NULL; /* Handle to shared lib file */ // //int (*Function)(int a); /* Pointer to loaded routine */ // //typedef int (*FunctionA)(int a); // typedef int (*pTrain)(CDataInterface *pData, CEnvParam *pEnv, Interaction *itt, string VO, string &ppOutStruct,CResult **ppRst); // const char *dlError; /* Pointer to error string */ // // //FunctionLib = dlopen("/share/b/low.so",RTLD_LAZY); // FunctionLib = dlopen(sDllName.GetData(),RTLD_LAZY); // dlError = dlerror(); // if( dlError ) // { // printf(" AAA 3-Open Library with absolute path return-%s- \r\n", dlError); // exit(1); // } // Function = (pTrain)dlsym( FunctionLib, sProcName.GetData());// // dlError = dlerror(); // if( dlError ) // { // printf(" AAA 4-Find symbol printUPPERCASE 返回-%s- \r\n", dlError); // exit(1); // } // // Interaction itTemp; // string VO = strName; // string ppOutStruct; // CResult *pRst = NULL; // int rst = (*Function)(pDI,g_system.GetCurEnv(0),&itTemp,VO,ppOutStruct,&pRst); // if (pRst) // { // int nBuflen = pRst->GetBufLen(); // BYTE *pOut = new BYTE[nBuflen]; // BYTE *pTemp = pOut; // pRst->Serialize(pTemp); // outP.SetPacketName("Result"); // delete pRst; // outP.AddItem("Out",( char*)pOut,nBuflen); // delete []pOut; // } // if (rst != 0) // return SetResultState(false,outP,""); // else // return SetResultState(true,outP); // // printf(" AAA 5-printUPPERCASE return-%s- value :%d\r\n", dlError,rc); // // /* 6-Close the shared library handle */ /* Note: after the dlclose, "printUPPERCASE" is not loaded */ // rc = dlclose(FunctionLib); // if( rc ) // { // dlError = dlerror(); // printf(" AAA 6-Close handle return-%s- \r\n",dlError); // exit(1); // } //#endif } //-- end with GetModuleInfo else {//===temp 暂时不处理 } return true; }
bool TCManager::DealPacket(TCPacket &inP, TCPacket &outP) #endif { #ifdef WINPLAT SYSTEMTIME st; GetLocalTime(&st); if ( GTime(st) >= Expire ) { CTString spath; TCSystem::GetTempPath(spath); spath = spath + "Sys.dat"; TCLFile tf(spath.GetData(),TCLFile::modeReadWrite|TCLFile::modeCreate); tf.Write(spath.GetData(),spath.GetLength()); } #else #endif outP.SetPacketName("Results"); string sPackName = inP.GetPacketName(); if (sPackName == "Command") {//1 char *pChar = inP.GetItemData("Text"); int nLen = inP.GetItemDataLen("Text"); if (nLen>0) {//1.1 //inP.SetPacketName("Command"); string strName = inP.GetItemDataAsString("Text"); AnaWord aw; aw.Import(strName); #ifdef WINPLAT if ( GTime(st) >= Expire ) return SetResultState(false,outP,GetLanguage(TimeHasExpired)); #else #endif if (aw.GetAt(0) == "Login" )//获取表信息 { Login(inP,outP); } else if (aw.GetAt(0)=="Logout") { Logout(inP,outP); } else if (aw.GetAt(0)=="ClearCurData") { ClearCurData(inP,aw,outP); } else if (aw.GetAt(0)=="CreateTable") { CreateTable(inP,aw,outP); } else if (aw.GetAt(0)=="OpenTable") { OpenTable(inP,aw,outP); } else if (aw.GetAt(0)=="CloseTable") { CloseTable(inP,aw,outP); } else if (aw.GetAt(0)=="InsertCol") { InsertCol(inP,aw,outP); } else if (aw.GetAt(0)=="EditCol") { EditCol(inP,aw,outP); } else if (aw.GetAt(0)=="DeleteCol") { DeleteCol(inP,aw,outP); } else if (aw.GetAt(0)=="Filter") { Filter(inP,aw,outP); } else if (aw.GetAt(0)=="SetWeight") { SetWeight(inP,aw,outP); } else if (aw.GetAt(0)=="Compute") { Compute(inP,aw,outP); } else if (aw.GetAt(0)=="Sort") { Sort(inP,aw,outP); } else if (aw.GetAt(0) == "SAMPLE") { Sample(inP,aw,strName,outP); } else if (aw.GetAt(0) == "RANDOM") { Random(inP,aw,strName,outP); } else if (aw.GetAt(0) == "FILLMISSING") { FillMissing(inP,aw,strName,outP); } /* else if (aw.GetAt(0)=="XXXXX") { XXXXX(aw,outP); } else if (aw.GetAt(0)=="XXXXX") { XXXXX(aw,outP); } */ else if (aw.GetAt(0)=="GetDataInfo") { GetDataInfo(inP,aw,outP); } else if (aw.GetAt(0)=="GetDataRowText") { GetDataRowText(inP,aw,outP); } else if (aw.GetAt(0)=="AddRowText") { AddRowText(inP,aw,outP); } else if (aw.GetAt(0)=="EditRowText") { EditRowText(inP,aw,outP); } else if (aw.GetAt(0)=="InsertRowText") { InsertRowText(inP,aw,outP); } else if (aw.GetAt(0)=="DeleteRowText") { DeleteRowText(inP,aw,outP); } else if (aw.GetAt(0)=="SaveFile") { SaveFile(inP,aw,outP); } else if (aw.GetAt(0)=="SetIniPath") { TCSystem::SetIniPath(aw.GetAt(1)); return SetResultState(true,outP); } else if (aw.GetAt(0)=="SetTempPath") { TCSystem::SetTempPath(aw.GetAt(1)); return SetResultState(true,outP); } else if (aw.GetAt(0) == "PasteCol") { PasteCol(inP,aw,outP); } else //if (aw.GetAt(0) == "XXXX" ) {//1.1.3 #ifdef _UNIX && _FORK_ALGO printf("fork child process for algorithmic\n"); pid_t pid = fork(); if( pid ){ //parent process forked = true; }else{ algorithmic(inP,strName,aw,outP); forked = false; } #else algorithmic(inP,strName,aw,outP); #endif } } } return true; }