/[PAMELA software]/chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp_30
ViewVC logotype

Annotation of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp_30

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (hide annotations) (download) (vendor branch)
Tue Sep 23 07:20:13 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: v0r00
CVS Tags: start
Changes since 1.1: +0 -0 lines
Imported sources, 23/09/2008

1 mocchiut 1.1 //============================================================================
2     // $Id: PamOffLineSW_Main.cpp,v 1.50 2008-04-01 16:05:45 messineo Exp $
3     // Description : Pamela Off-Line Software
4     //============================================================================
5    
6     #include <sys/time.h>
7     #include "PamInclude.h"
8     #include "StateManager.h"
9    
10     #include "PacketUser.h"
11    
12     extern "C" {
13     #include <dirent.h>
14     }
15    
16     using namespace PamOffLineSW;
17    
18     // dbg purposes
19     bool exitdbg = false;
20     void exitDBG(){exitdbg=true;}
21     //function needed only for dbg purposes:
22     void split_input(char fni[]);
23    
24     #define LENGTH_DATA_SIMU 1024
25     #define LENGTH_CADRE_VRL 1024
26     #define LENGTH_HEADER_VRL 8
27     #define LENGTH_DATA_CADRE (LENGTH_CADRE_VRL-LENGTH_HEADER_VRL-1)//1015
28    
29     //main functions
30     void readOptions(int argc, char *argv[]);
31     void init();
32     void firstLog();
33     void DB_config();
34     void mainRead_RealData(char fni[]);
35     void mainRead_SimulatedData(char fni[]);
36     void finish();
37     void deleteAll();
38    
39     //utilities functions
40     bool VRL_Header_Check(char * headVRL, int length);
41     short int CRC_Cadre_Check(char dataVRL[], int length);
42    
43     //global variables
44     bool simulated_data = false;
45     bool do_vrl_check = false;
46    
47     char* db_host = "";
48     int db_port = 0;
49     char* db_name = "";
50     char conn[100]="";
51     //char* connection = NULL;
52     //char* db_user = "yoda_user";
53     //char* db_pwd = "yoda_pwd";
54    
55     LogUtil::logLevel loglevel=LogUtil::LOGERROR;
56     char* logfilename = "pamofflinesw.log";
57     int route = 999;
58     bool skip_cadre = false;
59     long int iNumGoodCadres=0; //total number of good cadres
60    
61     // global variables used in all the project
62     namespace PamOffLineSW
63     {
64     char* db_user = "";
65     char* db_pwd = "";
66     char* connection = NULL;
67    
68     long int iNumCadres=0; //cadre's number
69     unsigned long long int iByte_tot=0;// how many bytes I have read till now
70    
71     bool isCadreGood = true;//if the cadre is good
72     char* fni; //path completo
73     short compression = 3;
74     // char *outDir = ".";
75     char *outDir = "";
76    
77     char * nome_output="yoda";
78     bool multiFile = 0;
79     unsigned long int step_pkt_number=0;
80     unsigned long int step_pkt_obt=0;
81     LogUtil* mainLogUtil = NULL;
82     TSQLServer* sqlServer = NULL;
83     unsigned long int max_pkt_number = 16777215;//biggest value before reset
84     unsigned long int max_pkt_obt = 4294967295u;//biggest value before reset
85    
86     bool is_new_route = true;
87     unsigned int download = 0;
88     unsigned int orbit_number=0;
89     unsigned int mmm_number = 0;//session_number
90     unsigned long int time_Offset=0;
91     bool tryMerge = false;
92    
93     bool do_cont_check=true;//if do_cont_check is false do not use a DB ...
94    
95     //TODO: now it is unused
96     unsigned long int delta_Time=0;//in seconds
97     }
98    
99     // main:
100     //
101     int main(int argc, char *argv[])
102     {
103     // read command line options
104     readOptions(argc,argv);
105    
106     cout<<"Please wait"<<endl;
107    
108     // initialize everything and write few things in the log file
109     init();
110    
111     // take time to measure the performance.
112     timeval tv1;
113     gettimeofday(&tv1,NULL);
114     unsigned long long timems1 = (unsigned long long)tv1.tv_sec * (unsigned long long)1000 + tv1.tv_usec / 1000;
115    
116     if(simulated_data)
117     {
118     mainLogUtil->logAll("Using simulated data");
119     mainRead_SimulatedData(fni);
120     }
121     else
122     {
123     mainLogUtil->logAll("Using real data");
124     mainRead_RealData(fni);
125     }
126    
127     finish();
128    
129     timeval tv2;
130     gettimeofday(&tv2,NULL);
131     unsigned long long timems2 = (unsigned long long)tv2.tv_sec * (unsigned long long)1000 + tv2.tv_usec / 1000;
132     unsigned long long timems = timems2 -timems1;
133    
134     string msg = "The analisys took: " + mainLogUtil->value2string(timems) + " ms";
135     mainLogUtil->logAlways(msg);
136    
137     deleteAll();
138    
139     cout<<"The end "<<endl;
140     return 0;
141     }
142    
143     void readOptions(int argc, char *argv[])
144     {
145     if (argc < 2)
146     {
147     cout << "You have forgotten the file name. \n";
148     cout << "Try '-help' for more information. \n";
149     exit(1);
150     }
151    
152     if((!strcmp(argv[1], "-help"))||(!strcmp(argv[1], "--help"))||(!strcmp(argv[1], "-h")))
153     {
154     // cout << "Usage: PamOffLineSW INPUT_FILE [OPTIONS] \n";
155     cout << "Usage: chewbacca INPUT_FILE [OPTIONS] \n";
156     cout << "\t (-help | --help | -h) print this help and exit \n";
157     cout << "\t -simu if the input file contains simulated data instead of real data\n";
158     cout << "\t -vrl if you want to perform the vrl check\n";
159     cout << "\t -filelog set the log filename. [default: pamofflinesw.log]\n";
160     cout << "\t -loglevel set the log level. Values: [0,3] (error,warning,info,all) [default:0]\n";
161     cout << "\t -c set the compression level for the generated ROOT file(s). Values: [0,9] [default = 3]\n";
162     cout << "\t -root_name set the root-name for the generated ROOT file(s). [default = yoda]\n";
163     cout << "\t -outDir set the output directory for the generated root file(s). [default = .]\n";
164     cout << "\t -delta_counter set the allowed difference in the Packet Counter between two continuos packets. If 0 all packets are considered continuos. [default = 0]\n";
165     cout << "\t -delta_obt set the allowed difference (ms) in the Packet Orbital Time between two continuos packets. If 0 all packets are considered continuos. [default = 0]\n";
166     cout << "\t -db_host set the host-name of the DataBase. [default = localhost]\n";
167     cout << "\t -db_port set the port of the DataBase. [default = 3306]\n";
168     cout << "\t -db_name set the name of the DataBase. [default = yoda_db]\n";
169     cout << "\t -db_user set the user of the DataBase. [default = yoda_user]\n";
170     cout << "\t -db_pwd set the user of the DataBase. [default = yoda_pwd]\n";
171     cout << "\t -max_pkt_number Maximum value for Packet Counter, after this value it is resetted. [default = 2^24 - 1]\n";
172     cout << "\t -max_pkt_obt Maximum value for Packet OBT, after this value it is resetted. [default = 2^32 - 1]\n";
173     cout << "\t -orbit_number Value of the orbital number. If 0 this is retrieved from the input file name. [default = 0]\n";
174     cout << "\t -session_number Value of the session number. If 0 this is retrieved from the input file name. [default = 0]\n";
175     cout << "\t -time_Offset Value of the timeOffset. If 0 this is retrieved using the orbital number. [default = 0]\n";
176     cout << "\t -tryMerge if you want to try to Merge ROOT files\n";
177     // cout << "\t -delta_Time set the allowed difference (seconds) in the Real Time between two root file. [default = 0]\n";
178    
179     //cout << "\t -multi generate multiple root files \n";
180     exit(1);
181     }
182    
183     if(!strcmp(argv[1], "-usage")){
184     cout << "\t chewbacca INPUT_FILE [OPTIONS] \n";
185     cout << "Try '-help' for more information. \n";
186     exit(1);
187     }
188    
189     //http://en.wikipedia.org/wiki/Chewbacca_defense
190     //http://www.urbandictionary.com/define.php?term=chewbacca+defense
191     if(!strcmp(argv[1], "-defense")){
192     cout << "\n Why would a Wookiee, an eight-foot tall Wookiee, want to live on Endor,\n with a bunch of two-foot tall Ewoks? That does not make sense!\n But more important, you have to ask yourself:\n What does this have to do with this case? \n Nothing. Ladies and gentlemen, it has nothing to do with this case! \n It does not make sense!\n Look at me. I'm a lawyer defending a major record company, \n and I'm talkin' about Chewbacca! Does that make sense? \n Ladies and gentlemen, I am not making any sense! \n None of this makes sense! \n And so you have to remember, when you're in that jury room\n deliberatin' and conjugatin' the Emancipation Proclamation,\n does it make sense? \n No! Ladies and gentlemen of this supposed jury, it does not make sense!\n If Chewbacca lives on Endor, you must acquit! \n The defense rests."<<endl;
193     cout << "\nTry '-help' for more information. \n";
194     exit(1);
195     }
196    
197     for (int i = 2; i < argc; i++)
198     {
199     if (!strcmp(argv[i], "-tryMerge"))
200     {
201     tryMerge = true;
202     continue;
203     }
204    
205     if (!strcmp(argv[i], "-time_Offset")){
206     if (++i >= argc){
207     cerr << "-time_Offset needs arguments. \n";
208     cout << "Try '-help' for more information. \n";
209     exit(1);
210     }
211     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
212     time_Offset = atoll(argv[i]);
213     } else {
214     cerr << "-time_Offset needs an integer value. \n";
215     cout << "Try '-help' for more information. \n";
216     exit(1);
217     }
218     continue;
219     }
220    
221     if (!strcmp(argv[i], "-orbit_number")){
222     if (++i >= argc){
223     cerr << "-orbit_number needs arguments. \n";
224     cout << "Try '-help' for more information. \n";
225     exit(1);
226     }
227     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
228     orbit_number = atoi(argv[i]);
229     } else {
230     cerr << "-orbit_number needs an integer value. \n";
231     cout << "Try '-help' for more information. \n";
232     exit(1);
233     }
234     continue;
235     }
236    
237     if (!strcmp(argv[i], "-session_number")){
238     if (++i >= argc){
239     cerr << "-session_number needs arguments. \n";
240     cout << "Try '-help' for more information. \n";
241     exit(1);
242     }
243     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
244     mmm_number = atoi(argv[i]);
245     } else {
246     cerr << "-session_number needs an integer value. \n";
247     cout << "Try '-help' for more information. \n";
248     exit(1);
249     }
250     continue;
251     }
252    
253     if (!strcmp(argv[i], "-max_pkt_number")){
254     if (++i >= argc){
255     cerr << "-max_pkt_number needs arguments. \n";
256     cout << "Try '-help' for more information. \n";
257     exit(1);
258     }
259     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
260     max_pkt_number = atoll(argv[i]);
261     } else {
262     cerr << "-max_pkt_number needs an integer value. \n";
263     cout << "Try '-help' for more information. \n";
264     exit(1);
265     }
266     continue;
267     }
268    
269     if (!strcmp(argv[i], "-max_pkt_obt")){
270     if (++i >= argc){
271     cerr << "-max_pkt_obt needs arguments. \n";
272     cout << "Try '-help' for more information. \n";
273     exit(1);
274     }
275     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
276     max_pkt_obt = atoll(argv[i]);
277     } else {
278     cerr << "-max_pkt_obt needs an integer value. \n";
279     cout << "Try '-help' for more information. \n";
280     exit(1);
281     }
282     continue;
283     }
284    
285    
286     if (!strcmp(argv[i], "-simu"))
287     {
288     simulated_data = true;
289     continue;
290     }
291    
292     if (!strcmp(argv[i], "-vrl"))
293     {
294     do_vrl_check = true;
295     if(simulated_data)
296     {
297     cout<<"Impossible to perform VRL check with simulated data. Don't use -simu option"<<endl;
298     cout << "Try '-help' for more information. \n";
299     exit(1);
300     }
301    
302     continue;
303     }
304    
305     if (!strcmp(argv[i], "-filelog")){
306     if (++i >= argc){
307     cerr << "-filelog needs arguments. \n";
308     cout << "Try '-help' for more information. \n";
309     exit(1);
310     }
311     logfilename=argv[i];
312     continue;
313     }
314    
315     if (!strcmp(argv[i], "-loglevel")){
316     if (++i >= argc){
317     cerr << "-loglevel needs arguments. \n";
318     cout << "Try '-help' for more information. \n";
319     exit(1);
320     }
321     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 3))) {
322     loglevel = (LogUtil::logLevel) atoi(argv[i]);
323     } else {
324     cerr << "-loglevel needs an integer value beetween 0 and 3. \n";
325     cout << "Try '-help' for more information. \n";
326     exit(1);
327     }
328     continue;
329     }
330    
331    
332     if (!strcmp(argv[i], "-c")){
333     if (++i >= argc){
334     cerr << "-c needs arguments. \n";
335     cout << "Try '-help' for more information. \n";
336     exit(1);
337     }
338     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 9))) {
339     compression = atoi(argv[i]);
340     } else {
341     cerr << "-c needs an integer value beetween 0 and 9. \n";
342     cout << "Try '-help' for more information. \n";
343     exit(1);
344     }
345     continue;
346     }
347    
348     if (!strcmp(argv[i], "-outDir")){
349     if (++i >= argc){
350     cerr << "-outDir needs arguments. \n";
351     cout << "Try '-help' for more information. \n";
352     exit(1);
353     }
354     DIR* tempdir;
355     if ((tempdir = opendir(argv[i])) != 0) {
356     outDir = argv[i];
357     closedir(tempdir);
358     } else {
359     cerr << "-outDir needs an existing/accessable directory. \n";
360     cout << "Try '-help' for more information. \n";
361     exit(1);
362     }
363     continue;
364     }
365    
366     if (!strcmp(argv[i], "-root_name")){
367     if (++i >= argc){
368     cerr << "-root_name needs arguments. \n";
369     cout << "Try '-help' for more information. \n";
370     exit(1);
371     }
372    
373     nome_output = argv[i];
374     continue;
375     }
376    
377     if (!strcmp(argv[i], "-delta_counter")){
378     if (++i >= argc){
379     cerr << "-delta_counter needs arguments. \n";
380     cout << "Try '-help' for more information. \n";
381     exit(1);
382     }
383     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
384     step_pkt_number = atoi(argv[i]);
385     } else {
386     cerr << "-delta_counter needs an integer value. \n";
387     cout << "Try '-help' for more information. \n";
388     exit(1);
389     }
390     continue;
391     }
392    
393     if (!strcmp(argv[i], "-delta_obt")){
394     if (++i >= argc){
395     cerr << "-delta_obt needs arguments. \n";
396     cout << "Try '-help' for more information. \n";
397     exit(1);
398     }
399     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
400     step_pkt_obt = atoi(argv[i]);
401     } else {
402     cerr << "-delta_obt needs an integer value. \n";
403     cout << "Try '-help' for more information. \n";
404     exit(1);
405     }
406     continue;
407     }
408     if (!strcmp(argv[i], "-delta_Time")){
409     if (++i >= argc){
410     cerr << "-delta_Time needs arguments. \n";
411     cout << "Try '-help' for more information. \n";
412     exit(1);
413     }
414     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
415     delta_Time = atoi(argv[i]);
416     } else {
417     cerr << "-delta_Time needs an integer value. \n";
418     cout << "Try '-help' for more information. \n";
419     exit(1);
420     }
421     continue;
422     }
423    
424     if (!strcmp(argv[i], "-db_host")){
425     if (++i >= argc){
426     cerr << "-db_host needs arguments. \n";
427     cout << "Try '-help' for more information. \n";
428     exit(1);
429     }
430     db_host = argv[i];
431     continue;
432     }
433    
434    
435     if (!strcmp(argv[i], "-db_port")){
436     if (++i >= argc){
437     cerr << "-db_port needs arguments. \n";
438     cout << "Try '-help' for more information. \n";
439     exit(1);
440     }
441     if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
442     db_port = atoi(argv[i]);
443     } else {
444     cerr << "-db_port needs an integer value. \n";
445     cout << "Try '-help' for more information. \n";
446     exit(1);
447     }
448     continue;
449     }
450    
451     if (!strcmp(argv[i], "-db_name")){
452     if (++i >= argc){
453     cerr << "-db_name needs arguments. \n";
454     cout << "Try '-help' for more information. \n";
455     exit(1);
456     }
457     db_name = argv[i];
458     continue;
459     }
460     if (!strcmp(argv[i], "-db_user")){
461     if (++i >= argc){
462     cerr << "-db_name needs arguments. \n";
463     cout << "Try '-help' for more information. \n";
464     exit(1);
465     }
466     db_user = argv[i];
467     continue;
468     }
469    
470     if (!strcmp(argv[i], "-db_pwd")){
471     if (++i >= argc){
472     cerr << "-db_name needs arguments. \n";
473     cout << "Try '-help' for more information. \n";
474     exit(1);
475     }
476     db_pwd = argv[i];
477     continue;
478     }
479    
480     //TODO: check that multiFile is always 0
481     // if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<<multiFile<<endl;}
482     }
483    
484     //input file
485     fni= argv[1];
486    
487     //TODO: maybe if simulated data I don't need nothig so I can skip the following lines
488     // and use instead something similar to:
489     //if(simulated data){orbit_number=99999; mmm_number=999; download=999; }
490    
491     //I need the following lines only to retrieve orbit_number and mmm_number (session_number)from the filename
492     if((!orbit_number)||(!mmm_number))
493     {
494     std::string input = fni;
495     std::string fileName;
496     int pos = input.find_last_of("/");
497     fileName = input.substr(pos+1);
498     pos = fileName.find_last_of(".");
499     fileName = fileName.substr(0,pos);
500    
501     if(fileName.length()<8){
502     cout<<"Wrong filename unable to retrieve orbit_number and session_number."<<endl;
503     cout<<"Rename input in format nnnnnmmm.pam or set orbit_number and session_number."<<endl;
504     cout << "Try '-help' for more information. \n";
505     exit(1);
506     }
507    
508     if(!orbit_number){
509     std::string downlink;
510     downlink = fileName.substr(0,5);
511     orbit_number= atoi(downlink.c_str());
512     }
513    
514     if(!mmm_number){
515     std::string mmm;
516     mmm = fileName.substr(5,3);
517     mmm_number= atoi(mmm.c_str());
518     }
519     }
520    
521     }
522    
523     void init()
524     {
525     //initial condition of automaton
526     StateManager::getInstance().init();
527     mainLogUtil = new LogUtil(logfilename,loglevel);
528    
529     //TODO check if you like this
530     if (!strcmp(outDir,""))
531     {
532     char *pam_out = getenv("PAM_L0");
533     if (pam_out)
534     {
535     outDir = pam_out;
536     }
537     else
538     {
539     outDir = ".";
540     }
541    
542     }
543     // write some initial things in the Log file
544     firstLog();
545    
546     if(do_cont_check){
547     DB_config();
548     sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
549     if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl; exit(1);}
550     }
551    
552     }
553    
554     void DB_config()
555     {
556    
557     if( (!strcmp(db_host,""))||(db_port==0)||(!strcmp(db_name,"")))
558     {
559     char *pamdbhost = getenv("PAM_DBHOST");
560     if (pamdbhost)
561     {
562     connection = pamdbhost;
563     }
564     else
565     {
566     if (!strcmp(db_host,""))
567     {
568     db_host="localhost";
569     }
570    
571     if (db_port==0)
572     {
573     db_port=3306;
574     }
575    
576     if (!strcmp(db_name,""))
577     {
578     db_name="yoda_db";
579     }
580     sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name);
581     connection=conn;
582     }
583     }
584     else{
585     sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name);
586     connection=conn;
587     }
588    
589     if (!strcmp(db_user,""))
590     {
591     char *pamdbuser = getenv("PAM_DBUSER");
592     if (pamdbuser)
593     {
594     db_user = pamdbuser;
595     }
596     else
597     {
598     db_user="yoda_user";
599     }
600    
601     }
602    
603     if(!strcmp(db_pwd,""))
604     {
605     char *pamdbpsw = getenv("PAM_DBPSW");
606     if (pamdbpsw)
607     {
608     db_pwd = pamdbpsw;
609     }
610     else
611     {
612     db_pwd="yoda_pwd";
613     }
614     }
615    
616     //char *pamdbhost = getenv("PAM_DBHOST");
617    
618     //if (pamdbhost) connection = pamdbhost;
619    
620     string msg = "Using DB: " + string(connection);// +" "+ string(db_user) +" "+ string(db_pwd);
621     mainLogUtil->logAlways(msg);
622     }
623    
624    
625     void firstLog(){
626     string msg = "Input file: " + (string)fni;
627     mainLogUtil->logAlways(msg);
628    
629     msg = "Output directory for the generated root file(s): " + (string)outDir;
630     mainLogUtil->logAlways(msg);
631    
632     msg = "Output root name for the generated root file(s): " + (string)nome_output;
633     mainLogUtil->logAlways(msg);
634    
635     stringstream oss1;
636     oss1.str()="";
637     oss1<< "Compression level for the generated root file(s): " <<compression;
638     msg = oss1.str();
639     mainLogUtil->logInfo(msg);
640    
641     stringstream oss2;
642     oss2.str()="";
643     oss2<< "Max value for Packet Counter: " <<max_pkt_number<<" - Max value for Packet OBT: " <<max_pkt_obt;
644     msg = oss2.str();
645     mainLogUtil->logAll(msg);
646    
647     if(do_vrl_check)
648     {
649     mainLogUtil->logInfo("VRL check set to true");
650     }
651     else
652     {
653     mainLogUtil->logWarning("VRL check skipped");
654     }
655    
656     if(step_pkt_number)
657     {
658     stringstream oss;
659     oss.str()="";
660     oss<<"The allowed difference in the Packet Counter between two continuos packets: "<<step_pkt_number;
661     msg = oss.str();
662     mainLogUtil->logAlways(msg);
663     }
664    
665     if(step_pkt_obt)
666     {
667     stringstream oss;
668     oss.str()="";
669     oss<<"The allowed difference in the Packet Orbital Time between two continuos packets: " <<step_pkt_obt;
670     msg = oss.str();
671     mainLogUtil->logAlways(msg);
672     }
673    
674     if((!step_pkt_number)&&(!step_pkt_obt)){
675     mainLogUtil->logAlways("No check about continuity; DB unused. No Merging ...");
676     do_cont_check=false;
677     tryMerge=false;
678     //return;
679     }
680    
681     /*
682     if(do_cont_check){
683     sprintf(connection,"mysql://%s:%d/%s",db_host,db_port,db_name);
684     string msg = "Using DB: " + (string)connection;
685     mainLogUtil->logAlways(msg);
686     }
687     */
688    
689     if(tryMerge)
690     {
691     mainLogUtil->logInfo("Try to Merge ROOT files");
692     }
693    
694     /*
695     if(tryMerge){
696     stringstream oss1;
697     oss1.str()="";
698     oss1<<"The allowed difference in the Real Time between two ROOT files: " <<delta_Time<<" s";
699     string msg1=oss1.str();
700     mainLogUtil->logAlways(msg1);
701     }
702     */
703    
704     }
705    
706     void finish()
707     {
708     PacketUser::getInstance().FinishLastGroup();
709     }
710    
711     void deleteAll()
712     {
713     if(mainLogUtil){delete mainLogUtil; mainLogUtil = NULL;}
714    
715     if(sqlServer){delete sqlServer; sqlServer = NULL;}
716     }
717    
718     //read simulated data
719     // without vrl header
720     void mainRead_SimulatedData(char fni[])
721     {
722     //we read LENGTH_DATA_SIMU bytes each time ...
723     char ccData[LENGTH_DATA_SIMU]={0};
724     ifstream fin;
725     fin.open(fni, ifstream::binary);
726     if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
727     //main read loop
728     isCadreGood = true;//all the cadres are good ;-)
729    
730     while(fin.eof()==0)
731     {
732     //reading data in the cadre
733     fin.read(ccData,LENGTH_DATA_SIMU);
734     stringstream oss;
735    
736     if (!fin.good())
737     {
738     // if (fin.eof()){break;}//if we reach the end of file
739     stringstream oss;
740     oss<<"Read only "<<fin.gcount()<<" bytes instead of "<<LENGTH_DATA_SIMU<<
741     " This error occurred after byte: "<<iByte_tot
742     <<" ... use the data read, then ... exit ";
743     string msg = oss.str();
744     mainLogUtil->logAll(msg);
745     //I want try to use it ...
746     for(int i=0;i<fin.gcount();i++)
747     {
748     StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
749     iByte_tot++;
750     }
751     //anyhow then exit
752     break;
753     }
754    
755     //main processing of the data, searching for Pamela Packets
756     for(int i=0;i<LENGTH_DATA_SIMU;i++)
757     {
758     StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
759     iByte_tot++;
760     }
761     iNumCadres++;
762     }
763    
764     fin.close();
765    
766     stringstream oss1;
767     oss1<<"Total bytes read "<<iByte_tot;
768     string msg = oss1.str();
769     mainLogUtil->logInfo(msg);
770     }
771    
772     //read real data
773     void mainRead_RealData(char fni[])
774     {
775     //header VRL
776     char ccHeader_VRL[LENGTH_HEADER_VRL]={0};
777     //data in each cadre, it can contain the PAMELA PKT
778     char ccData[LENGTH_DATA_CADRE]={0};
779     //last byte of each cadre, it is used to check CRC of the cadre
780     char cCrc_VRL=0;
781    
782     //file di input = fni[]
783     ifstream fin;
784     fin.open(fni, ifstream::binary);
785     if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
786    
787     //main read loop
788     while(fin.eof()==0)
789     {
790     isCadreGood = true;
791     //start reading the file
792     fin.read(ccHeader_VRL, LENGTH_HEADER_VRL);
793     if (!fin.good())
794     {
795     //TODO: check here
796     if (fin.eof())//if we reach the end of file
797     {
798     break;
799     }
800    
801     stringstream oss;
802     oss<<"The cadre: "<<iNumCadres<<" has something wrong in its header VRL lenght"<<
803     " This error occurred after byte: "<<iByte_tot<<" ... exit";
804     string msg = oss.str();
805     mainLogUtil->logError(msg);
806    
807     if(do_vrl_check){isCadreGood=false;}
808     break;
809     }
810    
811     //check of the VRL header
812     if(!VRL_Header_Check(ccHeader_VRL,LENGTH_HEADER_VRL))
813     {
814     isCadreGood=false;//Check if it is better set it to true ...
815     stringstream oss;
816     oss<<"The cadre: "<<iNumCadres<<" has something wrong in its header VRL"<<
817     " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully";
818     string msg = oss.str();
819     mainLogUtil->logWarning(msg);
820     }
821    
822     //we have read and analysed the header VRL of this cadre
823     iByte_tot+=fin.gcount();
824    
825     //reading data in the cadre
826     fin.read(ccData,LENGTH_DATA_CADRE);
827     if (!fin.good())
828     {
829     stringstream oss;
830     oss<<"The cadre: "<<iNumCadres<<" has something wrong in its data"<<
831     " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully then ... exit ";
832     string msg = oss.str();
833     mainLogUtil->logWarning(msg);
834    
835     //I am sure that this cadre is not good ...
836     if(do_vrl_check)
837     {
838     isCadreGood=false;
839     }
840    
841     //... but maybe I want try to use it ...
842     for(int i=0;i<fin.gcount();i++)
843     {
844     StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
845     iByte_tot++;
846     }
847    
848     //... anyhow then exit
849     break;
850     }
851    
852     //reading the last byte of the cadre
853     fin.get(cCrc_VRL);
854     if (!fin.good())
855     {
856     //if the last byte representing the CRC_VRL is not present
857     //or if we are not able to read it
858     stringstream oss;
859     oss<<"The cadre: "<<iNumCadres<<" has something wrong in its last byte"<<
860     " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully then ... exit ";
861     string msg = oss.str();
862     mainLogUtil->logWarning(msg);
863    
864     //I am sure that this cadre is not good ...
865     if(do_vrl_check)
866     {
867     isCadreGood=false;
868     }
869    
870     //don't exit now, we need to keep it alive to process data
871     //we will exit the main reading cicle after,
872     }
873    
874     if((do_vrl_check)&&(!skip_cadre))
875     {
876     //check CRC for the cadre
877     short int CalcCheckSum;
878     CalcCheckSum=CRC_Cadre_Check(ccData,(int)LENGTH_DATA_CADRE);
879    
880     if (CalcCheckSum!=(short int)(cCrc_VRL))
881     {
882     isCadreGood=false;
883     stringstream oss;
884     oss<<"The cadre: "<<iNumCadres<<" failed the CRC check"<<
885     " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully";
886     string msg = oss.str();
887     mainLogUtil->logWarning(msg);
888     }
889     }
890    
891     if(!skip_cadre)
892     {
893     //main processing of the data, searching for Pamela Packets
894     for(int i=0; i<LENGTH_DATA_CADRE; i++)
895     {
896     StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
897     iByte_tot++;
898     if(exitdbg==true){cout<<"**** DBG EXIT FOR****"<<endl; break;}
899     }
900     }
901     else{
902     iByte_tot+=LENGTH_DATA_CADRE;
903     }
904    
905     if(exitdbg==true){cout<<"**** DBG EXIT WHILE***##*#*#"<<endl; break;}
906    
907     //taking in account the cCrc_VRL read before
908     iByte_tot++;
909    
910     //increment the number of cadre processed
911     if(!skip_cadre)
912     iNumCadres++;
913    
914     if(isCadreGood)
915     {
916     if(!skip_cadre)
917     iNumGoodCadres++;
918     }
919    
920    
921     // if(iNumCadres>=0){cout<<"################DBG EXIT ##################"<<endl; break;}
922    
923     }//end reading cycle
924    
925     fin.close();
926    
927     stringstream oss1;
928     oss1<<"Total bytes read "<<iByte_tot
929     <<" Total number of cadres processed "<<iNumCadres
930     <<" Total number of good cadres "<<iNumGoodCadres;
931     string msg = oss1.str();
932     mainLogUtil->logInfo(msg);
933     }
934    
935     //function that check the header VRL
936     bool VRL_Header_Check(char* headVRL, int length)
937     {
938     if(((int)(unsigned char)headVRL[0]==0)&&((int)(unsigned char)headVRL[1]==0)&&((int)(unsigned char)headVRL[2]==0)&&((int)(unsigned char)headVRL[3]==0)&&((int)(unsigned char)headVRL[7]==0))
939     {
940     skip_cadre=true;
941     stringstream oss;
942     oss<<"The cadre is all zeros"<<
943     " This error occurred after byte: "<<iByte_tot<<" cadre ...DISCARDED and not computed ";
944     string msg = oss.str();
945     mainLogUtil->logWarning(msg);
946     return false;
947     }
948    
949     int old_route = route;
950     route = (int)headVRL[3];
951     if(route!=old_route)
952     {
953     is_new_route=true;
954     download++;
955     stringstream oss;
956     oss<<"Found a new download n: "<<download<<" at byte: "<<iByte_tot;
957     string msg = oss.str();
958     mainLogUtil->logAll(msg);
959     }
960     // is_new_route will be reset to false in PacketUser after the packet is used and stored
961    
962     if(
963     ((int)(unsigned char)headVRL[0]!=CODE_FF)||
964     ((int)(unsigned char)headVRL[1]!=CODE_46)||
965     ((int)(unsigned char)headVRL[2]!=CODE_D5)
966     )
967     {
968     return false;
969     }
970    
971     //TODO: check the cadre number and save this info?
972     //TODO check that the lenght is 8
973     return true;
974     }
975    
976     //function that computes the CRC for each cadre
977     short int CRC_Cadre_Check(char dataVRL[], int length)
978     {
979     //function used only if(do_vrl_check)
980     short int Summ;
981     int i;
982     int CSum, tmp;
983    
984     tmp=(int)dataVRL[0];
985     CSum=tmp;
986     for (i=1;i<length;i++) {tmp=dataVRL[i]; CSum=CSum^tmp;}
987     Summ=(short int) (CSum);
988     return Summ;
989     }
990    
991    
992     //TODO: funzione per debug, rimuovere marco
993     void split_input(char fni[])
994     {
995     cout<<"splitting input file"<<endl;
996    
997     system("mkdir splitted");
998    
999     ifstream fin;
1000     fin.open(fni, ifstream::binary);
1001     if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
1002    
1003     ofstream fout;
1004     static int num=0;
1005    
1006     int cic = 1024;
1007     int mille =1000;
1008     int quanti = 50;
1009     int tanti =quanti*mille*cic;
1010     char * tmp = new char[tanti];
1011     while(fin.eof()==0)
1012     {
1013     fin.read(tmp,tanti);
1014     stringstream fno;
1015     fno<<"./splitted/splitted_"<<++num;
1016     fout.open(fno.str().c_str(), ios::binary);
1017     fout.write(tmp,tanti);
1018     fout.close();
1019     }
1020    
1021     delete[] tmp;
1022     fin.close();
1023     cout<<"done, output in folder splitted"<<endl;
1024     }
1025    
1026    

  ViewVC Help
Powered by ViewVC 1.1.23