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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations) (download)
Thu Oct 1 08:55:42 2009 UTC (15 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.8: +5 -1 lines
Bugs in DB UTC settings fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23