/[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.4 - (show annotations) (download)
Thu Dec 18 12:58:37 2008 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.3: +18 -5 lines
December 18th version

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

  ViewVC Help
Powered by ViewVC 1.1.23