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