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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp_30

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.23