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

Annotation of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp_01

Parent Directory Parent Directory | Revision Log Revision Log


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

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

  ViewVC Help
Powered by ViewVC 1.1.23