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

Contents of /chewbacca/PamOffLineSW/PacketUser.cpp_02

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Tue Sep 23 07:20:13 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: v0r00
CVS Tags: start
Changes since 1.1: +0 -0 lines
Error occurred while calculating annotation data.
Imported sources, 23/09/2008

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

  ViewVC Help
Powered by ViewVC 1.1.23