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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations) (download)
Mon Dec 22 16:50:10 2008 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.4: +11 -9 lines
BOOT_NUMBER bug in chewbacca fixed, extractcalib updated

1 mocchiut 1.1 //============================================================================
2 mocchiut 1.5 // $Id: PacketUser.cpp,v 1.4 2008/12/18 14:46:56 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     //marco_new_01
25     extern bool single_connection;
26    
27     //don't perform check about continuity: it means don't use DB.
28     extern bool do_cont_check;
29    
30     //marco_new_31:
31     extern char* db_user;
32     extern char* db_pwd;
33     extern char* connection;
34    
35 mocchiut 1.2 //per tenere conto reset obt
36     extern unsigned long int max_pkt_obt;
37    
38 mocchiut 1.3 #define TAGVALUELEN 4
39     extern char tag_value[TAGVALUELEN];
40 mocchiut 1.2
41 mocchiut 1.1 PacketUser PacketUser::instance;
42    
43     int PacketUser::numDiscontinity=1;
44     int PacketUser::numPKT=0;
45     int PacketUser::numPKTSaved=0;
46    
47     PacketUser& PacketUser::getInstance()
48     {
49     return instance;
50     }
51    
52     PacketUser::PacketUser()
53     {
54     gROOT->SetBatch(kTRUE);
55     numDiscontinity=1;
56     numPKTSaved=0;
57     numPKT=0;
58     pkt_number_init=0;
59     obt_init=0;
60     pkt_number_last=0;
61     obt_last=0;
62     obt_time_sync=0;
63     last_time_sync_info=0;
64 mocchiut 1.3
65     //(tassa)
66     obt_time_sync_prevvalue=0;
67     last_time_sync_info_prevvalue=0;
68     time_is_estimated=false;
69    
70    
71 mocchiut 1.1 real_time_init=0;
72     real_time_last=0;
73     bad_pkt=0;
74     bad_pkt_EventReader=0;
75     bad_pkt_CalibReader=0;
76     reader= NULL;
77     pRun=NULL;
78     reader=new pamela::techmodel::EventReader();
79     Table_ROOT_Good="ROOT_TABLE";
80     Table_ROOT_Bad="ROOT_TABLE_BAD";
81     Table_GL_RESURS_OFFSET="GL_RESURS_OFFSET";
82     Table_ROOT_Merging="ROOT_TABLE_MERGING";
83     // good_pkt_Calib=0;
84     my_id=0;
85     boot_number=0;
86 mocchiut 1.3 //(tassa)
87     boot_number_prevvalue=0;
88    
89     //(tassa)
90     id_to_recover[1000];
91     id_to_recover_index=0;
92 mocchiut 1.1 }
93    
94     PacketUser::~PacketUser()
95     {
96     if(reader){delete reader; reader = NULL;}
97     numDiscontinity=1;
98     numPKTSaved=0;
99     numPKT=0;
100     pkt_number_init=0;
101     obt_init=0;
102     pkt_number_last=0;
103     obt_last=0;
104     obt_time_sync = 0;
105     last_time_sync_info = 0;
106 mocchiut 1.3 obt_time_sync_prevvalue=0;
107     last_time_sync_info_prevvalue=0;
108     time_is_estimated=false;
109    
110 mocchiut 1.1 real_time_init=0;
111     real_time_last=0;
112     bad_pkt=0;
113     bad_pkt_EventReader=0;
114     bad_pkt_CalibReader=0;
115     // good_pkt_Calib=0;
116     my_id=0;
117     boot_number=0;
118 mocchiut 1.3 boot_number_prevvalue=0;
119 mocchiut 1.1 }
120    
121 mocchiut 1.2
122 mocchiut 1.1 //Put the packet in a root file. Create a new ROOT file for each group of packets
123     void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type,
124     unsigned long int counter, unsigned long int obt)
125     {
126     //If the packet type was not recognised before
127     if(!type){return;}
128 mocchiut 1.2
129 mocchiut 1.1 //in order to start ...
130     if(numPKT==0)
131     {
132     if(single_connection){
133     OpenDBConnection(NULL);
134     }
135    
136     setInit(counter,obt);
137     StartGroup();
138     }
139    
140     //here we know if the incoming packet can be considered consecutive to the previuos one
141     if((!isCons)&&(numPKT!=0))
142     {
143     setReal_Time();
144 mocchiut 1.2 //closing the group in file rootfilename
145 mocchiut 1.1 FinishGroup(rootfilename);
146    
147 mocchiut 1.2 //se gia' e' cambiato download non cambio numdisco
148     if(!is_new_route){numDiscontinity++;}
149    
150 mocchiut 1.1 //the current pkt will be placed in a new group: rootfilename
151     setInit(counter,obt);
152     StartGroup();
153     }
154    
155     //add current packet to the group and calculate bad_pkt_EventReader and bad_pkt_CalibReader
156     int ret=
157     reader->PKT_RunEvent(headerPkt, pamPkt, length, type);
158     /*
159     ret
160     0 packet good
161     1 exception but used //never happen
162     2 CRC exception but used
163     3 CALIBRATION PACKET with error but used
164     -1 CRC exception packet DISCARDED
165     -2 FATAL exception packet DISCARDED //never happen
166     -3 No way to read events of this type. packet DISCARDED( es OLD CalibCal)
167     ret = 10 if the packet is good but comes from a cadre with VRL problems
168     */
169    
170 mocchiut 1.2 //here we also know if the packet comes from one or more corrupted cadres
171 mocchiut 1.1 if((!isPKTGood)&&(!ret)){ret=10;}
172     switch (ret)
173     {
174     case 0: {numPKTSaved++; break;}
175     case 1: {bad_pkt_EventReader++; numPKTSaved++; break;}
176     case 2: {bad_pkt_EventReader++;numPKTSaved++; break;}
177     case 3: {bad_pkt_CalibReader++; numPKTSaved++; break;}
178     case 10: {bad_pkt++; numPKTSaved++; break;}
179     default:{
180     stringstream oss;
181     oss.str()="";
182     oss<<"Packet DISCARDED, reason = "<<ret;
183     string msg = oss.str();
184     mainLogUtil->logAll(msg);
185     break;
186     }
187     }
188    
189     /*
190     if ((ret==0)&&((type==PacketType::CalibTrk1)||(type==PacketType::CalibTrk2)|| (type==PacketType::CalibCalPed)))
191     {
192     good_pkt_Calib++;//maybe I will remove this in future
193     }
194     */
195    
196     if(ret>=0)// only the packet not discarded of course
197     setLast(counter,obt);
198    
199     //TODO: decide what kind of packet I can use here
200     // if((ret==0)||(ret==10))//I want to use only good packet
201     if(ret>=0)//I want to use only accepted packet
202     {
203 mocchiut 1.2 setTimeSync(pamPkt, length, type);//here I retrieve also the boot_number if packet type is vardump
204 mocchiut 1.1 }
205    
206     //just to be sure ...
207     if(pamPkt){delete[] pamPkt; pamPkt = NULL;}
208     if(headerPkt){delete[] headerPkt; headerPkt = NULL;}
209    
210     numPKT++;
211     }
212    
213     //set the value of the counter and obt of the first packet of the group
214     void PacketUser::setInit(unsigned long int counter, unsigned long int obt)
215     {
216     sprintf(nnnn_mmm_ppp,"%05d_%03d_%03d", orbit_number, mmm_number, download);
217     sprintf(rootfilename,"%s_%s_%d", nome_output, nnnn_mmm_ppp, numDiscontinity);
218     pkt_number_init=counter;
219     obt_init=obt;
220     pkt_number_last=0;
221     obt_last=0;
222     real_time_init=0;
223     real_time_last=0;
224     bad_pkt=0;
225     numPKTSaved=0;
226     bad_pkt_EventReader=0; bad_pkt_CalibReader=0;
227     //good_pkt_Calib=0;
228    
229     //marco_NB:
230     //ogni volta che trovo una discontinuit� resetto come se mi trovassi in un nuovo download oppure uso i vecchi valori se esistono?
231 mocchiut 1.3
232     obt_time_sync_prevvalue=obt_time_sync;
233     last_time_sync_info_prevvalue=last_time_sync_info;
234     time_is_estimated=false;
235     obt_time_sync=0; last_time_sync_info=0;
236     boot_number_prevvalue=boot_number;
237     boot_number=0;
238    
239 mocchiut 1.1 // NB: se ho discontinuita in genere non so a che download appartiene potrebbe proprio essere sbagliato tenere i vecchi valori
240    
241     }
242    
243     //starts a new root file
244     void PacketUser::StartGroup()
245     {
246 mocchiut 1.4 pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI
247 mocchiut 1.1 strcat(rootfilename,".root");
248     reader->Init(pRun);
249     stringstream oss;
250     oss.str()="";
251     oss<<"######################### Start a new group in file: "<< rootfilename <<" ############################";
252     string msg = oss.str();
253     mainLogUtil->logInfo(msg);
254     }
255    
256     //set the value of the counter and obt of the last packet of the group
257     void PacketUser::setLast(unsigned long int counter, unsigned long int obt)
258     {
259     pkt_number_last=counter;
260     obt_last=obt;
261     }
262    
263    
264    
265 mocchiut 1.2 //retrieve obt_time_sync and last_time_sync_info from packet if type has special values and BOOT_NUMBER
266 mocchiut 1.1 void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type)
267     {
268     //do nothing
269     if(!do_cont_check) {
270     if(is_new_route)
271     is_new_route=false;
272     return;
273     }
274    
275     //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0
276 mocchiut 1.2 //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi
277     // devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download
278     //When I found a new download I need to reset all values
279 mocchiut 1.1 if(is_new_route)
280 mocchiut 1.2 {
281     //reset
282 mocchiut 1.1 is_new_route=false;
283 mocchiut 1.3 //(tassa) ci ho ripensato se stiamo in un secondo download non prendiamo i bootnumber e timesync del precedente!
284     obt_time_sync_prevvalue=0;//obt_time_sync;
285     last_time_sync_info_prevvalue=0;//last_time_sync_info;
286    
287 mocchiut 1.1 obt_time_sync=0; last_time_sync_info=0;
288 mocchiut 1.3 boot_number_prevvalue=0;//boot_number;
289 mocchiut 1.1 boot_number=0;
290 mocchiut 1.3 time_is_estimated=false;
291 mocchiut 1.1 if(time_Offset)
292     timeOffset=time_Offset;
293     else
294     timeOffset=retrieveTimeOffset(Table_GL_RESURS_OFFSET);
295     }
296    
297     //retrieve boot number if possible
298     setBootNumber(packet,pktLength,type);
299    
300     //Retrieve if possible obt_time_sync and last_time_sync_info
301     int offset=0;
302     if(type==PacketType::RunHeader)
303     {
304     offset=0;
305     }
306     else if(type==PacketType::RunTrailer)
307     {
308     offset=3;
309     }
310     else if(type==PacketType::Mcmd)
311     {
312     // mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");
313     return;
314     }
315     else
316     {
317     return;
318     }
319    
320     //I don't need to retrieve them again if they are good
321     if((obt_time_sync)||(last_time_sync_info)) return;
322    
323     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);
324     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);
325    
326     stringstream oss;
327     oss.str()="";
328     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();
329     string msg = oss.str();
330     mainLogUtil->logAll(msg);
331     }
332    
333     //Boot Number
334     void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)
335     {
336 mocchiut 1.2 //se gia calcolato esco: lo calcolo una volta per download e per continuita'
337 mocchiut 1.1 if(boot_number) return;
338 mocchiut 1.2
339 mocchiut 1.1 if(type==PacketType::VarDump)
340 mocchiut 1.2 {
341     int b_offset = 34;//4+5*6
342     boot_number = (((UINT32)packet[1+b_offset]<<24)&0xFF000000) + (((UINT32)packet[2+b_offset]<<16)&0x00FF0000) + (((UINT32)packet[3+b_offset]<<8)&0x0000FF00) + (((UINT32)packet[4+b_offset])&0x000000FF);
343    
344     /*
345 mocchiut 1.1 long int dataLength = pktLength - 2; //the block of data
346 mocchiut 1.2 int b_offset = 4;
347 mocchiut 1.1 while (b_offset < dataLength){
348     boot_number = (((UINT32)packet[1+b_offset]<<24)&0xFF000000) + (((UINT32)packet[2+b_offset]<<16)&0x00FF0000) + (((UINT32)packet[3+b_offset]<<8)&0x0000FF00) + (((UINT32)packet[4+b_offset])&0x000000FF);
349     b_offset = b_offset + 5;
350     }
351 mocchiut 1.2 */
352 mocchiut 1.1 stringstream oss;
353     oss.str()="";
354     oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();
355     string msg = oss.str();
356     mainLogUtil->logAll(msg);
357     return;
358     }
359     else
360     {
361     return;
362     }
363    
364     }
365     //set the real time of the first packet and the last packet oif a group of packet if possible
366     void PacketUser::setReal_Time()
367     {
368 mocchiut 1.3
369     if(!obt_time_sync && !last_time_sync_info && numPKTSaved > 1000 )
370 mocchiut 1.2 {
371 mocchiut 1.3 time_is_estimated=true;
372     obt_time_sync=obt_time_sync_prevvalue;
373     last_time_sync_info=last_time_sync_info_prevvalue;
374     }
375     if(obt_time_sync || last_time_sync_info)
376     {
377     real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
378 mocchiut 1.2 //se obt si e' resettato
379     if(obt_last<obt_init)
380     {
381     real_time_last=(max_pkt_obt/1000+ obt_last/1000-obt_time_sync)+last_time_sync_info;
382     stringstream oss;
383     oss.str()="";
384     oss<<"obt_last("<<obt_last<<") < obt_init("<<obt_init<<"). It is due to Pamela Reset. Adding max_pkt_obt("<<max_pkt_obt<<") to obt_last in order to compute real_time_last";
385     string msg = oss.str();
386     mainLogUtil->logWarning(msg);
387     }
388     else
389     real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
390 mocchiut 1.1 real_time_init+=timeOffset;
391 mocchiut 1.2 real_time_last+=timeOffset;
392     }
393 mocchiut 1.1 }
394    
395     //timeOffset
396     //retrieve from table Table_GL_RESURS_OFFSET timeOffset
397     unsigned long int PacketUser::retrieveTimeOffset(char * table)
398     {
399     if(!do_cont_check) return 0;
400    
401     //if(!table){table=Table_GL_RESURS_OFFSET}
402    
403     if(!single_connection){
404     OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte
405     }
406    
407 mocchiut 1.2 UInt_t t0 = 0;//toffset
408 mocchiut 1.1 stringstream oss;
409     oss.str("");
410     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<< table <<" WHERE SPECIAL_FILE='"
411     << nnnn_mmm_ppp << "';";
412    
413     string msg = oss.str();
414     mainLogUtil->logAll(msg);
415    
416     TSQLResult* res=NULL;
417     TSQLRow* row=NULL;
418     res= sqlServer->Query(oss.str().c_str());
419     if(!res)
420     {
421     mainLogUtil->logError("DBError retrieveTimeOffset 1");
422     return 0;
423     }
424    
425     row=res->Next();
426     if (!row )
427     {
428     if(res){delete res; res = NULL;}
429    
430     oss.str("");
431     oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<< table <<" WHERE FROM_ORBIT < "
432     << orbit_number << " order by FROM_ORBIT desc limit 1;";
433    
434     string msg2 = oss.str();
435     mainLogUtil->logAll(msg2);
436    
437     res= sqlServer->Query(oss.str().c_str());
438     if(!res)
439     {
440     mainLogUtil->logError("DBError retrieveTimeOffset 2");
441     return 0;
442     }
443     row=res->Next();
444     if (!row)
445     {
446     mainLogUtil->logError("DBError retrieveTimeOffset 3");
447     return 0;
448     }
449    
450     }
451    
452     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);
453     t0 = (UInt_t)tu.GetSec();
454    
455     if(res){delete res; res = NULL;}
456     if(row){delete row; row = NULL;}
457    
458     if(!single_connection){
459     CloseDBConnection();
460     }
461     return t0;
462     }
463    
464     //this is public and can be called from the main at the end of the game
465     void PacketUser::FinishLastGroup()
466     {
467     mainLogUtil->logAll("######################### Closing the last group ############################");
468     setReal_Time();
469     FinishGroup(rootfilename);
470     mainLogUtil->logAll("######################### Closed the last group ############################");
471     if(single_connection){
472     CloseDBConnection();
473     }
474     }
475    
476     //Finish the old root file
477     void PacketUser::FinishGroup(char * filename)
478     {
479     if(pRun)
480     {
481     pRun->WriteFiles();
482     delete pRun; pRun = NULL;
483     stringstream close;
484     close.str()="";
485     string msg;
486    
487     close<<"######################### Close group in file: "<< rootfilename <<" ############################";
488     msg = close.str();
489     mainLogUtil->logInfo(msg);
490    
491     // se non faccio proprio controlli su continuita' neanche salvo su DB
492     if(!do_cont_check) return;
493    
494     stringstream oss;
495     oss.str()="";
496    
497     //TODO: check when I don't want to log in DB informations
498     // se non ho salvato nessun pacchetto
499     if(numPKTSaved==0){
500     stringstream err;
501     err.str()="";
502     err<<"NO PACKET SAVED in file: "<< rootfilename <<" The group is empty: you can remove this file.";
503     msg = err.str();
504     mainLogUtil->logError(msg);
505     return;
506     }
507    
508     //se non ho salvato nessun pacchetto buono
509     if((numPKTSaved-bad_pkt_EventReader-bad_pkt_CalibReader-bad_pkt)==0){
510     stringstream err;
511     err.str()="";
512     err<<"NO GOOD PACKET SAVED in file: "<< rootfilename <<" The group is BAD: you may want to remove this file.";
513     msg = err.str();
514     mainLogUtil->logInfo(msg);
515     // mainLogUtil->logError(msg);
516     // return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???
517     }
518    
519     if(!single_connection){
520     OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
521     }
522    
523     //don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range
524     if((!real_time_init)&&(!real_time_last))
525     {
526     //saved in another table for future study
527 mocchiut 1.4 if(saveROOT_DB(Table_ROOT_Bad, outDir,// here outDir is good, no expand EMI
528 mocchiut 1.1 filename,
529     pkt_number_init, pkt_number_last,
530     obt_init, obt_last,
531     obt_time_sync, last_time_sync_info,
532     real_time_init, real_time_last,
533     boot_number,
534     timeOffset,
535     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
536 mocchiut 1.3 fni,time_is_estimated)==true)
537 mocchiut 1.1 {
538     oss.str()="";
539     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;
540     msg = oss.str();
541     mainLogUtil->logInfo(msg);
542     }
543     else
544     {
545     oss.str()="";
546     oss<<"Problem storing information in DB regarding file: "<<filename;
547     msg = oss.str();
548     mainLogUtil->logError(msg);
549     }
550 mocchiut 1.2
551 mocchiut 1.1 if(!single_connection){
552     CloseDBConnection();
553     }
554    
555     return;
556     }
557    
558     //saves info in DB in table Table_ROOT_Good
559 mocchiut 1.3 if(boot_number==0)
560     boot_number=boot_number_prevvalue;
561    
562 mocchiut 1.4 if(saveROOT_DB(Table_ROOT_Good, outDir, // here outDir is good, no expand EMI
563 mocchiut 1.1 filename,
564     pkt_number_init, pkt_number_last,
565     obt_init, obt_last,
566     obt_time_sync, last_time_sync_info,
567     real_time_init, real_time_last,
568     boot_number,
569     timeOffset,
570     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
571 mocchiut 1.3 fni,time_is_estimated)==true)
572 mocchiut 1.1 {
573     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good<< " id= "<<my_id;
574     msg = oss.str();
575     mainLogUtil->logInfo(msg);
576    
577     if(tryMerge){
578     merge_ROOTfiles();
579     }
580     }
581     else
582     {
583     oss<<"Problem storing information in DB regarding file: "<<filename;
584     msg = oss.str();
585     mainLogUtil->logError(msg);
586     }
587    
588     if(!single_connection){
589     CloseDBConnection();
590     }
591    
592     }//pRun
593     }
594    
595     //save in Table_ROOT_Good or in Table_ROOT_Bad
596     bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,
597     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
598     unsigned long int obt_in, unsigned long int obt_fin,
599     unsigned long int oT_sync, unsigned long int lT_sync_info,
600     unsigned long int mtime_init, unsigned long int mtime_last,
601     unsigned long int mboot_num,
602     unsigned long int time_offset,
603     int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,
604 mocchiut 1.3 char* nome_input, bool _time_is_estimated){
605 mocchiut 1.1 //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
606     stringstream oss;
607     oss.str("");
608 mocchiut 1.3 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, BOOT_NUMBER, TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME,TIME_IS_ESTIMATED)"
609 mocchiut 1.1 << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
610     << oT_sync << "','" << lT_sync_info << "','"
611     << mtime_init << "','" << mtime_last << "','"
612     << mboot_num << "','"
613     << time_offset << "','"
614 mocchiut 1.2 << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< num_PKT_Saved<<"','"
615 mocchiut 1.1 << nome_input <<"',"
616 mocchiut 1.3 <<"NULL,"<<_time_is_estimated<<
617 mocchiut 1.5 ");";
618 mocchiut 1.1 string msg = oss.str();
619     mainLogUtil->logAll(msg);
620    
621     stringstream oss1;
622     oss1.str()="";
623     string msg1;
624 mocchiut 1.3 string query;
625 mocchiut 1.1
626 mocchiut 1.3 TSQLResult* res=NULL;
627    
628     query=oss.str();
629     msg1="SaveROOT_DB query: ";
630     msg1 += query;
631     mainLogUtil->logInfo(msg1);
632    
633     res= sqlServer->Query(query.c_str());
634 mocchiut 1.1 if(!res)
635     {
636     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
637     msg1=oss1.str();
638     mainLogUtil->logError(msg1);
639     return false;
640     }
641    
642     //the ID of the current ROOT file in table Table_ROOT_Good
643     my_id = select_maxIDN_DB(table_name);
644 mocchiut 1.3 if(!boot_number){
645     if(id_to_recover_index<1000)
646     id_to_recover[id_to_recover_index++]=my_id;
647     }else if (boot_number && id_to_recover_index && !is_new_route){
648     recover_boot_number();
649     }
650 mocchiut 1.1 // cout<<"DBG: my_id = "<<my_id<<endl;
651     if(res){delete res; res = NULL;}
652     return true;
653     }
654    
655 mocchiut 1.3
656     void PacketUser::recover_boot_number(){
657     string msg1;
658     stringstream oss1;
659     stringstream oss;
660     oss.str("");
661     string query="";
662     TSQLResult* res=NULL;
663     if(!boot_number || !id_to_recover_index)
664     return;
665    
666     for (int i = 0; i<id_to_recover_index;i++){
667 mocchiut 1.5 oss.str("");
668     oss << "UPDATE "<< Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i] << ";";
669 mocchiut 1.3 query = oss.str();
670     res= sqlServer->Query(query.c_str());
671     if(!res)
672     {
673     oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;
674     msg1=oss1.str();
675     mainLogUtil->logError(msg1);
676     return ;
677     }
678     }
679    
680 mocchiut 1.5 for (int i = 0; i<id_to_recover_index;i++){
681     oss.str("");
682     oss << "UPDATE " << Table_ROOT_Merging << " SET BOOT_NUMBER=" << boot_number << " WHERE ROOT_ID_N=" << id_to_recover[i] << ";";
683     query=oss.str();
684     res= sqlServer->Query(query.c_str());
685     if(!res)
686     {
687 mocchiut 1.3 oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;
688     msg1=oss1.str();
689     mainLogUtil->logError(msg1);
690     return ;
691     }
692     }
693    
694     id_to_recover_index=0;
695     }
696    
697    
698    
699 mocchiut 1.1 /**********************************************************************************************/
700     /*###########################################################################################################*/
701     /********************************** MERGING *************************************************/
702     /*###########################################################################################################*/
703 mocchiut 1.3 // Merge ROT files: find other ROOT files in the same temporal ranges
704 mocchiut 1.1 // it finds ROOT files that covers particular temporal range of interest and save this info in DB
705     /**********************************************************************************************/
706     //try to merge current ROOT file with files in DB
707     bool PacketUser::merge_ROOTfiles()
708     {
709 mocchiut 1.2 stringstream log;
710     log.str("");
711     string slog;
712 mocchiut 1.1
713 mocchiut 1.2 mainLogUtil->logInfo("Trying merging ROOT files");
714 mocchiut 1.1 double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;
715    
716     TSQLResult* res=NULL;
717     TSQLRow* row =NULL;
718 mocchiut 1.2 //numero di ROOT files trovati sul DB che hanno relaz di tipo dato
719     unsigned int num_rows=0;
720    
721     //Record su DB:
722     unsigned int idN = 0;
723     unsigned int root_id_db=0;
724     unsigned long int pkt_num_in_db=0;
725     unsigned long int pkt_num_fin_db=0;
726     unsigned long int pkt_obt_in_db=0;
727     unsigned long int pkt_obt_fin_db=0;
728     unsigned long int time_in_db=0;
729     unsigned long int time_fin_db=0;
730     unsigned int boot_num_db=0;
731     double perc=0;
732 mocchiut 1.1
733 mocchiut 1.2
734     //marco_new: non considero mai caso di continuita'(mio finale=DB iniz o viceversa), non mi interessa
735 mocchiut 1.1 //marco_new: after e before contengono anche smaller ma con un bordo comune
736     for(int relaz=AFTER; relaz<=BIGGER; relaz++)
737 mocchiut 1.2 {
738 mocchiut 1.1 //marco_new: esci se sono arrivato a zero
739 mocchiut 1.2 if(real_time_last==real_time_init)
740     {
741     log.str("");
742     log<<"Current File Lenght=0; real_time_last=real_time_init= "<<real_time_last;
743     slog=log.str();
744     mainLogUtil->logAll(slog);
745 mocchiut 1.1 mainLogUtil->logInfo("Finish merging ROOT files");
746     if(res){delete res; res = NULL;}
747     if(row){delete row; row = NULL;}
748     return true;
749     }
750    
751 mocchiut 1.2 num_rows=0;
752     res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);
753 mocchiut 1.1
754     if (!res)
755     {
756     mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");
757     return false;
758     }
759    
760     num_rows=res->GetRowCount();
761 mocchiut 1.2 log.str("");
762     log<<"Found "<<num_rows<<" ROOT file in DB with relation of kind = "<<relaz;
763     slog=log.str();
764     mainLogUtil->logAll(slog);
765 mocchiut 1.1
766     if(num_rows>0)
767 mocchiut 1.2 {
768     for(unsigned int i=0; i<num_rows; i++)
769 mocchiut 1.1 {
770     row=res->Next();
771     if (!row)
772     {
773 mocchiut 1.2 mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");
774 mocchiut 1.1 if(res){delete res; res = NULL;}
775     return false;
776     }
777    
778 mocchiut 1.2 //Leggi Record dal DB:
779     idN=atoll(row->GetField(0));
780     root_id_db=atoll(row->GetField(1));
781     pkt_num_in_db=atoll(row->GetField(2));
782     pkt_num_fin_db=atoll(row->GetField(3));
783     pkt_obt_in_db=atoll(row->GetField(4));
784     pkt_obt_fin_db=atoll(row->GetField(5));
785     time_in_db=atoll(row->GetField(6));
786     time_fin_db=atoll(row->GetField(7));
787     boot_num_db=atoll(row->GetField(8));
788 mocchiut 1.1 perc=atof(row->GetField(9));
789 mocchiut 1.2
790     log.str("");
791     log<<"*** ROOT file found in DB has idN= "<<idN<<" root_id_db= "<<root_id_db;
792     log<<" pkt_num_in_db= "<<pkt_num_in_db<<" pkt_num_fin_db= "<<pkt_num_fin_db<<" pkt_obt_in_db= "<<pkt_obt_in_db<<" pkt_obt_fin_db= "<<pkt_obt_fin_db;
793     log<<" time_in_db= "<<time_in_db<<" time_fin_db= "<<time_fin_db<<" boot_num_db= "<<boot_num_db<<" perc= "<<perc<<" ***";
794     log<<"\n*** Current ROOT file has pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
795     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
796     slog=log.str();
797     mainLogUtil->logAll(slog);
798     if(perc>percentage)
799     {
800 mocchiut 1.1 mainLogUtil->logInfo("Current ROOT file is better than the one in DB");
801     //modifica DB
802     updateMergeROOT_DB(Table_ROOT_Merging,
803 mocchiut 1.2 root_id_db,
804     pkt_num_in_db, pkt_num_fin_db,
805     pkt_obt_in_db, pkt_obt_fin_db,
806     time_in_db, time_fin_db,
807     boot_num_db,
808     perc,
809     idN,(type_Rel_ROOT)relaz);
810 mocchiut 1.1 }
811 mocchiut 1.2 else
812     {
813     mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file");
814     if(relaz == AFTER)
815     {
816 mocchiut 1.1 mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
817 mocchiut 1.3 pkt_number_init = (pkt_num_fin_db+1);
818 mocchiut 1.2 obt_init = pkt_obt_fin_db;
819     real_time_init = time_fin_db;
820     log.str("");
821     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
822     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
823     slog=log.str();
824     mainLogUtil->logAll(slog);
825 mocchiut 1.1 }
826 mocchiut 1.2 else if(relaz == BEFORE)
827     {
828     mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");
829 mocchiut 1.3 pkt_number_last = (pkt_num_in_db-1);
830 mocchiut 1.2 obt_last = pkt_obt_in_db;
831     real_time_last = time_in_db;
832    
833     log.str("");
834     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
835     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
836     slog=log.str();
837     mainLogUtil->logAll(slog);
838 mocchiut 1.1 }
839     else if(relaz == SMALLER)
840     {
841     //non devo proprio salvarlo ed esco ...
842     mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
843 mocchiut 1.2 mainLogUtil->logInfo("\nFinish merging ROOT files");
844 mocchiut 1.1 if(res){delete res; res = NULL;}
845 mocchiut 1.2 if(row){delete row; row = NULL;}
846 mocchiut 1.1 return true;
847     }
848     else if(relaz == BIGGER)//spezzettamento
849     {
850     mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");
851     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
852 mocchiut 1.3 pkt_number_init, (pkt_num_in_db-1),
853 mocchiut 1.2 obt_init, pkt_obt_in_db,
854     real_time_init, time_in_db,
855 mocchiut 1.1 boot_number,
856     percentage);
857 mocchiut 1.2
858 mocchiut 1.1 mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");
859 mocchiut 1.3 pkt_number_init = (pkt_num_fin_db+1);
860 mocchiut 1.2 obt_init= pkt_obt_fin_db;
861     real_time_init= time_fin_db;
862    
863     log.str("");
864     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
865     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
866     slog=log.str();
867     mainLogUtil->logAll(slog);
868     }//if relaz
869     }//if perc
870 mocchiut 1.1 }//for num_rows
871 mocchiut 1.2 }//if num_rows
872 mocchiut 1.1
873     if(res){delete res; res = NULL;}
874     if(row){delete row; row = NULL;}
875 mocchiut 1.2
876     }//for relaz
877 mocchiut 1.1
878     mainLogUtil->logInfo("Saving Current ROOT file");
879     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
880     pkt_number_init, pkt_number_last,
881     obt_init, obt_last,
882     real_time_init, real_time_last,
883     boot_number,
884     percentage);
885    
886     mainLogUtil->logInfo("Finish merging ROOT files");
887     return true;
888 mocchiut 1.2
889 mocchiut 1.1 }
890    
891     //unsigned int root_id, percentage non le devo modificare
892     bool PacketUser::updateMergeROOT_DB(char* table_name,
893     unsigned int root_id,
894     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
895     unsigned long int obt_in, unsigned long int obt_fin,
896     unsigned long int mtime_init, unsigned long int mtime_last,
897     unsigned long int mboot_num,
898     double bad_perc,
899     unsigned int ID_record, type_Rel_ROOT type_rel){
900    
901 mocchiut 1.2 stringstream oss;
902     oss.str("");
903    
904 mocchiut 1.1 if(type_rel == AFTER){
905     mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
906     //marco_new:tolti tutti gli apici che qui non devono essere
907     oss << "UPDATE "<< table_name
908 mocchiut 1.3 << " SET PKT_NUMBER_FINAL ="<< (pkt_number_init - 1)
909 mocchiut 1.1 <<", PKT_OBT_FINAL ="<< obt_init
910     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";
911 mocchiut 1.2
912 mocchiut 1.1 }
913     else if(type_rel == BEFORE){
914 mocchiut 1.2 mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");
915 mocchiut 1.1 oss << "UPDATE "<< table_name
916 mocchiut 1.3 << " SET PKT_NUMBER_INIT ="<< (pkt_number_last+1)
917 mocchiut 1.1 <<", PKT_OBT_INIT ="<< obt_last
918     <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";
919     }
920     else if(type_rel == SMALLER){
921     //spezzettamentento
922     mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");
923    
924     oss << "UPDATE "<< table_name
925 mocchiut 1.3 << " SET PKT_NUMBER_FINAL="<< (pkt_number_init-1)
926 mocchiut 1.1 <<", PKT_OBT_FINAL ="<< obt_init
927     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record<<";";
928    
929 mocchiut 1.2 mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");
930    
931     saveMergeROOT_DB(table_name, root_id,
932 mocchiut 1.3 (pkt_number_last+1), pkt_number_fin,
933 mocchiut 1.2 obt_last, obt_fin,
934     real_time_last, mtime_last,
935     mboot_num,
936     bad_perc);
937 mocchiut 1.1 }
938     else if(type_rel == BIGGER){
939     //marco_new: ok anche se coincidenti etc
940 mocchiut 1.4 // oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO
941     oss << "UPDATE "<< table_name <<" SET GOOD=0 WHERE ID_N ="<< ID_record<<";"; // EMILIANO DO NOT DELETE FROM ROOT_TABLE_MERGING JUST SET GOOD FLAG TO ZERO
942 mocchiut 1.2 mainLogUtil->logInfo("Record deleted from merging table");
943 mocchiut 1.1 }
944     else
945     return false;
946    
947     string msg = oss.str();
948     mainLogUtil->logInfo(msg);
949    
950     stringstream oss1;
951     oss1.str()="";
952     string msg1;
953    
954     TSQLResult* res=NULL;
955     res= sqlServer->Query(oss.str().c_str());
956     if(!res)
957     {
958     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
959     msg1=oss1.str();
960     mainLogUtil->logError(msg1);
961     return false;
962     }
963     if(res){delete res; res = NULL;}
964     return true;
965     }
966    
967 mocchiut 1.2 //cerca eventuali ROOT files in DB che possono venire mergiati
968     //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
969     //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
970     //marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx
971 mocchiut 1.1 TSQLResult* PacketUser::Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel)
972     {
973     TSQLResult* res=NULL;
974     stringstream query;
975     query.str("");
976    
977     if(type_rel == AFTER)
978 mocchiut 1.4 query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_LAST > "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last <<" and REAL_TIME_INIT < "<< mtime_init; // EMI
979 mocchiut 1.1 else if(type_rel == BEFORE)
980 mocchiut 1.4 query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_INIT < "<< mtime_last <<" and REAL_TIME_LAST > "<< mtime_last; // EMI
981 mocchiut 1.1 else if(type_rel == SMALLER)
982 mocchiut 1.4 query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT < "<< mtime_init <<" and REAL_TIME_LAST > "<< mtime_last; // EMI
983 mocchiut 1.1 else if(type_rel == BIGGER)
984 mocchiut 1.4 query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last; // EMI
985 mocchiut 1.1 else
986     return 0;
987    
988     query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile
989 mocchiut 1.2 //se volessi mettere un filtro sulla qualita'
990 mocchiut 1.1 // query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
991     string msg = query.str();
992     mainLogUtil->logAll(msg);
993 mocchiut 1.2 res= sqlServer->Query(query.str().c_str());
994 mocchiut 1.1 stringstream oss1;
995 mocchiut 1.2 oss1.str()="";
996     string msg1;
997 mocchiut 1.1
998     if (!res)
999     {
1000     oss1<<"DBError UNABLE to: "<<query.str().c_str()<<endl;
1001     msg1=oss1.str();
1002     mainLogUtil->logError(msg1);
1003     }
1004    
1005     return res;
1006     }
1007    
1008     bool PacketUser::saveMergeROOT_DB(char* table_name, unsigned int root_id,
1009     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
1010     unsigned long int obt_in, unsigned long int obt_fin,
1011     unsigned long int mtime_init, unsigned long int mtime_last,
1012     unsigned long int mboot_num,
1013     double percentage)
1014     {
1015    
1016     stringstream oss;
1017     oss.str("");
1018 mocchiut 1.3 oss << "INSERT INTO "<< table_name <<" (ID_N, ROOT_ID_N, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, REAL_TIME_INIT, REAL_TIME_LAST, BOOT_NUMBER, BAD_PKT_PERCENTAGE, INSERT_TIME, INSERTED_BY)"
1019 mocchiut 1.1 << " VALUES ('"<<0<< "','" <<root_id<<"','"<< pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
1020     << mtime_init << "','" << mtime_last << "','"
1021     << mboot_num <<"','"
1022     << percentage <<"',"
1023 mocchiut 1.3 <<"NULL"<<",'"
1024     << tag_value<<"'"
1025     << ")";
1026 mocchiut 1.1
1027     string msg = oss.str();
1028     mainLogUtil->logAll(msg);
1029 mocchiut 1.2
1030 mocchiut 1.1 //marco_new: controlla
1031     //marco_new: esci se sono arrivato a zero
1032     // if(real_time_last==real_time_init){
1033     if(mtime_init==mtime_last){
1034     mainLogUtil->logAll("Nothing to be saved");
1035    
1036     // if(res){delete res; res = NULL;}
1037     // if(row){delete row; row = NULL;}
1038     return true;
1039     }
1040    
1041     stringstream oss1;
1042     oss1.str()="";
1043     string msg1;
1044    
1045     TSQLResult* res=NULL;
1046     res= sqlServer->Query(oss.str().c_str());
1047     if(!res)
1048     {
1049     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1050     msg1=oss1.str();
1051     mainLogUtil->logError(msg1);
1052     return false;
1053     }
1054     if(res){delete res; res = NULL;}
1055     return true;
1056     }
1057    
1058     //Utility: return max(IDN)
1059     unsigned int PacketUser::select_maxIDN_DB(char* table_name)
1060     {
1061    
1062     if (!strcmp(table_name,Table_ROOT_Bad))
1063     {
1064     return 0;
1065     }
1066     unsigned int idN=0;
1067     TSQLResult* res=NULL;
1068     stringstream oss;
1069     oss.str("");
1070     oss<<"select max(ID_N) from "<<table_name;
1071 mocchiut 1.2 // string msg = oss.str();
1072     // mainLogUtil->logAll(msg);
1073 mocchiut 1.1 res= sqlServer->Query(oss.str().c_str());
1074    
1075     stringstream oss1;
1076     oss1.str()="";
1077     string msg1;
1078    
1079     if(!res)
1080     {
1081     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1082     msg1=oss1.str();
1083     mainLogUtil->logError(msg1);
1084     return 0;
1085     }
1086    
1087     TSQLRow* row=NULL;
1088     row=res->Next();
1089     if (!row )
1090     {
1091     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1092     msg1=oss1.str();
1093     mainLogUtil->logError(msg1);
1094     return 0;
1095     }
1096    
1097     idN=atoi(row->GetField(0));
1098     if(row){delete row; row = NULL;}
1099     if(res){delete res; res = NULL;}
1100 mocchiut 1.2
1101     oss<<". idN = "<<idN;
1102     string msg = oss.str();
1103     mainLogUtil->logAll(msg);
1104    
1105 mocchiut 1.1 return idN;
1106     }
1107    
1108     /**
1109     * Lock par=table, if par=null lock all tables
1110     */
1111     int PacketUser::LockTables(char* tableTobeLocked)
1112     {
1113     //se non ho il DB :)
1114     if(!do_cont_check) return 999;
1115    
1116     TSQLResult* res=NULL;
1117     stringstream oss;
1118     oss.str("");
1119     if(!tableTobeLocked)
1120     oss << "lock table "<<Table_ROOT_Good<<" write, "<<Table_ROOT_Bad<<" write, "<<Table_GL_RESURS_OFFSET<<" write, "<<Table_ROOT_Merging<<" write;";
1121     else
1122     oss << "lock table "<<tableTobeLocked<<" write; ";
1123    
1124     string msg = oss.str();
1125     mainLogUtil->logAll(msg);
1126    
1127     res= sqlServer->Query(oss.str().c_str());
1128    
1129     stringstream oss1;
1130     oss1.str()="";
1131     string msg1;
1132    
1133     if(!res)
1134     {
1135     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1136     msg1=oss1.str();
1137     mainLogUtil->logError(msg1);
1138     return 1;
1139     }
1140    
1141     if(res){delete res; res = NULL;}
1142 mocchiut 1.2 mainLogUtil->logAll("TABLES LOCKED");
1143 mocchiut 1.1 return 0;
1144     }
1145    
1146     /**
1147     * UNLock tables
1148     */
1149     int PacketUser::UnLockTables(){
1150     //se non ho il DB :)
1151     if(!do_cont_check) return 999;
1152    
1153     TSQLResult* res=NULL;
1154     stringstream oss;
1155     oss.str("");
1156     oss << "unlock tables;";
1157    
1158     string msg = oss.str();
1159     mainLogUtil->logAll(msg);
1160    
1161     res= sqlServer->Query(oss.str().c_str());
1162    
1163     stringstream oss1;
1164     oss1.str()="";
1165     string msg1;
1166    
1167     if(!res)
1168     {
1169     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1170     msg1=oss1.str();
1171     mainLogUtil->logError(msg1);
1172     return 1;
1173     }
1174    
1175     if(res){delete res; res = NULL;}
1176 mocchiut 1.2 mainLogUtil->logAll("TABLES UNLOCKED");
1177 mocchiut 1.1
1178     return 0;
1179     }
1180    
1181     /*********************************************************/
1182     //marco_new_31
1183     void PacketUser::OpenDBConnection(char* tableTobeLocked){
1184     if(do_cont_check){
1185 mocchiut 1.2 //controllo che non sia gia' aperta
1186 mocchiut 1.1 if(!sqlServer){
1187     sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
1188     if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}
1189 mocchiut 1.2 mainLogUtil->logAll("Connected to DB");
1190     //blocca le tabelle
1191     LockTables(tableTobeLocked);
1192 mocchiut 1.1 }
1193     }
1194    
1195     }
1196    
1197     //marco_new_31
1198     void PacketUser::CloseDBConnection(){
1199     if(do_cont_check){
1200     //controllo che ce ne sia una aperta
1201     if(sqlServer && sqlServer->IsConnected()){
1202     //marco_new: sblocca tutte le tabelle
1203     UnLockTables();
1204     sqlServer->Close();
1205 mocchiut 1.2 delete sqlServer;
1206 mocchiut 1.1 sqlServer=NULL;
1207 mocchiut 1.2 mainLogUtil->logAll("Closed Connection to DB");
1208 mocchiut 1.1 }
1209     }
1210     }
1211    
1212    
1213    
1214     /***********************************************************************************************************************/
1215     /*#################################################### HIC SUNT LEONES ################################################*/
1216     /***********************************************************************************************************************/
1217     //UNUSED: dbg functions...
1218    
1219     //Return the system time in ms
1220     unsigned long long PacketUser::Record_Time(){
1221     timeval tv;
1222     gettimeofday(&tv,NULL);
1223     unsigned long long timems = (unsigned long long)tv.tv_sec * (unsigned long long)1000 + tv.tv_usec / 1000;
1224     return timems;
1225     }
1226    
1227     //dbg_functions:
1228    
1229     //UNUSED. save a packet in a file
1230     void PacketUser::savePKT_file(char* headerPkt,
1231     char* pamPkt,
1232     long int length,
1233     bool append,
1234     char* nomefile)
1235     {
1236     char fno[80]="";
1237     ofstream fout;
1238 mocchiut 1.4 sprintf(fno,"%s/pkt%d_of_%s.pkt", gSystem->ExpandPathName(outDir), numPKTSaved, nomefile); // EMI
1239 mocchiut 1.1
1240     if(append==true)
1241     fout.open(fno, ios::binary | ios::app);
1242     else
1243     fout.open(fno, ios::binary);
1244    
1245     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1246     fout.write(headerPkt,LENGTH_HEADER_PKT);
1247     fout.write(pamPkt,length);
1248     fout.close();
1249     }
1250    
1251     //UNUSED
1252     void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append)
1253     {
1254     char fno[80]="";
1255     ofstream fout;
1256     if(append==true)
1257     {
1258 mocchiut 1.4 sprintf(fno,"%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI
1259 mocchiut 1.1 fout.open(fno, ios::binary | ios::app);
1260     }
1261     else{
1262 mocchiut 1.4 sprintf(fno,"%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT ); // EMI
1263 mocchiut 1.1 fout.open(fno, ios::binary);
1264     }
1265    
1266     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1267     fout.write(headerPkt,LENGTH_HEADER_PKT);
1268     fout.write(pamPkt,length);
1269     fout.close();
1270     }
1271    
1272    
1273     }

  ViewVC Help
Powered by ViewVC 1.1.23