InPortCorbaCdrProviderTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log: InPortCorbaCdrProviderTests.cpp,v $
14  *
15  *
16  */
17 
18 #ifndef InPortCorbaCdrProvider_cpp
19 #define InPortCorbaCdrProvider_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/RTC.h>
29 #include <rtm/Typename.h>
31 #include <rtm/CdrBufferBase.h>
32 #include <rtm/PortAdmin.h>
33 #include <rtm/CORBA_SeqUtil.h>
34 #include <rtm/ConnectorListener.h>
36 
42 {
43 
45  : public RTC::ConnectorDataListenerT<RTC::TimedLong>
46  {
47  public:
48  DataListener(const char* name) : m_name(name) {}
49  virtual ~DataListener()
50  {
51  //std::cout << "dtor of " << m_name << std::endl;
52  }
53 
54  virtual void operator()(const RTC::ConnectorInfo& info,
55  const RTC::TimedLong& data)
56  {
57  std::cout << "------------------------------" << std::endl;
58  std::cout << "Data Listener: " << m_name << std::endl;
59  std::cout << "Profile::name: " << info.name << std::endl;
60  std::cout << "------------------------------" << std::endl;
61  };
62  std::string m_name;
63  };
64 
65 
67  : public RTC::ConnectorListener
68  {
69  public:
70  ConnListener(const char* name) : m_name(name) {}
71  virtual ~ConnListener()
72  {
73  //std::cout << "dtor of " << m_name << std::endl;
74  }
75 
76  virtual void operator()(const RTC::ConnectorInfo& info)
77  {
78  std::cout << "------------------------------" << std::endl;
79  std::cout << "Connector Listener: " << m_name << std::endl;
80  std::cout << "Profile::name: " << info.name << std::endl;
81  std::cout << "------------------------------" << std::endl;
82  };
83  std::string m_name;
84  };
85 
92  {
93  public:
99  {
100  }
106  {
107  }
113  {
114  return m_properties;
115  }
116  };
118  : public CppUnit::TestFixture
119  {
120  CPPUNIT_TEST_SUITE(InPortCorbaCdrProviderTests);
121 
122  CPPUNIT_TEST(test_case0);
123  CPPUNIT_TEST_SUITE_END();
124 
125  private:
126  CORBA::ORB_ptr m_pORB;
127  PortableServer::POA_ptr m_pPOA;
130 
131  public:
132 
137  {
138  int argc(0);
139  char** argv(NULL);
140  m_pORB = CORBA::ORB_init(argc, argv);
141  m_pPOA = PortableServer::POA::_narrow(
142  m_pORB->resolve_initial_references("RootPOA"));
143  m_pPOA->the_POAManager()->activate();
144  }
145 
150  {
151  delete connector;
152  }
153 
157  virtual void setUp()
158  {
159  }
160 
164  virtual void tearDown()
165  {
166  }
167 
172  void test_case0()
173  {
175  CORBA::Long index;
176 
177  //ConnectorInfo
179  coil::vstring ports;
180  RTC::ConnectorInfo info("name", "id", ports, prop);
181 
182  //ConnectorDataListeners
184  new DataListener("ON_BUFFER_WRITE"), true);
186  new DataListener("ON_BUFFER_FULL"), true);
188  new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
190  new DataListener("ON_BUFFER_OVERWRITE"), true);
192  new DataListener("ON_BUFFER_READ"), true);
194  new DataListener("ON_SEND"), true);
196  new DataListener("ON_RECEIVED"), true);
198  new DataListener("ON_RECEIVER_FULL"), true);
200  new DataListener("ON_RECEIVER_TIMEOUT"), true);
202  new DataListener("ON_RECEIVER_ERROR"), true);
203 
204  //ConnectorListeners
206  new ConnListener("ON_BUFFER_EMPTY"), true);
208  new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
210  new ConnListener("ON_SENDER_EMPTY"), true);
212  new ConnListener("ON_SENDER_TIMEOUT"), true);
214  new ConnListener("ON_SENDER_ERROR"), true);
216  new ConnListener("ON_CONNECT"), true);
218  new ConnListener("ON_DISCONNECT"), true);
219 
220  //IOR をプロパティに追加することを確認
221  index = NVUtil::find_index(provider.get_m_properties(),"dataport.corba_cdr.inport_ior");
222  CPPUNIT_ASSERT(0<=index);
223 
224  //ref をプロパティに追加することを確認
225  index = NVUtil::find_index(provider.get_m_properties(),"dataport.corba_cdr.inport_ref");
226  CPPUNIT_ASSERT(0<=index);
227 
228  provider.init(prop);
229  provider.setListener(info, &m_listeners);
230 
231  ::OpenRTM::PortStatus ret;
232  ::OpenRTM::CdrData data;
233  int testdata[10] = { 0,1,2,3,4,5,6,7,8,9 };
234  cdrMemoryStream cdr;
235  RTC::TimedLong td;
236  CORBA::ULong len;
237 
238  td.data = testdata[0];
239  td >>= cdr;
240  len = (CORBA::ULong)cdr.bufSize();
241  data.length(len);
242  cdr.get_octet_array(&(data[0]), len);
243  ret = provider.put(data);
244 
245  // buffer 未設定によるPORT_ERROR
246  CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_ERROR,ret);
247 
248  RTC::CdrBufferBase* buffer;
249  buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
250  provider.setBuffer(buffer);
251 
252  connector = new RTC::InPortPushConnector(info, &provider, m_listeners, buffer);
253  if (connector == 0)
254  {
255  std::cout << "ERROR: Connector creation failed." << std::endl;
256  }
257  provider.setConnector(connector);
258 
259  ret = provider.put(data);
260  CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
261 
262  for( int i(1); i<7; ++i )
263  {
264  OpenRTM::PortStatus ret;
265  cdrMemoryStream cdr;
266  RTC::TimedLong td;
267  CORBA::ULong len;
268  td.data = testdata[i];
269  td >>= cdr;
270  len = (CORBA::ULong)cdr.bufSize();
271  data.length(len);
272  cdr.get_octet_array(&(data[0]), len);
273  ret = provider.put(data);
274  CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
275 
276  }
277  cdrMemoryStream cdr2;
278  td.data = testdata[7];
279  td >>= cdr2;
280  len = (CORBA::ULong)cdr2.bufSize();
281  data.length(len);
282  cdr2.get_octet_array(&(data[0]), len);
283  ret = provider.put(data);
284  CPPUNIT_ASSERT_EQUAL(::OpenRTM::PORT_OK,ret);
285 
286  // delete connector;
287  // ここで delete すると落ちるので、デストラクタで行う。
288  }
289 
290  };
291 }; // namespace InPortCorbaCdrProvider
292 
293 /*
294  * Register test suite
295  */
297 
298 #ifdef LOCAL_MAIN
299 int main(int argc, char* argv[])
300 {
301 
302  FORMAT format = TEXT_OUT;
303  int target = 0;
304  std::string xsl;
305  std::string ns;
306  std::string fname;
307  std::ofstream ofs;
308 
309  int i(1);
310  while (i < argc)
311  {
312  std::string arg(argv[i]);
313  std::string next_arg;
314  if (i + 1 < argc) next_arg = argv[i + 1];
315  else next_arg = "";
316 
317  if (arg == "--text") { format = TEXT_OUT; break; }
318  if (arg == "--xml")
319  {
320  if (next_arg == "")
321  {
322  fname = argv[0];
323  fname += ".xml";
324  }
325  else
326  {
327  fname = next_arg;
328  }
329  format = XML_OUT;
330  ofs.open(fname.c_str());
331  }
332  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
333  if ( arg == "--cerr" ) { target = 1; break; }
334  if ( arg == "--xsl" )
335  {
336  if (next_arg == "") xsl = "default.xsl";
337  else xsl = next_arg;
338  }
339  if ( arg == "--namespace" )
340  {
341  if (next_arg == "")
342  {
343  std::cerr << "no namespace specified" << std::endl;
344  exit(1);
345  }
346  else
347  {
348  xsl = next_arg;
349  }
350  }
351  ++i;
352  }
353  CppUnit::TextUi::TestRunner runner;
354  if ( ns.empty() )
355  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
356  else
357  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
358  CppUnit::Outputter* outputter = 0;
359  std::ostream* stream = target ? &std::cerr : &std::cout;
360  switch ( format )
361  {
362  case TEXT_OUT :
363  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
364  break;
365  case XML_OUT :
366  std::cout << "XML_OUT" << std::endl;
367  outputter = new CppUnit::XmlOutputter(&runner.result(),
368  ofs, "shift_jis");
369  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
370  break;
371  case COMPILER_OUT :
372  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
373  break;
374  }
375  runner.setOutputter(outputter);
376  runner.run();
377  return 0; // runner.run() ? 0 : 1;
378 }
379 #endif // MAIN
380 #endif // InPortCorbaCdrProvider_cpp
InPortPushConnector class.
ConnectorListeners class.
int main(int argc, char **argv)
InPortConnector base class.
ConnectorListener class.
virtual void operator()(const RTC::ConnectorInfo &info)
Virtual Callback method.
void addListener(ConnectorDataListener *listener, bool autoclean)
Add the listener.
std::vector< std::pair< std::string, std::string > > NVList
Definition: IRTC.h:67
ConnectorListenerHolder connector_[CONNECTOR_LISTENER_NUM]
ConnectorListenerType listener array The ConnectorListenerType listener is stored.
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
Typename function.
void addListener(ConnectorListener *listener, bool autoclean)
Add the listener.
static GlobalFactory< AbstractClass, Identifier, Compare, Creator, Destructor > & instance()
Create instance.
Definition: Singleton.h:131
InPortCorbaCdrProvider class.
CPPUNIT_TEST_SUITE_REGISTRATION(InPortCorbaCdrProvider::InPortCorbaCdrProviderTests)
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
CORBA sequence utility template functions.
Push type connector class.
std::string name
Connection name.
prop
Organization::get_organization_property ();.
virtual void operator()(const RTC::ConnectorInfo &info, const RTC::TimedLong &data)
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.
virtual void init(coil::Properties &prop)
Initializing configuration.
RTComponent header.
BufferBase abstract class.
Definition: BufferBase.h:104
connector listener class
virtual ::OpenRTM::PortStatus put(const ::OpenRTM::CdrData &data)
[CORBA interface] Write data into the buffer
RTC&#39;s Port administration class.


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