GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
adf/Macros/Deprecated/Utilities.C
Go to the documentation of this file.
1 
2 
3 #include <iostream>
4 #include <fstream>
5 #include <string>
6 
7 #include "TSystem.h"
8 #include "TRegexp.h"
9 #include "TList.h"
10 #include "TSystemDirectory.h"
11 #include "TNamed.h"
12 #include "TDirectory.h"
13 #include "TString.h"
14 #include "TSocket.h"
15 
17 
32 void MakeRootAFPConfFile( const char * directory, const char * pat = "*.adf", Option_t * opt ="" );
33 
34 void GetListOfBadDirFromOption( TList *, std::string );
35 void GetListOfBadDirPatternFromOption( TList *, std::string );
36 void GetListOfBadFilePatternFromOption( TList *, std::string );
37 bool IsAGoodDir( TList * listBadDir, TList * listBadPattern, const char * );
38 bool IsAGoodFile( TList * listOfBadPattern, const char * );
39 
40 const char * currentdir = "zCurrentNarvalDataDir";
41 
42 void MakeRootAFPConfFile( const char * directory, const char * pat, Option_t * opt )
43 {
44  TString soption = opt;
45  TList * listofBaddir = new TList;
46  TList * listofBadPatternf = new TList;
47  TList * listofBadPatternd = new TList;
48  std::string ssoption = soption.Data();
49 
50  GetListOfBadDirFromOption(listofBaddir, ssoption );
51  GetListOfBadDirPatternFromOption( listofBadPatternd, ssoption );
52  GetListOfBadFilePatternFromOption( listofBadPatternf, ssoption );
53 
54  TString presentDir = gSystem->pwd();
55  TList * listofsubdir = new TList;
56  TList * listofFile = new TList;
57  TList * listOfGoodFile = new TList;
58  TString patname = pat;
59 
60  // pattern expression
61  Bool_t wildcard = false;
62  if ( patname.Contains("*") )
63  wildcard = true;
64  TRegexp all("*",kTRUE), pattern(patname.Data(),wildcard);
65 
66  if ( pattern.Status() != TRegexp::kOK )
67  {
68  if ( gDebug > 0 )
69  std::cerr << "pattern [*] intead of " << patname << std::endl;
70  pattern = all;
71  }
72  else
73  {
74  if ( gDebug > 0 ) std::cerr << "pattern " << patname << std::endl ;
75  }
76  TSystemDirectory dir;
77  dir.SetDirectory(directory);
78  TList *list = dir.GetListOfFiles();
79  TIter iter(list);
80  TObject *entry;
81 
82  while ( (entry = iter()) )
83  {
84  if ( entry->InheritsFrom("TSystemDirectory") &&
85  strncmp(entry->GetName(),".",1) &&
86  strcmp(entry->GetName(),currentdir) &&
87  IsAGoodDir( listofBaddir, listofBadPatternd, entry->GetName()) )
88  {
89  TString pathname = gSystem->pwd();
90  pathname += "/";
91  pathname += entry->GetName();
92  listofsubdir->Add(new TNamed(entry->GetName(),pathname.Data()));
93  }
94  else if ( entry->InheritsFrom("TSystemFile") &&
95  strncmp(entry->GetName(),".",1) &&
96  IsAGoodFile( listofBadPatternf, entry->GetName() ) )
97  {
98  TString pathname = gSystem->pwd();
99  listofFile->Add(new TNamed(entry->GetName(),pathname.Data()));
100  }
101  }
102 
103 
104  if ( soption.Contains("r") )
105  {
106  while ( listofsubdir->GetSize() != 0 )
107  {
108  dir.SetDirectory(listofsubdir->At(0)->GetTitle());
109  list = dir.GetListOfFiles();
110  TIter iter2(list);
111  while ( (entry = iter2()) )
112  {
113  if ( entry->InheritsFrom("TSystemDirectory") &&
114  strncmp(entry->GetName(),".",1) &&
115  strcmp(entry->GetName(),currentdir) &&
116  IsAGoodDir( listofBaddir, listofBadPatternd, entry->GetName()) )
117  {
118  TString pathname = gSystem->pwd();
119  pathname += "/";
120  pathname += entry->GetName();
121  listofsubdir->Add(new TNamed(entry->GetName(),pathname.Data()));
122  }
123  else if ( entry->InheritsFrom("TSystemFile") &&
124  strncmp(entry->GetName(),".",1) &&
125  IsAGoodFile( listofBadPatternf, entry->GetName()) )
126  {
127  TString pathname = gSystem->pwd();
128  listofFile->Add(new TNamed(entry->GetName(),pathname.Data()));
129  }
130  }
131  TNamed * tmp;
132  tmp = (TNamed*)listofsubdir->At(0);
133  listofsubdir->Remove( listofsubdir->At(0) );
134  delete tmp;
135  }
136  }
137 
138  for ( Int_t i = 0; i < listofFile->GetSize(); i++ )
139  {
140  TString testname = listofFile->At(i)->GetName();
141  if ( testname.Contains(pattern) )
142  listOfGoodFile->Add( listofFile->At(i) );
143  }
144 
145  std::clog << listOfGoodFile->GetSize() << " file was found " << std::endl;
146 
147  gSystem->cd(presentDir);
148 
149  std::ofstream outputfile("./RootAFP.conf");
150  std::clog << "file is created : " << presentDir << "/RootAFP.conf" << std::endl;
151  for ( Int_t i = 0; i < listOfGoodFile->GetSize(); i++ )
152  {
153  outputfile << "l " << listOfGoodFile->At(i)->GetTitle() << "/"
154  << listOfGoodFile->At(i)->GetName() << std::endl;
155  }
156  outputfile.close();
157 
158  listOfGoodFile->Clear();
159  listofFile->Clear();
160  list->Clear();
161 }
162 
163 void GetListOfBadDirFromOption( TList * l, std::string s )
164 {
165  for ( size_t i = 2; i < s.size(); i++ )
166  {
167  if ( s[i-2] == '-' && s[i-1] == 'd' && s[i] == '=' )
168  {
169  std::string newtmp;
170  newtmp.clear();
171  for ( size_t j = i+1; j < s.size() && s[j]!='-';j++ )
172  newtmp.push_back(s[j]);
173  l->Add(new TNamed(newtmp.c_str(),newtmp.c_str()));
174  }
175 
176  }
177 }
178 void GetListOfBadDirPatternFromOption( TList * l, std::string s)
179 {
180  for ( size_t i = 3; i < s.size(); i++ )
181  {
182  if ( s[i-3] == '-' && s[i-2] == 'p' && s[i-1] == 'd' && s[i] == '=' )
183  {
184  std::string newtmp;
185  newtmp.clear();
186  for ( size_t j = i+1; j < s.size() && s[j]!='-';j++ )
187  newtmp.push_back(s[j]);
188  l->Add(new TNamed(newtmp.c_str(),newtmp.c_str()));
189  }
190 
191  }
192 }
193 void GetListOfBadFilePatternFromOption( TList * l, std::string s)
194 {
195  for ( size_t i = 3; i < s.size(); i++ )
196  {
197  if ( s[i-3] == '-' && s[i-2] == 'p' && s[i-1] == 'f' && s[i] == '=' )
198  {
199  std::string newtmp;
200  newtmp.clear();
201  for ( size_t j = i+1; j < s.size() && s[j]!='-';j++ )
202  newtmp.push_back(s[j]);
203  l->Add(new TNamed(newtmp.c_str(),newtmp.c_str()));
204  }
205 
206  }
207 }
208 bool IsAGoodDir( TList * listBadDir, TList * listBadPattern, const char * name )
209 {
210  for ( Int_t i = 0; i < listBadDir->GetSize(); i++ )
211  if ( !strcmp( name, listBadDir->At(i)->GetName()) ) return( false );
212  TString sname = name;
213  for ( Int_t i = 0; i < listBadPattern->GetSize(); i++ )
214  {
215  TString tmp(listBadPattern->At(i)->GetName());
216  Bool_t wildcard = false;
217  if ( tmp.Contains("*") )
218  wildcard = true;
219  TRegexp pattern(listBadPattern->At(i)->GetName(),wildcard);
220  if ( sname.Contains(pattern) )
221  return( false );
222  }
223  return( true );
224 }
225 bool IsAGoodFile( TList * listOfBadPattern, const char * name )
226 {
227  TString sname = name;
228  for ( Int_t i = 0; i < listOfBadPattern->GetSize(); i++ )
229  {
230  TString tmp(listBadPattern->At(i)->GetName());
231  Bool_t wildcard = false;
232  if ( tmp.Contains("*") )
233  wildcard = true;
234  TRegexp pattern(listOfBadPattern->At(i)->GetName(),wildcard);
235  if ( sname.Contains(pattern) )
236  return( false );
237  }
238  return( true );
239 }
240 
242 
243 namespace { TList PingNames; char buffer[4*1024*1024]; }
244 
246 /*
247 */
248 void LoadPing( const char * filename, Option_t * opt = "+" )
249 {
250  TString option = opt;
251  std::ifstream inputfile(filename);
252  TList listName_tmp;
253  std::string machineName;
254  std::string portName;
255  bool test_is;
256  TNamed * tmp_name = NULL;
257  while(1){
258  machineName.clear();
259  portName.clear();
260  inputfile >> machineName >> portName;
261  if( inputfile.eof() ) break;
262  std::cerr << machineName << " " << portName << std::endl;
263  listName_tmp.Add(new TNamed( machineName.c_str(),
264  portName.c_str() ) );
265 
266  };
267  std::cerr << listName_tmp.GetSize() << std::endl;
268  if( PingNames.GetSize() == 0 && option.Contains("+") ){
269  for( Int_t i = 0; i < listName_tmp.GetSize(); i++)
270  PingNames.Add( listName_tmp.At(i) );
271  listName_tmp.Clear("nodelete");
272  }
273  else if( option.Contains("+") ){
274  for( Int_t i = 0; i<listName_tmp.GetSize(); i++){
275  test_is = false;
276  for( Int_t j = 0; j<PingNames.GetSize(); j++){
277  if(std::string(PingNames.At(j)->GetName()) ==
278  std::string(listName_tmp.At(i)->GetName()) &&
279  std::string(PingNames.At(j)->GetTitle()) ==
280  std::string(listName_tmp.At(i)->GetTitle()) ) test_is = true;
281  }
282  if( test_is == false ) PingNames.Add(listName_tmp.At(i));
283  }
284  listName_tmp.Clear("nodelete");
285  }
286 
287  if( option.Contains("-") ){
288  for( Int_t i = 0; i<listName_tmp.GetSize(); i++ ){
289  test_is = false;
290  for( Int_t j = 0; j<PingNames.GetSize(); j++){
291  if(std::string(PingNames.At(j)->GetName()) ==
292  std::string(listName_tmp.At(i)->GetName()) &&
293  std::string(PingNames.At(j)->GetTitle()) ==
294  std::string(listName_tmp.At(i)->GetTitle()) ){
295  test_is = true;
296  tmp_name = (TNamed*)PingNames.At(j);
297  }
298  }
299  if( test_is == true ){
300  PingNames.Remove(tmp_name);
301  delete tmp_name;
302  }
303  }
304  listName_tmp.Clear("delete");
305  }
306 
307  PingNames.Print();
308  for( Int_t ii = 0; ii < PingNames.GetSize(); ii++ ){
309  std::cerr << PingNames.At(ii)->GetName() << " " << PingNames.At(ii)->GetTitle() << std::endl;}
310 }
311 
313 /*
314  */
315 void Ping( UInt_t nbtimes = 5, bool dodump = false)
316 {
317  TIter next(&PingNames);
318  TObject * obj;
319  while ( (obj = next()) ) {
320 
321  TString option = obj->GetTitle(), ifdump;
322  cout << "Ping on : " << obj->GetName() << " with Port # " << obj->GetTitle() << endl;
323 
324  TSocket *fSocket = new TSocket(obj->GetName(),option.Atoi());
325  if ( fSocket ) {
326  if ( fSocket->IsValid() ) {
327  Int_t bsize, ret;
328  cout << "Select with 1 millisecond " << endl;
329  for (UInt_t i = 0u; i < nbtimes; i++) {
330  cout << " - try # " << i << endl;
331  ifdump = Form("%s_%s_%d",obj->GetName(),obj->GetTitle(),i);
332 
333  switch ( fSocket->Select(TSocket::kRead,1000) )
334  {
335  case 0:
336  cout << "--> timeout " << endl;
337  break;
338  case -1:
339  cout << "--> Error " << endl;
340  break;
341  default:
342  fSocket->RecvRaw(&bsize, sizeof(Int_t));
343  cout << " Size expected " << bsize << endl;
344  ret = fSocket->RecvRaw(buffer, bsize);
345  cout << " Size received " << ret << endl;
346  if ( dodump ) {
347  ofstream file(ifdump.Data(),ios_base::app);
348  file << bsize;
349  file.write(buffer,bsize);
350  }
351  break;
352  }
353  }
354  }
355  else cout << " *** Invalid connection *** " << endl;
356  }
357  else cout << " *** Invalid connection *** " << endl;
358  delete fSocket;
359  }
360 }
361 
362 void Ping(const char *machine, UInt_t port, UInt_t nbtimes = 5, Option_t *opt = "")
363 {
364  TString option = opt, sport = Form("%d",port);
365  bool test_is;
366  TNamed * tmp_name = NULL;
367  // add or removed from the list
368  if ( option.Contains("+") ) {
369  test_is = false;
370  for( Int_t i = 0; i < PingNames.GetSize(); i++ )
371  if( std::string(PingNames.At(i)->GetName()) == std::string(machine) &&
372  std::string(PingNames.At(i)->GetTitle()) == std::string(sport) ) test_is = true;
373  if( test_is == false )
374  PingNames.Add ( new TNamed(machine,sport.Data()) );
375  }
376  if ( option.Contains("-") ){
377  test_is = false;
378  for( Int_t i = 0; i < PingNames.GetSize(); i++ )
379  if( std::string(PingNames.At(i)->GetName()) == std::string(machine) &&
380  std::string(PingNames.At(i)->GetTitle()) == std::string(sport) ) {
381  test_is = true;
382  tmp_name = (TNamed*)PingNames.At(i);
383  }
384  if ( test_is == true ) {
385  PingNames.Remove(tmp_name);
386  delete tmp_name;
387  }
388  }
389 
390  Ping(nbtimes);
391 }
392 
394 /*
395  */
396 void SSHPing( UInt_t nbtimes = 1, bool dodump = false)
397 {
398 // TIter next(&PingNames);
399 // TObject * obj;
400 // while ( (obj = next()) ) {
401 
402  TNamed *obj = new TNamed("localhost","10001");
403 
404  TString option = obj->GetTitle(), ifdump;
405  cout << "Ping on : " << obj->GetName() << " with Port # " << obj->GetTitle() << endl;
406 
407 
408  TSocket *fSocket = new TSocket(obj->GetName(),option.Atoi());
409  if ( fSocket ) {
410  if ( fSocket->IsValid() ) {
411  Int_t bsize, ret;
412  cout << "Select with 1 millisecond " << endl;
413  for (UInt_t i = 0u; i < nbtimes; i++) {
414  cout << " - try # " << i << endl;
415  ifdump = Form("%s_%s_%d",obj->GetName(),obj->GetTitle(),i);
416 
417  switch ( fSocket->Select(TSocket::kRead,1000) )
418  {
419  case 0:
420  cout << "--> timeout " << endl;
421  break;
422  case -1:
423  cout << "--> Error " << endl;
424  break;
425  default:
426  fSocket->RecvRaw(&bsize, sizeof(Int_t));
427  cout << " Size expected " << bsize << endl;
428  ret = fSocket->RecvRaw(buffer, bsize);
429  cout << " Size received " << ret << endl;
430  if ( dodump ) {
431  ofstream file(ifdump.Data(),ios_base::app);
432  file << bsize;
433  file.write(buffer,bsize);
434  }
435  break;
436  }
437  }
438  }
439  else cout << " *** Invalid connection *** " << endl;
440  }
441  else cout << " *** Invalid connection *** " << endl;
442  delete fSocket;
443 
444  delete obj;
445 // }
446 }
447 
448 
450 
452 {
453  gSystem->Exec("rm -rf DefaultWatchers/*_C.so DefaultWatchers/*_C.d DefaultWatchers/*.o ./*_C.so ./*_C.d ./*.o");
454  gSystem->Exec("rm -rf MyWatchers/*_C.so MyWatchers/*_C.d MyWatchers/*.o ./*_C.so ./*_C.d ./*.o");
455 
456 }
457 
458 
void GetListOfBadDirPatternFromOption(TList *, std::string)
void Ping(UInt_t nbtimes=5, bool dodump=false)
function to check if a watcher connexion is possible with an actor
bool IsAGoodFile(TList *listOfBadPattern, const char *)
void SSHPing(UInt_t nbtimes=1, bool dodump=false)
function to check if a watcher connexion is possible with an actor
void MakeRootAFPConfFile(const char *directory, const char *pat="*.adf", Option_t *opt="")
function to construct a RootAFP.conf file
const char * currentdir
void LoadPing(const char *filename, Option_t *opt="+")
Load list of server to ping from a file.
void GetListOfBadFilePatternFromOption(TList *, std::string)
bool IsAGoodDir(TList *listBadDir, TList *listBadPattern, const char *)
ADF::LogMessage & endl(ADF::LogMessage &log)
void GetListOfBadDirFromOption(TList *, std::string)