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

Annotation of /chewbacca/PamOffLineSW/old_PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Thu Dec 18 14:47:07 2008 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
FILE REMOVED
Default values changed, symbolic path handling implemented, cleanup of backup files, do not delete files from ROOT_TABLE_MERGING

1 mocchiut 1.1 //============================================================================
2 mocchiut 1.2 // $Id: old_PacketUser.cpp,v 1.1 2008/09/23 07:20:12 mocchiut Exp $
3 mocchiut 1.1 // Description :
4     //============================================================================
5     #include "PacketUser.h"
6     #include <sys/time.h>
7    
8     namespace PamOffLineSW
9     {
10     extern LogUtil* mainLogUtil;
11     extern short compression ;
12     extern char *outDir;
13     extern TSQLServer *sqlServer;
14     extern bool multiFile;
15     extern char* fni;
16     extern char * nome_output;
17     extern bool is_new_route;
18     extern unsigned int download;
19    
20     extern unsigned int mmm_number;
21     extern unsigned int orbit_number;
22     extern unsigned long int time_Offset;
23     extern bool tryMerge;
24    
25     extern bool do_cont_check;
26    
27     //extern unsigned long int delta_Time;//unused ??
28    
29     PacketUser PacketUser::instance;
30    
31     int PacketUser::numDiscontinity=0;
32     int PacketUser::numPKT=0;
33     int PacketUser::numPKTSaved=0;
34    
35     PacketUser& PacketUser::getInstance()
36     {
37     return instance;
38     }
39    
40     PacketUser::PacketUser()
41     {
42     gROOT->SetBatch(kTRUE);
43     numDiscontinity=0;
44     numPKTSaved=0;
45     numPKT=0;
46     pkt_number_init=0;
47     obt_init=0;
48     pkt_number_last=0;
49     obt_last=0;
50     obt_time_sync=0;
51     last_time_sync_info=0;
52     real_time_init=0;
53     real_time_last=0;
54     bad_pkt=0;
55     bad_pkt_EventReader=0;
56     bad_pkt_CalibReader=0;
57     reader= NULL;
58     pRun=NULL;
59     reader=new pamela::techmodel::EventReader();
60     Table_ROOT_Good="ROOT_TABLE";
61     Table_ROOT_Bad="ROOT_TABLE_BAD";
62     Table_PKT_Bad= "BAD_PKT_TABLE";
63     Table_GL_RESURS_OFFSET="GL_RESURS_OFFSET";
64    
65     // good_pkt_Calib=0;
66    
67     }
68    
69     PacketUser::~PacketUser()
70     {
71     if(reader){delete reader; reader = NULL;}
72     numDiscontinity=0;
73     numPKTSaved=0;
74     numPKT=0;
75     pkt_number_init=0;
76     obt_init=0;
77     pkt_number_last=0;
78     obt_last=0;
79     obt_time_sync = 0;
80     last_time_sync_info = 0;
81     real_time_init=0;
82     real_time_last=0;
83     bad_pkt=0;
84     bad_pkt_EventReader=0;
85     bad_pkt_CalibReader=0;
86     // good_pkt_Calib=0;
87     }
88    
89     //Put the packet in a root file. Create a new ROOT file for each group of packets
90     void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type,
91     unsigned long int counter, unsigned long int obt)
92     {
93     //If the packet type was not recognised before
94     if(!type){return;} //numPKT++;
95    
96     //in order to start ...
97     if(numPKT==0)
98     {
99     setInit(counter,obt);
100     StartGroup();
101     }
102    
103     //here we know if the incoming packet can be considered consecutive to the previuos one
104     if((!isCons)&&(numPKT!=0))
105     {
106     setReal_Time();
107     //closing the group in file rootfilename
108     FinishGroup(rootfilename);
109    
110     //the current pkt will be placed in a new group: rootfilename
111     numDiscontinity++;
112     setInit(counter,obt);
113     StartGroup();
114     }
115    
116     //add current packet to the group and calculate bad_pkt_EventReader and bad_pkt_CalibReader
117     int ret=
118     reader->PKT_RunEvent(headerPkt, pamPkt, length, type);
119     /*
120     ret
121     0 packet good
122     1 exception but used //never happen
123     2 CRC exception but used
124     3 CALIBRATION PACKET with error but used
125     -1 CRC exception packet DISCARDED
126     -2 FATAL exception packet DISCARDED //never happen
127     -3 No way to read events of this type. packet DISCARDED( es OLD CalibCal)
128     ret = 10 if the packet is good but comes from a cadre with VRL problems
129     */
130    
131     //here we know if the packet comes from one or more corrupted cadres
132     //if(!isPKTGood){bad_pkt++;}
133     if((!isPKTGood)&&(!ret)){ret=10;}
134    
135     switch (ret)
136     {
137     case 0: {numPKTSaved++; break;}
138     case 1: {bad_pkt_EventReader++; numPKTSaved++; break;}
139     case 2: {bad_pkt_EventReader++;numPKTSaved++; break;}
140     case 3: {bad_pkt_CalibReader++; numPKTSaved++; break;}
141     case 10: {bad_pkt++; numPKTSaved++; break;}
142     default:{
143     stringstream oss;
144     oss.str()="";
145     oss<<"Packet DISCARDED, reason = "<<ret;
146     string msg = oss.str();
147     mainLogUtil->logAll(msg);
148     break;
149     }
150     }
151     /*
152     if ((ret==0)&&((type==PacketType::CalibTrk1)||(type==PacketType::CalibTrk2)|| (type==PacketType::CalibCalPed)))
153     {
154     good_pkt_Calib++;//maybe I will remove this in future
155     }
156     */
157    
158     if(ret>=0)// only the packet not discarded of course
159     setLast(counter,obt);
160    
161     //TODO: decide what kind of packet I can use here
162     // if((ret==0)||(ret==10))//I want to use only good packet
163     if(ret>=0)//I want to use only accepted packet
164     setTimeSync(pamPkt, type);
165    
166     //if(ret>0)// if I want to save all the packets with error and the reason
167     if(ret==3)//I want to save only Calibration packets
168     {
169     /*
170     //dbg: save in file,
171     //TODO remove this later
172     //if(SaveInFile)
173     savePKT_file(headerPkt,
174     pamPkt,
175     length,
176     false,
177     rootfilename);
178     mainLogUtil->logInfo("saved bad packet in file");
179     */
180     //TODO: move the storing in DB after, when I know the ROOT_ID_N
181     // If I don't have the DB I can exit
182     if(!do_cont_check){
183     if(pamPkt){delete[] pamPkt; pamPkt = NULL;}
184     if(headerPkt){delete[] headerPkt; headerPkt = NULL;}
185     numPKT++;
186     return;
187     }
188    
189     if(saveBADPKT_DB(Table_PKT_Bad,
190     outDir,
191     rootfilename,
192     counter,
193     obt,
194     fni,
195     ret,
196     999)==true)
197     {
198     stringstream oss1;
199     oss1.str()="";
200     oss1<<"Saved information regarding bad packet of file: "<<rootfilename<<" in table "<<Table_PKT_Bad;
201     string msg1 = oss1.str();
202     mainLogUtil->logInfo(msg1);
203     }
204     else
205     {
206     stringstream oss1;
207     oss1.str()="";
208     oss1<<"Problem storing information regarding bad packet of file: "<<rootfilename<<" in table "<<Table_PKT_Bad;
209     string msg1 = oss1.str();
210     mainLogUtil->logError(msg1);
211     }
212     }
213    
214     //just to be sure ...
215     if(pamPkt){delete[] pamPkt; pamPkt = NULL;}
216     //just to be sure ...
217     if(headerPkt){delete[] headerPkt; headerPkt = NULL;}
218    
219     numPKT++;
220     }
221    
222     //set the value of the counter and obt of the first packet of the group
223     void PacketUser::setInit(unsigned long int counter, unsigned long int obt)
224     {
225     sprintf(nnnn_mmm_ppp,"%05d_%03d_%03d", orbit_number, mmm_number, download);
226     sprintf(rootfilename,"%s_%s_%d", nome_output, nnnn_mmm_ppp, numDiscontinity);
227     pkt_number_init=counter;
228     obt_init=obt;
229     pkt_number_last=0;
230     obt_last=0;
231     real_time_init=0;
232     real_time_last=0;
233     bad_pkt=0;
234     numPKTSaved=0;
235     bad_pkt_EventReader=0; bad_pkt_CalibReader=0;
236     //good_pkt_Calib=0;
237     }
238    
239     //starts a new root file
240     void PacketUser::StartGroup()
241     {
242     pRun = new PamelaRun(rootfilename, outDir , multiFile, compression);
243     reader->Init(pRun);
244     stringstream oss;
245     oss.str()="";
246     oss<<"######################### Start a new group in file: "<< rootfilename <<" ############################";
247     string msg = oss.str();
248     mainLogUtil->logInfo(msg);
249     }
250    
251     //set the value of the counter and obt of the last packet of the group
252     void PacketUser::setLast(unsigned long int counter, unsigned long int obt)
253     {
254     pkt_number_last=counter;
255     obt_last=obt;
256     }
257    
258     //retrieve obt_time_sync and last_time_sync_info from packet if type has special values
259     void PacketUser::setTimeSync(char* packet, const PacketType* type)
260     {
261     //do nothing
262     if(!do_cont_check) {
263     if(is_new_route)
264     is_new_route=false;
265     return;
266     }
267    
268     //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0
269     //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi
270    
271     //When I found a new download I need to reset all values
272     if(is_new_route)
273     {
274     //reset
275     obt_time_sync=0; last_time_sync_info=0; is_new_route=false;
276     if(time_Offset)
277     timeOffset=time_Offset;
278     else
279     timeOffset=retrieveTimeOffset();
280     }
281    
282     //Retrieve if possible obt_time_sync and last_time_sync_info
283     //i don't need to retrieve them again if they are good
284     if((obt_time_sync)||(last_time_sync_info)) return;
285    
286     int offset=0;
287     if(type==PacketType::RunHeader)
288     {
289     offset=0;
290     }
291     else if(type==PacketType::RunTrailer)
292     {
293     offset=3;
294     }
295     else if(type==PacketType::Mcmd)
296     {
297     mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");
298     return;
299     }
300     else
301     {
302     return;
303     }
304     obt_time_sync = (((UINT32)packet[5+offset]<<24)&0xFF000000) + (((UINT32)packet[6+offset]<<16)&0x00FF0000) + (((UINT32)packet[7+offset]<<8)&0x0000FF00) + (((UINT32)packet[8+offset])&0x000000FF);
305     last_time_sync_info = (((UINT32)packet[9+offset]<<24)&0xFF000000) + (((UINT32)packet[10+offset]<<16)&0x00FF0000) + (((UINT32)packet[11+offset]<<8)&0x0000FF00) + (((UINT32)packet[12+offset])&0x000000FF);
306    
307     stringstream oss;
308     oss.str()="";
309     oss<<"In download: "<<download<<" timeOffset: "<<timeOffset<<" obt_time_sync: "<<obt_time_sync<<" last_time_sync_info: "<<last_time_sync_info<<" using Packet Type: "<<type->GetName().c_str();
310     string msg = oss.str();
311     mainLogUtil->logAll(msg);
312     }
313    
314     //set the real time of the first packet and the last packet oif a group of packet if possible
315     void PacketUser::setReal_Time()
316     {
317     if((obt_time_sync)||(last_time_sync_info))
318     {
319     real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
320     real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
321     real_time_init+=timeOffset;
322     real_time_last+=timeOffset;
323     }
324     }
325    
326     //timeOffset
327     //retrieve from table GL_RESURS_OFFSET timeOffset
328     unsigned long int PacketUser::retrieveTimeOffset()
329     {
330     //TODO check here ... marco
331     if(!do_cont_check) return 0;
332     UInt_t t0 = 0; //toffset
333    
334     stringstream oss;
335     oss.str("");
336     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
337     << nnnn_mmm_ppp << "';";
338    
339     TSQLResult* res=NULL;
340     TSQLRow* row=NULL;
341     res= sqlServer->Query(oss.str().c_str());
342     if(!res)
343     {
344     cout<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
345     return 0;
346     }
347    
348     row=res->Next();
349     if (!row )
350     {
351     if(res){delete res; res = NULL;}
352    
353     oss.str("");
354     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT < "
355     << orbit_number << " order by FROM_ORBIT desc limit 1;";
356     res= sqlServer->Query(oss.str().c_str());
357     if(!res)
358     {
359     cout<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
360     return 0;
361     }
362     row=res->Next();
363     if (!row)
364     {
365     cout<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
366     return 0;
367     }
368    
369     }
370    
371     TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
372     t0 = (UInt_t)tu.GetSec();
373    
374     if(res){delete res; res = NULL;}
375     if(row){delete row; row = NULL;}
376    
377     return t0;
378     }
379    
380     //this is public and can be called from the main at the end of the game
381     void PacketUser::FinishLastGroup()
382     {
383     mainLogUtil->logAll("######################### Closing the last group ############################");
384     setReal_Time();
385     FinishGroup(rootfilename);
386     mainLogUtil->logAll("######################### Closed the last group ############################");
387    
388     }
389    
390     //finish the old root file
391     void PacketUser::FinishGroup(char * filename)
392     {
393     if(pRun)
394     {
395     pRun->WriteFiles();
396     delete pRun; pRun = NULL;
397     stringstream close;
398     close.str()="";
399     string msg;
400    
401     close<<"######################### Close group in file: "<< rootfilename <<" ############################";
402     msg = close.str();
403     mainLogUtil->logInfo(msg);
404    
405     //TODO: check ... se non faccio proprio controlli su continuita' neanche salvo su DB
406     // if((!step_pkt_number)&&(!step_pkt_obt)) return;
407     if(!do_cont_check) return;
408    
409     stringstream oss;
410     oss.str()="";
411    
412     //TODO: check when I don't want to log in DB informations
413    
414     // se non ho salvato nessun pacchetto
415     if(numPKTSaved==0){
416     stringstream err;
417     err.str()="";
418     err<<"NO PACKET SAVED in file: "<< rootfilename <<" The group is empty: you can remove this file.";
419     msg = err.str();
420     mainLogUtil->logError(msg);
421     return;
422     }
423    
424     //se non ho salvato nessun pacchetto buono
425     if((numPKTSaved-bad_pkt_EventReader-bad_pkt_CalibReader-bad_pkt)==0){
426     stringstream err;
427     err.str()="";
428     err<<"NO GOOD PACKET SAVED in file: "<< rootfilename <<" The group is BAD: you may want to remove this file.";
429     msg = err.str();
430     mainLogUtil->logInfo(msg);
431     // mainLogUtil->logError(msg);
432     // return; //TODO: ???
433     }
434     /*
435     //se non ho salvato nessun pacchetto buono di calibrazione
436     if(good_pkt_Calib==0){
437     stringstream err;
438     err.str()="";
439     err<<"NO GOOD CALIB PACKET SAVED in file: "<< rootfilename <<" this is strange";
440     msg = err.str();
441     mainLogUtil->logInfo(msg);
442     // mainLogUtil->logError(msg);
443     // return;
444     }
445     //TODO: I should/can use good_pkt_Calib storing this info in DB??
446     */
447    
448     //don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range
449     if((!real_time_init)&&(!real_time_last))
450     {
451     //saved in another table for future study Record_Time()
452     if(saveROOT_DB(Table_ROOT_Bad, outDir,
453     filename,
454     pkt_number_init, pkt_number_last,
455     obt_init, obt_last,
456     obt_time_sync, last_time_sync_info,
457     real_time_init, real_time_last,
458     timeOffset,
459     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
460     fni)==true)
461     {
462     oss.str()="";
463     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;
464     msg = oss.str();
465     mainLogUtil->logInfo(msg);
466     }
467     else
468     {
469     oss.str()="";
470     oss<<"Problem storing information in DB regarding file: "<<filename;
471     msg = oss.str();
472     mainLogUtil->logError(msg);
473     }
474     return;
475     }
476    
477     //saves info in DB in table Table_ROOT_Good
478     if(saveROOT_DB(Table_ROOT_Good, outDir,
479     filename,
480     pkt_number_init, pkt_number_last,
481     obt_init, obt_last,
482     obt_time_sync, last_time_sync_info,
483     real_time_init, real_time_last,
484     timeOffset,
485     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
486     fni)==true)
487     {
488     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good;
489     msg = oss.str();
490     mainLogUtil->logInfo(msg);
491    
492     //TODO : work on this ....
493     if(tryMerge){
494     merge_ROOTfiles();
495     }
496     }
497     else
498     {
499     oss<<"Problem storing information in DB regarding file: "<<filename;
500     msg = oss.str();
501     mainLogUtil->logError(msg);
502     }
503     }
504     }
505    
506     //inserisci in DB in apposita tabella il pkt con errore
507     bool PacketUser::saveBADPKT_DB(char* table_name,
508     char* folder_name,
509     char* file_name,
510     unsigned long int pkt_number,
511     unsigned long int pkt_obt,
512     char* nome_input,
513     int reason,
514     int rootID)
515     {
516     stringstream oss;
517     oss.str("");
518     oss << "INSERT INTO "<< table_name <<" (ID_N, FOLDER_NAME, FILE_NAME, PKT_NUMBER, PKT_OBT, INPUT_NAME, REASON, ROOT_ID_N, INSERT_TIME)"
519     << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','"
520     << pkt_number <<"','"
521     << pkt_obt << "','"
522     << nome_input <<"','"
523     << reason <<"','"<< rootID<< "',"
524     <<"NULL"<<
525     ")";
526     string msg = oss.str();
527     mainLogUtil->logAll(msg);
528    
529     stringstream oss1;
530     oss1.str()="";
531     string msg1;
532    
533     TSQLResult* res=NULL;
534     res= sqlServer->Query(oss.str().c_str());
535     if(!res)
536     {
537     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
538     msg1=oss1.str();
539     mainLogUtil->logError(msg1);
540     return false;
541     }
542     if(res){delete res; res = NULL;}
543     return true;
544     }
545    
546     //save in Table_ROOT_Good or in Table_ROOT_Bad
547     bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,
548     unsigned long int pkt_number_init, unsigned long int pkt_number_final,
549     unsigned long int obt_init, unsigned long int obt_final,
550     unsigned long int oT_sync, unsigned long int lT_sync_info,
551     unsigned long int mtime_init, unsigned long int mtime_last,
552     unsigned long int time_offset,
553     int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,
554     char* nome_input)
555     {
556     //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
557     stringstream oss;
558     oss.str("");
559     oss << "INSERT INTO "<< table_name <<" (ID_N, FOLDER_NAME, FILE_NAME, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, OBT_TIME_SYNC, LAST_TIME_SYNC_INFO, REAL_TIME_INIT,REAL_TIME_LAST,TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME)"
560     << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_init <<"','"<< pkt_number_final << "','"<< obt_init <<"','"<< obt_final<< "','"
561     << oT_sync << "','" << lT_sync_info << "','"
562     << mtime_init << "','" << mtime_last << "','"
563     << time_offset << "','"
564     << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< numPKTSaved<<"','"
565     << nome_input <<"',"
566     <<"NULL"<<
567     ")";
568     string msg = oss.str();
569     mainLogUtil->logAll(msg);
570    
571     stringstream oss1;
572     oss1.str()="";
573     string msg1;
574    
575     TSQLResult* res=NULL;
576     res= sqlServer->Query(oss.str().c_str());
577     if(!res)
578     {
579     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
580     msg1=oss1.str();
581     mainLogUtil->logError(msg1);
582     return false;
583     }
584     if(res){delete res; res = NULL;}
585     return true;
586     }
587    
588     //save a packet in a file
589     void PacketUser::savePKT_file(char* headerPkt,
590     char* pamPkt,
591     long int length,
592     bool append,
593     char* nomefile)
594     {
595     char fno[80]="";
596     ofstream fout;
597     sprintf(fno,"%s/pkt%d_of_%s.pkt", outDir, numPKTSaved, nomefile);
598    
599     if(append==true)
600     fout.open(fno, ios::binary | ios::app);
601     else
602     fout.open(fno, ios::binary);
603    
604     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
605     fout.write(headerPkt,LENGTH_HEADER_PKT);
606     fout.write(pamPkt,length);
607     fout.close();
608     }
609    
610    
611     /**********************************************************************************************/
612     /*###########################################################################################################*/
613     /********************************** MERGING *************************************************/
614     /*###########################################################################################################*/
615     // Merge ROOT files: found other ROOT files in the same temporal ranges
616     //it finds ROOT files that covers particular temporal range of interest and save this info in DB
617     /**********************************************************************************************/
618     bool PacketUser::merge_ROOTfiles()
619     {
620     //do nothing....
621     //mainLogUtil->logAlways(" Merging functionality doesn't work properly. Try to do it later !!");
622     //cout<<"DBG: Merging functionality doesn't work properly. Try to do it later !!"<<endl;
623     //return false;
624    
625     //TODO: se non ho almeno due packetti buoni non uso
626     if((numPKTSaved-bad_pkt_EventReader-bad_pkt_CalibReader-bad_pkt)<2){
627     stringstream oss;
628     string msg;
629     oss.str()="";
630     oss<<"NO ENOUGH GOOD PACKET SAVED in file: "<< rootfilename <<" The group is BAD and don't used in Merging.";
631    
632     msg = oss.str();
633     mainLogUtil->logError(msg);
634     return false;
635     }
636    
637     //the ID of the current ROOT file in table Table_ROOT_Good
638     unsigned int my_id = select_maxIDN_DB(Table_ROOT_Good);
639     unsigned int n_tot_ass =0;
640     unsigned int *idN_ass = NULL;
641     unsigned int num_ass =0;
642     for(int relaz=AFTER; relaz<=BIGGER; relaz++){
643     idN_ass = Return_IDN_ASS(real_time_init, real_time_last, (type_Rel_ROOT)relaz, my_id, num_ass);
644    
645     for(int i=0;i<num_ass;i++)
646     saveROOT_ASS_DB("ROOT_TABLE_ASS", my_id, idN_ass[i], relaz);//change here
647    
648     n_tot_ass += num_ass;
649     num_ass =0;
650     if(idN_ass){delete[] idN_ass; idN_ass=NULL;}
651     }
652    
653     if(n_tot_ass)
654     {
655     mainLogUtil->logInfo(" Merged ");
656     return true;
657     }
658     else
659     {
660     mainLogUtil->logInfo(" Nothing to Merge ");
661     return false;
662     }
663     }
664    
665    
666     unsigned int* PacketUser::Return_IDN_ASS(unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel, unsigned int my_id, unsigned int &num_ass)
667     {
668     unsigned int *idNass = NULL;
669     stringstream query;
670     query.str("");
671    
672     //ordinare in qualche modo
673     //TODO usare delta_Time: almeno per AFTER e BEFORE ho una certa tolleranza?
674    
675     if(type_rel == AFTER)
676     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_LAST = "<< real_time_init<<" and ID_N != " <<my_id;
677     else if(type_rel == AFTER2)
678     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_LAST > "<< real_time_init <<" and REAL_TIME_LAST < "<< real_time_last <<" and REAL_TIME_INIT < "<< real_time_init<<" and ID_N != " <<my_id ;
679     else if(type_rel == BEFORE)
680     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_INIT = "<< real_time_last<<" and ID_N != " <<my_id ;
681     else if(type_rel == BEFORE2)
682     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_INIT > "<< real_time_init <<" and REAL_TIME_INIT < "<< real_time_last <<" and REAL_TIME_LAST > "<< real_time_last <<" and ID_N != " <<my_id ;
683     else if(type_rel == SMALLER)
684     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_INIT < "<< real_time_init <<" and REAL_TIME_LAST > "<< real_time_last<<" and ID_N != " <<my_id ;
685     else if(type_rel == BIGGER)
686     query<<"select ID_N from "<<Table_ROOT_Good<<" where REAL_TIME_INIT > "<< real_time_init <<" and REAL_TIME_LAST < "<< real_time_last<<" and ID_N != " <<my_id ;
687     else
688     return 0;
689     //TODO: change here ....
690     query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
691     ///cout<<"$$$$$ DBG: "<<query.str().c_str()<<endl;
692    
693     TSQLResult* res=NULL;
694     TSQLRow* row=NULL;
695     unsigned int num_rows=0;
696    
697     res= sqlServer->Query(query.str().c_str());
698    
699     stringstream oss1;
700     oss1.str()="";
701     string msg1;
702    
703     if (!res)
704     {
705     oss1<<"DBError UNABLE to: "<<query.str().c_str()<<endl;
706     msg1=oss1.str();
707     mainLogUtil->logError(msg1);
708     return 0;
709     }
710    
711     num_rows=res->GetRowCount();
712     if(num_rows>0)
713     {
714     idNass = new unsigned int[num_rows];
715    
716     for(unsigned int i=0; i<num_rows; i++)
717     {
718     row=res->Next();
719     if (!row)
720     {
721     oss1<<"DBError UNABLE to: "<<query.str().c_str()<<endl;
722     msg1=oss1.str();
723     mainLogUtil->logError(msg1);
724     return 0;
725     }
726     idNass[i]=atoi(row->GetField(0));
727     stringstream oss2;
728     oss2.str()="";
729     oss2<<" my_id "<<my_id<<" ha type_rel = "<<type_rel<<" rispetto: "<<idNass[i];
730     string msg2 = oss2.str();
731     mainLogUtil->logAll(msg2);
732    
733     }
734     }
735    
736     if(res){delete res; res = NULL;}
737     if(row){delete row; row = NULL;}
738    
739     num_ass=num_rows;
740     return idNass;
741     }
742    
743     //Utility: return max(IDN)
744     unsigned int PacketUser::select_maxIDN_DB(char* table_name)
745     {
746     unsigned int idN=0;
747     TSQLResult* res=NULL;
748     stringstream oss;
749     oss.str("");
750     oss<<"select max(ID_N) from "<<table_name;
751     res= sqlServer->Query(oss.str().c_str());
752    
753     stringstream oss1;
754     oss1.str()="";
755     string msg1;
756    
757     if(!res)
758     {
759     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
760     msg1=oss1.str();
761     mainLogUtil->logError(msg1);
762     return 0;
763     }
764    
765     TSQLRow* row=NULL;
766     row=res->Next();
767     if (!row )
768     {
769     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
770     msg1=oss1.str();
771     mainLogUtil->logError(msg1);
772     return 0;
773     }
774    
775     idN=atoi(row->GetField(0));
776     if(row){delete row; row = NULL;}
777     if(res){delete res; res = NULL;}
778     return idN;
779     }
780    
781    
782     //save in Table_ROOT_ASS
783     bool PacketUser::saveROOT_ASS_DB(char* table_name,
784     unsigned int my_idn,
785     unsigned int idn_ass,
786     int type_ass)
787     {
788     stringstream oss;
789     oss.str("");
790     oss << "INSERT INTO "<< table_name <<" (ID_N, ROOT_ID_N, ID_N_ASS, TYPE_ASS, INSERT_TIME)"
791     << " VALUES ('"<<0<< "','" <<my_idn<<"','" <<idn_ass<< "','" << type_ass <<"',"<<"NULL"<<")";
792    
793     // cout<<"DBG: "<<oss.str().c_str()<<endl;
794     string msg = oss.str();
795     mainLogUtil->logAll(msg);
796    
797     TSQLResult* res=NULL;
798     res= sqlServer->Query(oss.str().c_str());
799     stringstream oss1;
800     oss1.str()="";
801     string msg1;
802    
803     if (!res)
804     {
805     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
806     msg1=oss1.str();
807     mainLogUtil->logError(msg1);
808     return false;
809     }
810     if(res){delete res; res = NULL;}
811     return true;
812     }
813    
814     /**********************************************************************************************/
815     //UNUSED: dbg functions...
816    
817     //Return the system time in ms
818     unsigned long long PacketUser::Record_Time(){
819     timeval tv;
820     gettimeofday(&tv,NULL);
821     unsigned long long timems = (unsigned long long)tv.tv_sec * (unsigned long long)1000 + tv.tv_usec / 1000;
822     return timems;
823     }
824    
825     //dbg_functions
826     void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append)
827     {
828     char fno[80]="";
829     ofstream fout;
830     if(append==true)
831     {
832     sprintf(fno,"%s/packets.pkt", outDir);
833     fout.open(fno, ios::binary | ios::app);
834     }
835     else{
836     sprintf(fno,"%s/packet%d.pkt", outDir, numPKT );
837     fout.open(fno, ios::binary);
838     }
839    
840     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
841     fout.write(headerPkt,LENGTH_HEADER_PKT);
842     fout.write(pamPkt,length);
843     fout.close();
844     }
845    
846    
847     }

  ViewVC Help
Powered by ViewVC 1.1.23