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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (hide annotations) (download)
Fri Jul 24 13:53:38 2009 UTC (15 years, 4 months ago) by mocchiut
Branch: MAIN
Changes since 1.6: +6 -4 lines
Runtime error with ROOT 5.24 fixed, many small changes and warnings fixing

1 mocchiut 1.1 //============================================================================
2 mocchiut 1.7 // $Id: PacketUser.cpp,v 1.6 2008/12/23 20:43:08 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 mocchiut 1.7 // id_to_recover[1000]; // Emiliano: che si vuole fare con questa riga? cosi` non fa nulla...
91 mocchiut 1.3 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 mocchiut 1.7 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();
329     // 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();
330 mocchiut 1.1 string msg = oss.str();
331     mainLogUtil->logAll(msg);
332     }
333    
334     //Boot Number
335     void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)
336     {
337 mocchiut 1.2 //se gia calcolato esco: lo calcolo una volta per download e per continuita'
338 mocchiut 1.1 if(boot_number) return;
339 mocchiut 1.2
340 mocchiut 1.1 if(type==PacketType::VarDump)
341 mocchiut 1.2 {
342     int b_offset = 34;//4+5*6
343     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);
344    
345     /*
346 mocchiut 1.1 long int dataLength = pktLength - 2; //the block of data
347 mocchiut 1.2 int b_offset = 4;
348 mocchiut 1.1 while (b_offset < dataLength){
349     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);
350     b_offset = b_offset + 5;
351     }
352 mocchiut 1.2 */
353 mocchiut 1.1 stringstream oss;
354     oss.str()="";
355 mocchiut 1.7 // oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();
356     oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName();
357 mocchiut 1.1 string msg = oss.str();
358     mainLogUtil->logAll(msg);
359     return;
360     }
361     else
362     {
363     return;
364     }
365    
366     }
367     //set the real time of the first packet and the last packet oif a group of packet if possible
368     void PacketUser::setReal_Time()
369     {
370 mocchiut 1.3
371     if(!obt_time_sync && !last_time_sync_info && numPKTSaved > 1000 )
372 mocchiut 1.2 {
373 mocchiut 1.3 time_is_estimated=true;
374     obt_time_sync=obt_time_sync_prevvalue;
375     last_time_sync_info=last_time_sync_info_prevvalue;
376     }
377     if(obt_time_sync || last_time_sync_info)
378     {
379     real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
380 mocchiut 1.2 //se obt si e' resettato
381     if(obt_last<obt_init)
382     {
383     real_time_last=(max_pkt_obt/1000+ obt_last/1000-obt_time_sync)+last_time_sync_info;
384     stringstream oss;
385     oss.str()="";
386     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";
387     string msg = oss.str();
388     mainLogUtil->logWarning(msg);
389     }
390     else
391     real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
392 mocchiut 1.1 real_time_init+=timeOffset;
393 mocchiut 1.2 real_time_last+=timeOffset;
394     }
395 mocchiut 1.1 }
396    
397     //timeOffset
398     //retrieve from table Table_GL_RESURS_OFFSET timeOffset
399     unsigned long int PacketUser::retrieveTimeOffset(char * table)
400     {
401     if(!do_cont_check) return 0;
402    
403     //if(!table){table=Table_GL_RESURS_OFFSET}
404    
405     if(!single_connection){
406     OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte
407     }
408    
409 mocchiut 1.2 UInt_t t0 = 0;//toffset
410 mocchiut 1.1 stringstream oss;
411     oss.str("");
412     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='"
413     << nnnn_mmm_ppp << "';";
414    
415     string msg = oss.str();
416     mainLogUtil->logAll(msg);
417    
418     TSQLResult* res=NULL;
419     TSQLRow* row=NULL;
420     res= sqlServer->Query(oss.str().c_str());
421     if(!res)
422     {
423     mainLogUtil->logError("DBError retrieveTimeOffset 1");
424     return 0;
425     }
426    
427     row=res->Next();
428     if (!row )
429     {
430     if(res){delete res; res = NULL;}
431    
432     oss.str("");
433     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 < "
434 mocchiut 1.6 << orbit_number << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
435 mocchiut 1.1
436     string msg2 = oss.str();
437     mainLogUtil->logAll(msg2);
438    
439     res= sqlServer->Query(oss.str().c_str());
440     if(!res)
441     {
442     mainLogUtil->logError("DBError retrieveTimeOffset 2");
443     return 0;
444     }
445     row=res->Next();
446     if (!row)
447     {
448     mainLogUtil->logError("DBError retrieveTimeOffset 3");
449     return 0;
450     }
451    
452     }
453    
454     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);
455     t0 = (UInt_t)tu.GetSec();
456    
457     if(res){delete res; res = NULL;}
458     if(row){delete row; row = NULL;}
459    
460     if(!single_connection){
461     CloseDBConnection();
462     }
463     return t0;
464     }
465    
466     //this is public and can be called from the main at the end of the game
467     void PacketUser::FinishLastGroup()
468     {
469     mainLogUtil->logAll("######################### Closing the last group ############################");
470     setReal_Time();
471     FinishGroup(rootfilename);
472     mainLogUtil->logAll("######################### Closed the last group ############################");
473     if(single_connection){
474     CloseDBConnection();
475     }
476     }
477    
478     //Finish the old root file
479     void PacketUser::FinishGroup(char * filename)
480     {
481     if(pRun)
482     {
483     pRun->WriteFiles();
484     delete pRun; pRun = NULL;
485     stringstream close;
486     close.str()="";
487     string msg;
488    
489     close<<"######################### Close group in file: "<< rootfilename <<" ############################";
490     msg = close.str();
491     mainLogUtil->logInfo(msg);
492    
493     // se non faccio proprio controlli su continuita' neanche salvo su DB
494     if(!do_cont_check) return;
495    
496     stringstream oss;
497     oss.str()="";
498    
499     //TODO: check when I don't want to log in DB informations
500     // se non ho salvato nessun pacchetto
501     if(numPKTSaved==0){
502     stringstream err;
503     err.str()="";
504     err<<"NO PACKET SAVED in file: "<< rootfilename <<" The group is empty: you can remove this file.";
505     msg = err.str();
506     mainLogUtil->logError(msg);
507     return;
508     }
509    
510     //se non ho salvato nessun pacchetto buono
511     if((numPKTSaved-bad_pkt_EventReader-bad_pkt_CalibReader-bad_pkt)==0){
512     stringstream err;
513     err.str()="";
514     err<<"NO GOOD PACKET SAVED in file: "<< rootfilename <<" The group is BAD: you may want to remove this file.";
515     msg = err.str();
516     mainLogUtil->logInfo(msg);
517     // mainLogUtil->logError(msg);
518     // return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???
519     }
520    
521     if(!single_connection){
522     OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
523     }
524    
525     //don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range
526     if((!real_time_init)&&(!real_time_last))
527     {
528     //saved in another table for future study
529 mocchiut 1.4 if(saveROOT_DB(Table_ROOT_Bad, outDir,// here outDir is good, no expand EMI
530 mocchiut 1.1 filename,
531     pkt_number_init, pkt_number_last,
532     obt_init, obt_last,
533     obt_time_sync, last_time_sync_info,
534     real_time_init, real_time_last,
535     boot_number,
536     timeOffset,
537     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
538 mocchiut 1.3 fni,time_is_estimated)==true)
539 mocchiut 1.1 {
540     oss.str()="";
541     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;
542     msg = oss.str();
543     mainLogUtil->logInfo(msg);
544     }
545     else
546     {
547     oss.str()="";
548     oss<<"Problem storing information in DB regarding file: "<<filename;
549     msg = oss.str();
550     mainLogUtil->logError(msg);
551     }
552 mocchiut 1.2
553 mocchiut 1.1 if(!single_connection){
554     CloseDBConnection();
555     }
556    
557     return;
558     }
559    
560     //saves info in DB in table Table_ROOT_Good
561 mocchiut 1.3 if(boot_number==0)
562     boot_number=boot_number_prevvalue;
563    
564 mocchiut 1.4 if(saveROOT_DB(Table_ROOT_Good, outDir, // here outDir is good, no expand EMI
565 mocchiut 1.1 filename,
566     pkt_number_init, pkt_number_last,
567     obt_init, obt_last,
568     obt_time_sync, last_time_sync_info,
569     real_time_init, real_time_last,
570     boot_number,
571     timeOffset,
572     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
573 mocchiut 1.3 fni,time_is_estimated)==true)
574 mocchiut 1.1 {
575     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good<< " id= "<<my_id;
576     msg = oss.str();
577     mainLogUtil->logInfo(msg);
578    
579     if(tryMerge){
580     merge_ROOTfiles();
581     }
582     }
583     else
584     {
585     oss<<"Problem storing information in DB regarding file: "<<filename;
586     msg = oss.str();
587     mainLogUtil->logError(msg);
588     }
589    
590     if(!single_connection){
591     CloseDBConnection();
592     }
593    
594     }//pRun
595     }
596    
597     //save in Table_ROOT_Good or in Table_ROOT_Bad
598     bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,
599     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
600     unsigned long int obt_in, unsigned long int obt_fin,
601     unsigned long int oT_sync, unsigned long int lT_sync_info,
602     unsigned long int mtime_init, unsigned long int mtime_last,
603     unsigned long int mboot_num,
604     unsigned long int time_offset,
605     int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,
606 mocchiut 1.3 char* nome_input, bool _time_is_estimated){
607 mocchiut 1.1 //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
608     stringstream oss;
609     oss.str("");
610 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)"
611 mocchiut 1.1 << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
612     << oT_sync << "','" << lT_sync_info << "','"
613     << mtime_init << "','" << mtime_last << "','"
614     << mboot_num << "','"
615     << time_offset << "','"
616 mocchiut 1.2 << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< num_PKT_Saved<<"','"
617 mocchiut 1.1 << nome_input <<"',"
618 mocchiut 1.3 <<"NULL,"<<_time_is_estimated<<
619 mocchiut 1.5 ");";
620 mocchiut 1.1 string msg = oss.str();
621     mainLogUtil->logAll(msg);
622    
623     stringstream oss1;
624     oss1.str()="";
625     string msg1;
626 mocchiut 1.3 string query;
627 mocchiut 1.1
628 mocchiut 1.3 TSQLResult* res=NULL;
629    
630     query=oss.str();
631     msg1="SaveROOT_DB query: ";
632     msg1 += query;
633     mainLogUtil->logInfo(msg1);
634    
635     res= sqlServer->Query(query.c_str());
636 mocchiut 1.1 if(!res)
637     {
638     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
639     msg1=oss1.str();
640     mainLogUtil->logError(msg1);
641     return false;
642     }
643    
644     //the ID of the current ROOT file in table Table_ROOT_Good
645     my_id = select_maxIDN_DB(table_name);
646 mocchiut 1.3 if(!boot_number){
647     if(id_to_recover_index<1000)
648     id_to_recover[id_to_recover_index++]=my_id;
649     }else if (boot_number && id_to_recover_index && !is_new_route){
650     recover_boot_number();
651     }
652 mocchiut 1.1 // cout<<"DBG: my_id = "<<my_id<<endl;
653     if(res){delete res; res = NULL;}
654     return true;
655     }
656    
657 mocchiut 1.3
658     void PacketUser::recover_boot_number(){
659     string msg1;
660     stringstream oss1;
661     stringstream oss;
662     oss.str("");
663     string query="";
664     TSQLResult* res=NULL;
665     if(!boot_number || !id_to_recover_index)
666     return;
667    
668     for (int i = 0; i<id_to_recover_index;i++){
669 mocchiut 1.5 oss.str("");
670     oss << "UPDATE "<< Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i] << ";";
671 mocchiut 1.3 query = oss.str();
672     res= sqlServer->Query(query.c_str());
673     if(!res)
674     {
675     oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;
676     msg1=oss1.str();
677     mainLogUtil->logError(msg1);
678     return ;
679     }
680     }
681    
682 mocchiut 1.5 for (int i = 0; i<id_to_recover_index;i++){
683     oss.str("");
684     oss << "UPDATE " << Table_ROOT_Merging << " SET BOOT_NUMBER=" << boot_number << " WHERE ROOT_ID_N=" << id_to_recover[i] << ";";
685     query=oss.str();
686     res= sqlServer->Query(query.c_str());
687     if(!res)
688     {
689 mocchiut 1.3 oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;
690     msg1=oss1.str();
691     mainLogUtil->logError(msg1);
692     return ;
693     }
694     }
695    
696     id_to_recover_index=0;
697     }
698    
699    
700    
701 mocchiut 1.1 /**********************************************************************************************/
702     /*###########################################################################################################*/
703     /********************************** MERGING *************************************************/
704     /*###########################################################################################################*/
705 mocchiut 1.3 // Merge ROT files: find other ROOT files in the same temporal ranges
706 mocchiut 1.1 // it finds ROOT files that covers particular temporal range of interest and save this info in DB
707     /**********************************************************************************************/
708     //try to merge current ROOT file with files in DB
709     bool PacketUser::merge_ROOTfiles()
710     {
711 mocchiut 1.2 stringstream log;
712     log.str("");
713     string slog;
714 mocchiut 1.1
715 mocchiut 1.2 mainLogUtil->logInfo("Trying merging ROOT files");
716 mocchiut 1.1 double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;
717    
718     TSQLResult* res=NULL;
719     TSQLRow* row =NULL;
720 mocchiut 1.2 //numero di ROOT files trovati sul DB che hanno relaz di tipo dato
721     unsigned int num_rows=0;
722    
723     //Record su DB:
724     unsigned int idN = 0;
725     unsigned int root_id_db=0;
726     unsigned long int pkt_num_in_db=0;
727     unsigned long int pkt_num_fin_db=0;
728     unsigned long int pkt_obt_in_db=0;
729     unsigned long int pkt_obt_fin_db=0;
730     unsigned long int time_in_db=0;
731     unsigned long int time_fin_db=0;
732     unsigned int boot_num_db=0;
733     double perc=0;
734 mocchiut 1.1
735 mocchiut 1.2
736     //marco_new: non considero mai caso di continuita'(mio finale=DB iniz o viceversa), non mi interessa
737 mocchiut 1.1 //marco_new: after e before contengono anche smaller ma con un bordo comune
738     for(int relaz=AFTER; relaz<=BIGGER; relaz++)
739 mocchiut 1.2 {
740 mocchiut 1.1 //marco_new: esci se sono arrivato a zero
741 mocchiut 1.2 if(real_time_last==real_time_init)
742     {
743     log.str("");
744     log<<"Current File Lenght=0; real_time_last=real_time_init= "<<real_time_last;
745     slog=log.str();
746     mainLogUtil->logAll(slog);
747 mocchiut 1.1 mainLogUtil->logInfo("Finish merging ROOT files");
748     if(res){delete res; res = NULL;}
749     if(row){delete row; row = NULL;}
750     return true;
751     }
752    
753 mocchiut 1.2 num_rows=0;
754     res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);
755 mocchiut 1.1
756     if (!res)
757     {
758     mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");
759     return false;
760     }
761    
762     num_rows=res->GetRowCount();
763 mocchiut 1.2 log.str("");
764     log<<"Found "<<num_rows<<" ROOT file in DB with relation of kind = "<<relaz;
765     slog=log.str();
766     mainLogUtil->logAll(slog);
767 mocchiut 1.1
768     if(num_rows>0)
769 mocchiut 1.2 {
770     for(unsigned int i=0; i<num_rows; i++)
771 mocchiut 1.1 {
772     row=res->Next();
773     if (!row)
774     {
775 mocchiut 1.2 mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");
776 mocchiut 1.1 if(res){delete res; res = NULL;}
777     return false;
778     }
779    
780 mocchiut 1.2 //Leggi Record dal DB:
781     idN=atoll(row->GetField(0));
782     root_id_db=atoll(row->GetField(1));
783     pkt_num_in_db=atoll(row->GetField(2));
784     pkt_num_fin_db=atoll(row->GetField(3));
785     pkt_obt_in_db=atoll(row->GetField(4));
786     pkt_obt_fin_db=atoll(row->GetField(5));
787     time_in_db=atoll(row->GetField(6));
788     time_fin_db=atoll(row->GetField(7));
789     boot_num_db=atoll(row->GetField(8));
790 mocchiut 1.1 perc=atof(row->GetField(9));
791 mocchiut 1.2
792     log.str("");
793     log<<"*** ROOT file found in DB has idN= "<<idN<<" root_id_db= "<<root_id_db;
794     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;
795     log<<" time_in_db= "<<time_in_db<<" time_fin_db= "<<time_fin_db<<" boot_num_db= "<<boot_num_db<<" perc= "<<perc<<" ***";
796     log<<"\n*** Current ROOT file has pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
797     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
798     slog=log.str();
799     mainLogUtil->logAll(slog);
800     if(perc>percentage)
801     {
802 mocchiut 1.1 mainLogUtil->logInfo("Current ROOT file is better than the one in DB");
803     //modifica DB
804     updateMergeROOT_DB(Table_ROOT_Merging,
805 mocchiut 1.2 root_id_db,
806     pkt_num_in_db, pkt_num_fin_db,
807     pkt_obt_in_db, pkt_obt_fin_db,
808     time_in_db, time_fin_db,
809     boot_num_db,
810     perc,
811     idN,(type_Rel_ROOT)relaz);
812 mocchiut 1.1 }
813 mocchiut 1.2 else
814     {
815     mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file");
816     if(relaz == AFTER)
817     {
818 mocchiut 1.1 mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
819 mocchiut 1.3 pkt_number_init = (pkt_num_fin_db+1);
820 mocchiut 1.2 obt_init = pkt_obt_fin_db;
821     real_time_init = time_fin_db;
822     log.str("");
823     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
824     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
825     slog=log.str();
826     mainLogUtil->logAll(slog);
827 mocchiut 1.1 }
828 mocchiut 1.2 else if(relaz == BEFORE)
829     {
830     mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");
831 mocchiut 1.3 pkt_number_last = (pkt_num_in_db-1);
832 mocchiut 1.2 obt_last = pkt_obt_in_db;
833     real_time_last = time_in_db;
834    
835     log.str("");
836     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
837     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
838     slog=log.str();
839     mainLogUtil->logAll(slog);
840 mocchiut 1.1 }
841     else if(relaz == SMALLER)
842     {
843     //non devo proprio salvarlo ed esco ...
844     mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
845 mocchiut 1.2 mainLogUtil->logInfo("\nFinish merging ROOT files");
846 mocchiut 1.1 if(res){delete res; res = NULL;}
847 mocchiut 1.2 if(row){delete row; row = NULL;}
848 mocchiut 1.1 return true;
849     }
850     else if(relaz == BIGGER)//spezzettamento
851     {
852     mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");
853     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
854 mocchiut 1.3 pkt_number_init, (pkt_num_in_db-1),
855 mocchiut 1.2 obt_init, pkt_obt_in_db,
856     real_time_init, time_in_db,
857 mocchiut 1.1 boot_number,
858     percentage);
859 mocchiut 1.2
860 mocchiut 1.1 mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");
861 mocchiut 1.3 pkt_number_init = (pkt_num_fin_db+1);
862 mocchiut 1.2 obt_init= pkt_obt_fin_db;
863     real_time_init= time_fin_db;
864    
865     log.str("");
866     log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<" obt_init= "<<obt_init<<" real_time_init= "<<real_time_init;
867     log<<" pkt_number_last= "<<pkt_number_last<<" obt_last= "<<obt_last<<" real_time_last= "<<real_time_last<<" ***";
868     slog=log.str();
869     mainLogUtil->logAll(slog);
870     }//if relaz
871     }//if perc
872 mocchiut 1.1 }//for num_rows
873 mocchiut 1.2 }//if num_rows
874 mocchiut 1.1
875     if(res){delete res; res = NULL;}
876     if(row){delete row; row = NULL;}
877 mocchiut 1.2
878     }//for relaz
879 mocchiut 1.1
880     mainLogUtil->logInfo("Saving Current ROOT file");
881     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
882     pkt_number_init, pkt_number_last,
883     obt_init, obt_last,
884     real_time_init, real_time_last,
885     boot_number,
886     percentage);
887    
888     mainLogUtil->logInfo("Finish merging ROOT files");
889     return true;
890 mocchiut 1.2
891 mocchiut 1.1 }
892    
893     //unsigned int root_id, percentage non le devo modificare
894     bool PacketUser::updateMergeROOT_DB(char* table_name,
895     unsigned int root_id,
896     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
897     unsigned long int obt_in, unsigned long int obt_fin,
898     unsigned long int mtime_init, unsigned long int mtime_last,
899     unsigned long int mboot_num,
900     double bad_perc,
901     unsigned int ID_record, type_Rel_ROOT type_rel){
902    
903 mocchiut 1.2 stringstream oss;
904     oss.str("");
905    
906 mocchiut 1.1 if(type_rel == AFTER){
907     mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
908     //marco_new:tolti tutti gli apici che qui non devono essere
909     oss << "UPDATE "<< table_name
910 mocchiut 1.3 << " SET PKT_NUMBER_FINAL ="<< (pkt_number_init - 1)
911 mocchiut 1.1 <<", PKT_OBT_FINAL ="<< obt_init
912     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";
913 mocchiut 1.2
914 mocchiut 1.1 }
915     else if(type_rel == BEFORE){
916 mocchiut 1.2 mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");
917 mocchiut 1.1 oss << "UPDATE "<< table_name
918 mocchiut 1.3 << " SET PKT_NUMBER_INIT ="<< (pkt_number_last+1)
919 mocchiut 1.1 <<", PKT_OBT_INIT ="<< obt_last
920     <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";
921     }
922     else if(type_rel == SMALLER){
923     //spezzettamentento
924     mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");
925    
926     oss << "UPDATE "<< table_name
927 mocchiut 1.3 << " SET PKT_NUMBER_FINAL="<< (pkt_number_init-1)
928 mocchiut 1.1 <<", PKT_OBT_FINAL ="<< obt_init
929     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record<<";";
930    
931 mocchiut 1.2 mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");
932    
933     saveMergeROOT_DB(table_name, root_id,
934 mocchiut 1.3 (pkt_number_last+1), pkt_number_fin,
935 mocchiut 1.2 obt_last, obt_fin,
936     real_time_last, mtime_last,
937     mboot_num,
938     bad_perc);
939 mocchiut 1.1 }
940     else if(type_rel == BIGGER){
941     //marco_new: ok anche se coincidenti etc
942 mocchiut 1.4 // oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO
943     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
944 mocchiut 1.2 mainLogUtil->logInfo("Record deleted from merging table");
945 mocchiut 1.1 }
946     else
947     return false;
948    
949     string msg = oss.str();
950     mainLogUtil->logInfo(msg);
951    
952     stringstream oss1;
953     oss1.str()="";
954     string msg1;
955    
956     TSQLResult* res=NULL;
957     res= sqlServer->Query(oss.str().c_str());
958     if(!res)
959     {
960     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
961     msg1=oss1.str();
962     mainLogUtil->logError(msg1);
963     return false;
964     }
965     if(res){delete res; res = NULL;}
966     return true;
967     }
968    
969 mocchiut 1.2 //cerca eventuali ROOT files in DB che possono venire mergiati
970     //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
971     //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
972     //marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx
973 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)
974     {
975     TSQLResult* res=NULL;
976     stringstream query;
977     query.str("");
978    
979     if(type_rel == AFTER)
980 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
981 mocchiut 1.1 else if(type_rel == BEFORE)
982 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
983 mocchiut 1.1 else if(type_rel == SMALLER)
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 if(type_rel == BIGGER)
986 mocchiut 1.4 query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last; // EMI
987 mocchiut 1.1 else
988     return 0;
989    
990     query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile
991 mocchiut 1.2 //se volessi mettere un filtro sulla qualita'
992 mocchiut 1.1 // query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
993     string msg = query.str();
994     mainLogUtil->logAll(msg);
995 mocchiut 1.2 res= sqlServer->Query(query.str().c_str());
996 mocchiut 1.1 stringstream oss1;
997 mocchiut 1.2 oss1.str()="";
998     string msg1;
999 mocchiut 1.1
1000     if (!res)
1001     {
1002     oss1<<"DBError UNABLE to: "<<query.str().c_str()<<endl;
1003     msg1=oss1.str();
1004     mainLogUtil->logError(msg1);
1005     }
1006    
1007     return res;
1008     }
1009    
1010     bool PacketUser::saveMergeROOT_DB(char* table_name, unsigned int root_id,
1011     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
1012     unsigned long int obt_in, unsigned long int obt_fin,
1013     unsigned long int mtime_init, unsigned long int mtime_last,
1014     unsigned long int mboot_num,
1015     double percentage)
1016     {
1017    
1018     stringstream oss;
1019     oss.str("");
1020 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)"
1021 mocchiut 1.1 << " VALUES ('"<<0<< "','" <<root_id<<"','"<< pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
1022     << mtime_init << "','" << mtime_last << "','"
1023     << mboot_num <<"','"
1024     << percentage <<"',"
1025 mocchiut 1.3 <<"NULL"<<",'"
1026     << tag_value<<"'"
1027     << ")";
1028 mocchiut 1.1
1029     string msg = oss.str();
1030     mainLogUtil->logAll(msg);
1031 mocchiut 1.2
1032 mocchiut 1.1 //marco_new: controlla
1033     //marco_new: esci se sono arrivato a zero
1034     // if(real_time_last==real_time_init){
1035     if(mtime_init==mtime_last){
1036     mainLogUtil->logAll("Nothing to be saved");
1037    
1038     // if(res){delete res; res = NULL;}
1039     // if(row){delete row; row = NULL;}
1040     return true;
1041     }
1042    
1043     stringstream oss1;
1044     oss1.str()="";
1045     string msg1;
1046    
1047     TSQLResult* res=NULL;
1048     res= sqlServer->Query(oss.str().c_str());
1049     if(!res)
1050     {
1051     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1052     msg1=oss1.str();
1053     mainLogUtil->logError(msg1);
1054     return false;
1055     }
1056     if(res){delete res; res = NULL;}
1057     return true;
1058     }
1059    
1060     //Utility: return max(IDN)
1061     unsigned int PacketUser::select_maxIDN_DB(char* table_name)
1062     {
1063    
1064     if (!strcmp(table_name,Table_ROOT_Bad))
1065     {
1066     return 0;
1067     }
1068     unsigned int idN=0;
1069     TSQLResult* res=NULL;
1070     stringstream oss;
1071     oss.str("");
1072     oss<<"select max(ID_N) from "<<table_name;
1073 mocchiut 1.2 // string msg = oss.str();
1074     // mainLogUtil->logAll(msg);
1075 mocchiut 1.1 res= sqlServer->Query(oss.str().c_str());
1076    
1077     stringstream oss1;
1078     oss1.str()="";
1079     string msg1;
1080    
1081     if(!res)
1082     {
1083     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1084     msg1=oss1.str();
1085     mainLogUtil->logError(msg1);
1086     return 0;
1087     }
1088    
1089     TSQLRow* row=NULL;
1090     row=res->Next();
1091     if (!row )
1092     {
1093     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1094     msg1=oss1.str();
1095     mainLogUtil->logError(msg1);
1096     return 0;
1097     }
1098    
1099     idN=atoi(row->GetField(0));
1100     if(row){delete row; row = NULL;}
1101     if(res){delete res; res = NULL;}
1102 mocchiut 1.2
1103     oss<<". idN = "<<idN;
1104     string msg = oss.str();
1105     mainLogUtil->logAll(msg);
1106    
1107 mocchiut 1.1 return idN;
1108     }
1109    
1110     /**
1111     * Lock par=table, if par=null lock all tables
1112     */
1113     int PacketUser::LockTables(char* tableTobeLocked)
1114     {
1115     //se non ho il DB :)
1116     if(!do_cont_check) return 999;
1117    
1118     TSQLResult* res=NULL;
1119     stringstream oss;
1120     oss.str("");
1121     if(!tableTobeLocked)
1122     oss << "lock table "<<Table_ROOT_Good<<" write, "<<Table_ROOT_Bad<<" write, "<<Table_GL_RESURS_OFFSET<<" write, "<<Table_ROOT_Merging<<" write;";
1123     else
1124     oss << "lock table "<<tableTobeLocked<<" write; ";
1125    
1126     string msg = oss.str();
1127     mainLogUtil->logAll(msg);
1128    
1129     res= sqlServer->Query(oss.str().c_str());
1130    
1131     stringstream oss1;
1132     oss1.str()="";
1133     string msg1;
1134    
1135     if(!res)
1136     {
1137     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1138     msg1=oss1.str();
1139     mainLogUtil->logError(msg1);
1140     return 1;
1141     }
1142    
1143     if(res){delete res; res = NULL;}
1144 mocchiut 1.2 mainLogUtil->logAll("TABLES LOCKED");
1145 mocchiut 1.1 return 0;
1146     }
1147    
1148     /**
1149     * UNLock tables
1150     */
1151     int PacketUser::UnLockTables(){
1152     //se non ho il DB :)
1153     if(!do_cont_check) return 999;
1154    
1155     TSQLResult* res=NULL;
1156     stringstream oss;
1157     oss.str("");
1158     oss << "unlock tables;";
1159    
1160     string msg = oss.str();
1161     mainLogUtil->logAll(msg);
1162    
1163     res= sqlServer->Query(oss.str().c_str());
1164    
1165     stringstream oss1;
1166     oss1.str()="";
1167     string msg1;
1168    
1169     if(!res)
1170     {
1171     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1172     msg1=oss1.str();
1173     mainLogUtil->logError(msg1);
1174     return 1;
1175     }
1176    
1177     if(res){delete res; res = NULL;}
1178 mocchiut 1.2 mainLogUtil->logAll("TABLES UNLOCKED");
1179 mocchiut 1.1
1180     return 0;
1181     }
1182    
1183     /*********************************************************/
1184     //marco_new_31
1185     void PacketUser::OpenDBConnection(char* tableTobeLocked){
1186     if(do_cont_check){
1187 mocchiut 1.2 //controllo che non sia gia' aperta
1188 mocchiut 1.1 if(!sqlServer){
1189     sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
1190     if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}
1191 mocchiut 1.2 mainLogUtil->logAll("Connected to DB");
1192     //blocca le tabelle
1193     LockTables(tableTobeLocked);
1194 mocchiut 1.1 }
1195     }
1196    
1197     }
1198    
1199     //marco_new_31
1200     void PacketUser::CloseDBConnection(){
1201     if(do_cont_check){
1202     //controllo che ce ne sia una aperta
1203     if(sqlServer && sqlServer->IsConnected()){
1204     //marco_new: sblocca tutte le tabelle
1205     UnLockTables();
1206     sqlServer->Close();
1207 mocchiut 1.2 delete sqlServer;
1208 mocchiut 1.1 sqlServer=NULL;
1209 mocchiut 1.2 mainLogUtil->logAll("Closed Connection to DB");
1210 mocchiut 1.1 }
1211     }
1212     }
1213    
1214    
1215    
1216     /***********************************************************************************************************************/
1217     /*#################################################### HIC SUNT LEONES ################################################*/
1218     /***********************************************************************************************************************/
1219     //UNUSED: dbg functions...
1220    
1221     //Return the system time in ms
1222     unsigned long long PacketUser::Record_Time(){
1223     timeval tv;
1224     gettimeofday(&tv,NULL);
1225     unsigned long long timems = (unsigned long long)tv.tv_sec * (unsigned long long)1000 + tv.tv_usec / 1000;
1226     return timems;
1227     }
1228    
1229     //dbg_functions:
1230    
1231     //UNUSED. save a packet in a file
1232     void PacketUser::savePKT_file(char* headerPkt,
1233     char* pamPkt,
1234     long int length,
1235     bool append,
1236     char* nomefile)
1237     {
1238     char fno[80]="";
1239     ofstream fout;
1240 mocchiut 1.4 sprintf(fno,"%s/pkt%d_of_%s.pkt", gSystem->ExpandPathName(outDir), numPKTSaved, nomefile); // EMI
1241 mocchiut 1.1
1242     if(append==true)
1243     fout.open(fno, ios::binary | ios::app);
1244     else
1245     fout.open(fno, ios::binary);
1246    
1247     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1248     fout.write(headerPkt,LENGTH_HEADER_PKT);
1249     fout.write(pamPkt,length);
1250     fout.close();
1251     }
1252    
1253     //UNUSED
1254     void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append)
1255     {
1256     char fno[80]="";
1257     ofstream fout;
1258     if(append==true)
1259     {
1260 mocchiut 1.4 sprintf(fno,"%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI
1261 mocchiut 1.1 fout.open(fno, ios::binary | ios::app);
1262     }
1263     else{
1264 mocchiut 1.4 sprintf(fno,"%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT ); // EMI
1265 mocchiut 1.1 fout.open(fno, ios::binary);
1266     }
1267    
1268     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1269     fout.write(headerPkt,LENGTH_HEADER_PKT);
1270     fout.write(pamPkt,length);
1271     fout.close();
1272     }
1273    
1274    
1275     }

  ViewVC Help
Powered by ViewVC 1.1.23