//============================================================================ // $Id: PamOffLineSW_Main.cpp,v 1.52 2008-09-12 14:03:55 messineo Exp $ // Description : Pamela Off-Line Software //============================================================================ #include #include "PamInclude.h" #include "StateManager.h" #include "PacketUser.h" extern "C" { #include } using namespace PamOffLineSW; // dbg purposes bool exitdbg = false; void exitDBG(){exitdbg=true;} //function needed only for dbg purposes: void split_input(char fni[]); #define LENGTH_DATA_SIMU 1024 #define LENGTH_CADRE_VRL 1024 #define LENGTH_HEADER_VRL 8 #define LENGTH_DATA_CADRE (LENGTH_CADRE_VRL-LENGTH_HEADER_VRL-1)//1015 //main functions void readOptions(int argc, char *argv[]); void init(); void firstLog(); void DB_config(); void mainRead_RealData(char fni[]); void mainRead_SimulatedData(char fni[]); void finish(); void deleteAll(); //utilities functions bool VRL_Header_Check(char * headVRL, int length); short int CRC_Cadre_Check(char dataVRL[], int length); //global variables bool simulated_data = false; bool do_vrl_check = false; char* db_host = ""; int db_port = 0; char* db_name = ""; char conn[100]=""; LogUtil::logLevel loglevel=LogUtil::LOGERROR; char* logfilename = "chewbacca.log"; //current route int route = 999; //previous route int old_route = 999; bool skip_cadre = false; long int iNumGoodCadres=0; //total number of good cadres // global variables used in all the project namespace PamOffLineSW { char* db_user = ""; char* db_pwd = ""; char* connection = NULL; //marco_new_01 bool single_connection=false; long int iNumCadres=0; //cadre's number unsigned long long int iByte_tot=0;// how many bytes I have read till now bool isCadreGood = true;//if the cadre is good char* fni; //path completo short compression = 3; // char *outDir = "."; char *outDir = ""; char * nome_output="chewbacca"; bool multiFile = 0; unsigned long int step_pkt_number=0; unsigned long int step_pkt_obt=0; LogUtil* mainLogUtil = NULL; TSQLServer* sqlServer = NULL; unsigned long int max_pkt_number = 16777215;//biggest value before reset unsigned long int max_pkt_obt = 4294967295u;//biggest value before reset bool is_new_route = false; unsigned int download = 0; unsigned int orbit_number=0; unsigned int mmm_number = 0;//session_number unsigned long int time_Offset=0; bool tryMerge = false; bool do_cont_check=true;//if do_cont_check is false do not use a DB ... //TODO: now it is unused unsigned long int delta_Time=0;//in seconds } // main: // int main(int argc, char *argv[]) { // read command line options readOptions(argc,argv); cout<<"Please wait"<logAll("Using simulated data"); mainRead_SimulatedData(fni); } else { mainLogUtil->logAll("Using real data"); mainRead_RealData(fni); } finish(); timeval tv2; gettimeofday(&tv2,NULL); unsigned long long timems2 = (unsigned long long)tv2.tv_sec * (unsigned long long)1000 + tv2.tv_usec / 1000; unsigned long long timems = timems2 -timems1; string msg = "The analisys took: " + mainLogUtil->value2string(timems) + " ms"; mainLogUtil->logAlways(msg); deleteAll(); cout<<"The end "<= argc){ cerr << "-time_Offset needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { time_Offset = atoll(argv[i]); } else { cerr << "-time_Offset needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-orbit_number")){ if (++i >= argc){ cerr << "-orbit_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { orbit_number = atoi(argv[i]); } else { cerr << "-orbit_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-session_number")){ if (++i >= argc){ cerr << "-session_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { mmm_number = atoi(argv[i]); } else { cerr << "-session_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-max_pkt_number")){ if (++i >= argc){ cerr << "-max_pkt_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { max_pkt_number = atoll(argv[i]); } else { cerr << "-max_pkt_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-max_pkt_obt")){ if (++i >= argc){ cerr << "-max_pkt_obt needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { max_pkt_obt = atoll(argv[i]); } else { cerr << "-max_pkt_obt needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-simu")) { simulated_data = true; continue; } if (!strcmp(argv[i], "-vrl")) { do_vrl_check = true; if(simulated_data) { cout<<"Impossible to perform VRL check with simulated data. Don't use -simu option"<= argc){ cerr << "-filelog needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } logfilename=argv[i]; continue; } if (!strcmp(argv[i], "-loglevel")){ if (++i >= argc){ cerr << "-loglevel needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 3))) { loglevel = (LogUtil::logLevel) atoi(argv[i]); } else { cerr << "-loglevel needs an integer value beetween 0 and 3. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-c")){ if (++i >= argc){ cerr << "-c needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 9))) { compression = atoi(argv[i]); } else { cerr << "-c needs an integer value beetween 0 and 9. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-outDir")){ if (++i >= argc){ cerr << "-outDir needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } DIR* tempdir; if ((tempdir = opendir(argv[i])) != 0) { outDir = argv[i]; closedir(tempdir); } else { cerr << "-outDir needs an existing/accessable directory. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-root_name")){ if (++i >= argc){ cerr << "-root_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } nome_output = argv[i]; continue; } if (!strcmp(argv[i], "-delta_counter")){ if (++i >= argc){ cerr << "-delta_counter needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { step_pkt_number = atoi(argv[i]); } else { cerr << "-delta_counter needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-delta_obt")){ if (++i >= argc){ cerr << "-delta_obt needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { step_pkt_obt = atoi(argv[i]); } else { cerr << "-delta_obt needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-delta_Time")){ if (++i >= argc){ cerr << "-delta_Time needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { delta_Time = atoi(argv[i]); } else { cerr << "-delta_Time needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-db_host")){ if (++i >= argc){ cerr << "-db_host needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_host = argv[i]; continue; } if (!strcmp(argv[i], "-db_port")){ if (++i >= argc){ cerr << "-db_port needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { db_port = atoi(argv[i]); } else { cerr << "-db_port needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-db_name")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_name = argv[i]; continue; } if (!strcmp(argv[i], "-db_user")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_user = argv[i]; continue; } if (!strcmp(argv[i], "-db_pwd")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_pwd = argv[i]; continue; } //TODO: check that multiFile is always 0 // if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<IsConnected()))){cout<<"Can not connect with MYSQL sever"<logAlways(msg); } void firstLog(){ string msg = "Input file: " + (string)fni; mainLogUtil->logAlways(msg); msg = "Output directory for the generated root file(s): " + (string)outDir; mainLogUtil->logAlways(msg); msg = "Output root name for the generated root file(s): " + (string)nome_output; mainLogUtil->logAlways(msg); stringstream oss1; oss1.str()=""; oss1<< "Compression level for the generated root file(s): " <logInfo(msg); stringstream oss2; oss2.str()=""; oss2<< "Max value for Packet Counter: " <logAll(msg); if(do_vrl_check) { mainLogUtil->logInfo("VRL check set to true"); } else { mainLogUtil->logWarning("VRL check skipped"); } if(step_pkt_number) { stringstream oss; oss.str()=""; oss<<"The allowed difference in the Packet Counter between two continuos packets: "<logAlways(msg); } if(step_pkt_obt) { stringstream oss; oss.str()=""; oss<<"The allowed difference in the Packet Orbital Time between two continuos packets: " <logAlways(msg); } if((!step_pkt_number)&&(!step_pkt_obt)){ mainLogUtil->logAlways("No check about continuity; DB unused. No Merging ..."); do_cont_check=false; tryMerge=false; //return; } /* if(do_cont_check){ sprintf(connection,"mysql://%s:%d/%s",db_host,db_port,db_name); string msg = "Using DB: " + (string)connection; mainLogUtil->logAlways(msg); } */ if(tryMerge) { mainLogUtil->logInfo("Try to Merge ROOT files"); } /* if(tryMerge){ stringstream oss1; oss1.str()=""; oss1<<"The allowed difference in the Real Time between two ROOT files: " <logAlways(msg1); } */ } void finish() { PacketUser::getInstance().FinishLastGroup(); } void deleteAll() { if(mainLogUtil){delete mainLogUtil; mainLogUtil = NULL;} //marco_new_01: questo non serve sicuramente ... if(sqlServer){delete sqlServer; sqlServer = NULL;} } //read simulated data // without vrl header void mainRead_SimulatedData(char fni[]) { //we read LENGTH_DATA_SIMU bytes each time ... char ccData[LENGTH_DATA_SIMU]={0}; ifstream fin; fin.open(fni, ifstream::binary); if (!fin) {cout<<"Can not open input file "<logAll(msg); //I want try to use it ... for(int i=0;ireadInput(ccData[i]); iByte_tot++; } //anyhow then exit break; } //main processing of the data, searching for Pamela Packets for(int i=0;ireadInput(ccData[i]); iByte_tot++; } iNumCadres++; } fin.close(); stringstream oss1; oss1<<"Total bytes read "<logInfo(msg); } //read real data void mainRead_RealData(char fni[]) { //header VRL char ccHeader_VRL[LENGTH_HEADER_VRL]={0}; //data in each cadre, it can contain the PAMELA PKT char ccData[LENGTH_DATA_CADRE]={0}; //last byte of each cadre, it is used to check CRC of the cadre char cCrc_VRL=0; //file di input = fni[] ifstream fin; fin.open(fni, ifstream::binary); if (!fin) {cout<<"Can not open input file "<logError(msg); if(do_vrl_check){isCadreGood=false;} break; } //check of the VRL header e setta route number if(!VRL_Header_Check(ccHeader_VRL,LENGTH_HEADER_VRL)) { isCadreGood=false;//Check if it is better set it to true ... stringstream oss; oss<<"The cadre: "<logWarning(msg); } //we have read and analysed the header VRL of this cadre iByte_tot+=fin.gcount(); //reading data in the cadre fin.read(ccData,LENGTH_DATA_CADRE); if (!fin.good()) { stringstream oss; oss<<"The cadre: "<logWarning(msg); //I am sure that this cadre is not good ... if(do_vrl_check) { isCadreGood=false; } //... but maybe I want try to use it ... for(int i=0;ireadInput(ccData[i]); iByte_tot++; } //... anyhow then exit break; } //reading the last byte of the cadre fin.get(cCrc_VRL); if (!fin.good()) { //if the last byte representing the CRC_VRL is not present //or if we are not able to read it stringstream oss; oss<<"The cadre: "<logWarning(msg); //I am sure that this cadre is not good ... if(do_vrl_check) { isCadreGood=false; } //don't exit now, we need to keep it alive to process data //we will exit the main reading cicle after, } if((do_vrl_check)&&(!skip_cadre)) { //check CRC for the cadre short int CalcCheckSum; CalcCheckSum=CRC_Cadre_Check(ccData,(int)LENGTH_DATA_CADRE); if (CalcCheckSum!=(short int)(cCrc_VRL)) { isCadreGood=false; stringstream oss; oss<<"The cadre: "<logError(msg); //non mi fido del contatore del route stringstream oss1; oss1<<"Route number unused = "<logAll(msg); route=old_route; } } if(!skip_cadre) { if(route!=old_route) { is_new_route=true; download++; stringstream oss; oss<<"Found a new download n: "<logAll(msg); } // is_new_route will be reset to false in PacketUser after the packet is used and stored //main processing of the data, searching for Pamela Packets for(int i=0; ireadInput(ccData[i]); iByte_tot++; if(exitdbg==true){cout<<"**** DBG EXIT FOR****"<=0){cout<<"################DBG EXIT ##################"<logInfo(msg); } //function that check the header VRL bool VRL_Header_Check(char* headVRL, int length) { /* stringstream oss; oss<<"Pippooooooo: at byte+ "<logAll(msg); return false; } if( ((int)(unsigned char)headVRL[0]!=CODE_FF)|| ((int)(unsigned char)headVRL[1]!=CODE_46)|| ((int)(unsigned char)headVRL[2]!=CODE_D5) ) { stringstream oss; oss<<"The cadre does not start with FF 46 D5"<< " This error occurred after byte: "<logAll(msg); return false; } route = (int)headVRL[3]; if(route!=old_route) { stringstream oss; oss<<"Changing ROUTE at byte: "<logAll(msg); } //TODO: check the cadre number and save this info? //TODO check that the lenght is 8 return true; } //function that computes the CRC for each cadre short int CRC_Cadre_Check(char dataVRL[], int length) { //function used only if(do_vrl_check) short int Summ; int i; int CSum, tmp; tmp=(int)dataVRL[0]; CSum=tmp; for (i=1;i