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

Contents of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (show annotations) (download)
Thu Dec 18 14:47:02 2008 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r00, v1r01
Changes since 1.4: +28 -9 lines
Default values changed, symbolic path handling implemented, cleanup of backup files, do not delete files from ROOT_TABLE_MERGING

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

  ViewVC Help
Powered by ViewVC 1.1.23