OutPortCorbaCdrConsumerTests.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
12 /*
13  * $Log: OutPortCorbaCdrConsumerTests.cpp,v $
14  *
15  *
16  */
17 
18 #ifndef OutPortCorbaCdrConsumer_cpp
19 #define OutPortCorbaCdrConsumer_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>
31 #include <rtm/SystemLogger.h>
34 #include <rtm/CdrBufferBase.h>
35 #include <rtm/PortAdmin.h>
36 #include <rtm/CORBA_SeqUtil.h>
37 #include <rtm/NVUtil.h>
38 #include <rtm/ConnectorListener.h>
39 
40 
46 {
47 
49  : public RTC::ConnectorDataListenerT<RTC::TimedLong>
50  {
51  public:
52  DataListener(const char* name) : m_name(name) {}
53  virtual ~DataListener()
54  {
55  //std::cout << "dtor of " << m_name << std::endl;
56  }
57 
58  virtual void operator()(const RTC::ConnectorInfo& info,
59  const RTC::TimedLong& data)
60  {
61  std::cout << "------------------------------" << std::endl;
62  std::cout << "Data Listener: " << m_name << std::endl;
63  std::cout << "Profile::name: " << info.name << std::endl;
64  std::cout << "------------------------------" << std::endl;
65  };
66  std::string m_name;
67  };
68 
69 
71  : public RTC::ConnectorListener
72  {
73  public:
74  ConnListener(const char* name) : m_name(name) {}
75  virtual ~ConnListener()
76  {
77  //std::cout << "dtor of " << m_name << std::endl;
78  }
79 
80  virtual void operator()(const RTC::ConnectorInfo& info)
81  {
82  std::cout << "------------------------------" << std::endl;
83  std::cout << "Connector Listener: " << m_name << std::endl;
84  std::cout << "Profile::name: " << info.name << std::endl;
85  std::cout << "------------------------------" << std::endl;
86  };
87  std::string m_name;
88  };
89 
90 
97  {
98  public:
104  {
105  }
111  {
112  }
117  CORBA::Object_var get_m_objre()
118  {
119  return m_objref;
120  }
121  };
123  : public CppUnit::TestFixture
124  {
125  CPPUNIT_TEST_SUITE(OutPortCorbaCdrConsumerTests);
126 
127  CPPUNIT_TEST(test_case0);
128 
129  CPPUNIT_TEST_SUITE_END();
130 
131  private:
132  CORBA::ORB_ptr m_pORB;
133  PortableServer::POA_ptr m_pPOA;
135 
136  public:
137 
142  {
143  int argc(0);
144  char** argv(NULL);
145  m_pORB = CORBA::ORB_init(argc, argv);
146  m_pPOA = PortableServer::POA::_narrow(
147  m_pORB->resolve_initial_references("RootPOA"));
148  m_pPOA->the_POAManager()->activate();
149  }
150 
155  {
156  }
157 
161  virtual void setUp()
162  {
163  }
164 
168  virtual void tearDown()
169  {
170  }
171 
176  void test_case0()
177  {
178  //
179  //
180  //
182 
183  //ConnectorInfo
185  coil::vstring ports;
186  RTC::ConnectorInfo info("name", "id", ports, prop);
187 
188  //ConnectorDataListeners
190  new DataListener("ON_BUFFER_WRITE"), true);
192  new DataListener("ON_BUFFER_FULL"), true);
194  new DataListener("ON_BUFFER_WRITE_TIMEOUT"), true);
196  new DataListener("ON_BUFFER_OVERWRITE"), true);
198  new DataListener("ON_BUFFER_READ"), true);
200  new DataListener("ON_SEND"), true);
202  new DataListener("ON_RECEIVED"), true);
204  new DataListener("ON_RECEIVER_FULL"), true);
206  new DataListener("ON_RECEIVER_TIMEOUT"), true);
208  new DataListener("ON_RECEIVER_ERROR"), true);
209 
210  //ConnectorListeners
212  new ConnListener("ON_BUFFER_EMPTY"), true);
214  new ConnListener("ON_BUFFER_READ_TIMEOUT"), true);
216  new ConnListener("ON_SENDER_EMPTY"), true);
218  new ConnListener("ON_SENDER_TIMEOUT"), true);
220  new ConnListener("ON_SENDER_ERROR"), true);
222  new ConnListener("ON_CONNECT"), true);
224  new ConnListener("ON_DISCONNECT"), true);
225 
226  consumer.setListener(info, &m_listeners);
227 
228  consumer.init(prop);
229 
230  RTC::ConnectorProfile prof;
231  bool ret;
232 
233  ret = consumer.subscribeInterface(prof.properties);
234  //ior を設定していないのでfalseとなることを確認する。
235  CPPUNIT_ASSERT_EQUAL(false, ret);
236 
238 
239  CORBA_SeqUtil::push_back(prof.properties,
240  NVUtil::newNV("dataport.interface_type",
241  "corba_cdr"));
242  provider.setListener(info, &m_listeners);
243  provider.publishInterface(prof.properties);
244 
245  ret = consumer.subscribeInterface(prof.properties);
246  CPPUNIT_ASSERT_EQUAL(true, ret);
247 
248  cdrMemoryStream indata;
249  indata.setByteSwapFlag(true);
251 
252  RTC::CdrBufferBase* buffer;
253  buffer = RTC::CdrBufferFactory::instance().createObject("ring_buffer");
254 
255  consumer.setBuffer(buffer);
256 
257  //provider 側の buffer がない状態でコール(unkown error)
258  retcode = consumer.get(indata);
259  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)13, retcode);
260  provider.setBuffer(buffer);
261 
262  //データなしの状態でコール(empty)
263  retcode = consumer.get(indata);
264  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)4, retcode);
265 
266  int testdata[8] = { 12,34,56,78,90,23,45,99 };
267  cdrMemoryStream cdr;
268  RTC::TimedLong td;
269  for(int ic(0);ic<8;++ic)
270  {
271  td.data = testdata[ic];
272  td >>= cdr;
273  buffer->write(cdr);
274  }
275 
276  retcode = consumer.get(indata);
277  CPPUNIT_ASSERT_EQUAL((::RTC::DataPortStatus::Enum)0, retcode);
278 
279  CORBA::ULong inlen = indata.bufSize();
280  CPPUNIT_ASSERT_EQUAL(12,(int)inlen);
281 
282  RTC::TimedLong rtd;
283  rtd <<= indata;
284  CPPUNIT_ASSERT_EQUAL((long)testdata[0], (long)rtd.data);
285 
286  CPPUNIT_ASSERT(!CORBA::is_nil(consumer.get_m_objre()));
287  consumer.unsubscribeInterface(prof.properties);
288  CPPUNIT_ASSERT(CORBA::is_nil(consumer.get_m_objre()));
289 
290  int index;
291  index = NVUtil::find_index(prof.properties,
292  "dataport.corba_cdr.outport_ior");
293 
294  const char* ior;
295  if (prof.properties[index].value >>= ior)
296  {
297  CORBA::Object_ptr var = m_pORB->string_to_object(ior);
298  PortableServer::Servant ser = m_pPOA->reference_to_servant(var);
299  m_pPOA->deactivate_object(*m_pPOA->servant_to_id(ser));
300  }
301  }
302 
303  };
304 }; // namespace OutPortBase
305 
306 /*
307  * Register test suite
308  */
310 
311 #ifdef LOCAL_MAIN
312 int main(int argc, char* argv[])
313 {
314 
315  FORMAT format = TEXT_OUT;
316  int target = 0;
317  std::string xsl;
318  std::string ns;
319  std::string fname;
320  std::ofstream ofs;
321 
322  int i(1);
323  while (i < argc)
324  {
325  std::string arg(argv[i]);
326  std::string next_arg;
327  if (i + 1 < argc) next_arg = argv[i + 1];
328  else next_arg = "";
329 
330  if (arg == "--text") { format = TEXT_OUT; break; }
331  if (arg == "--xml")
332  {
333  if (next_arg == "")
334  {
335  fname = argv[0];
336  fname += ".xml";
337  }
338  else
339  {
340  fname = next_arg;
341  }
342  format = XML_OUT;
343  ofs.open(fname.c_str());
344  }
345  if ( arg == "--compiler" ) { format = COMPILER_OUT; break; }
346  if ( arg == "--cerr" ) { target = 1; break; }
347  if ( arg == "--xsl" )
348  {
349  if (next_arg == "") xsl = "default.xsl";
350  else xsl = next_arg;
351  }
352  if ( arg == "--namespace" )
353  {
354  if (next_arg == "")
355  {
356  std::cerr << "no namespace specified" << std::endl;
357  exit(1);
358  }
359  else
360  {
361  xsl = next_arg;
362  }
363  }
364  ++i;
365  }
366  CppUnit::TextUi::TestRunner runner;
367  if ( ns.empty() )
368  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
369  else
370  runner.addTest(CppUnit::TestFactoryRegistry::getRegistry(ns).makeTest());
371  CppUnit::Outputter* outputter = 0;
372  std::ostream* stream = target ? &std::cerr : &std::cout;
373  switch ( format )
374  {
375  case TEXT_OUT :
376  outputter = new CppUnit::TextOutputter(&runner.result(),*stream);
377  break;
378  case XML_OUT :
379  std::cout << "XML_OUT" << std::endl;
380  outputter = new CppUnit::XmlOutputter(&runner.result(),
381  ofs, "shift_jis");
382  static_cast<CppUnit::XmlOutputter*>(outputter)->setStyleSheet(xsl);
383  break;
384  case COMPILER_OUT :
385  outputter = new CppUnit::CompilerOutputter(&runner.result(),*stream);
386  break;
387  }
388  runner.setOutputter(outputter);
389  runner.run();
390  return 0; // runner.run() ? 0 : 1;
391 }
392 #endif // MAIN
393 #endif // OutPortBase_cpp
SDOPackage::NameValue newNV(const char *name, Value value)
Create NameValue.
Definition: NVUtil.h:79
ConnectorListeners class.
int main(int argc, char **argv)
ConnectorListener class.
virtual ReturnCode get(cdrMemoryStream &data)
Read data.
RT component logger class.
void addListener(ConnectorDataListener *listener, bool autoclean)
Add the listener.
ConnectorListenerHolder connector_[CONNECTOR_LISTENER_NUM]
ConnectorListenerType listener array The ConnectorListenerType listener is stored.
virtual void operator()(const RTC::ConnectorInfo &info, const RTC::TimedLong &data)
Virtual Callback method.
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
virtual void unsubscribeInterface(const SDOPackage::NVList &properties)
Unsubscribe the data receive notification.
OutPortCorbaCdrConsumer class.
std::vector< std::string > vstring
Definition: stringutil.h:37
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...
ConnectorDataListenerT class.
list index
Definition: rtimages.py:10
NameValue and NVList utility functions.
CORBA sequence utility template functions.
std::string name
Connection name.
virtual void operator()(const RTC::ConnectorInfo &info)
Virtual Callback method.
OutPortCorbaCdrConsumer class.
virtual void setListener(ConnectorInfo &info, ConnectorListeners *listeners)
Set the listener.
prop
Organization::get_organization_property ();.
virtual void setBuffer(CdrBufferBase *buffer)
Setting outside buffer&#39;s pointer.
virtual bool subscribeInterface(const SDOPackage::NVList &properties)
Subscribe the data receive notification.
Class represents a set of properties.
Definition: Properties.h:101
virtual bool publishInterface(SDOPackage::NVList &properties)
Publish interface information.
CPPUNIT_TEST_SUITE_REGISTRATION(OutPortCorbaCdrConsumer::OutPortCorbaCdrConsumerTests)
void push_back(CorbaSequence &seq, SequenceElement elem)
Push the new element back to the CORBA sequence.
RTComponent header.
virtual ReturnCode write(const DataType &value, long int sec=-1, long int nsec=-1)=0
Write data into the buffer.
virtual void setListener(ConnectorInfo &info, ConnectorListeners *listeners)
Set the listener.
virtual void setBuffer(CdrBufferBase *buffer)
Setting outside buffer&#39;s pointer.
OutPortCorbaCdrProvider class.
BufferBase abstract class.
Definition: BufferBase.h:104
connector listener class
Enum
DataPortStatus return codes.
RTC&#39;s Port administration class.
virtual void init(coil::Properties &prop)
Initializing configuration.
OutPortCorbaCdrProvider class.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:44