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