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

Annotation of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Tue Nov 4 09:44:33 2008 UTC (16 years ago) by mocchiut
Branch: MAIN
Changes since 1.1: +13 -18 lines
chewbacca upgrade, YP upgrade

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

  ViewVC Help
Powered by ViewVC 1.1.23