00001 #ifndef _ACSDDSNC_DATAREADER_LISTENER_IMPL
00002 #define _ACSDDSNC_DATAREADER_LISTENER_IMPL
00003
00004 #include <iostream>
00005 #include "DataReaderListener.h"
00006
00007 namespace ddsnc{
00008
00024 template<class DRV, class DR, class D>
00025 class ACSDDSNCDataReaderListener :
00026 public ddsnc::DataReaderListenerImpl
00027 {
00028 public:
00029 typedef void (*eventHandlerFunction)(D eventData, void *handlerParam);
00030 eventHandlerFunction templateFunction_mp;
00031 void *handlerParam_mp;
00032
00039 ACSDDSNCDataReaderListener(eventHandlerFunction templateFunction,
00040 void * handlerParam=0) :
00041 ddsnc::DataReaderListenerImpl()
00042 {
00043 templateFunction_mp = templateFunction;
00044 handlerParam_mp = handlerParam;
00045 }
00046
00047 void on_data_available(DDS::DataReader_ptr reader)
00048 throw (CORBA::SystemException)
00049 {
00050 num_reads_ ++;
00051 try {
00052 DRV message_dr= DR::_narrow(reader);
00053 if (CORBA::is_nil (message_dr.in ())) {
00054 ::std::cerr << "read: _narrow failed." << ::std::endl;
00055 }
00056 D message;
00057 DDS::SampleInfo si;
00058 DDS::ReturnCode_t status = message_dr->take_next_sample(message, si);
00059 if (status == DDS::RETCODE_OK) {
00060
00061
00062 if (si.valid_data == 1){
00063 (*templateFunction_mp)(message, handlerParam_mp);
00064 }else if (si.instance_state == DDS::NOT_ALIVE_DISPOSED_INSTANCE_STATE){
00065 ::std::cerr << "instance is disposed" << ::std::endl;
00066 }else if (si.instance_state == DDS::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE){
00067 ::std::cerr << "instance is unregistered" << ::std::endl;
00068 }else {
00069 ACE_ERROR ((LM_ERROR,
00070 "(%P|%t)DataReaderListenerImpl::on_data_available:"
00071 " received unknown instance state %d\n",
00072 si.instance_state));
00073 }
00074 }else if (status == DDS::RETCODE_NO_DATA) {
00075 ::std::cerr << "ERROR: reader received DDS::RETCODE_NO_DATA!" << ::std::endl;
00076 }else {
00077 ::std::cerr << "ERROR: read Message: Error: " << status << ::std::endl;
00078 }
00079 }catch (CORBA::Exception& e) {
00080 ::std::cerr << "Exception caught in read:" << ::std::endl << e << ::std::endl;
00081 exit(1);
00082 }
00083 }
00084
00085 };
00086 }
00087 #endif