tdaq-develop-2025-02-12
ECLTest.cc
1 #include "TRACE/trace.h"
2 #define TRACE_NAME "ECLTest"
3 
4 #define __E__ std::endl
5 #define QQQQ(X) #X
6 #define QUOTE(X) QQQQ(X)
7 
8 #define __SHORTFILE__ \
9  (__builtin_strstr(&__FILE__[0], "/srcs/") \
10  ? __builtin_strstr(&__FILE__[0], "/srcs/") + 6 \
11  : __FILE__)
12 #define __COUT_HDR_L__ ":" << std::dec << __LINE__ << " |\t"
13 #define __COUT_HDR__ __SHORTFILE__ << "" << __COUT_HDR_L__
14 
15 #define __COUTV__(X) std::cout << __COUT_HDR__ << QUOTE(X) << " = " << X << __E__
16 #define __COUT__ std::cout << __COUT_HDR__
17 
18 #include "otsdaq-utilities/ECLWriter/ECLConnection.h"
19 
20 #include <boost/program_options.hpp>
21 namespace bpo = boost::program_options;
22 
23 //==============================================================================
24 std::string encodeURIComponent(const std::string& sourceStr)
25 {
26  std::string retStr = "";
27  char encodeStr[4];
28  for(const auto& c : sourceStr)
29  if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))
30  retStr += c;
31  else
32  {
33  sprintf(encodeStr, "%%%2.2X", (uint8_t)c);
34  retStr += encodeStr;
35  }
36  return retStr;
37 } // end encodeURIComponent()
38 
52 int main(int argc, char* argv[])
53 {
54  std::ostringstream descstr;
55  descstr << argv[0]
56  << " --host <ECL host> --user <Username> --pwd <Password> [--cat <Category "
57  "name>] [--title <Message "
58  "title>] [--msg message] #add quotes for any entry with spaces";
59  bpo::options_description desc(descstr.str());
60  desc.add_options()("host,i", bpo::value<std::string>(), "ECL Instance Address")(
61  "user,u", bpo::value<std::string>(), "ECL Username")(
62  "pwd,p", bpo::value<std::string>(), "ECL Password")(
63  "cat,c", bpo::value<std::string>(), "ECL Category")(
64  "title,t", bpo::value<std::string>(), "Message title")(
65  "msg,m", bpo::value<std::string>(), "Log Message")("help,h",
66  "produce help message");
67  bpo::positional_options_description p;
68  p.add("msg", -1);
69  bpo::variables_map vm;
70  try
71  {
72  bpo::store(bpo::command_line_parser(argc, argv).options(desc).positional(p).run(),
73  vm);
74  bpo::notify(vm);
75  }
76  catch(bpo::error const& e)
77  {
78  std::cout << "Exception from command line processing in " << argv[0] << ": "
79  << e.what() << "\n";
80  exit(-1);
81  }
82  if(vm.count("help"))
83  {
84  std::cout << desc << std::endl;
85  exit(1);
86  }
87 
88  std::string ECLUser = "", ECLPwd = "", ECLHost = "",
89  ECLCategory = "general" /* e.g. "TDAQ" */, title = "ECL Test Message",
90  message = "";
91  if(vm.count("user"))
92  {
93  ECLUser = vm["user"].as<std::string>();
94  }
95  if(vm.count("pwd"))
96  {
97  ECLPwd = vm["pwd"].as<std::string>();
98  }
99  if(vm.count("host"))
100  {
101  ECLHost = vm["host"].as<std::string>();
102  }
103  if(vm.count("cat"))
104  {
105  ECLCategory = vm["cat"].as<std::string>();
106  }
107  if(vm.count("title"))
108  {
109  title = vm["title"].as<std::string>();
110  }
111  if(vm.count("msg"))
112  {
113  message = vm["msg"].as<std::string>();
114  }
115  __COUTV__(ECLUser);
116  __COUTV__(ECLHost);
117  __COUTV__(title);
118  __COUTV__(message);
119 
120  ECLEntry_t eclEntry;
121  eclEntry.author(ECLUser);
122  eclEntry.category(ECLCategory);
123  eclEntry.subject(title);
124  Form_t form;
125  Field_t field;
126  Form_t::field_sequence fields;
127 
128  form.name("default");
129 
130  field = Field_t(message, "text");
131  fields.push_back(field);
132 
133  form.field(fields);
134 
135  eclEntry.form(form);
136 
137  ECLConnection eclConn(ECLUser, ECLPwd, ECLHost);
138 
139  if(ECLCategory == "GetRecent")
140  {
141  __COUT__ << "Getting posts... matching category=" << title << __E__;
142  std::string response,
143  url = "/E/xml_search?l=100&c=" + encodeURIComponent(title); //limit to 100
144  eclConn.Get(url, response);
145  __COUTV__(response);
146  return 0;
147  }
148  else if(ECLCategory == "GetCategories")
149  {
150  __COUT__ << "Getting categories..." << __E__;
151  std::string response, url = "/A/xml_category_list";
152  eclConn.Get(url, response);
153  __COUTV__(response);
154  return 0;
155  }
156  else if(!eclConn.Post(eclEntry))
157  {
158  return -1;
159  }
160 
161  return 0;
162 }
Definition: ECL.hxx:495
Definition: ECL.hxx:386
Definition: ECL.hxx:436