InPortCorbaCdrConsumerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log: InPortCorbaCdrConsumerTests.cpp,v $
14  *
15  *
16  */
17 
18 #ifndef InPortCorbaCdrConsumer_cpp
19 #define InPortCorbaCdrConsumer_cpp
20 
21 #include <cppunit/ui/text/TestRunner.h>
22 #include <cppunit/TextOutputter.h>
23 #include <cppunit/extensions/TestFactoryRegistry.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/TestAssert.h>
26 
27 #include <rtm/idl/BasicDataTypeSkel.h>
28 #include <rtm/idl/DataPortSkel.h>
29 #include <rtm/RTC.h>
30 #include <rtm/Typename.h>
33 #include <rtm/CdrBufferBase.h>
34 #include <rtm/PortAdmin.h>
35 #include <rtm/CORBA_SeqUtil.h>
36 #include <rtm/NVUtil.h>
37 #include <rtm/ConnectorListener.h>
39 
45 {
46 
48  : public RTC::ConnectorDataListenerT<RTC::TimedLong>
49  {
50  public:
51  DataListener(const char* name) : m_name(name) {}
52  virtual ~DataListener()
53  {
54  //std::cout << "dtor of " << m_name << std::endl;
55  }
56 
57  virtual void operator()(const RTC::ConnectorInfo& info,
58  const RTC::TimedLong& data)
59  {
60  std::cout << "------------------------------" << std::endl;
61  std::cout << "Data Listener: " << m_name << std::endl;
62  std::cout << "Profile::name: " << info.name << std::endl;
63  std::cout << "------------------------------" << std::endl;
64  };
65  std::string m_name;
66  };
67 
68 
70  : public RTC::ConnectorListener
71  {
72  public:
73  ConnListener(const char* name) : m_name(name) {}
74  virtual ~ConnListener()
75  {
76  //std::cout << "dtor of " << m_name << std::endl;
77  }
78 
79  virtual void operator()(const RTC::ConnectorInfo& info)
80  {
81  std::cout << "------------------------------" << std::endl;
82  std::cout << "Connector Listener: " << m_name << std::endl;
83  std::cout << "Profile::name: " << info.name << std::endl;
84  std::cout << "------------------------------" << std::endl;
85  };
86  std::string m_name;
87  };
88 
95  {
96  public:
102  {
103  }
109  {
110  }
115  CORBA::Object_var get_m_objre()
116  {
117  return m_objref;
118  }
119  };
121  : public CppUnit::TestFixture
122  {
123  CPPUNIT_TEST_SUITE(InPortCorbaCdrConsumerTests);
124 
125  CPPUNIT_TEST(test_case0);
126 
127  CPPUNIT_TEST_SUITE_END();
128 
129  private:
130  CORBA::ORB_ptr m_pORB;
131  PortableServer::POA_ptr m_pPOA;
134 
135  public:
136 
141  {
142  int argc(0);
143  char** argv(NULL);
144  m_pORB = CORBA::ORB_init(argc, argv);
145  m_pPOA = PortableServer::POA::_narrow(
146  m_pORB->resolve_initial_references("RootPOA"));
147  m_pPOA->the_POAManager()->activate();
148  }
149 
154  {
155  delete connector;
156  }
157 
161  virtual void setUp()
162  {
163  }
164 
168  virtual void tearDown()
169  {
170  }
171 
176  void test_case0()
177  {
179 
180  //ConnectorInfo
182  coil::vstring ports;
183  RTC::ConnectorInfo info("name", "id", ports, prop);
184 
185  //ConnectorDataListeners
187  new DataListener("ON_BUFFER_WRITE"), true);
189  new DataListener("ON_BUFFER_FULL"), true);
191  new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
193  new DataListener("ON_BUFFER_OVERWRITE"), true);
195  new DataListener("ON_BUFFER_READ"), true);
197  new DataListener("ON_SEND"), true);
199  new DataListener("ON_RECEIVED"), true);
201  new DataListener("ON_RECEIVER_FULL"), true);
203  new DataListener("ON_RECEIVER_TIMEOUT"), true);
205  new DataListener("ON_RECEIVER_ERROR"), true);
206 
207  //ConnectorListeners
209  new ConnListener("ON_BUFFER_EMPTY"), true);
211  new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
213  new ConnListener("ON_SENDER_EMPTY"), true);
215  new ConnListener("ON_SENDER_TIMEOUT"), true);
217  new ConnListener("ON_SENDER_ERROR"), true);
219  new ConnListener("ON_CONNECT"), true);
221  new ConnListener("ON_DISCONNECT"), true);
222 
223  RTC::ConnectorProfile prof;
224  bool ret;
225  int testdata[8] = { 12,34,56,78,90,23,45,99 };
226 
227  consumer.init(prop);
228  ret = consumer.subscribeInterface(prof.properties);
229  //ior を設定していないのでfalseとなることを確認する。
230  CPPUNIT_ASSERT_EQUAL(false, ret);
231 
233  provider.setListener(info, &m_listeners);
234  consumer.publishInterfaceProfile(prof.properties);
235 
236  CORBA_SeqUtil::push_back(prof.properties,
237  NVUtil::newNV("dataport.interface_type",
238  "corba_cdr"));
239  provider.publishInterface(prof.properties);
240 
241  ret = consumer.subscribeInterface(prof.properties);
242  CPPUNIT_ASSERT_EQUAL(true, ret);
243 
244  cdrMemoryStream indata;
245  indata.setByteSwapFlag(true);
247 
248  for(int ic(0);ic<8;++ic)
249  {
250  RTC::TimedLong td;
251  td.data = testdata[ic];
252  td >>= indata;
253  }
254 
255  //provider 側の buffer がない状態でコール(error)
256  retcode = consumer.put(indata);
257  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)1, retcode);
258 
259  RTC::CdrBufferBase* buffer;
260  buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
261  provider.setBuffer(buffer);
262 
263  connector = new RTC::InPortPushConnector(info, &provider, m_listeners, buffer);
264  if (connector == 0)
265  {
266  std::cout << "ERROR: PushConnector creation failed." << std::endl;
267  }
268  provider.setConnector(connector);
269 
270  for(int ic(0);ic<8;++ic)
271  {
272  retcode = consumer.put(indata);
273  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)0, retcode);
274  }
275 
276  //full の状態でコール(full)
277  retcode = consumer.put(indata);
278  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)0, retcode);
279 
280  for(int icc(0);icc<8;++icc)
281  {
282  cdrMemoryStream cdr;
283  buffer->read(cdr);
284 
285  CORBA::ULong inlen = cdr.bufSize();
286  CPPUNIT_ASSERT_EQUAL(96,(int)inlen);
287  RTC::TimedLong rtd;
288  rtd <<= cdr;
289  CPPUNIT_ASSERT_EQUAL(testdata[0], (int)rtd.data);
290  }
291 
292  CPPUNIT_ASSERT(!CORBA::is_nil(consumer.get_m_objre()));
293  consumer.unsubscribeInterface(prof.properties);
294  CPPUNIT_ASSERT(CORBA::is_nil(consumer.get_m_objre()));
295 
296  int index;
297  index = NVUtil::find_index(prof.properties,
298  "dataport.corba_cdr.inport_ior");
299  const char* ior;
300  if (prof.properties[index].value >>= ior)
301  {
302  CORBA::Object_ptr var = m_pORB->string_to_object(ior);
303  PortableServer::Servant ser = m_pPOA->reference_to_servant(var);
304  m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ser));
305  }
306 
307  // delete connector;
308  // ここで delete すると落ちるので、デストラクタで行う。
309  }
310 
311  };
312 }; // namespace OutPortBase
313 
314 /*
315  * Register test suite
316  */
318 
319 #ifdef LOCAL_MAIN
320 int main(int argc, char* argv[])
321 {
322 
323  FORMAT format = TEXT_OUT;
324  int target = 0;
325  std::string xsl;
326  std::string ns;
327  std::string fname;
328  std::ofstream ofs;
329 
330  int i(1);
331  while (i < argc)
332  {
333  std::string arg(argv[i]);
334  std::string next_arg;
335  if (i + 1 < argc) next_arg = argv[i + 1];
336  else next_arg = "";
337 
338  if (arg == "--text") { format = TEXT_OUT; break; }
339  if (arg == "--xml")
340  {
341  if (next_arg == "")
342  {
343  fname = argv[0];
344  fname += ".xml";
345  }
346  else
347  {
348  fname = next_arg;
349  }
350  format = XML_OUT;
351  ofs.open(fname.c_str());
352  }
353  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
354  if ( arg == "--cerr" ) { target = 1; break; }
355  if ( arg == "--xsl" )
356  {
357  if (next_arg == "") xsl = "default.xsl";
358  else xsl = next_arg;
359  }
360  if ( arg == "--namespace" )
361  {
362  if (next_arg == "")
363  {
364  std::cerr << "no namespace specified" << std::endl;
365  exit(1);
366  }
367  else
368  {
369  xsl = next_arg;
370  }
371  }
372  ++i;
373  }
374  CppUnit::TextUi::TestRunner runner;
375  if ( ns.empty() )
376  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
377  else
378  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
379  CppUnit::Outputter* outputter = 0;
380  std::ostream* stream = target ? &std::cerr : &std::cout;
381  switch ( format )
382  {
383  case TEXT_OUT :
384  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
385  break;
386  case XML_OUT :
387  std::cout << "XML_OUT" << std::endl;
388  outputter = new CppUnit::XmlOutputter(&runner.result(),
389  ofs, "shift_jis");
390  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
391  break;
392  case COMPILER_OUT :
393  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
394  break;
395  }
396  runner.setOutputter(outputter);
397  runner.run();
398  return 0; // runner.run() ? 0 : 1;
399 }
400 #endif // MAIN
401 #endif // OutPortBase_cpp
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
InPortPushConnector class.
ConnectorListeners class.
virtual bool subscribeInterface(const SDOPackage::NVList &properties)
Subscribe to the data sending notification.
int main(int argc, char **argv)
InPortConnector base class.
InPortCorbaCdrConsumer class.
ConnectorListener class.
void addListener(ConnectorDataListener *listener, bool autoclean)
Add the listener.
ConnectorListenerHolder connector_[CONNECTOR_LISTENER_NUM]
ConnectorListenerType listener array The ConnectorListenerType listener is stored.
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
virtual void unsubscribeInterface(const SDOPackage::NVList &properties)
Unsubscribe the data send notification.
virtual ReturnCode put(const cdrMemoryStream &data)
Send data to the destination port.
Typename function.
void addListener(ConnectorListener *listener, bool autoclean)
Add the listener.
virtual void operator()(const RTC::ConnectorInfo &info, const RTC::TimedLong &data)
Virtual Callback method.
static GlobalFactory< AbstractClass, Identifier, Compare, Creator, Destructor > & instance()
Create instance.
Definition: Singleton.h:131
InPortCorbaCdrProvider class.
std::vector< std::string > vstring
Definition: stringutil.h:37
virtual void setListener(ConnectorInfo &info, ConnectorListeners *listeners)
Set the listener.
const CORBA::Long find_index(const SDOPackage::NVList &nv, const char *name)
Return the index of element specified by name from NVList.
Definition: NVUtil.cpp:227
ConnectorDataListenerHolder connectorData_[CONNECTOR_DATA_LISTENER_NUM]
ConnectorDataListenerType listener array The ConnectorDataListenerType listener is stored...
InPortCorbaCdrProvider class.
virtual void setConnector(InPortConnector *connector)
set Connector
ConnectorDataListenerT class.
list index
Definition: rtimages.py:10
NameValue and NVList utility functions.
CORBA sequence utility template functions.
Push type connector class.
std::string name
Connection name.
prop
Organization::get_organization_property ();.
virtual ReturnCode read(DataType &value, long int sec=-1, long int nsec=-1)=0
Read data from the buffer.
virtual void operator()(const RTC::ConnectorInfo &info)
Virtual Callback method.
Class represents a set of properties.
Definition: Properties.h:101
virtual void setBuffer(BufferBase< cdrMemoryStream > *buffer)
Setting outside buffer&#39;s pointer.
InPortCorbaCdrConsumer class.
virtual bool publishInterface(SDOPackage::NVList &properties)
Publish interface information.
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
RTComponent header.
BufferBase abstract class.
Definition: BufferBase.h:104
connector listener class
CPPUNIT_TEST_SUITE_REGISTRATION(InPortCorbaCdrConsumer::InPortCorbaCdrConsumerTests)
Enum
DataPortStatus return codes.
RTC&#39;s Port administration class.
virtual void init(coil::Properties &prop)
Initializing configuration.
virtual void publishInterfaceProfile(SDOPackage::NVList &properties)
Publish InterfaceProfile information.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:52