/****************************************************************
*
* file: HelloMsgSub.cpp
* desc: Provides a simple C++ 'Hello World' DDS subscriber.
* This subscribing application will receive data
* from the example 'hello world' publishing
* application (hello_pub).
*
****************************************************************/
#include <dds/DCPS/Service_Participant.h>
#include <dds/DCPS/Marked_Default_Qos.h>
#include <dds/DCPS/PublisherImpl.h>
#include <ace/streams.h>
#include <orbsvcs/Time_Utilities.h>
#include "dds/DCPS/StaticIncludes.h"
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "HelloMsgTypeSupportImpl.h"
#ifdef _WIN32
# define SLEEP(a) Sleep(a*1000)
#else
# define SLEEP(a) sleep(a);
#endif
int all_done = 0;
/****************************************************************
* Construct a DataReaderListener and override the
* on_data_available() method with our own. All other
* listener methods will be default (no-op) functions.
****************************************************************/
class SubListener : public DDS::DataReaderListener
{
public:
void on_data_available( DDS::DataReader * dr );
void on_requested_deadline_missed (
DDS::DataReader_ptr reader,
const DDS::RequestedDeadlineMissedStatus & status);
void on_requested_incompatible_qos (
DDS::DataReader_ptr reader,
const DDS::RequestedIncompatibleQosStatus & status);
void on_liveliness_changed (
DDS::DataReader_ptr reader,
const DDS::LivelinessChangedStatus & status);
virtual void on_subscription_matched (
DDS::DataReader_ptr reader,
const DDS::SubscriptionMatchedStatus & status
);
void on_sample_rejected(
DDS::DataReader_ptr reader,
const DDS::SampleRejectedStatus& status
);
void on_sample_lost(
DDS::DataReader_ptr reader,
const DDS::SampleLostStatus& status
);
};
/****************************************************************
* DataReader Listener Method: on_data_avail()
*
* This listener method is called when data is available to
* be read on this DataReader.
****************************************************************/
void SubListener::on_data_available( DDS::DataReader * dr)
{
Hello::StringMsgSeq samples;
DDS::SampleInfoSeq samples_info;
DDS::ReturnCode_t retval;
DDS::SampleStateMask ss = DDS::ANY_SAMPLE_STATE;
DDS::ViewStateMask vs = DDS::ANY_VIEW_STATE;
DDS::InstanceStateMask is = DDS::ANY_INSTANCE_STATE;
/* Convert to our type-specific DataReader */
Hello::StringMsgDataReader* reader = Hello::StringMsgDataReader::_narrow( dr );
/* Take any and all available samples. The take() operation
* will remove the samples from the DataReader so they
* won't be available on subsequent read() or take() calls.
*/
retval = reader->take( samples, samples_info,
DDS::LENGTH_UNLIMITED,
ss,
vs,
is );
if ( retval == DDS::RETCODE_OK )
{
/* iterrate through the samples */
for ( unsigned int i = 0;i < samples.length(); i++)
{
/* If this sample does not contain valid data,
* it is a dispose or other non-data command,
* and, accessing any member from this sample
* would be invalid.
*/
if ( samples_info[i].valid_data)
printf("OpenDDS received a sample, No=%d/%d, [%s]\n", i, samples.length(),
samples[i].msg.in());
}
fflush(stdout);
/* read() and take() always "loan" the data, we need to
* return it so OpenDDS can release resources associated
* with it.
*/
reader->return_loan( samples, samples_info );
}
else
{
printf("ERROR (%s) taking samples from DataReader\n",
"DDS_error(retval)");
}
}
void SubListener::on_requested_deadline_missed (
DDS::DataReader_ptr reader,
const DDS::RequestedDeadlineMissedStatus & status)
{
printf("ERROR (%s) on_requested_deadline_missed\n",
"DDS_error(retval)");
}
void SubListener::on_requested_incompatible_qos (
DDS::DataReader_ptr reader,
const DDS::RequestedIncompatibleQosStatus & status)
{
printf("ERROR (%s) on_requested_incompatible_qos\n",
"DDS_error(retval)");
}
void SubListener::on_liveliness_changed (
DDS::DataReader_ptr reader,
const DDS::LivelinessChangedStatus & status)
{
printf("ERROR (%s) on_liveliness_changed\n",
"DDS_error(retval)");
}
void SubListener::on_subscription_matched (
DDS::DataReader_ptr reader,
const DDS::SubscriptionMatchedStatus & status
)
{
printf("ERROR (%s) on_subscription_matched\n",
"DDS_error(retval)");
}
void SubListener::on_sample_rejected(
DDS::DataReader_ptr reader,
const DDS::SampleRejectedStatus& status
)
{
printf("ERROR (%s) on_sample_rejected\n",
"DDS_error(retval)");
}
void SubListener::on_sample_lost(
DDS::DataReader_ptr reader,
const DDS::SampleLostStatus& status
)
{
printf("ERROR (%s) on_sample_lost\n",
"DDS_error(retval)");
}
/****************************************************************
* main()
*
* Perform OpenDDS setup activities:
* - create a Domain Participant
* - create a Subscriber
* - register the StringMsg data type
* - create a Topic
* - create a DataReader and attach the listener created above
* And wait for data
****************************************************************/
#if defined(__vxworks) && !defined(__RTP__)
int hello_sub(char * args)
#else
int main(int argc, char * argv[])
#endif
{
DDS::DomainParticipant * domain;
DDS::Subscriber * subscriber;
DDS::Topic * topic;
DDS::DataReader * dr;
SubListener drListener;
DDS::ReturnCode_t retval;
/* Get an instance of the DDS DomainPartiticpantFactory --
* we will use this to create a DomainParticipant.
*/
DDS::DomainParticipantFactory *dpf =
TheParticipantFactoryWithArgs(argc, argv);
if ( dpf == NULL )
{
printf("ERROR initializing domainParticipantFactory.\n");
return -1;
}
/* create a DomainParticipant */
domain =
dpf->create_participant( 2,
PARTICIPANT_QOS_DEFAULT,
NULL,
0 );
if ( domain == NULL )
{
printf("ERROR creating domain participant.\n");
return -1;
}
/* create a Subscriber */
subscriber = domain->create_subscriber(SUBSCRIBER_QOS_DEFAULT,
NULL,
0 );
if ( subscriber == NULL )
{
printf("ERROR creating subscriber\n");
return -1;
}
/* Register the data type with the OpenDDS middleware.
* This is required before creating a Topic with
* this data type.
*/
Hello::StringMsgTypeSupport *stringMsgTS = new Hello::StringMsgTypeSupportImpl();;
retval = stringMsgTS->register_type( domain, "StringMsg" );
if (retval != DDS::RETCODE_OK)
{
printf("ERROR registering type: %s\n", "DDS_error(retval)");
return -1;
}
/* create a DDS Topic with the StringMsg data type. */
topic = domain->create_topic( "helloTopic",
"StringMsg",
TOPIC_QOS_DEFAULT,
NULL,
0 );
if ( ! topic )
{
printf("ERROR creating topic\n");
return -1;
}
/* create a DDS_DataReader on the hello topic (notice
* the TopicDescription is used) with default QoS settings,
* and attach our listener with our on_data_available method.
*/
dr = subscriber->create_datareader( (DDS::TopicDescription*)topic,
DATAREADER_QOS_DEFAULT,
&drListener,
DDS::DATA_AVAILABLE_STATUS );
if ( ! dr )
{
printf("ERROR creating data reader\n");
return -1;
}
/* Wait forever. When data arrives at our DataReader,
* our dr_on_data

pony12
- 粉丝: 414
- 资源: 26