tdaq-develop-2025-02-12
ots_mm_udp_test.cpp
1 
2 #include "test/ots_mm_udp_interface.h"
3 
4 int main(int argc, char* argv[])
5 try
6 {
7  if(argc < 3)
8  {
9  fprintf(stderr,
10  "usage: ots_mm_udp_test <mm IP> <mm Port> <Front-end UID> <FEMacro "
11  "Name> <Macro Input args...> \n");
12  fprintf(stderr,
13  "\t If <Front-end UID> not specified, then the list of existing FE UIDs "
14  "will be output \n");
15  fprintf(stderr,
16  "\t If <FEMacro Name> not specified, then the list of existing FE Macro "
17  "Names will be output associated with the specified FE UID\n");
18  fprintf(stderr,
19  "\t Note: use quotes around command line arguments with spaces to "
20  "prevent them from being interpreted as multiple arguments.\n");
21  exit(1);
22  }
23 
24  std::string target_mm_ip = argv[1];
25  int target_mm_port = atoi(argv[2]);
26 
27  __COUTV__(target_mm_ip);
28  __COUTV__(target_mm_port);
29 
30  __COUT__ << "instantiating..." << __E__;
31  ots_mm_udp_interface mm(target_mm_ip.c_str(), target_mm_port);
32 
33  std::string feStr = mm.getFrontendList();
34  __COUTV__(feStr);
35  std::vector<std::string> fes = getVectorFromString(feStr, {';'});
36 
37  uint32_t fe = -1;
38  uint32_t cmd = -1;
39 
40  if(argc >= 4)
41  {
42  //look for FE
43  std::string target_fe = argv[3];
44  for(fe = 0; fe < fes.size(); ++fe)
45  if(fes[fe] == target_fe)
46  break;
47  if(fe >= fes.size())
48  {
49  __COUT_ERR__ << "Illegal front-end UID " << target_fe
50  << " not found in FE list: " << vectorToString(fes, {';'}) << "."
51  << __E__;
52  return 0;
53  }
54  }
55  else
56  {
57  __COUT_ERR__
58  << "No front-end UID specified, here is the list of existing Front-end UIDs:"
59  << __E__;
60  for(fe = 0; fe < fes.size(); ++fe)
61  __COUT_ERR__ << "\t\t" << fes[fe] << __E__;
62 
63  return 0;
64  }
65 
66  if(fe >= fes.size())
67  {
68  __COUT_ERR__ << "Illegal front-end index " << fe << " vs " << fes.size()
69  << " count." << __E__;
70  return 0;
71  }
72 
73  __COUTV__(fes[fe]);
74  std::string commandStr = mm.getCommandList(fes[fe]);
75  __COUTV__(commandStr);
76 
77  std::vector<std::string> commands = getVectorFromString(commandStr, {';'});
78 
79  if(argc >= 5)
80  {
81  //look for command
82  std::string target_command = argv[4];
83  for(cmd = 0; cmd < commands.size(); ++cmd)
84  if(commands[cmd] == target_command)
85  break;
86  if(cmd >= commands.size())
87  {
88  __COUT_ERR__ << "Illegal Front-end Macro Command name '" << target_command
89  << "' not found in '" << fes[fe]
90  << "' Command list: " << vectorToString(commands, {';'}) << "."
91  << __E__;
92  return 0;
93  }
94  }
95  else
96  {
97  __COUT_ERR__ << "No FE Macro Name specified, here is the list of existing FE "
98  "Macro Names corresponding to the specified Front-end UID '"
99  << argv[3] << "':" << __E__;
100  for(cmd = 0; cmd < commands.size(); ++cmd)
101  __COUT_ERR__ << "\t\t" << commands[cmd] << __E__;
102 
103  return 0;
104  }
105 
106  if(cmd >= commands.size())
107  {
108  __COUT_ERR__ << "Illegal command index " << cmd << " vs " << commands.size()
109  << " count." << __E__;
110  return 0;
111  }
112 
113  __COUTV__(commands[cmd]);
114 
115  uint32_t numberOfInputs = mm.getCommandInputCount(fes[fe], commands[cmd]);
116  __COUTV__(numberOfInputs);
117 
118  uint32_t numberOfOutputs = mm.getCommandOutputCount(fes[fe], commands[cmd]);
119  __COUTV__(numberOfOutputs);
120 
121  std::vector<std::string> inputs;
122  if(argc != int(5 + numberOfInputs))
123  {
124  __COUT_WARN__
125  << "Incorrect number of inputs provided for Front-end Macro Command name '"
126  << commands[cmd] << "' on FE UID '" << fes[fe] << "': " << argc - 5 << " vs "
127  << numberOfInputs << " expected." << __E__;
128  __COUT_INFO__ << "Will not run command." << __E__;
129 
130  for(uint32_t i = 0; i < numberOfInputs; ++i)
131  {
132  std::string inputName = mm.getCommandInputName(fes[fe], commands[cmd], i);
133  __COUT_INFO__ << "\tExpected input #" << i + 1 << ": " << inputName << __E__;
134  }
135  return 0;
136  }
137 
138  __COUTV__(vectorToString(inputs, {';'}));
139  for(uint32_t i = 0; i < numberOfInputs; ++i)
140  {
141  inputs.push_back(ots_mm_udp_interface::encodeURIComponent(argv[5 + i]));
142  std::string inputName = mm.getCommandInputName(fes[fe], commands[cmd], i);
143  __COUT_INFO__ << "\tInput #" << i << ": " << inputName << " = " << inputs.back()
144  << __E__;
145  }
146 
147  __COUT_INFO__ << "Running command with these expected outputs..." << __E__;
148  for(uint32_t i = 0; i < numberOfOutputs; ++i)
149  {
150  std::string outputName = mm.getCommandOutputName(fes[fe], commands[cmd], i);
151  __COUT_INFO__ << "\tOutput #" << i << ": " << outputName << __E__;
152  }
153 
154  std::string commandResult =
155  mm.runCommand(fes[fe], commands[cmd], vectorToString(inputs, {';'}));
156 
157  std::vector<std::string> outputs = getVectorFromString(commandResult, {';'});
158 
159  if(numberOfOutputs != outputs.size())
160  {
161  __COUTV__(commandResult);
162  __COUT_ERR__ << "Illegal response, mismatch in output arguments returned: "
163  << outputs.size() << " vs " << numberOfOutputs << " expected."
164  << __E__;
165  return 0;
166  }
167 
168  __COUTV__(vectorToString(outputs, {';'}));
169  for(uint32_t i = 0; i < numberOfOutputs; ++i)
170  {
171  std::string outputName = mm.getCommandOutputName(fes[fe], commands[cmd], i);
172  __COUT__ << "\tOutput #" << i << ": " << outputName << " = "
173  << mm.decodeURIComponent(outputs[i]) << __E__;
174  }
175  __COUT_INFO__ << "Done." << __E__;
176  return 0;
177 }
178 catch(const std::runtime_error& e)
179 {
180  __COUT_ERR__ << "Error caught during test execution: \n" << e.what() << __E__;
181  return 1;
182 }