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