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

Contents of /chewbacca/PamOffLineSW/PacketUser.cpp_01

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download)
Thu Dec 18 14:46:56 2008 UTC (15 years, 11 months ago) by mocchiut
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +1 -1 lines
FILE REMOVED
Default values changed, symbolic path handling implemented, cleanup of backup files, do not delete files from ROOT_TABLE_MERGING

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

  ViewVC Help
Powered by ViewVC 1.1.23