GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConfAgent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ADF_ConfAgent
24 #include "ConfAgent.h"
25 #endif
26 #ifndef ADF_ConfigurationFrame
27 #include "ConfigurationFrame.h"
28 #endif
29 #ifndef ADF_RunAgent
30 #include "RunAgent.h"
31 #endif
32 #ifndef ADF_DFAgent
33 #include "DFAgent.h"
34 #endif
35 
36 #include <stdlib.h>
37 
38 ADF::ConfAgent *ADF::ConfAgent::gTheGlobalAgent = 0x0;
39 
40 using namespace ADF;
41 using namespace std;
42 
43 namespace { UInt_t gAllocated = 0u; }
44 
45 ConfAgent::ConfAgent() : DotConf("ADF::ConfAgent")
46 {
47  gAllocated++;
48  SetWithRecord(true);
49 }
50 
52 {
53  gAllocated--;
54 
55  if ( gAllocated == 1 ) {
56 #ifdef DEBUGSTATIC
57  std::clog << "[[static]] ConfAgent::~ConfAgent() ]\n[...\n" ;
58 #endif
60 #ifdef DEBUGSTATIC
61  std::clog << " done\n...]\n";
62 #endif
63  }
64 }
65 
67 {
69 
70  if ( model == "kStrict" )
72  else if ( model == "kGrowing" )
74 
75  return m;
76 }
77 
79 {
80  const char *model = 0x0;
81  switch (m) {
82  case kStrict:
83  model = "kStrict";
84  break;
85  case kSafe:
86  model = "kSafe";
87  break;
88  case kGrowing:
89  model = "kGrowing";
90  break;
91  }
92  return model;
93 }
94 
96 {
97  if ( gTheGlobalAgent && gAllocated == 1 ) {
98 #ifdef DEBUGSTATIC
99  std::clog << "[[static]] KilltheGlobalAgent() ]\n[...\n" ;
100  std::clog << " Destroying the global agent ... " << std::endl;
101 #endif
102  delete gTheGlobalAgent;
103 #ifdef DEBUGSTATIC
104  std::clog << " done\n...]\n";
105 #endif
106  gTheGlobalAgent = 0x0;
107  }
108 }
109 
110 
111 namespace ADF {
112 
114 
183  class AgataConfAgent : public ConfAgent
184  {
185  private:
187  static AgataConfAgent *gTheGlobalAgent;
188 
189  private:
191  DFAgent *fDFAgent;
193  RunAgent *fRunAgent;
194 
195  public:
197  AgataConfAgent();
199  AgataConfAgent(const char *);
201  virtual ~AgataConfAgent();
202 
204  virtual DFAgent *GetDFAgent()
205  { return fDFAgent ;}
208  { return fRunAgent ;}
209  virtual const DFAgent *GetConstDFAgent() const
210  { return const_cast<const DFAgent *> (fDFAgent) ;}
211  virtual const RunAgent *GetConstRunAgent() const
212  { return const_cast<const RunAgent *> (fRunAgent) ;}
213 
214  ConfAgent *Clone(const char *comment = "Clone of the global ConfAgent");
215 
216  // to get the global AGATA agent
217  static AgataConfAgent *theGlobalAgent();
218 
220  virtual Bool_t Configure(const Char_t *name, const char *option = "in", Bool_t allow_init = true)
221  { return DotConf::Configure(name,option,allow_init); }
222  virtual Bool_t Configure(ConfigurationFrame *, const char *option = "in", Bool_t allow_init = false);
223  };
224 } // namespace
225 
226 ADF::AgataConfAgent *ADF::AgataConfAgent::gTheGlobalAgent = 0x0;
227 
229 ConfAgent(),
230 fDFAgent(new DFAgent("ADF::ConfAgent","Agata",true,true)),
231 fRunAgent(new RunAgent("ADF::ConfAgent","Run"))
232 {
233  fLog.GetProcessName() = "ADF::AgataConfAgent";
234  SetLastModif();
235 
236  if ( fDFAgent )
237  AddConf(fDFAgent);
238  if ( fRunAgent )
239  AddConf(fRunAgent);
240 
241  // configuration from the input file of the distribution
242  // then the one pointed by ADF_CONF_PATH. if none of these two are found, try the current directory
243  // if not found in the install path (make install not done properly)
244  // try in the current directory
245  std::string tmp;
246 
247  // first the one in the distribution
248 
249  tmp = GetPathToConfFile();
250  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
251  tmp += '/';
252  tmp += "ADF.conf";
253 
254  if ( Configure(tmp.data(),"in") )
255  fLog << info << "Init Global Conf Agent from distribution " << tmp << nline;
256  else
257  fLog << error << "Failed to init Global Conf Agent from distribution " << tmp << nline;
258 
259  // send snapshot of the agent in the log system
260  if ( fLog.IsDebug(2) ) {
262  DoConfigure( fLog() );
264  }
265  // second the one in the ADF_CONF_PATH path or if not defined in the current directory
266  if ( ::getenv("ADF_CONF_PATH") ) {
267 
268  tmp = ::getenv("ADF_CONF_PATH");
269 
270  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
271  tmp += '/';
272  tmp += "ADF.conf";
273  if ( Configure(tmp.data(),"in") )
274  fLog << info << "Init Global Conf Agent from ADF_CONF_PATH " << tmp << nline;
275  else
276  fLog << warning << "Failed to init Global Conf Agent from ADF_CONF_PATH " << tmp << nline;
277  }
278  else {
279  fLog << warning << "ADF_CONF_PATH NOT DEFINED !, try init Global Conf Agent from current dir ..." << nline;
280  if ( Configure("./ADF.conf","in") )
281  fLog << warning << "... DONE" << nline;
282  else
283  fLog << warning << "... FAILED" << nline;
284  }
285 
286  // send snapshot of the agent in the log system
287  if ( fLog.IsDebug(2) ) {
289  DoConfigure( fLog() );
291  }
292  fLog << dolog;
293 }
294 
295 AgataConfAgent::AgataConfAgent(const char *comment) :
296 ConfAgent(),
297 fDFAgent(new DFAgent("ADF::ConfAgent","Agata",true,false)),
298 fRunAgent(new RunAgent("ADF::ConfAgent","Run"))
299 {
300  fLog.GetProcessName() = "ADF::AgataConfAgent";
301  SetComment(comment);
302 
303  if ( fDFAgent )
304  AddConf(fDFAgent);
305  if ( fRunAgent )
306  AddConf(fRunAgent);
307 }
308 
310 {
311  if ( fDFAgent ) {
312  delete fDFAgent; fDFAgent = 0x0;
313  }
314  if ( fRunAgent ) {
315  delete fRunAgent; fRunAgent = 0x0;
316  }
317 }
318 
320 {
321  if ( gTheGlobalAgent == 0x0 ) {
322 #ifdef DEBUGSTATIC
323  std::clog << "[[static]] Creating the global ConfAgent ]\n[...\n";
324 #endif
325  // be sure factories global main factories are there
326  // the two main factories are added to the main factory in case it has not yet been done
327  // key factory
328  if ( !MainKeyFactory::theMainFactory().IsKnown("Agata") )
330  // Frame factory
331  if ( !MainFrameFactory::theMainFactory().IsKnown("Agata") )
333 
334  gTheGlobalAgent = new AgataConfAgent();
335 
336 #ifdef DEBUGSTATIC
337  if ( gTheGlobalAgent )
338  std::clog << " done\n...]\n";
339  else
340  { std::clog << " *** ERROR *** \n"; std::clog << "...]\n"; }
341 #endif
342  }
343  return gTheGlobalAgent;
344 
345 }
346 
347 ConfAgent *AgataConfAgent::Clone(const char *comment)
348 {
349  // check the global agent exists
351 
352  // first creats a new agent
353  AgataConfAgent *aconf = new AgataConfAgent(comment);
354  if ( aconf == 0x0 )
355  return 0x0;
356 
357  // now copy the content of the global agent to it using Configure
358  std::ostringstream o;
360  std::string tmp;
361  tmp = o.str();
362  std::istringstream i(tmp);
363  aconf->DoConfigure(i,true); // the new agent is cloned, so reset it first ... even if aconf has just been created ... needed ?
364 
365  return aconf;
366 }
367 
368 Bool_t AgataConfAgent::Configure(ConfigurationFrame *frame, const char *option, Bool_t allow_init)
369 {
370  // set reference to this method for log system
371  fLog.SetProcessMethod("Configure(ConfigurationFrame *name)");
372 
373  Bool_t ok; std::string opt = option;
374 
375  if ( opt == "in" ) {
376  // load the content of the frame in the string
377  frame->Read();
378  // use stream facility
379  std::istringstream in;
380  in.clear();
381  in.str((frame->String()));
382  ok = DoConfigure(in, allow_init);
383  }
384  else {
385  // use stream facility
386  std::ostringstream out;
387 
388  // the global conf frame starts an adf file. if the global conf file is not set properly (too high !)
389  // the reading of the conf frame is cut and this could cause a wrong initialisation of the Agent.
390  // to be safe, add some blank space
391  out << ends << ends << ends << ends << ends << ends << ends << ends << ends << ends << ends << ends;
392  out << ends << ends << ends << ends << ends << ends << ends << ends << "\n";
393  ok = DoConfigure(out);
394  frame->String() = out.str();
395  frame->Write();
396  }
397  fLog << dolog;
398 
399  return ok;
400 }
401 
402 ConfAgent *ConfAgent::theGlobalAgent(std::string which_agent)
403 {
404  if ( gTheGlobalAgent == 0x0 ) {
405 #ifdef DEBUGSTATIC
406  std::clog << "[[static]] Creating the global ConfAgent ]\n[...\n";
407 #endif
408  if ( which_agent == "Agata" ) {
409  gTheGlobalAgent = AgataConfAgent::theGlobalAgent();
410  }
411 #ifdef DEBUGSTATIC
412  if ( gTheGlobalAgent )
413  std::clog << " done\n...]\n";
414  else
415  { std::clog << " *** ERROR *** \n"; std::clog << "...]\n"; }
416 #endif
417  }
418  return gTheGlobalAgent;
419 }
virtual void SetLevel(ELevel lev, unsigned short verbosity=0u)
To get the current level of the message.
static MainFrameFactory & theMainFactory()
the main (global) keyfactory
static void KilltheGlobalAgent()
to kill the global agent
Definition: ConfAgent.cpp:95
DotConf : Utility for class configuration from ascii file or Configuration frames.
Definition: DotConf.h:96
LogMessage & info(LogMessage &)
manipulator to modify the LogMessage
static const Char_t * GetStringModel(ConfAgent::EModel)
Definition: ConfAgent.cpp:78
void SetLastModif()
set in last modif string the current date.
Definition: DotConf.cpp:105
static AgataConfAgent * theGlobalAgent()
Definition: ConfAgent.cpp:319
A configuration frame is just an encapsulation of a string.
void SetComment(const Char_t *comment)
Definition: DotConf.h:233
LogMessage & error(LogMessage &)
ConfAgent * Clone(const char *comment="Clone of the global ConfAgent")
Clone of the main (global) Agent.
Definition: ConfAgent.cpp:347
virtual ~ConfAgent()
Definition: ConfAgent.cpp:51
AgataConfAgent()
used to set global agent
Definition: ConfAgent.cpp:228
The Agata Data Flow agent.
Definition: ConfAgent.cpp:183
static ConfAgent * theGlobalAgent(std::string="Agata")
to get the global agent
Definition: ConfAgent.cpp:402
virtual UInt_t Read()
It reads the content of the string from the Frame.
RunAgent.
Definition: RunAgent.h:43
static ConfAgent::EModel GetModel(std::string)
Definition: ConfAgent.cpp:66
virtual Bool_t Configure(ConfigurationFrame *, const char *option="in", Bool_t allow_init=false)
configuration from/to a configuration frame : extact string from the frame and call DoConfigure ...
Definition: DotConf.cpp:343
ConfAgent.
Definition: ConfAgent.h:63
virtual UInt_t Write()
It writes to the Frame the content of the string.
virtual const DFAgent * GetConstDFAgent() const
Definition: ConfAgent.cpp:209
virtual void SetProcessMethod(const char *)
To set the current method.
bool IsDebug(short debug_to_test) const
to test if the required debug level is lower that the global one
LogMessage & nline(LogMessage &)
LogMessage fLog
the log messenger ... to know the object hae been properly intitialised/modified
Definition: DotConf.h:100
virtual Bool_t DoConfigure(std::istream &, Bool_t allow_init)
configure this from an input stream
virtual ~AgataConfAgent()
destructor
Definition: ConfAgent.cpp:309
header file for DFAgent.cpp
Bool_t Register(KeyFactory *)
Add one factory to this collection of keyfactories: name has to be unique so return false if a factor...
Definition: KeyFactory.cpp:116
A DFAgent contains all the information concerning the structure of the data flow. ...
Definition: DFAgent.h:71
EModel
Model for FrameIO.
Definition: ConfAgent.h:69
virtual RunAgent * GetRunAgent()
to get the run Agent in charge of knowing the conditions of the DF being processed ...
Definition: ConfAgent.cpp:207
ADF::LogMessage & endl(ADF::LogMessage &log)
header file for ConfAgent.cpp
header file for RunAgent.cpp
Bool_t Register(FrameFactory *)
Add one factory to this collection of keyfactories: name has to be unique so return false if a factor...
virtual void AddConf(DotConf *)
add a conf object to this. The added objects are not owned by this so not deleted.
Definition: DotConf.cpp:222
LogMessage & dolog(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
static MainKeyFactory & theMainFactory()
the main (global) keyfactory
Definition: KeyFactory.cpp:150
virtual const RunAgent * GetConstRunAgent() const
Definition: ConfAgent.cpp:211
virtual DFAgent * GetDFAgent()
to get the Data Flow Agent in charge of knowing the structure of the DF
Definition: ConfAgent.cpp:204
std::string GetPathToConfFile()
Definition: ADFConfig.cpp:31
virtual Bool_t Configure(const Char_t *name, const char *option="in", Bool_t allow_init=true)
to avoid some warning of hidden methods
Definition: ConfAgent.cpp:220
void SetWithRecord(Bool_t is_record=true)
to change the behavior
Definition: DotConf.h:230
LogMessage & warning(LogMessage &)