ExtTrigger/ConnectorComp.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
16 #include <iostream>
17 #include <vector>
18 #include <string>
19 #include <rtm/idl/OpenRTMSkel.h>
20 #include <rtm/CorbaNaming.h>
21 #include <rtm/RTObject.h>
22 #include <rtm/NVUtil.h>
23 #include <rtm/CORBA_SeqUtil.h>
24 #include <rtm/CorbaConsumer.h>
25 #include <assert.h>
26 #include <coil/stringutil.h>
27 
28 
29 using namespace RTC;
30 
31 #ifdef __QNX__
32 using std::exit;
33 #endif
34 
35 void usage()
36 {
37  std::cout << std::endl;
38  std::cout << "usage: ConnectorCompExt [options].." << std::endl;
39  std::cout << std::endl;
40  std::cout << " --flush ";
41  std::cout << ": Set subscription type flush" << std::endl;
42  std::cout << " --new ";
43  std::cout << ": Set subscription type new" << std::endl;
44  std::cout << " --periodic [Hz] ";
45  std::cout << ": Set subscription type periodic" << std::endl;
46  std::cout << " --policy [any] ";
47  std::cout << ": Set push policy ALL or FIFO or SKIP or NEW" << std::endl;
48  std::cout << " --skip [n] ";
49  std::cout << ": Set skip count 0..n" << std::endl;
50  std::cout << std::endl;
51  std::cout << "exsample:" << std::endl;
52  std::cout << " ConnectorCompExt --flush" << std::endl;
53  std::cout << " ConnectorCompExt --new" << std::endl;
54  std::cout << " ConnectorCompExt --new --policy ALL" << std::endl;
55  std::cout << " ConnectorCompExt --new --policy SKIP --skip 100" << std::endl;
56  std::cout << " ConnectorCompExt --periodic 10" << std::endl;
57  std::cout << " ConnectorCompExt --periodic 10 --policy FIFO" << std::endl;
58  std::cout << " ConnectorCompExt --periodic 10 --policy NEW" << std::endl;
59  std::cout << std::endl;
60 }
61 
62 int main (int argc, char** argv)
63 {
64  int _argc(0);
65  char** _argv(0);
66 
67  std::string subs_type;
68  std::string period;
69  std::string push_policy;
70  std::string skip_count;
71  if (argc < 2)
72  {
73  usage();
74  }
75 
76  for (int i = 1; i < argc; ++i)
77  {
78  std::string arg(argv[i]);
79  coil::normalize(arg);
80  if (arg == "--flush") subs_type = "flush";
81  else if (arg == "--new") subs_type = "new";
82  else if (arg == "--periodic")
83  {
84  subs_type = "periodic";
85  if (++i < argc) period = argv[i];
86  else period = "1.0";
87  }
88  else if (arg == "--help")
89  {
90  usage();
91  exit(1);
92  }
93  else if (arg == "--policy")
94  {
95  if (++i < argc)
96  {
97  std::string arg2(argv[i]);
98  coil::normalize(arg2);
99  push_policy = arg2;
100  }
101  else push_policy = "new";
102  }
103  else if (arg == "--skip")
104  {
105  if (++i < argc) skip_count = argv[i];
106  else skip_count = "0";
107  }
108  else
109  {
110  subs_type = "flush";
111  }
112  }
113 
114  std::cout << "Subscription Type: " << subs_type << std::endl;
115  if (period != "")
116  std::cout << "Period: " << period << " [Hz]" << std::endl;
117  std::cout << "push policy: " << push_policy << std::endl;
118  std::cout << "skip count: " << skip_count << std::endl;
119 
120 
121  CORBA::ORB_var orb = CORBA::ORB_init(_argc, _argv);
122  CorbaNaming naming(orb, "localhost:9876");
123 
124  CorbaConsumer<RTObject> conin, conout;
126 
127  PortServiceList_var pin;
128  PortServiceList_var pout;
129 
130  // find ConsoleIn0 component
131  conin.setObject(naming.resolve("ConsoleIn0.rtc"));
132 
133  // get ports
134  pin = conin->get_ports();
135  pin[(CORBA::ULong)0]->disconnect_all();
136  assert(pin->length() > 0);
137  // activate ConsoleIn0
138  ExecutionContextList_var eclisti;
139  eclisti = conin->get_owned_contexts();
140  eclisti[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conin._ptr()));
141  ec0.setObject(eclisti[(CORBA::ULong)0]);
142 
143  // find ConsoleOut0 component
144  conout.setObject(naming.resolve("ConsoleOut0.rtc"));
145  // get ports
146  pout = conout->get_ports();
147  pout[(CORBA::ULong)0]->disconnect_all();
148  assert(pout->length() > 0);
149  // activate ConsoleOut0
150  ExecutionContextList_var eclisto;
151  eclisto = conout->get_owned_contexts();
152  eclisto[(CORBA::ULong)0]->activate_component(RTObject::_duplicate(conout._ptr()));
153  ec1.setObject(eclisto[(CORBA::ULong)0]);
154 
155  // connect ports
156  ConnectorProfile prof;
157  prof.connector_id = "";
158  prof.name = CORBA::string_dup("connector0");
159  prof.ports.length(2);
160  prof.ports[0] = pin[(CORBA::ULong)0];
161  prof.ports[1] = pout[(CORBA::ULong)0];
162  CORBA_SeqUtil::push_back(prof.properties,
163  NVUtil::newNV("dataport.interface_type",
164  "corba_cdr"));
165  CORBA_SeqUtil::push_back(prof.properties,
166  NVUtil::newNV("dataport.dataflow_type",
167  "push"));
168  if (subs_type != "")
169  CORBA_SeqUtil::push_back(prof.properties,
170  NVUtil::newNV("dataport.subscription_type",
171  subs_type.c_str()));
172  else
173  CORBA_SeqUtil::push_back(prof.properties,
174  NVUtil::newNV("dataport.subscription_type",
175  "flush"));
176  if (subs_type == "periodic" && period != "")
177  CORBA_SeqUtil::push_back(prof.properties,
178  NVUtil::newNV("dataport.publisher.push_rate",
179  period.c_str()));
180  if (push_policy != "")
181  CORBA_SeqUtil::push_back(prof.properties,
182  NVUtil::newNV("dataport.publisher.push_policy",
183  push_policy.c_str()));
184  if (push_policy == "skip" && skip_count != "")
185  CORBA_SeqUtil::push_back(prof.properties,
186  NVUtil::newNV("dataport.publisher.skip_count",
187  skip_count.c_str()));
188 
190  ret = pin[(CORBA::ULong)0]->connect(prof);
191  assert(ret == RTC::RTC_OK);
192 
193  std::cout << "Connector ID: " << prof.connector_id << std::endl;
194  NVUtil::dump(prof.properties);
195 
196  std::string cmd;
197  while (1)
198  {
199  try
200  {
201  std::cout << std::endl;
202  std::cout << std::endl;
203  std::cout << "0: tick ConsoleIn component" << std::endl;
204  std::cout << "1: tick ConsoleOut component" << std::endl;
205  std::cout << "2: tick both components" << std::endl;
206  std::cout << "cmd? >";
207  std::cin >> cmd;
208  if (cmd == "0")
209  {
210  ec0->tick();
211  }
212  else if (cmd == "1")
213  {
214  ec1->tick();
215  }
216  else if (cmd == "2")
217  {
218  ec0->tick();
219  ec1->tick();
220  }
221  }
222  catch (...)
223  {
224  }
225  }
226  orb->destroy();
227  exit(1);
228 }
229 
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)
CORBA::Object_ptr resolve(const CosNaming::Name &name)
Return object bound on the specified NameComponent.
list cmd
Definition: omniwxs.py:88
virtual bool setObject(CORBA::Object_ptr obj)
Set Object.
int main(int argc, char **argv)
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
ObjectTypePtr _ptr()
Get Object reference narrowed as ObjectType.
void usage()
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