GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NarvalInterface.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_NarvalInterface
24 #include "NarvalInterface.h"
25 #endif
26 
27 #ifndef ADF_FrameIO
28 #include "FrameIO.h"
29 #endif
30 
31 #ifndef ADF_KeyFactory
32 #include "KeyFactory.h"
33 #endif
34 
35 #ifndef ADF_FrameFactory
36 #include "FrameFactory.h"
37 #endif
38 
39 #include <iostream>
40 #include <fstream>
41 #include <cstdlib>
42 
43 #if HAVE_NARVAL_H == 1
44 // function to access narval paramerters
45 extern "C" {
46  void ada_add_new_parameter(const char *,int *);
47  // void ada_get_parameter("c_param2", buffer, 20, &loc_error_code);
48  void ada_get_parameter(const char *, const char *, int, int *);
49  // ada_set_parameter ("c_param2", buffer, &loc_error_code);
50  void ada_set_parameter(const char *, const char *,int *);
51 } // extern C
52 // used to exchange parameter informations with narval
53 namespace {
54  const UShort_t gMaxSizeBufferNarvalParameters = 512;
55  Char_t gBufferNarvalParameters[gMaxSizeBufferNarvalParameters];
56 }
57 #endif
58 
59 using namespace ADF;
60 
61 /*
62  template<typename Data_T> class NarvalParameter : public NamedItem<Data_T>
63  {
64  const Int_t gDefaultMaxSize = 20;
65 
66  private:
67  mutable Int_t fLastError;
68  mutable Int_t fMaxSize;
69 
70  mutable Int_t fBuffer;
71 
72  public:
73  NarvalParameter(const char *name, Data_T d = 0, Int_t max_size = gDefaultMaxSize):
74  NamedItem<Data_T>(name,d), fLastError(0), fMaxSize(0), fBuffer(0x0) { SetMaxSize(max_size); }
75  virtual ~NarvalParameter()
76  { SetMaxSize(0); // delete fBuffer }
77 
78  void SetMaxSize(Int_t max_size)
79  {
80  fMaxSize = max_size;
81  if ( fBuffer )
82  delete [] fBuffer;
83  fBuffer = new Char_t [fMaxSize];
84  }
85  virtual void Set(Data_T t)
86  {
87  fLastError = 0;
88  {
89  std::ostringstream oss;
90  oss << t;
91  ada_set_parameter (NamedItem<Data_T>::Name().data(), oss.str().c_str(), &fLastError);
92  }
93  }
94 
96  virtual Data_T Get() const
97  {
98  Data_T t;
99 
100  fLastError = 0;
101  ada_get_parameter (NamedItem<Data_T>::GetName().data(), fBuffer, fMaxSize, &fLastError);
102 
103  if ( fLastError == 0 ) {
104  std::istringstream iss(fBuffer);
105  iss >> t;
106  }
107 
108  return t;
109  }
110  };
111  */
112 
113 //------------------------------------------------------------------------------------
114 
115 // default local for configuration path
116 std::string NarvalInterface::G::ConfPath = "./";
117 // to init process id
118 UInt_t NarvalInterface::G::ProcessID = 0u;
119 
121 fProcessID(0u),
122 fConfPath(),
123 fVersion(),
124 Log("NarvalInterface")
125 {
126  fConfPath = G::ConfPath;
127  fProcessID = G::ProcessID++;
128  Log.SetPID(fProcessID);
129 }
131 {
132  ;
133 }
134 
135 #if HAVE_NARVAL_H == 1
136 Int_t NarvalInterface::GetGlobalParameter(const char *name, UShort_t &val)
137 #else
138 Int_t NarvalInterface::GetGlobalParameter(const char * /*name*/, UShort_t & /*val*/)
139 #endif
140 {
141 #if HAVE_NARVAL_H == 1
142  // should be locked ?
143  UShort_t t;
144 
145  Int_t err = 0;
146  ada_get_parameter (name, gBufferNarvalParameters, gMaxSizeBufferNarvalParameters, &err);
147  if ( err == 0 ) {
148  std::istringstream iss(gBufferNarvalParameters);
149  iss >> t;
150  if ( iss.fail() )
151  err = 2;
152  else
153  val = t;
154  }
155  return err;
156 #else
157  return -1;
158 #endif
159 }
160 
161 #if HAVE_NARVAL_H == 1
162 Int_t NarvalInterface::SetGlobalParameter(const char *name, const UShort_t &val)
163 #else
164 Int_t NarvalInterface::SetGlobalParameter(const char * /*name*/, const UShort_t & /*val*/)
165 #endif
166 {
167 #if HAVE_NARVAL_H == 1
168  // should be locked ?
169  Int_t err = 0;
170  {
171  std::ostringstream oss1;
172  oss1 << val;
173  ada_set_parameter (name, oss1.str().c_str(), &err);
174  if ( err == 1 ) { // unknown parameter, try to add a new parameter
175  std::ostringstream oss2;
176  oss2 << name << ";unsigned_8_type;read_write;" << val;
177  // send the request to narval
178  ada_add_new_parameter (oss2.str().c_str(),&err);
179  }
180  }
181  return err;
182 #else
183  return -1;
184 #endif
185 }
186 
187 #if HAVE_NARVAL_H == 1
188 Int_t NarvalInterface::GetGlobalParameter(const char *name, Int_t &val)
189 #else
190 Int_t NarvalInterface::GetGlobalParameter(const char * /*name*/, Int_t & /*val*/)
191 #endif
192 {
193 #if HAVE_NARVAL_H == 1
194  // should be locked ?
195  Int_t err = 0;
196  {
197  std::ostringstream oss1;
198  oss1 << val;
199  ada_set_parameter (name, oss1.str().c_str(), &err);
200  if ( err == 1 ) { // unknown parameter, try to add a new parameter
201  std::ostringstream oss2;
202  oss2 << name << ";integer_type;read_write;" << val;
203  // send the request to narval
204  ada_add_new_parameter (oss2.str().c_str(),&err);
205  }
206  }
207  return err;
208 #else
209  return -1;
210 #endif
211 }
212 
213 #if HAVE_NARVAL_H == 1
214 Int_t NarvalInterface::GetGlobalParameter(const char *name, std::string &val)
215 #else
216 Int_t NarvalInterface::GetGlobalParameter(const char * /*name*/, std::string & /*val*/)
217 #endif
218 {
219 #if HAVE_NARVAL_H == 1
220  // should be locked ?
221  std::string t;
222 
223  Int_t err = 0;
224  ada_get_parameter (name, gBufferNarvalParameters, gMaxSizeBufferNarvalParameters, &err);
225  if ( err == 0 ) {
226  std::istringstream iss(gBufferNarvalParameters);
227  iss >> t;
228  if ( iss.fail() )
229  err = 2;
230  else
231  val = t;
232  }
233  return err;
234 #else
235  return -1;
236 #endif
237 }
238 
239 #if HAVE_NARVAL_H == 1
240 Int_t NarvalInterface::SetGlobalParameter(const char *name, const std::string &val)
241 #else
242 Int_t NarvalInterface::SetGlobalParameter(const char * /*name*/, const std::string & /*val*/)
243 #endif
244 {
245 #if HAVE_NARVAL_H == 1
246  // should be locked ?
247  Int_t err = 0;
248  {
249  std::ostringstream oss1;
250  oss1 << val;
251  ada_set_parameter (name, oss1.str().c_str(), &err);
252  if ( err == 1 ) { // unknown parameter, try to add a new parameter
253  std::ostringstream oss2;
254  oss2 << name << ";string_type;read_write;" << val;
255  // send the request to narval
256  ada_add_new_parameter (oss2.str().c_str(),&err);
257  }
258  }
259  return err;
260 #else
261  return -1;
262 #endif
263 }
264 
266 {
267  return ConfAgent::theGlobalAgent();
268 }
269 
270 void NarvalInterface::process_config (const Char_t *directory, UInt_t *error_code, Short_t do_adf_conf)
271 {
272  // just in case
273  *error_code = 0u;
274 
275  // add / at the end of path in case it is not there
276  std::string tmp = directory;
277  if ( tmp.size() > 0 && !(tmp.at(tmp.size()-1) == '/') )
278  tmp += '/';
279 
280  // keep the path in a static variable in case this is needed in Initialise
281  G::ConfPath = tmp;
282 
283  // now try to open an ADF configuration file (if required) and
284  // configure/reconfigure (or add information) to the global agent
285  switch( do_adf_conf ){
286  case 0:
287  break;
288  case 1:
289  if ( ! ::getenv("ADF_CONF_PATH") ) {
290  tmp += "ADF.conf";
291  ConfAgent::theGlobalAgent()->Configure(tmp.data(),"in");
292  //Log.GetProcessName() = "NarvalInterface::process_config(..,..,1)";
293  //Log << info << " ADF Reconfiguration with " << tmp.data() << dolog;
294  }
295  //else {
296  // Log.GetProcessName() = "NarvalInterface::process_config(..,..,1)";
297  // Log << info << " No need to reconfigure ADF " << dolog;
298  //}
299  break;
300  case 2:
301  tmp += "ADF.conf";
302  ConfAgent::theGlobalAgent()->Configure(tmp.data(),"in");
303  //Log.GetProcessName() = "NarvalInterface::process_config(..,..,2)";
304  //if ( ::getenv("ADF_CONF_PATH") )
305  // Log << warning << " ADF Reconfiguration with " << tmp.data() << dolog;
306  //else
307  // Log << info << " ADF Reconfiguration with " << tmp.data() << dolog;
308  break;
309  default:
310  break;
311  }
312 }
313 
314 
315 //------------------------------------------------------------------------------------
317  NarvalInterface(),
318  fAttachedOutputBlock(0x0),
319  fOneBlockOut(1u,ConfAgent::kWrite),
320  fFrameIO("FrameIO attached to NarvalProducer")
321 {
322  Log.GetProcessName() = "NarvalProducer";
323 }
325 {
326  ;
327 }
328 void NarvalProducer::process_config (const Char_t *directory, UInt_t *error_code)
329 {
330  NarvalInterface::process_config(directory,error_code);
331 
332  if ( (*error_code) == 0u ) {
333  // do some additionnal stuff for a producer ?
334  }
335 }
337 {
338  Log.SetProcessMethod("ProcessBlocks()");
339 
340  if ( fAttachedOutputBlock == 0x0 || fAttachedOutputBlock->IsOwner(this) == false ) {
341  Log << warning << " No output defined or not owner ... so no process ... so not error ! ==> error = 0u. Block address is " << fAttachedOutputBlock << dolog;
342  return 0u;
343  }
344  // do the process
345  Bool_t is_rewind = GetFrameIO().DoRewind(false); UInt_t error_code;
346  //
347  error_code =
348  ProcessBlock( (*fAttachedOutputBlock ) );
349  //
350  GetFrameIO().DoRewind(is_rewind);
351  //
352  if ( fAttachedOutputBlock->IsEoB() ) {
353  fAttachedOutputBlock->SetOwner(0x0);
354  }
355 
356  Log << dolog;
357 
358  return error_code;
359 
360 }
361 void NarvalProducer::process_block ( void *output_buffer,
362  UInt_t size_of_output_buffer,
363  UInt_t *used_size_of_output_buffer,
364  UInt_t *error_code)
365 {
366  fOneBlockOut.SetBlock((Char_t *)output_buffer,size_of_output_buffer);
367  *used_size_of_output_buffer = 0;
368 
369  (*error_code) = ProcessBlock(fOneBlockOut);
370  (*used_size_of_output_buffer) = UInt_t(fOneBlockOut.GetSize());
371 }
372 
373 //------------------------------------------------------------------------------------
375  NarvalInterface(),
376  fAttachedInputBlock(0x0),
377  fOneBlockIn(1u,ConfAgent::kRead),
378  fFrameIO("FrameIO attached to NarvalConsumer")
379 {
380  Log.GetProcessName() = "NarvalConsumer";
381 }
382 
384 {
385  ;
386 }
387 
389 {
390  Log.SetProcessMethod("ProcessBlocks()");
391 
392  if ( fAttachedInputBlock == 0x0 || fAttachedInputBlock->IsOwner(this) == false ) {
393  Log << warning << " No input defined or not owner ... so no process ... so not error ! ==> error = 0u. Block address is " << fAttachedInputBlock << dolog;
394  return 0u;
395  }
396  // do the process
397  Bool_t is_rewind = GetFrameIO().DoRewind(false); UInt_t error_code;
398  //
399  error_code =
400  ProcessBlock( (*fAttachedInputBlock ) );
401  //
402  GetFrameIO().DoRewind(is_rewind);
403  //
404  if ( fAttachedInputBlock->IsEoB() ) {
405  fAttachedInputBlock->SetOwner(0x0);
406  }
407 
408  Log << dolog;
409 
410  return error_code;
411 }
412 
413 void NarvalConsumer::process_config (const Char_t *directory, UInt_t *error_code)
414 {
415  NarvalInterface::process_config(directory,error_code);
416 
417  if ( (*error_code) == 0u ) {
418  // do some additionnal stuff for a consumer ?
419  }
420 }
421 
422 void NarvalConsumer::process_block (void *input_buffer,
423  UInt_t size_of_input_buffer,
424  UInt_t *error_code)
425 {
426  fOneBlockIn.SetBlock((Char_t *)input_buffer,size_of_input_buffer);
427 
428  (*error_code) = ProcessBlock(fOneBlockIn);
429 }
430 
431 //------------------------------------------------------------------------------------
432 
435 {
436  Log.GetProcessName() = "NarvalShunter";
437 }
439 {
440  ;
441 }
442 
443 //------------------------------------------------------------------------------------
445  NarvalInterface(),
446  fAttachedInputBlock(0x0),
447  fAttachedOutputBlock(0x0),
448  fOneBlockIn(1u,ConfAgent::kRead),
449  fOneBlockOut(1u,ConfAgent::kWrite),
450  fFrameIO("FrameIO attached to NarvalFilter")
451 {
452  Log.GetProcessName() = "NarvalFilter";
453 
454  // Default ... to be sure if there were no triggers evrything
455  // is going through the data flow.
457 }
459 {
460  ;
461 }
462 void NarvalFilter::process_config (const Char_t *directory, UInt_t *error_code)
463 {
464  NarvalInterface::process_config(directory,error_code);
465 
466  if ( (*error_code) == 0u ) {
467  // do some additionnal stuff for a filter ?
468  }
469 }
470 void NarvalFilter::process_reset (UInt_t *error_code)
471 {
472  *error_code = 0u;
473 
474  Log.ClearMessage(); Log.SetProcessMethod("process_reset");
475  Log << hline << nline ;
476  GetFrameIO().Print( Log() );
477  Log << hline << dolog;
478 }
479 
481 {
482  Log.SetProcessMethod("ProcessBlocks()");
483 
484  if ( fAttachedInputBlock == 0x0 || fAttachedInputBlock->IsOwner(this) == false ) {
485  Log << warning << " No input defined or not owner ... so no process ... so not error ! ==> error = 0u. Block address is " << fAttachedInputBlock << dolog;
486  return 0u;
487  }
488  else {
489  if ( fAttachedOutputBlock == 0x0 || fAttachedOutputBlock->IsOwner(this) == false ) {
490  Log << warning << " No output defined or not owner ... so no process ... so not error ! ==> error = 0u. Block address is " << fAttachedOutputBlock << dolog;
491  return 0u;
492  }
493  }
494 
495  // do the process
496  Bool_t is_rewind = GetFrameIO().DoRewind(false); UInt_t error_code;
497  //
498  error_code =
499  ProcessBlock( (*fAttachedInputBlock ), (*fAttachedOutputBlock ) );
500  //
501  GetFrameIO().DoRewind(is_rewind);
502  //
503  if ( fAttachedInputBlock->IsEoB() ) {
504  fAttachedInputBlock->SetOwner(0x0);
505  }
506  if ( fAttachedOutputBlock->IsEoB() ) {
507  fAttachedOutputBlock->SetOwner(0x0);
508  }
509 
510  Log << dolog;
511 
512  return error_code;
513 }
514 
515 
516 void NarvalFilter::process_block (void *input_buffer,
517  UInt_t size_of_input_buffer,
518  void *output_buffer,
519  UInt_t size_of_output_buffer,
520  UInt_t *used_size_of_output_buffer,
521  UInt_t *error_code)
522 {
523  fOneBlockIn.SetBlock((Char_t *)input_buffer,size_of_input_buffer);
524  fOneBlockOut.SetBlock((Char_t *)output_buffer,size_of_output_buffer);
525  *used_size_of_output_buffer = 0;
526 
527  (*error_code) = ProcessBlock(fOneBlockIn, fOneBlockOut);
528  (*used_size_of_output_buffer) = UInt_t(fOneBlockOut.GetSize());
529 }
virtual void * SetOwner(void *owner=0x0)
Name of the block ... used to distinguish several blocks in a topology.
Definition: FrameBlock.h:104
virtual void process_reset(UInt_t *error_code)
To reset the internal values (real destructor)
virtual Bool_t SetBlock(Char_t *=0x0, UInt_t=0u, char= 'r', char= 'l', UInt_t=0u)
Attach a external block to this.
Definition: FrameBlock.cpp:51
static void process_config(const Char_t *, UInt_t *, Short_t do_adf_conf=0)
Have to be overwritten and called in your implementation.
header file for KeyFactory.cpp
Bool_t DoRewind(Bool_t rewind=true)
if set true, rewind is done once a block is attached. Return the previous value
Definition: FrameIO.h:202
static void process_config(const Char_t *, UInt_t *)
Have to be overwritten and called in your implementation.
virtual UInt_t ProcessBlocks()
process blocks attached by AttachInput/AttachOutput. No need to overwrite it if ProcessBlock(FrameBlo...
std::ostream & hline(std::ostream &)
virtual void process_block(void *input_buffer, UInt_t size_of_input_buffer, UInt_t *error_code)
Narval interface, Ask the algorithm to process the input data block.
static void process_config(const Char_t *, UInt_t *)
Narval interface.
void SetModel(ConfAgent::EModel model)
Definition: FrameIO.cpp:93
static ConfAgent * theGlobalAgent(std::string="Agata")
to get the global agent
Definition: ConfAgent.cpp:402
virtual UInt_t ProcessBlocks()
process blocks attached by AttachInput/AttachOutput. No need to overwrite it if ProcessBlock(FrameBlo...
virtual void process_block(void *input_buffer, UInt_t size_of_input_buffer, void *output_buffer, UInt_t size_of_output_buffer, UInt_t *used_size_of_output_buffer, UInt_t *error_code)
Ask the algorithm to process the data block.
virtual UInt_t ProcessBlocks()
process blocks attached by AttachInput/AttachOutput. No need to overwrite it if ProcessBlock(FrameBlo...
static Int_t GetGlobalParameter(const char *name, UShort_t &val)
to get from narval a parameter () ... IS IT mandatory to have there a static method ...
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
Bool_t IsEoB() const
Definition: FrameBlock.h:86
virtual void SetPID(unsigned int pid)
To set the Process ID number.
ConfAgent.
Definition: ConfAgent.h:63
static Int_t SetGlobalParameter(const char *name, const UShort_t &)
virtual void SetProcessMethod(const char *)
To set the current method.
static void process_config(const Char_t *, UInt_t *)
Have to be overwritten and called in your implementation.
LogMessage & nline(LogMessage &)
virtual void ClearMessage()
To clear the current message.
virtual Bool_t IsOwner(void *owner=0x0) const
Definition: FrameBlock.h:108
FrameIO & GetFrameIO()
virtual UInt_t ProcessBlock(FrameBlock &)=0
virtual method to be implemented
virtual Long64_t GetSize(UInt_t=0u) const
size of the current block
Definition: FrameBlock.h:234
header file for FrameIO.cpp
virtual void Print(std::ostream &out=std::cout) const
Print some informations (statistics)
Definition: FrameIO.cpp:571
header file for NarvalInterface.cpp
LogMessage & dolog(LogMessage &)
virtual std::string & GetProcessName()
To get the Process name.
virtual UInt_t ProcessBlock(FrameBlock &)=0
process one block, virtual method to be implemented
header file for FrameFactory.cpp
virtual UInt_t ProcessBlock(FrameBlock &, FrameBlock &)=0
virtual method to be implemented
LogMessage Log
to send messages to the log server
It defines the general interface needed to be a narval actor.
virtual ConfAgent * GetConfAgent() const
virtual void process_block(void *output_buffer, UInt_t size_of_output_buffer, UInt_t *used_size_of_output_buffer, UInt_t *error_code)
Ask the algorithm to process the data block.
LogMessage & warning(LogMessage &)