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