Cat
Application.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/stat.h>
4 #include <errno.h>
5 
6 // Include files
7 #include "Options.h"
8 #include "DLLMgr.h"
9 #include "Hierarchy.h"
10 #include "Element.h"
11 #include "Computer.h"
12 #include "Processus.h"
13 #include "ProcDataBase.h"
14 #include "System.h"
15 #include "Server.h"
16 
17 // local
18 #include "Application.h"
19 
20 //-----------------------------------------------------------------------------
21 // Implementation file for class : Application
22 //
23 // 2006-10-23 :
24 //-----------------------------------------------------------------------------
25 
26 //=============================================================================
27 // Standard constructor, initializes variables
28 //=============================================================================
30  m_options (0),
31  m_dllMgr (0),
32  m_procDb (0),
33  m_computer (0),
34  m_processus(0),
35  m_state (0),
36  m_config (0),
37  m_server (0)
38 {
39  setApplication(this);
40  m_options = new Options ();
45  m_dllMgr = new DLLMgr ();
46  m_procDb = new ProcDataBase ();
47  m_computer = new Computer ();
48  m_state = new State();
49  m_config = new Config();
51 }
52 
53 //=============================================================================
54 // Destructor
55 //=============================================================================
57  if ( 0!=m_server ) {
58  delete m_server;
59  m_server = 0;
60  }
61  delete m_state;
62  delete m_config;
63  delete m_options;
64  delete m_dllMgr;
65  delete m_procDb;
66  // delete m_computer;
67 }
68 
69 //=========================================================================
70 //
71 //=========================================================================
73  setType("SOFTWARE");
75 
76  setName ( hostname() );
77 
78  m_network=false;
79  m_server=0;
80 
81  m_ctrl=std::string("");
82 
83  m_state = new State();
84  m_config = new Config();
85 
86  /*
87  setRunSvc ( *svcRunning );
88  setPlotSvc (*svcPlot );
89  */
90 
91  setState ( "READY" );
92  setConfig( );
93 
94  banner ();
96 
97  return StatusCode::SUCCESS;
98 }
99 
100 //=========================================================================
101 //
102 //=========================================================================
104 #ifdef _WIN32
105  // system("cls");
106 #else
107  // system("clear");
108 #endif
109  msgSvc(0,"","");
110  msgSvc(0," ***********************************************","");
111  msgSvc(0," * C A T *","");
112  msgSvc(0," * *","");
113  msgSvc(0," * |\\-''-/|.___..--''\"`-._ *","");
114  msgSvc(0," * `6_ 6 ) `-. ( ).`-.__.`) *","");
115  msgSvc(0," * (_Y_.)' ._ ) `._ `. ``-..-' *","");
116  msgSvc(0," * _..`--'_..- _/ /--'_.' ,' *","");
117  msgSvc(0," * (il),-'' (li),' ((!.-' *","");
118  msgSvc(0," ***********************************************","");
119  msgSvc(0,"","");
120  msgSvc(0,"Application running on "+
121  m_accountName+"@"+
122  m_hostName+" ("+
123  m_osName+" - "+
124  m_osVersion+")","");
125 }
126 
127 //=========================================================================
128 //
129 //=========================================================================
130 StatusCode Application::run ( std::string procName,
131  Element* element,
132  int nEvent ) {
133  if ( prepare(procName, element, nEvent).isFailure() ) return StatusCode::FAILURE;
135  if (terminate().isFailure()) return StatusCode::FAILURE;
136  return status;
137 }
138 
139 //=========================================================================
140 //
141 //=========================================================================
143  return StatusCode::SUCCESS;
144 }
145 
146 //=========================================================================
147 //
148 //=========================================================================
150  // Load last session parameters
151  std::string filename = System::getEnv("CATPATH");
152  filename.append("/.cathist");
153  std::ifstream last;
154  last.open( filename.c_str() );
155  if ( !last.fail() ) {
156  int nrun;
157  last >> nrun;
158  info("Recovering last run number : "+itos(nrun)+". Will start from "+itos(nrun+1)+".","Application:loadHistoryFile");
159  application()->options()->setRunNumber( nrun+1 );
160  }
161  else {
162  info("No history file found.","Application:loadHistoryFile");
163  return StatusCode::FAILURE;
164  }
165  last.close();
166  return StatusCode::SUCCESS;
167 }
168 
169 //=========================================================================
171  std::string filename = System::getEnv("CATPATH");
172  filename.append("/.cathist");
173  std::ofstream last;
174  last.open( filename.c_str() );
175  if ( last.fail() ) {
176  warning("Cannot do the bookkeeping","Application::bookkeeping");
177  return StatusCode::FAILURE;
178  }
179  else {
180  last << application()->options()->runNumber();
181  }
182  last.close();
183  return StatusCode::SUCCESS;
184 }
185 
186 //=========================================================================
187 //
188 //=========================================================================
189 StatusCode Application::makeDir ( std::string directory ){
190  if (mkdir(directory.c_str(), 0777) == -1) { // Create the directory
191  warning("Could not create directory "+directory+". Error "+strerror(errno),"Application:makeDir");
192  return(StatusCode::FAILURE);
193  }
194  return StatusCode::SUCCESS;
195 }
196 
197 //=========================================================================
198 //
199 //=========================================================================
201  /*
202  info("nRun "+itos(m_state->nRun),"Application::state");
203  info("nEvtMax "+itos(m_state->nEventMax),"Application::state");
204  info("nEvt "+itos(m_state->nEvent),"Application::state");
205  info("nErrors "+itos(m_state->nErrors),"Application::state");
206  info("status "+std::string(m_state->status) ,"Application::state");
207  */
208  return m_state;
209 }
210 
211 //=========================================================================
212 //
213 //=========================================================================
215  return m_config;
216 }
217 
218 //=========================================================================
219 //
220 //=========================================================================
221 void Application::setState (std::string status) {
222  if ( 0 != m_server ) {
227  if (status.compare("")!=0) strcpy(m_state->status,status.c_str());
228  // and ... update !
230  }
231 }
232 
233 //=========================================================================
234 //
235 //=========================================================================
237  if ( 0 != m_server ) {
238  // current element
239  Element* element=dynamic_cast<Element*>(m_current);
240  if (0==element)
241  {
242  warning("Cannot find the Element at the current position of the Hierarchy",
243  "Application::setConfig");
244  }
245  std::string current=m_current->name();
246  current.append("[");
247  current.append(m_current->type());
248  current.append("] ");
249  // strcpy(m_config->currentElement,current.c_str());
250 
251  // reachable processus list
252  std::list<Processus*> listproc = procDb()->procList ( element );
253  std::list<Processus*>::iterator proc;
254  for (proc=listproc.begin(); proc!=listproc.end(); ++proc) {
255  if ( proc!=listproc.begin() ) { current.append(":"); }
256  current.append((*proc)->name());
257  }
258  current.append(" ");
259  // strcpy(m_config->procList,process.c_str());
260 
261  // list of children
262  std::vector<Hierarchy*> list = m_current->children ();
263  std::vector<Hierarchy*>::iterator iter;
264  for (iter=list.begin(); iter!=list.end(); ++iter) {
265  if ( iter!=list.begin() ) { current.append(":"); }
266  current.append((*iter)->name());
267  current.append("[");
268  current.append((*iter)->type());
269  current.append("]");
270  }
271 
272  // finally ...update !
273  strcpy(m_config->config,current.c_str());
275  }
276 }
277 
278 
279 //=========================================================================
280 //
281 //=========================================================================
282 void Application::network ( std::string name = std::string("")) {
283  if ( name.compare("")==0 ) {
284  application()->setName ( name );
285  }
286  if (!m_network) {
287  m_network = true;
288  m_server = new Server ( this );
289  m_server->start();
290  if ( m_network ) {
291  info("Update server information.","Application::network");
292  setConfig ();
293  setState ("READY");
294  }
295  }
296  else {
297  info("Stop network connection.","Application::network");
298  m_network = false;
299  delete m_server;
300  m_server = 0;
301  }
302 }
303 
304 
305 //=========================================================================
306 //
307 //=========================================================================
309  if ( 0 != m_server ) {
310  info("Delete CAT server.","Application::server");
311  delete m_server;
312  m_server = 0;
313  }
314  else {
315  m_server = new Server ( this );
316  m_server->start();
317  info("Update server information.","Application::server");
318  setConfig ();
319  setState ();
320  }
321 }
322 
323 //=========================================================================
324 //
325 //=========================================================================
326 StatusCode Application::create( std::string name, std::string type)
327 {
328  Element *obj = m_dllMgr->createElement(type);
329  if ( !obj ) {
330  warning("Type '" + type + "' not understood");
331  return StatusCode::FAILURE;
332  }
333  obj->setName( name );
334  obj->setParent(m_current);
335  m_current->addChild( obj );
336  setCurrent( obj );
337  return StatusCode::SUCCESS;
338 }
339 
340 //=========================================================================
341 //
342 //=========================================================================
344  while ( m_options->nEvt() < m_options->nEvtMax() ) {
345  if ( loop().isFailure() )
346  {
347  return StatusCode::FAILURE;
348  break;
349  }
350  std::string control = ctrl();
351  if ( 0 == control.compare("pause") ) {
352  setState ( "PAUSE" );
353  warning("Pause.","Application::svcRunning");
354  bool pause = true;
355  while ( pause ) {
356  std::string newctrl = ctrl();
357  int delay = 1000; // delay in loop in ms
358  usleep( 1000 * delay );
359  if ( 0==newctrl.compare ("stop") ) {
360  pause = false;
361  }
362  if ( 0==newctrl.compare ("resume") ) {
363  setState ( "RUNNING" );
364  pause = false;
365  }
366  newctrl.clear();
367  }
368  control.clear();
369  }
370  if ( 0 == control.compare("stop") ) {
371  info("User interruption.","Application::svcRunning");
372  control.clear();
373  return StatusCode::SUCCESS;
374  break;
375  }
376  }
377  return StatusCode::SUCCESS;
378 }
379 
380 
381 //=========================================================================
382 //
383 //=========================================================================
385  warning("No implementation in cmd mode yet.","Application::svcPlot");
386  return StatusCode::FAILURE;
387 }
388 
389 
390 //=========================================================================
391 //
392 //=========================================================================
393 StatusCode Application::prepare ( std::string procName,
394  Element* element,
395  int nEvent ) {
396 
397  verbose("Prepare for a new run.","Application::prepare");
398 
399  if (0==element) element = dynamic_cast<Element*>(m_current);
400 
401  std::list<Processus*> listproc = procDb()->procList ( element );
402  std::list<Processus*>::iterator proc;
403 
404  Processus *process = 0;
405 
406  for (proc=listproc.begin(); proc!=listproc.end(); ++proc) {
407  if ( procName.compare( (*proc)->name() ) == 0 ) {
408  process = (*proc)->clone();
409  //--------------------
410  // Did not chose to make copies : use original processus !
411  // info("orig="+itos((int)(*proc)));
412  // process = (*proc)->clone();
413  // info("orig="+itos((int)(process)));
414  break;
415  }
416  }
417  if ( 0==process) {
418  warning("Processus " + procName + " not found.","Application::prepare");
419  setState ( "READY" );
420  return StatusCode::FAILURE;
421  }
422  if (0!=m_processus) {
423  m_processus->clean();
424  //--------------------
425  // did not chose to make copies
426  // delete m_processus;
427  }
429  m_options->setNErrors ( 0 );
430  m_options->setNEvt ( 0 );
431  // unsigned long currentNEvtMax = m_options->nEvtMax();
432  m_options->setNEvtMax ( nEvent );
433  if (nEvent<0) {
434  m_options->setTimeMax(int(((double)(CLOCKS_PER_SEC))/1.e11*(clock()-nEvent)));
435  // info("Time max is ", itos(m_options->timeMax()));
436  }
437  bookkeeping();
438  if (m_options->dataStorage() || m_options->logStorage()) {
439  if (makeDir(m_options->storageFullPath()).isFailure()) {
440  warning("Could not create storage directory "+m_options->storageFullPath(),"Application::prepare");
441  return StatusCode::FAILURE;
442  }
443  }
444  if (m_options->dataStorage() || m_options->logStorage()) {
447  }
448  m_processus->setElement( element );
449  if ( (m_processus->startProcessing()).isFailure() ) {
450  warning("Program initialization interrupted.","Application::prepare");
451  return StatusCode::FAILURE;
452  }
453  setState ( "RUNNING" );
454  return StatusCode::SUCCESS;
455 }
456 
457 //=========================================================================
458 //
459 //=========================================================================
461  m_options->incNEvt();
462  unsigned long currentNEvt = m_options->nEvt();
463  state()->nEvent++;
464  if( m_options->printFreq()>0) {
465  if ( 0 == currentNEvt % m_options->printFreq() ) {
466  info("[Processing evt "+itos(currentNEvt)+"]","Application::loop");
467  }
468  }
469  if ( (m_processus->execute()).isFailure() ) {
470  warning("An Error occured ["+itos(m_options->nErrors())+"].","Application::loop");
472  if (m_options->stopOnError()) {
473  warning("Program automatically Stopped after " +
474  itos ( currentNEvt ) + " event(s).","Application::loop");
475  // m_options->setNEvt(m_options->nEvtMax());
476  return StatusCode::FAILURE;
477  }
478  }
479  return StatusCode::SUCCESS;
480 }
481 
482 //=========================================================================
483 //
484 //=========================================================================
487  info("Processus " + m_processus->name() +" '" +
488  std::string(m_processus->title().c_str()) +
489  "' completed [" +
490  itos( m_options->nEvt() ) + " events]",
491  "Application::svcRunning");
492  if ( m_options->logStorage()) {
493  m_options->setStream();
494  }
495  // graphSvc();
496  // m_options->setNEvtMax ( currentNEvtMax );
498  m_options->setNEvt ( 0 );
499  setState ( "READY" );
500 #ifdef _NETWORK_
501  m_processus->updateServices();
502 #endif
503  return StatusCode::SUCCESS;
504 }
505 
506 
507 //=============================================================================
void info(std::string mymsg)
Definition: Object.h:38
std::string itos(int)
Definition: Tools.cpp:46
Processus * m_processus
Definition: Application.h:366
void verbose(std::string mymsg)
Definition: Object.h:36
bool isFailure() const
Definition: StatusCode.h:68
Server * m_server
Definition: Application.h:374
void incNErrors()
Definition: Options.h:384
StatusCode svcPlot()
unsigned long nEvt()
Definition: Options.h:317
std::string m_hostName
Definition: Application.h:352
StatusCode create(std::string, std::string)
Application()
Standard constructor.
Definition: Application.cpp:29
StatusCode loop()
void setElement(Element *element)
Definition: Processus.h:217
Definition: Server.h:22
void setNEvt(unsigned long nEvt)
Definition: Options.h:309
int nRun
Definition: Application.h:40
StatusCode endProcessing()
Definition: Processus.cpp:115
Element * createElement(std::string)
Definition: DLLMgr.cpp:128
int nEvtMax()
Definition: Options.h:333
void incRunNumber()
Definition: Options.cpp:74
int nEvent
Definition: Application.h:42
StatusCode prepare(std::string procName, Element *element=0, int nEvt=1)
void setParent(Hierarchy *parent)
Definition: Hierarchy.cpp:67
StatusCode start()
Start Service.
Definition: Server.cpp:98
std::string title()
Definition: Object.h:31
void add(int attribut)
Definition: Attrib.h:67
bool runNumberManagement()
Definition: Options.h:123
std::string m_osName
Definition: Application.h:353
const std::string & osName()
OS name.
Definition: System.cpp:51
void setName(std::string name)
Definition: Object.h:51
std::vector< Hierarchy * > children()
Definition: Hierarchy.h:33
bool stopOnError()
Definition: Options.h:91
Config * m_config
Definition: Application.h:369
StatusCode updateConfig()
Update Config Service.
Definition: Server.cpp:83
unsigned long runNumber()
Definition: Options.h:107
const std::string & osVersion()
OS version.
Definition: System.cpp:64
std::string hostname()
Definition: Application.h:173
int nEventMax
Definition: Application.h:41
std::string m_osVersion
Definition: Application.h:354
unsigned long nErrors()
Definition: Options.h:365
DLLMgr * m_dllMgr
Definition: Application.h:362
void clean()
Definition: Processus.h:85
StatusCode svcRunning()
Config * config()
StatusCode terminate()
Definition: DLLMgr.h:19
StatusCode initialize()
Definition: Application.cpp:72
char config[10000]
Definition: Application.h:49
Application * application()
Definition: Tools.cpp:42
StatusCode run(std::string procName, Element *element=0, int nEvt=1)
bool dataStorage()
Definition: Options.h:252
Options * options()
Definition: Application.h:86
std::list< Processus *> procList(Element *)
void setType(std::string type)
Definition: Object.h:52
virtual Processus * clone()=0
virtual StatusCode execute()=0
void setNEvtMax(int nEvtMax)
Definition: Options.h:325
void setApplication(Application *)
Definition: Tools.cpp:38
std::string m_accountName
Definition: Application.h:351
bool m_network
Definition: Application.h:375
void setState(std::string state=std::string(""))
bool logStorage()
Definition: Options.h:268
std::string storageFile()
Definition: Options.h:200
std::string m_ctrl
Definition: Application.h:376
ProcDataBase * procDb()
Definition: Application.h:111
void setNErrors(unsigned long nErrors)
Definition: Options.h:357
unsigned long printFreq()
Definition: Options.h:58
void network(std::string)
Computer * m_computer
Definition: Application.h:364
ProcDataBase * m_procDb
Definition: Application.h:363
State * state()
StatusCode finalize()
void setStorageFile(std::string filename=std::string(""))
Definition: Options.h:188
StatusCode updateState()
Update State Service.
Definition: Server.cpp:68
StatusCode bookkeeping()
std::string storageFullPath()
Definition: Options.h:159
StatusCode makeDir(std::string)
std::string name() const
Definition: Object.h:28
void setRunNumber(unsigned long runNumber)
Definition: Options.h:99
std::string ctrl()
Definition: Application.h:314
void setStream(std::string file=std::string(""))
Definition: Options.h:400
Hierarchy * current()
Definition: Application.h:149
StatusCode startProcessing()
Definition: Processus.cpp:93
Options * m_options
Definition: Application.h:361
virtual void addChild(Hierarchy *element)
Definition: Hierarchy.cpp:83
StatusCode loadHistoryFile()
void msgSvc(int level, std::string msg, std::string name)
Definition: Object.h:33
Hierarchy * m_current
Definition: Application.h:358
std::string type()
Definition: Object.h:29
void setCurrent(Hierarchy *current)
Definition: Application.h:141
def process(object)
Definition: shell.py:139
Definition: proc.py:1
void setTimeMax(unsigned long timeMax)
Definition: Options.h:341
void setConfig()
std::string storage()
Definition: Processus.h:205
void warning(std::string mymsg)
Definition: Object.h:39
int nErrors
Definition: Application.h:43
virtual ~Application()
Destructor.
Definition: Application.cpp:56
def obj()
Definition: shell.py:26
const std::string & accountName()
User login name.
Definition: System.cpp:88
char * status()
Definition: Application.h:220
char status[16]
Definition: Application.h:44
State * m_state
Definition: Application.h:368
const std::string getEnv(const char *var)
get a particular environment variable
Definition: System.cpp:143
void incNEvt()
Definition: Options.h:377
const std::string & hostName()
Host name.
Definition: System.cpp:39