GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseEmulator.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004-2006 by Olivier Stezowski & Christian Finck *
3  * stezow(AT)ipnl.in2p3.fr, cfinck(AT)ires.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 Gw_BaseEmulator
24 #define Gw_BaseEmulator
25 
26 #include <vector>
27 #include <sstream>
28 
29 #include "ALoadedActor.h"
30 #include "AnActorConnection.h"
31 #include "NarvalInterface.h"
32 #include "DataHandling.h"
33 
34 #include "GwLogMessage.h"
35 
36 namespace Gw {
37 
39 
62 {
63 private:
64  LogMessage fLog;
65 private:
67  std::string fName;
68 protected:
70  std::vector < ANamedItem * > fGlobals;
71 protected:
73  std::vector < ALoadedActor * > fAllActors;
74 protected:
76  std::vector < AnActorConnection * > fAllConnections;
77 protected:
79  std::vector < std::vector< ALoadedActor * > > fTopology;
80 
81 protected:
83  template <class T_item>
84  NamedItem<T_item> *GetItem(const Char_t *name, std::vector <ANamedItem *> &v)
85  {
86  NamedItem<T_item> *item = 0x0;
87 
88  for (size_t i = 0; i < v.size(); i++) {
89  if ( v[i]->GetName().compare(name) == 0 ) {
90  item = dynamic_cast< NamedItem<T_item> * > (v[i]);
91  break;
92  }
93  }
94  return item;
95  }
96 
97 protected:
99  virtual void LinkActors(ALoadedActor *left, ALoadedActor *middle, ALoadedActor *right) = 0;
100 
102 
105  template <class T_loader, class T_actor>
106  T_loader *AddLoader(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right, const Char_t *conf_dir, ConfigFunction f_config)
107  {
108  T_loader *new_node = 0x0; ALoadedActor *input_node = 0x0, *output_node = 0x0;
109 
110  // check whether the name has already been used ... an actor is unique by name !
111  if ( GetLoaderByName(name_new_actor) ) {
112  std::cout << " " << name_new_actor << " already in the topology, name has to be unique " << std::endl;
113  return new_node;
114  }
115  // try and creat a loaded actor
116  new_node = new T_loader( name_new_actor );
117  if ( new_node == 0x0 ) {
118  return new_node;
119  }
120  else {
121  // add it to the list of loaded actors in the emulator
122  fAllActors.push_back(new_node);
123  // the loaded actor inherits from all the current globals
124  new_node->SetGlobals( fGlobals );
125  // and change the one to be changed here
126  new_node->SetGlobal( NamedItem<std::string>("ConfDir",std::string(conf_dir)) );
127  // Set configurator
128  AConfigurator *configurator =
129  new Configurator<T_actor>(new_node,f_config);
130  new_node->SetConfigurator(configurator);
131  }
132 
133  // look for left and right
134  input_node = GetLoaderByName(name_left); output_node = GetLoaderByName(name_right);
135  // and link them
136  LinkActors(input_node, new_node, output_node);
137 
138  return new_node;
139  /*
140  // check if it can be added i.e. allocated and connected with the network, input and/or output accept
141  new_node = new T_loader( name_new_actor, new T_actor() );
142  if ( new_node ) {
143  if ( new_node->GetActor() == 0x0 )
144  return new_actor;
145  }
146  else return new_actor;
147 
148  // now connect the new actor to the network
149  new_actor = dynamic_cast<T_actor *>(new_node->GetActor());
150 
151  std::cout << "[+] Trying to add " << name_new_actor << " to the topology " << std::endl;
152 
153  // to be done : check with the max # of input output
154  Bool_t is_link_possible = true;
155  if ( input_node ) {
156  if ( (input_node->GetNbOutput()+1) > input_node->GetActor()->GetMaxOutput() ) { // cannot add a link with input
157  is_link_possible = false;
158  std::cout << new_node->GetName()
159  << " cannot have "
160  << input_node->GetName()
161  << " as input since all its output channels are occupied : "
162  << (input_node->GetNbOutput()+1)
163  << " asked, "
164  << input_node->GetActor()->GetMaxOutput() << " available " << std::endl;
165  }
166  if ( new_node->GetActor()->GetMaxInput() < 1 ) { // this is a producer ! cannot have a input
167  is_link_possible = false;
168  std::cout << new_node->GetName()
169  << " cannot be linked as output of "
170  << input_node->GetName()
171  << " , it is probably a producer !"
172  << std::endl;
173  }
174  }
175  else {
176  // std::cout << " Cannot find actor with name " << name_left << " in the topology, ==> try to add new actor " << name_new_actor << " without input " << std::endl;
177  }
178  if ( output_node ) {
179  if ( (output_node->GetNbInput()+1) > output_node->GetActor()->GetMaxInput() ) { // cannot add a link with input
180  is_link_possible = false;
181  std::cout << new_node->GetName()
182  << " cannot have "
183  << output_node->GetName()
184  << " as output since all its input channels are occupied : "
185  << (output_node->GetNbInput()+1)
186  << " asked, "
187  << output_node->GetActor()->GetMaxInput() << " available " << std::endl;
188  }
189  if ( new_node->GetActor()->GetMaxOutput() < 1 ) { // cannot add a link with new node
190  is_link_possible = false;
191  std::cout << new_node->GetName()
192  << " cannot be linked as input of "
193  << output_node->GetName()
194  << " , it is probably a consumer !"
195  << std::endl;
196  }
197  }
198  else {
199  // std::cout << " Cannot find actor with name " << name_right << " in the topology, ==> try to add new actor " << name_new_actor << " without output " << std::endl;
200  }
201  if ( !is_link_possible ) {
202  std::cout << "[-] Trying to add " << name_new_actor << " to the topology : IMPOSSIBLE !! " << std::endl;
203 
204  delete new_node;
205  return 0x0;
206  }
207 
208  // the new actor is filter, consumer or dispatcher
209  if ( input_node ) {
210 
211  fAllActors.push_back(new_node);
212 
213  // check nb input/output
214  std::cout << new_node->GetName() << " has for input node " << input_node->GetName() << std::endl;
215  //
216  LinkActors(input_node, new_node, buffer_size_left);
217  if ( output_node ) {
218  //
219  std::cout << new_node->GetName() << " has for output node " << output_node->GetName() << std::endl;
220  //
221  LinkActors(new_node, output_node, buffer_size_right);
222  }
223  }
224  else { // the new actor is a producer ! ...
225 
226  fAllActors.push_back(new_node);
227  if ( output_node ) {
228  std::cout << "Consumer " << new_node->GetName() << " has for output node " << output_node->GetName() << std::endl;
229  LinkActors(new_node, output_node, buffer_size_right);
230  }
231  else std::cout << "Consumer " << new_node->GetName() << " added to the emulator " << std::endl;
232  }
233  std::cout << "[-] Trying to add " << name_new_actor << " to the topology " << std::endl;
234  // return new actor so that it can be modified in case particular methods should be called
235  return new_actor;
236  */
237  }
238  /*
239  template <class T_actor,class T_loader>
240  T_actor *Add(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right = "-",
241  const Char_t *conf_path = "./", UInt_t buffer_size_left = 100u, UInt_t buffer_size_right = 100u)
242  {
243  ALoadedActor *new_node = 0x0, *input_node = 0x0, *output_node = 0x0; T_actor * new_actor = 0x0;
244 
245  // check whether the name has already been used ... an actor is unique by name !
246  if ( GetLoaderByName(name_new_actor) ) {
247  std::cout << " " << name_new_actor << " already in the topology, name has to be unique " << std::endl;
248  return new_actor;
249  }
250  // look for left and right
251  //
252  input_node = GetLoaderByName(name_left);
253  //
254  output_node = GetLoaderByName(name_right);
255 
256  // check if it can be added i.e. allocated and connected with the network, input and/or output accept
257  new_node = new T_loader( name_new_actor, new T_actor() );
258  if ( new_node ) {
259  if ( new_node->GetActor() == 0x0 )
260  return new_actor;
261  }
262  else return new_actor;
263 
264  // now connect the new actor to the network
265  new_actor = dynamic_cast<T_actor *>(new_node->GetActor());
266 
267  std::cout << "[+] Trying to add " << name_new_actor << " to the topology " << std::endl;
268 
269  // to be done : check with the max # of input output
270  Bool_t is_link_possible = true;
271  if ( input_node ) {
272  if ( (input_node->GetNbOutput()+1) > input_node->GetActor()->GetMaxOutput() ) { // cannot add a link with input
273  is_link_possible = false;
274  std::cout << new_node->GetName()
275  << " cannot have "
276  << input_node->GetName()
277  << " as input since all its output channels are occupied : "
278  << (input_node->GetNbOutput()+1)
279  << " asked, "
280  << input_node->GetActor()->GetMaxOutput() << " available " << std::endl;
281  }
282  if ( new_node->GetActor()->GetMaxInput() < 1 ) { // this is a producer ! cannot have a input
283  is_link_possible = false;
284  std::cout << new_node->GetName()
285  << " cannot be linked as output of "
286  << input_node->GetName()
287  << " , it is probably a producer !"
288  << std::endl;
289  }
290  }
291  else {
292  // std::cout << " Cannot find actor with name " << name_left << " in the topology, ==> try to add new actor " << name_new_actor << " without input " << std::endl;
293  }
294  if ( output_node ) {
295  if ( (output_node->GetNbInput()+1) > output_node->GetActor()->GetMaxInput() ) { // cannot add a link with input
296  is_link_possible = false;
297  std::cout << new_node->GetName()
298  << " cannot have "
299  << output_node->GetName()
300  << " as output since all its input channels are occupied : "
301  << (output_node->GetNbInput()+1)
302  << " asked, "
303  << output_node->GetActor()->GetMaxInput() << " available " << std::endl;
304  }
305  if ( new_node->GetActor()->GetMaxOutput() < 1 ) { // cannot add a link with new node
306  is_link_possible = false;
307  std::cout << new_node->GetName()
308  << " cannot be linked as input of "
309  << output_node->GetName()
310  << " , it is probably a consumer !"
311  << std::endl;
312  }
313  }
314  else {
315  // std::cout << " Cannot find actor with name " << name_right << " in the topology, ==> try to add new actor " << name_new_actor << " without output " << std::endl;
316  }
317  if ( !is_link_possible ) {
318  std::cout << "[-] Trying to add " << name_new_actor << " to the topology : IMPOSSIBLE !! " << std::endl;
319 
320  delete new_node;
321  return 0x0;
322  }
323 
324  // the new actor is filter, consumer or dispatcher
325  if ( input_node ) {
326 
327  fAllActors.push_back(new_node);
328 
329  // check nb input/output
330  std::cout << new_node->GetName() << " has for input node " << input_node->GetName() << std::endl;
331  //
332  LinkActors(input_node, new_node, buffer_size_left);
333  if ( output_node ) {
334  //
335  std::cout << new_node->GetName() << " has for output node " << output_node->GetName() << std::endl;
336  //
337  LinkActors(new_node, output_node, buffer_size_right);
338  }
339  }
340  else { // the new actor is a producer ! ...
341 
342  fAllActors.push_back(new_node);
343  if ( output_node ) {
344  std::cout << "Consumer " << new_node->GetName() << " has for output node " << output_node->GetName() << std::endl;
345  LinkActors(new_node, output_node, buffer_size_right);
346  }
347  else std::cout << "Consumer " << new_node->GetName() << " added to the emulator " << std::endl;
348  }
349  std::cout << "[-] Trying to add " << name_new_actor << " to the topology " << std::endl;
350  // return new actor so that it can be modified in case particular methods should be called
351  return new_actor;
352  }
353  */
354 
355 public:
357  void BuildTopology();
358 
359 public:
360  BaseEmulator(std::string name);
361  virtual ~BaseEmulator();
362 
364 
371  virtual void SetGlobal( const ANamedItem & );
372  // virtual void SetGlobalParameter( const ANamedItem & );
373 
375  std::string &GetName()
376  {
377  return fName;
378  }
380  virtual ALoadedActor *GetLoaderByName(const Char_t *) const;
382  virtual NarvalInterface *GetActorByName(const Char_t *) const;
384  template <class T_actor>
385  T_actor *GetActorByName(const Char_t *name) const
386  {
387  NarvalInterface *actor = GetActorByName(name); T_actor *t_actor= 0x0;
388  if( actor ) {
389  t_actor = dynamic_cast<T_actor *> (actor);
390  }
391  return t_actor;
392  }
393 
395 
406  virtual void Init( const NamedItem<std::string> &init_sequence = NamedItem<std::string>("InitSequence","config load init") );
407 
409  virtual void Run(Int_t max_loop = -1 /*, Bool_t is_interrupt_on ... or some kind of option*/);
410 
412  virtual void PrintTopology();
413 
415  virtual void Draw()
416  { PrintTopology(); }
417 };
418 
419 } // end namespace
420 
421 #endif
422 
423 
virtual void Draw()
Draw ... default is print.
Definition: BaseEmulator.h:415
std::vector< ANamedItem * > fGlobals
to configure the emulator. The name of each item should be unique !
Definition: BaseEmulator.h:70
void(* ConfigFunction)(const Char_t *, UInt_t *)
the function used to configure the underlying actor. Added to allow configuration by calling explicit...
Definition: ALoadedActor.h:35
virtual void SetGlobal(const ANamedItem &)
This method allows to modified some options of the emulator.
std::vector< std::vector< ALoadedActor * > > fTopology
Actors are memorized depending on their depth in the data processing chain.
Definition: BaseEmulator.h:79
std::string & GetName()
Name of the emulator.
Definition: BaseEmulator.h:375
header file for AnActorConnection.cpp
T_actor * GetActorByName(const Char_t *name) const
search for an actor in the emulator
Definition: BaseEmulator.h:385
header file for ALoadedActor.cpp
virtual void Run(Int_t max_loop=-1)
Run max loop.
Base class for a Log message.
Definition: GwLogMessage.h:94
T_loader * AddLoader(const Char_t *name_left, const Char_t *name_new_actor, const Char_t *name_right, const Char_t *conf_dir, ConfigFunction f_config)
Actors are memorized depending on their depth in the data processing chain.
Definition: BaseEmulator.h:106
BaseEmulator(std::string name)
std::vector< AnActorConnection * > fAllConnections
list of all connections. Owned by the emulator thus delete by this
Definition: BaseEmulator.h:76
header file for GwLogMessage.cpp
virtual void PrintTopology()
print topology
virtual ~BaseEmulator()
An actor looded in an emulator.
Definition: ALoadedActor.h:88
virtual void Init(const NamedItem< std::string > &init_sequence=NamedItem< std::string >("InitSequence","config load init"))
Init the emulator. To be called before calling Run.
ANamedItem.
Definition: DataHandling.h:188
header file for DataHandling.cpp
NamedItem< T_item > * GetItem(const Char_t *name, std::vector< ANamedItem * > &v)
to get back an item from its name
Definition: BaseEmulator.h:84
ADF::LogMessage & endl(ADF::LogMessage &log)
Base class for an emulator.
Definition: BaseEmulator.h:61
virtual ALoadedActor * GetLoaderByName(const Char_t *) const
search for a node in the emulator
header file for NarvalInterface.cpp
void BuildTopology()
Built the map, starting from producer is calculates the depth of each actor in the data flow...
std::vector< ALoadedActor * > fAllActors
list of all actors to start building the map and to delete them @ the end
Definition: BaseEmulator.h:73
It defines the general interface needed to be a narval actor.
virtual void LinkActors(ALoadedActor *left, ALoadedActor *middle, ALoadedActor *right)=0
link middle (new actor in the emulator) with left and right (could be null) ... the type of connecion...
base class in charge of configurating, allocating the true actor loaded in a LoadedActor ...
Definition: ALoadedActor.h:43
virtual NarvalInterface * GetActorByName(const Char_t *) const
search for an actor in the emulator