SimpleIO/ConnectorComp.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
16 #include <iostream>
17 #include <vector>
18 #include <string>
19 #include <rtm/CorbaNaming.h>
20 #include <rtm/RTObject.h>
21 #include <rtm/NVUtil.h>
22 #include <rtm/CORBA_SeqUtil.h>
23 #include <rtm/CorbaConsumer.h>
24 #include <assert.h>
25 #include <coil/stringutil.h>
26 
27 
28 using namespace RTC;
29 
30 void usage()
31 {
32  std::cout << std::endl;
33  std::cout << "usage: ConnectorComp [options].." << std::endl;
34  std::cout << std::endl;
35  std::cout << " --port * ";
36  std::cout << ": Set port no 2809 (default:9876)" << std::endl;
37  std::cout << " --origin * ";
38  std::cout << ": Set origin of connection OUT (default:IN)" << std::endl;
39  std::cout << " : Set origin OUT , ConsoleOut -> ConsoleIn" << std::endl;
40  std::cout << " : default connection, ConsoleIn -> ConsoleOut" << std::endl;
41  std::cout << " --flush ";
42  std::cout << ": Set subscription type flush (default:flush)" << std::endl;
43  std::cout << " --new ";
44  std::cout << ": Set subscription type new" << std::endl;
45  std::cout << " --periodic [Hz] ";
46  std::cout << ": Set subscription type periodic" << std::endl;
47  std::cout << " --policy [any] ";
48  std::cout << ": Set push policy ALL or FIFO or SKIP or NEW" << std::endl;
49  std::cout << " --skip [n] ";
50  std::cout << ": Set skip count 0..n" << std::endl;
51  std::cout << " --endian * ";
52  std::cout << ": Set endian type Little or Big" << std::endl;
53  std::cout << " --buffer : Set buffer configuration" << std::endl;
54  std::cout << " --buffer [inport|outport].buffer.[length|[read|write.[policy|timeout]] value" << std::endl;
55  std::cout << " policy: [write.full_policy,read.empty_policy]" << std::endl;
56  std::cout << " timeout: [write.timeout,read.timeout]" << std::endl;
57  std::cout << " value: " << std::endl;
58  std::cout << " full_policy: [overwrite, do_nothing, block]" << std::endl;
59  std::cout << " empty_policy: [readback, do_nothing, block]" << std::endl;
60  std::cout << " timeout: sec (e.g. 1.0)" << std::endl;
61  std::cout << " length: The length of the ring buffer. (e.g. 10)" << std::endl;
62  std::cout << std::endl;
63  std::cout << "exsample:" << std::endl;
64  std::cout << " ConnectorComp --port 2809 --origin OUT" << std::endl;
65  std::cout << " ConnectorComp --flush" << std::endl;
66  std::cout << " ConnectorComp --new" << std::endl;
67  std::cout << " ConnectorComp --new --policy ALL" << std::endl;
68  std::cout << " ConnectorComp --new --policy SKIP --skip 100" << std::endl;
69  std::cout << " ConnectorComp --periodic 10" << std::endl;
70  std::cout << " ConnectorComp --periodic 10 --policy FIFO" << std::endl;
71  std::cout << " ConnectorComp --periodic 10 --policy NEW" << std::endl;
72  std::cout << " ConnectorComp --flush --endian Little" << std::endl;
73  std::cout << " ConnectorComp --buffer inport.buffer.read.empty_policy block \\" << std::endl;
74  std::cout << " --buffer inport.buffer.read.timeout 0.1 \\ " << std::endl;
75  std::cout << " --buffer inport.buffer.length 10 " << std::endl;
76  std::cout << std::endl;
77 }
78 
79 int main (int argc, char** argv)
80 {
81  int _argc(0);
82  char** _argv(0);
83 
84  std::string subs_type("flush");
85  std::string period;
86  std::string push_policy;
87  std::string skip_count;
88  std::string endian;
89  std::string port_no("9876");
90  std::string connect_origin("in");
91  std::map<std::string, std::string> buffer_prop;
92 
93  if (argc < 2)
94  {
95  usage();
96  }
97 
98  for (int i = 1; i < argc; ++i)
99  {
100  std::string arg(argv[i]);
101  coil::normalize(arg);
102  if (arg == "--flush") subs_type = "flush";
103  else if (arg == "--new") subs_type = "new";
104  else if (arg == "--periodic")
105  {
106  subs_type = "periodic";
107  if (++i < argc) period = argv[i];
108  else period = "1.0";
109  }
110  else if (arg == "--help")
111  {
112  usage();
113  exit(1);
114  }
115  else if (arg == "--policy")
116  {
117  if (++i < argc)
118  {
119  std::string arg2(argv[i]);
120  coil::normalize(arg2);
121  push_policy = arg2;
122  }
123  else push_policy = "new";
124  }
125  else if (arg == "--skip")
126  {
127  if (++i < argc) skip_count = argv[i];
128  else skip_count = "0";
129  }
130 
131  if (arg == "--endian")
132  {
133  if (++i < argc)
134  {
135  std::string arg2(argv[i]);
136  coil::normalize(arg2);
137  endian = arg2;
138  }
139  else endian = "";
140  }
141  if (arg == "--port")
142  {
143  if (++i < argc)
144  {
145  port_no = argv[i];
146  }
147  }
148  if (arg == "--origin")
149  {
150  if (++i < argc)
151  {
152  std::string arg2(argv[i]);
153  coil::normalize(arg2);
154  connect_origin = arg2;
155  }
156  }
157  if (arg == "--buffer")
158  {
159  if (++i < argc)
160  {
161  std::string key(argv[i]);
162  key = coil::normalize(key);
163  std::string val(argv[++i]);
164  val = coil::normalize(val);
165  buffer_prop.insert(std::pair< std::string, std::string >(key,val));
166  }
167  }
168  }
169 
170  CORBA::ORB_var orb = CORBA::ORB_init(_argc, _argv);
171  std::string name_server("localhost:");
172  name_server.append(port_no);
173  CorbaNaming naming(orb, name_server.c_str());
174 
175  CorbaConsumer<RTObject> conin, conout;
176  PortServiceList_var pin;
177  PortServiceList_var pout;
178 
179  // option dump
180  std::cout << " Name Server: " << name_server << std::endl;
181  std::cout << "Subscription Type: " << subs_type << std::endl;
182  if (period != "")
183  std::cout << " Period: " << period << " [Hz]" << std::endl;
184 
185  std::cout << " push policy: " << push_policy << std::endl;
186  std::cout << " skip count: " << skip_count << std::endl;
187 
188  if (endian != "")
189  std::cout << " endian: " << endian << std::endl;
190 
191  if (connect_origin == "in")
192  std::cout << " connect: ConsoleIn -> ConsoleOut" << std::endl;
193  else
194  std::cout << " connect: ConsoleOut -> ConsoleIn" << std::endl;
195  std::cout << std::endl;
196 
197  // find ConsoleIn0 component
198  conin.setObject(naming.resolve("ConsoleIn0.rtc"));
199 
200  // get ports
201  pin = conin->get_ports();
202  pin[(CORBA::ULong)0]->disconnect_all();
203  assert(pin->length() > 0);
204  // activate ConsoleIn0
205  ExecutionContextList_var eclisti;
206  eclisti = conin->get_owned_contexts();
207  eclisti[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conin._ptr()));
208 
209  // find ConsoleOut0 component
210  conout.setObject(naming.resolve("ConsoleOut0.rtc"));
211  // get ports
212  pout = conout->get_ports();
213  pout[(CORBA::ULong)0]->disconnect_all();
214  assert(pout->length() > 0);
215  // activate ConsoleOut0
216  ExecutionContextList_var eclisto;
217  eclisto = conout->get_owned_contexts();
218  eclisto[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conout._ptr()));
219 
220  // connect ports
221  ConnectorProfile prof;
222  prof.connector_id = (char*)"";
223  prof.name = CORBA::string_dup("connector0");
224  prof.ports.length(2);
225  prof.ports[0] = pin[(CORBA::ULong)0];
226  prof.ports[1] = pout[(CORBA::ULong)0];
227  CORBA_SeqUtil::push_back(prof.properties,
228  NVUtil::newNV("dataport.interface_type",
229  "corba_cdr"));
230  CORBA_SeqUtil::push_back(prof.properties,
231  NVUtil::newNV("dataport.dataflow_type",
232  "push"));
233  if (subs_type != "")
234  CORBA_SeqUtil::push_back(prof.properties,
235  NVUtil::newNV("dataport.subscription_type",
236  subs_type.c_str()));
237  else
238  CORBA_SeqUtil::push_back(prof.properties,
239  NVUtil::newNV("dataport.subscription_type",
240  "flush"));
241  if (subs_type == "periodic" && period != "")
242  CORBA_SeqUtil::push_back(prof.properties,
243  NVUtil::newNV("dataport.publisher.push_rate",
244  period.c_str()));
245  if (push_policy != "")
246  CORBA_SeqUtil::push_back(prof.properties,
247  NVUtil::newNV("dataport.publisher.push_policy",
248  push_policy.c_str()));
249  if (push_policy == "skip" && skip_count != "")
250  CORBA_SeqUtil::push_back(prof.properties,
251  NVUtil::newNV("dataport.publisher.skip_count",
252  skip_count.c_str()));
253  if (endian != "")
254  CORBA_SeqUtil::push_back(prof.properties,
255  NVUtil::newNV("dataport.serializer.cdr.endian",
256  endian.c_str()));
257 
258  std::map<std::string, std::string>::iterator it=buffer_prop.begin();
259  while (it != buffer_prop.end()) {
260  std::string key("dataport.");
261  key += it->first;
262  CORBA_SeqUtil::push_back(prof.properties,
263  NVUtil::newNV(key.c_str(),
264  it->second.c_str()));
265  ++it;
266  }
267 
269  if (connect_origin == "in")
270  {
271  ret = pin[(CORBA::ULong)0]->connect(prof);
272  }
273  else
274  {
275  ret = pout[(CORBA::ULong)0]->connect(prof);
276  }
277 
278  assert(ret == RTC::RTC_OK);
279 
280  std::cout << "Connector ID: " << prof.connector_id << std::endl;
281  NVUtil::dump(prof.properties);
282 
283  orb->destroy();
284  exit(1);
285 }
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
std::string normalize(std::string &str)
Erase the head/tail blank and replace upper case to lower case.
Definition: stringutil.cpp:303
CORBA Consumer class.
RT-Component.
ReturnCode_t
Definition: doil.h:53
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
virtual bool setObject(CORBA::Object_ptr obj)
Set Object.
CORBA naming service helper class.
NameValue and NVList utility functions.
CORBA sequence utility template functions.
void dump(const SDOPackage::NVList &nv)
Print information configured in NVList as a string type to Standard Outport.
Definition: NVUtil.cpp:394
void usage()
int main(int argc, char **argv)
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:25:57