artdaq  v3_02_01
xmlrpc_commander.cc
1 /* DarkSide 50 DAQ program
2  * This file add the xmlrpc commander as a client to the SC
3  * Author: Alessandro Razeto <Alessandro.Razeto@ge.infn.it>
4  */
5 
6 #pragma GCC diagnostic push
7 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
8 #define _LIBCPP_ENABLE_CXX17_REMOVED_FEATURES 1
9 #include <xmlrpc-c/base.hpp>
10 #include <xmlrpc-c/registry.hpp>
11 #include <xmlrpc-c/server_abyss.hpp>
12 #include <xmlrpc-c/girerr.hpp>
13 #include <xmlrpc-c/client_simple.hpp>
14 #undef _LIBCPP_ENABLE_CXX17_REMOVED_FEATURES
15 #pragma GCC diagnostic pop
16 #include <stdexcept>
17 #include <iostream>
18 #include <limits>
19 #include <memory>
20 #include <cstdint>
21 #define TRACE_NAME "xmlrpc_commander"
22 #include "tracemf.h"
23 
24 #include "artdaq-core/Utilities/ExceptionHandler.hh"
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <errno.h>
28 #include <cstring>
29 #include <exception>
30 
31 #include "canvas/Persistency/Provenance/RunID.h"
32 #include "fhiclcpp/make_ParameterSet.h"
33 
34 #include "artdaq/ExternalComms/xmlrpc_commander.hh"
35 #include "artdaq/DAQdata/Globals.hh"
36  //#include "artdaq/Application/LoadParameterSet.hh"
37 
38 namespace {
42  class env_wrap {
43  public:
44  env_wrap() { xmlrpc_env_init(&this->env_c); };
45  ~env_wrap() { xmlrpc_env_clean(&this->env_c); };
46  xmlrpc_env env_c;
47  };
48 } // namespace
49 static xmlrpc_c::paramList
50 pListFromXmlrpcArray(xmlrpc_value * const arrayP)
51 {
52  env_wrap env;
53  XMLRPC_ASSERT_ARRAY_OK(arrayP);
54  unsigned int const arraySize = xmlrpc_array_size(&env.env_c, arrayP);
55  assert(!env.env_c.fault_occurred);
56  xmlrpc_c::paramList paramList(arraySize);
57  for (unsigned int i = 0; i < arraySize; ++i) {
58  xmlrpc_value * arrayItemP;
59  xmlrpc_array_read_item(&env.env_c, arrayP, i, &arrayItemP);
60  assert(!env.env_c.fault_occurred);
61  paramList.add(xmlrpc_c::value(arrayItemP));
62  xmlrpc_DECREF(arrayItemP);
63  }
64  return paramList;
65 }
66 static xmlrpc_value *
67 c_executeMethod(xmlrpc_env * const envP,
68  xmlrpc_value * const paramArrayP,
69  void * const methodPtr,
70  void * const callInfoPtr)
71 {
72  xmlrpc_c::method * const methodP(static_cast<xmlrpc_c::method *>(methodPtr));
73  xmlrpc_c::paramList const paramList(pListFromXmlrpcArray(paramArrayP));
74  xmlrpc_c::callInfo * const callInfoP(static_cast<xmlrpc_c::callInfo *>(callInfoPtr));
75  xmlrpc_value * retval;
76  retval = NULL; // silence used-before-set warning
77  try {
78  xmlrpc_c::value result;
79  try {
80  xmlrpc_c::method2 * const method2P(dynamic_cast<xmlrpc_c::method2 *>(methodP));
81  if (method2P)
82  method2P->execute(paramList, callInfoP, &result);
83  else
84  methodP->execute(paramList, &result);
85  }
86  catch (xmlrpc_c::fault const& fault) {
87  xmlrpc_env_set_fault(envP, fault.getCode(),
88  fault.getDescription().c_str());
89  }
90  if (!envP->fault_occurred) {
91  if (result.isInstantiated())
92  retval = result.cValue();
93  else
94  girerr::throwf("Xmlrpc-c user's xmlrpc_c::method object's "
95  "'execute method' failed to set the RPC result "
96  "value.");
97  }
98  }
99  catch (std::exception const& e) {
100  xmlrpc_faultf(envP, "Unexpected error executing code for "
101  "particular method, detected by Xmlrpc-c "
102  "method registry code. Method did not "
103  "fail; rather, it did not complete at all. %s",
104  e.what());
105  }
106  catch (...) {
107  xmlrpc_env_set_fault(envP, XMLRPC_INTERNAL_ERROR,
108  "Unexpected error executing code for "
109  "particular method, detected by Xmlrpc-c "
110  "method registry code. Method did not "
111  "fail; rather, it did not complete at all.");
112  }
113  return retval;
114 }
115 
116 
117 
118 namespace artdaq
119 {
126  std::string exception_msg(const std::runtime_error& er,
127  const std::string& helpText = "execute request")
128  {
129  std::string msg("Exception when trying to ");
130  msg.append(helpText);
131  msg.append(": ");
132  msg.append(er.what()); //std::string(er.what ()).substr (2);
133  if (msg[msg.size() - 1] == '\n') msg.erase(msg.size() - 1);
134  return msg;
135  }
136 
143  std::string exception_msg(const art::Exception& er,
144  const std::string& helpText)
145  {
146  std::string msg("Exception when trying to ");
147  msg.append(helpText);
148  msg.append(": ");
149  msg.append(er.what());
150  if (msg[msg.size() - 1] == '\n') msg.erase(msg.size() - 1);
151  return msg;
152  }
153 
160  std::string exception_msg(const cet::exception& er,
161  const std::string& helpText)
162  {
163  std::string msg("Exception when trying to ");
164  msg.append(helpText);
165  msg.append(": ");
166  msg.append(er.what());
167  if (msg[msg.size() - 1] == '\n') msg.erase(msg.size() - 1);
168  return msg;
169  }
170 
177  std::string exception_msg(const std::string& erText,
178  const std::string& helpText)
179  {
180  std::string msg("Exception when trying to ");
181  msg.append(helpText);
182  msg.append(": ");
183  msg.append(erText);
184  if (msg[msg.size() - 1] == '\n') msg.erase(msg.size() - 1);
185  return msg;
186  }
187 
188 
206  class cmd_ : public xmlrpc_c::method
207  {
208  public:
209 
210  // Can't seem to initialize "_signature" and "_help" in the initialization list...
217  cmd_(xmlrpc_commander& c, const std::string& signature, const std::string& description) : _c(c)
218  {
219  _signature = signature;
220  _help = description;
221  }
222 
228  void execute(const xmlrpc_c::paramList& paramList, xmlrpc_c::value* const retvalP) final;
229 
230  protected:
231 
233 
239  virtual bool execute_(const xmlrpc_c::paramList&, xmlrpc_c::value* const retvalP) = 0;
240 
250  template <typename T>
251  T getParam(const xmlrpc_c::paramList& paramList, int index);
252 
276  template <typename T>
277  T getParam(const xmlrpc_c::paramList& paramList, int index, T default_value);
278  };
279 
280  // Users are only allowed to call getParam for predefined types; see
281  // template specializations below this default function
282 
283  template <typename T>
284  T cmd_::getParam(const xmlrpc_c::paramList&, int)
285  {
286  throw cet::exception("cmd_") << "Error in cmd_::getParam(): value type not supported" << std::endl;
287  }
288 
297  template <>
298  uint64_t cmd_::getParam<uint64_t>(const xmlrpc_c::paramList& paramList, int index)
299  {
300  TLOG(TLVL_TRACE) << "Getting parameter " << index << " from list as uint64_t.";
301  TLOG(TLVL_TRACE) << "Param value: " << paramList.getString(index);
302  return boost::lexical_cast<uint64_t>(paramList.getString(index));
303  }
304 
313  template <>
314  std::string cmd_::getParam<std::string>(const xmlrpc_c::paramList& paramList, int index)
315  {
316  TLOG(TLVL_TRACE) << "Getting parameter " << index << " from list as string.";
317  TLOG(TLVL_TRACE) << "Param value: " << paramList.getString(index);
318  return static_cast<std::string>(paramList.getString(index));
319  }
320 
329  template <>
330  art::RunID cmd_::getParam<art::RunID>(const xmlrpc_c::paramList& paramList, int index)
331  {
332  TLOG(TLVL_TRACE) << "Getting parameter " << index << " from list as Run Number.";
333  TLOG(TLVL_TRACE) << "Param value: " << paramList.getString(index);
334  std::string run_number_string = paramList.getString(index);
335  art::RunNumber_t run_number =
336  boost::lexical_cast<art::RunNumber_t>(run_number_string);
337  art::RunID run_id(run_number);
338 
339  return run_id;
340  }
341 
350  template <>
351  fhicl::ParameterSet cmd_::getParam<fhicl::ParameterSet>(const xmlrpc_c::paramList& paramList, int index)
352  {
353  TLOG(TLVL_TRACE) << "Getting parameter " << index << " from list as ParameterSet.";
354  TLOG(TLVL_TRACE) << "Param value: " << paramList.getString(index);
355  std::string configString = std::string(paramList.getString(index).c_str());
356  TLOG(TLVL_DEBUG) << "Loading Parameter Set from string: " << configString << std::endl;
357  fhicl::ParameterSet pset;
358 
359  try
360  {
361  fhicl::make_ParameterSet(configString, pset);
362  }
363  catch (fhicl::exception e)
364  {
365  if (getenv("FHICL_FILE_PATH") == nullptr)
366  {
367  std::cerr << "INFO: environment variable FHICL_FILE_PATH was not set. Using \".\"\n";
368  setenv("FHICL_FILE_PATH", ".", 0);
369  }
370  cet::filepath_lookup_after1 lookup_policy("FHICL_FILE_PATH");
371  fhicl::make_ParameterSet(configString, lookup_policy, pset);
372  }
373 
374  TLOG(TLVL_INFO) << "Parameter Set Loaded." << std::endl;
375  return pset;
376  }
377 
378  template <typename T>
379  T cmd_::getParam(const xmlrpc_c::paramList& paramList, int index,
380  T default_value)
381  {
382  T val = default_value;
383 
384  try
385  {
386  val = getParam<T>(paramList, index);
387  }
388  catch (const cet::exception& exception)
389  {
390  throw exception;
391  }
392  catch (...) {}
393 
394  return val;
395  }
396 
397  void cmd_::execute(const xmlrpc_c::paramList& paramList, xmlrpc_c::value* const retvalP)
398  {
399  std::unique_lock<std::timed_mutex> lk(_c.mutex_, std::chrono::milliseconds(250));
400  if (lk.owns_lock())
401  {
402  try
403  {
404  // JCF, 9/4/14
405 
406  // Assuming the execute_ function returns true, then if the
407  // retvalP argument was untouched, assign it the string
408  // "Success"
409 
410  // See
411  // http://xmlrpc-c.sourceforge.net/doc/libxmlrpc++.html#isinstantiated
412  // for more on the concept of instantiation in xmlrpc_c::value objects
413 
414  if (execute_(paramList, retvalP))
415  {
416  if (!retvalP->isInstantiated())
417  {
418  *retvalP = xmlrpc_c::value_string("Success");
419  }
420  }
421  else
422  {
423  std::string problemReport = _c._commandable.report("transition_status");
424  *retvalP = xmlrpc_c::value_string(problemReport);
425  }
426  }
427  catch (std::runtime_error& er)
428  {
429  std::string msg = exception_msg(er, _help);
430  *retvalP = xmlrpc_c::value_string(msg);
431  TLOG(TLVL_ERROR) << msg ;
432  }
433  catch (art::Exception& er)
434  {
435  std::string msg = exception_msg(er, _help);
436  *retvalP = xmlrpc_c::value_string(msg);
437  TLOG(TLVL_ERROR) << msg ;
438  }
439  catch (cet::exception& er)
440  {
441  std::string msg = exception_msg(er, _help);
442  *retvalP = xmlrpc_c::value_string(msg);
443  TLOG(TLVL_ERROR) << msg ;
444  }
445  catch (...)
446  {
447  std::string msg = exception_msg("Unknown exception", _help);
448  *retvalP = xmlrpc_c::value_string(msg);
449  TLOG(TLVL_ERROR) << msg ;
450  }
451  }
452  else
453  {
454  *retvalP = xmlrpc_c::value_string("busy");
455  }
456  }
457 
458 
460 
461  // JCF, 9/5/14
462 
463  // The three "init" transitions all take a FHiCL parameter list, and
464  // optionally a timeout and a timestamp; thus we can kill three birds
465  // with one stone in the GENERATE_INIT_TRANSITION macro
466 
467 #define GENERATE_INIT_TRANSITION(NAME, CALL, DESCRIPTION) \
468  \
469  class NAME ## _: public cmd_ { \
470  \
471  public: \
472  \
475  explicit NAME ## _(xmlrpc_commander& c): \
476  cmd_(c, "s:sii", DESCRIPTION) {} \
477  \
478  \
479  static const uint64_t defaultTimeout = 45; \
480  \
481  static const uint64_t defaultTimestamp = std::numeric_limits<const uint64_t>::max(); \
482  \
483  private: \
484  bool execute_(const xmlrpc_c::paramList& paramList, xmlrpc_c::value* const retvalP ) { \
485  fhicl::ParameterSet ps; \
486  try { \
487  ps = getParam<fhicl::ParameterSet>(paramList, 0); \
488  } catch (...) { \
489  *retvalP = xmlrpc_c::value_string ("The "#NAME" message requires a single argument that is a string containing the initialization ParameterSet"); \
490  return true; \
491  } \
492  \
493  return _c._commandable.CALL(ps, \
494  getParam<uint64_t>(paramList, 1, defaultTimeout), \
495  getParam<uint64_t>(paramList, 2, defaultTimestamp) \
496  ); \
497  } \
498  };
499 
500  GENERATE_INIT_TRANSITION(init, initialize, "initialize the program")
501 
502  GENERATE_INIT_TRANSITION(soft_init, soft_initialize, "initialize software components in the program")
503 
504  GENERATE_INIT_TRANSITION(reinit, reinitialize, "re-initialize the program")
505 
506 #undef GENERATE_INIT_TRANSITION
507 
509 
513  class start_ : public cmd_
514  {
515  public:
520  explicit start_(xmlrpc_commander& c) :
521  cmd_(c, "s:iii", "start the run")
522  {}
523 
525  static const uint64_t defaultTimeout = 45;
527  static const uint64_t defaultTimestamp = std::numeric_limits<const uint64_t>::max();
528 
529  private:
530 
531  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP) override
532  {
533  try
534  {
535  getParam<art::RunID>(paramList, 0);
536  }
537  catch (...)
538  {
539  *retvalP = xmlrpc_c::value_string("The start message requires the run number as an argument.");
540  return true;
541  }
542 
543  return _c._commandable.start(getParam<art::RunID>(paramList, 0),
544  getParam<uint64_t>(paramList, 1, defaultTimeout),
545  getParam<uint64_t>(paramList, 2, defaultTimestamp)
546  );
547  }
548  };
549 
550 
552 
553  // JCF, 9/5/14
554 
555  // "pause", "resume" and "stop" all take an optional timeout and
556  // timestamp parameter, so we can generate them all with the
557  // GENERATE_TIMEOUT_TIMESTAMP_TRANSITION macro
558 
559 #define GENERATE_TIMEOUT_TIMESTAMP_TRANSITION(NAME, CALL, DESCRIPTION, TIMEOUT) \
560  \
561  class NAME ## _: public cmd_ { \
562  \
563 public: \
564  \
566  NAME ## _(xmlrpc_commander& c): \
567  cmd_(c, "s:ii", DESCRIPTION) {} \
568  \
569  \
570  static const uint64_t defaultTimeout = TIMEOUT ; \
571  \
572  static const uint64_t defaultTimestamp = std::numeric_limits<const uint64_t>::max(); \
573  \
574 private: \
575  \
576  bool execute_ (const xmlrpc_c::paramList& paramList , xmlrpc_c::value* const ) { \
577  \
578  return _c._commandable.CALL( getParam<uint64_t>(paramList, 0, defaultTimeout), \
579  getParam<uint64_t>(paramList, 1, defaultTimestamp) \
580  ); \
581  } \
582  };
583 
584  GENERATE_TIMEOUT_TIMESTAMP_TRANSITION(pause, pause, "pause the program", 45)
585 
586  GENERATE_TIMEOUT_TIMESTAMP_TRANSITION(resume, resume, "resume the program", 45)
587 
588  GENERATE_TIMEOUT_TIMESTAMP_TRANSITION(stop, stop, "stop the program", 45)
589 
590 #undef GENERATE_TIMEOUT_TIMESTAMP_TRANSITION
591 
592 
596  class shutdown_ : public cmd_
597  {
598  public:
604  cmd_(c, "s:i", "shutdown the program")
605  {}
606 
608  static const uint64_t defaultTimeout = 45;
609 
610  private:
611 
612  bool execute_(const xmlrpc_c::paramList& paramList, xmlrpc_c::value* const)
613  {
614  auto ret = _c._commandable.shutdown(getParam<uint64_t>(paramList, 0, defaultTimeout));
615 
616 #if 1
617  if (_c.server) _c.server->terminate();
618 #endif
619 
620  return ret;
621  }
622  };
623 
624 
628  class status_ : public cmd_
629  {
630  public:
636  cmd_(c, "s:n", "report the current state")
637  {}
638 
639  private:
640 
641  bool execute_(xmlrpc_c::paramList const&, xmlrpc_c::value* const retvalP)
642  {
643  *retvalP = xmlrpc_c::value_string(_c._commandable.status());
644  return true;
645  }
646  };
647 
648 
652  class report_ : public cmd_
653  {
654  public:
660  cmd_(c, "s:s", "report statistics")
661  {}
662 
663  private:
664  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
665  {
666  try
667  {
668  getParam<std::string>(paramList, 0);
669  }
670  catch (...)
671  {
672  *retvalP = xmlrpc_c::value_string("The report message requires a single argument that selects the type of statistics to be reported.");
673  return true;
674  }
675 
676  *retvalP = xmlrpc_c::value_string(_c._commandable.report(getParam<std::string>(paramList, 0)));
677  return true;
678  }
679  };
680 
684  class legal_commands_ : public cmd_
685  {
686  public:
692  cmd_(c, "s:n", "return the currently legal commands")
693  {}
694 
695  private:
696  bool execute_(xmlrpc_c::paramList const&, xmlrpc_c::value* const retvalP)
697  {
698  std::vector<std::string> cmdList = _c._commandable.legal_commands();
699  std::string resultString;
700 
701  for (auto& cmd : cmdList)
702  {
703  resultString.append(cmd + " ");
704  if (cmd == "shutdown")
705  {
706  resultString.append(" reset");
707  }
708  }
709  *retvalP = xmlrpc_c::value_string(resultString);
710 
711  return true;
712  }
713  };
714 
718  class register_monitor_ : public cmd_
719  {
720  public:
726  cmd_(c, "s:s", "Get notified of a new monitor")
727  {}
728 
729  private:
730  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
731  {
732  try
733  {
734  getParam<fhicl::ParameterSet>(paramList, 0);
735  }
736  catch (...)
737  {
738  *retvalP = xmlrpc_c::value_string("The register_monitor command expects a string representing the FHiCL definition of a Transfer plugin");
739  return true;
740  }
741 
742  *retvalP = xmlrpc_c::value_string(_c._commandable.register_monitor(getParam<fhicl::ParameterSet>(paramList, 0)));
743  return true;
744  }
745  };
746 
750  class unregister_monitor_ : public cmd_
751  {
752  public:
758  cmd_(c, "s:s", "Remove a monitor")
759  {}
760 
761  private:
762  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
763  {
764  try
765  {
766  getParam<std::string>(paramList, 0);
767  }
768  catch (...)
769  {
770  *retvalP = xmlrpc_c::value_string("The unregister_monitor command expects a string representing the label of the monitor to be removed");
771  return true;
772  }
773 
774  *retvalP = xmlrpc_c::value_string(_c._commandable.unregister_monitor(getParam<std::string>(paramList, 0)));
775  return true;
776  }
777  };
778 
779 
783  class trace_set_ : public cmd_
784  {
785  public:
791  cmd_(c, "s:ssi", "Set TRACE mask")
792  {}
793 
794  private:
795  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
796  {
797  try
798  {
799  getParam<std::string>(paramList, 0);
800  getParam<std::string>(paramList, 1);
801  getParam<uint64_t>(paramList, 2);
802  }
803  catch (...)
804  {
805  *retvalP = xmlrpc_c::value_string("The trace_set command expects a mask type (M, S , or T), a name (ALL for all) and a mask");
806  return true;
807  }
808 
809  return _c._commandable.do_trace_set(getParam<std::string>(paramList, 0), getParam<std::string>(paramList, 1), getParam<uint64_t>(paramList, 2));
810  }
811  };
812 
816  class trace_get_ : public cmd_
817  {
818  public:
824  cmd_(c, "s:s", "Get TRACE mask")
825  {}
826 
827  private:
828  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
829  {
830  try
831  {
832  getParam<std::string>(paramList, 0);
833  }
834  catch (...)
835  {
836  *retvalP = xmlrpc_c::value_string("The trace_msgfacility_set command expects a name (ALL for all)");
837  return true;
838  }
839 
840  *retvalP = xmlrpc_c::value_string(_c._commandable.do_trace_get(getParam<std::string>(paramList, 0)));
841  return true;
842  }
843  };
844 
848  class meta_command_ : public cmd_
849  {
850  public:
856  cmd_(c, "s:ss", "Run custom command")
857  {}
858 
859  private:
860  bool execute_(xmlrpc_c::paramList const& paramList, xmlrpc_c::value* const retvalP)
861  {
862  try
863  {
864  getParam<std::string>(paramList, 0);
865  getParam<std::string>(paramList, 1);
866  }
867  catch (...)
868  {
869  *retvalP = xmlrpc_c::value_string("The meta_command command expects a string command and a string argument");
870  return true;
871  }
872 
873  return _c._commandable.do_meta_command(getParam<std::string>(paramList, 0), getParam<std::string>(paramList, 1));
874  }
875  };
876 
880  class rollover_subrun_ : public cmd_
881  {
882  public:
888  cmd_(c, "s:i", "create a new subrun")
889  {}
890 
892  static const uint64_t defaultSequenceID = 0xFFFFFFFFFFFFFFFF;
893 
894  private:
895 
896  bool execute_(const xmlrpc_c::paramList& paramList, xmlrpc_c::value* const)
897  {
898  auto ret = _c._commandable.do_rollover_subrun(getParam<uint64_t>(paramList, 0, defaultSequenceID));
899  return ret;
900  }
901  };
902 
903  // JCF, 9/4/14
904 
905  // Not sure if anyone was planning to resurrect this code by changing
906  // the preprocessor decision; as such, I'll leave it in for now...
907 
908 #if 0
909  class shutdown_ : public xmlrpc_c::registry::shutdown
910  {
911  public:
912  shutdown_(xmlrpc_c::serverAbyss *server) : _server(server) {}
913 
914  virtual void doit(const std::string& paramString, void*) const
915  {
916  TLOG(TLVL_INFO) << "A shutdown command was sent "
917  << "with parameter "
918  << paramString << "\"" ;
919  _server->terminate();
920  }
921  private:
922  xmlrpc_c::serverAbyss *_server;
923  };
924 #endif
925 
926 
927 
928  xmlrpc_commander::xmlrpc_commander(fhicl::ParameterSet ps, artdaq::Commandable& commandable)
929  : CommanderInterface(ps, commandable)
930  , port_(ps.get<int>("id", 0))
931  , serverUrl_(ps.get<std::string>("server_url", ""))
932  , server(nullptr)
933  {
934  //std::cout << "XMLRPC COMMANDER CONSTRUCTOR: Port: " << port_ << ", Server Url: " << serverUrl_ << std::endl;
935  }
936 
938  {
939  //std::cout << "XMLRPC_COMMANDER RUN_SERVER CALLED!" << std::endl;
940  xmlrpc_c::registry registry;
941  struct xmlrpc_method_info3 methodInfo;
942  memset(&methodInfo, 0, sizeof(methodInfo));
943 
944  /*#define register_method(m) \
945  // xmlrpc_c::methodPtr const ptr_ ## m(new m ## _(*this));\
946  registry.addMethod ("daq." #m, ptr_ ## m) */
947 #define register_method(m) register_method2(m,0x400000)
948 
949  xmlrpc_env env; // xmlrpc_env_init(&env);
950  xmlrpc_registry ***c_registryPPP;
951  c_registryPPP = (xmlrpc_registry ***)(((char*)&registry) + sizeof(girmem::autoObject));
952 
953 #define register_method2(m,ss) \
954  xmlrpc_c::method * ptr_ ## m(dynamic_cast<xmlrpc_c::method *>(new m ## _(*this))); \
955  methodInfo.methodName = "daq." #m; \
956  methodInfo.methodFunction = &c_executeMethod; \
957  methodInfo.serverInfo = ptr_ ## m; \
958  methodInfo.stackSize = ss; \
959  methodInfo.signatureString = ptr_ ## m ->signature().c_str(); \
960  methodInfo.help = ptr_ ## m ->help().c_str(); \
961  xmlrpc_env_init(&env); \
962  xmlrpc_registry_add_method3(&env,**c_registryPPP,&methodInfo); \
963  if(env.fault_occurred)throw(girerr::error(env.fault_string)); \
964  xmlrpc_env_clean(&env)
965 
966  register_method2(init, 0x200000);
967  register_method(soft_init);
968  register_method(reinit);
969  register_method(start);
970  register_method(status);
971  register_method(report);
972  register_method(stop);
973  register_method(pause);
974  register_method(resume);
975  register_method(register_monitor);
976  register_method(unregister_monitor);
977  register_method(legal_commands);
978  register_method(trace_set);
979  register_method(trace_get);
980  register_method(meta_command);
981  register_method(rollover_subrun);
982 
983  register_method(shutdown);
984 
985  // alias "daq.reset" to the internal shutdown transition
986  xmlrpc_c::methodPtr const ptr_reset(new shutdown_(*this));
987  registry.addMethod("daq.reset", ptr_reset);
988 
989 #undef register_method
990 
991  // JCF, 6/3/15
992 
993  // In the following code, I configure a socket to have the
994  // SO_REUSEADDR option so that once an artdaq process closes, the
995  // port it was communicating on becomes immediately available
996  // (desirable if, say, the DAQ program is terminated and then
997  // immediately restarted)
998 
999  // Much of the following code is cribbed from
1000  // http://fossies.org/linux/freeswitch/libs/xmlrpc-c/src/cpp/test/server_abyss.cpp
1001 
1002  // Below, "0" is the default protocol (in this case, given the IPv4
1003  // Protocol Family (PF_INET) and the SOCK_STREAM communication
1004  // method)
1005 
1006  XMLRPC_SOCKET socket_file_descriptor = socket(PF_INET, SOCK_STREAM, 0);
1007 
1008  if (socket_file_descriptor < 0)
1009  {
1010  throw cet::exception("xmlrpc_commander::run") <<
1011  "Problem with the socket() call; C-style errno == " <<
1012  errno << " (" << strerror(errno) << ")";
1013  }
1014 
1015  int enable = 1;
1016  int retval = setsockopt(socket_file_descriptor,
1017  SOL_SOCKET, SO_REUSEADDR,
1018  &enable, sizeof(int));
1019 
1020  if (retval < 0)
1021  {
1022  throw cet::exception("xmlrpc_commander::run") <<
1023  "Problem with the call to setsockopt(); C-style errno == " <<
1024  errno << " (" << strerror(errno) << ")";
1025  }
1026 
1027  struct sockaddr_in sockAddr;
1028 
1029  sockAddr.sin_family = AF_INET;
1030  sockAddr.sin_port = htons(port_);
1031  sockAddr.sin_addr.s_addr = 0;
1032 
1033  retval = bind(socket_file_descriptor,
1034  reinterpret_cast<struct sockaddr*>(&sockAddr),
1035  sizeof(sockAddr));
1036 
1037  if (retval != 0)
1038  {
1039  close(socket_file_descriptor);
1040  throw cet::exception("xmlrpc_commander::run") <<
1041  "Problem with the bind() call; C-style errno == " <<
1042  errno << " (" << strerror(errno) << ")";
1043  }
1044 
1045  server.reset(new xmlrpc_c::serverAbyss(xmlrpc_c::serverAbyss::constrOpt().registryP(&registry).socketFd(socket_file_descriptor)));
1046 
1047 #if 0
1048  xmlrpc_c::serverAbyss::shutdown shutdown_obj(&server);
1049  registry.setShutdown(&shutdown_obj);
1050 #endif
1051 
1052  TLOG(TLVL_DEBUG) << "running server" ;
1053 
1054  // JCF, 6/3/15
1055 
1056  // Use a catch block to clean up (i.e., close the socket). An
1057  // opportunity for RAII, although all control paths are limited to
1058  // this section of the file...
1059 
1060  try
1061  {
1062  server->run();
1063  }
1064  catch (...)
1065  {
1066  TLOG(TLVL_WARNING) << "server threw an exception; closing the socket and rethrowing" ;
1067  close(socket_file_descriptor);
1068  throw;
1069  }
1070 
1071  close(socket_file_descriptor);
1072  TLOG(TLVL_DEBUG) << "server terminated" ;
1073  }
1074  catch (...)
1075  {
1076  throw;
1077  }
1078 
1079 
1080  std::string xmlrpc_commander::send_register_monitor(std::string monitor_fhicl)
1081  {
1082  if (serverUrl_ == "")
1083  {
1084  std::stringstream errmsg;
1085  errmsg << "Problem attempting XML-RPC call: No server URL set!";
1086  ExceptionHandler(ExceptionHandlerRethrow::yes,
1087  errmsg.str());
1088 
1089  }
1090  xmlrpc_c::clientSimple myClient;
1091  xmlrpc_c::value result;
1092 
1093  try
1094  {
1095  myClient.call(serverUrl_, "daq.register_monitor", "s", &result, monitor_fhicl.c_str());
1096  }
1097  catch (...)
1098  {
1099  std::stringstream errmsg;
1100  errmsg << "Problem attempting XML-RPC call on host " << serverUrl_
1101  << "; possible causes are malformed FHiCL or nonexistent process at requested port";
1102  ExceptionHandler(ExceptionHandlerRethrow::yes,
1103  errmsg.str());
1104  }
1105 
1106  return xmlrpc_c::value_string(result);
1107  }
1108 
1109  std::string xmlrpc_commander::send_unregister_monitor(std::string monitor_label)
1110  {
1111  if (serverUrl_ == "")
1112  {
1113  std::stringstream errmsg;
1114  errmsg << "Problem attempting XML-RPC call: No server URL set!";
1115  ExceptionHandler(ExceptionHandlerRethrow::yes,
1116  errmsg.str());
1117 
1118  }
1119 
1120  xmlrpc_c::clientSimple myClient;
1121  xmlrpc_c::value result;
1122 
1123  try
1124  {
1125  myClient.call(serverUrl_, "daq.unregister_monitor", "s", &result, monitor_label.c_str());
1126  }
1127  catch (...)
1128  {
1129  std::stringstream errmsg;
1130  errmsg << "Problem attempting to unregister monitor via XML-RPC call on host " << serverUrl_
1131  << "; possible causes are that the monitor label \""
1132  << monitor_label
1133  << "\" is unrecognized by contacted process or process at requested port doesn't exist";
1134  ExceptionHandler(ExceptionHandlerRethrow::no, errmsg.str());
1135  }
1136 
1137  return xmlrpc_c::value_string(result);
1138 
1139  }
1140 } // namespace artdaq
1141 
1142 DEFINE_ARTDAQ_COMMANDER(artdaq::xmlrpc_commander)
This interface defines the functions used to transfer data between artdaq applications.
cmd_(xmlrpc_commander &c, const std::string &signature, const std::string &description)
cmd_ Constructor
static const uint64_t defaultTimeout
std::string send_unregister_monitor(std::string monitor_label) override
Send an unregister_monitor command over XMLRPC
std::string send_register_monitor(std::string monitor_fhicl) override
Send a register_monitor command over XMLRPC
virtual std::string report(std::string const &) const
Default report implementation returns current report_string.
Definition: Commandable.hh:121
Commandable is the base class for all artdaq components which implement the artdaq state machine...
Definition: Commandable.hh:20
legal_commands_(xmlrpc_commander &c)
legal_commands_ Constructor
xmlrpc_commander(fhicl::ParameterSet ps, artdaq::Commandable &commandable)
xmlrpc_commander Constructor
Command class representing a start transition.
rollover_subrun_(xmlrpc_commander &c)
shutdown_ Constructor
status_ Command class
report_ Command class
virtual std::string unregister_monitor(std::string const &)
Perform the unregister_monitor action.
Definition: Commandable.hh:150
meta_command_ Command class
The xmlrpc_commander class serves as the XMLRPC server run in each artdaq application.
static const uint64_t defaultTimestamp
The &quot;cmd_&quot; class serves as the base class for all artdaq&#39;s XML-RPC commands.
trace_set_(xmlrpc_commander &c)
unregister_monitor_ Constructor
shutdown_ Command class
void run_server() override
Run the XMLRPC server.
meta_command_(xmlrpc_commander &c)
meta_command_ Constructor
std::unique_ptr< xmlrpc_c::serverAbyss > server
XMLRPC server.
std::string status() const
Returns the current state of the Commandable.
Definition: Commandable.cc:272
std::string exception_msg(const std::runtime_error &er, const std::string &helpText="execute request")
Write an exception message.
virtual bool do_trace_set(std::string const &type, std::string const &name, uint64_t mask)
Set the given TRACE mask for the given TRACE name.
Definition: Commandable.cc:434
static const uint64_t defaultSequenceID
virtual std::string register_monitor(fhicl::ParameterSet const &)
Perform the register_monitor action.
Definition: Commandable.hh:139
virtual bool do_meta_command(std::string const &command, std::string const &args)
Run a module-defined command with the given parameter string.
Definition: Commandable.cc:484
register_monitor_ Command class
legal_commands_ Command class
artdaq::Commandable & _commandable
Reference to the Commandable that this Commander Commands.
xmlrpc_commander & _c
The xmlrpc_commander instance that the command will be sent to.
trace_get_ Command class
status_(xmlrpc_commander &c)
status_ Constructor
virtual bool execute_(const xmlrpc_c::paramList &, xmlrpc_c::value *const retvalP)=0
&quot;execute_&quot; is a wrapper function around the call to the commandable object&#39;s function ...
Wrapper for XMLRPC environment construction/destruction
std::timed_mutex mutex_
XMLRPC mutex.
register_monitor_(xmlrpc_commander &c)
register_monitor_ Constructor
static const uint64_t defaultTimeout
unregister_monitor_(xmlrpc_commander &c)
unregister_monitor_ Constructor
void execute(const xmlrpc_c::paramList &paramList, xmlrpc_c::value *const retvalP) final
Execute trhe command with the given parameters.
shutdown_(xmlrpc_commander &c)
shutdown_ Constructor
virtual bool do_rollover_subrun(uint64_t eventNum)
Perform the rollover_subrun transition.
Definition: Commandable.cc:490
std::vector< std::string > legal_commands() const
Get the legal transition commands from the current state.
Definition: Commandable.cc:285
rollover_subrun_ Command class
bool start(art::RunID id, uint64_t timeout, uint64_t timestamp)
Processes the start transition.
Definition: Commandable.cc:59
report_(xmlrpc_commander &c)
report_ Constructor
trace_set_ Command class
start_(xmlrpc_commander &c)
start_ Command (cmd_ derived class) Constructor
virtual std::string do_trace_get(std::string const &name)
Get the TRACE mask for the given TRACE name If name is &quot;ALL&quot;, then all TRACE masks will be printed...
Definition: Commandable.cc:405
trace_get_(xmlrpc_commander &c)
trace_msgfacility_set_ Constructor
unregister_monitor_ Command class
T getParam(const xmlrpc_c::paramList &paramList, int index)
Get a parameter from the parameter list.
bool shutdown(uint64_t timeout)
Processes the shutdown transition.
Definition: Commandable.cc:166