void MultiTopicDataReaderBase::cleanup() { DDS::Subscriber_var sub = resulting_reader_->get_subscriber(); for (std::map<std::string, QueryPlan>::iterator it = query_plans_.begin(); it != query_plans_.end(); ++it) { sub->delete_datareader(it->second.data_reader_); } DataReaderImpl* dri = dynamic_cast<DataReaderImpl*>(resulting_reader_.in()); SubscriberImpl* si = dynamic_cast<SubscriberImpl*>(sub.in()); si->remove_from_datareader_set(dri); dri->cleanup(); }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { DDS::DomainParticipantFactory_var dpf; DDS::DomainParticipant_var participant; dpf = TheParticipantFactoryWithArgs(argc, argv); if( parse_args(argc, argv) != 0) return 1; participant = dpf->create_participant(411, PARTICIPANT_QOS_DEFAULT, DDS::DomainParticipantListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil (participant.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) create_participant failed.\n") , -1); } MessageTypeSupportImpl* mts_servant = new MessageTypeSupportImpl; if (DDS::RETCODE_OK != mts_servant->register_type(participant.in (), "")) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to register the MessageTypeTypeSupport.\n") , -1); } CORBA::String_var type_name = mts_servant->get_type_name (); DDS::TopicQos topic_qos; participant->get_default_topic_qos(topic_qos); DDS::Topic_var topic = participant->create_topic("Movie Discussion List", type_name.in (), topic_qos, DDS::TopicListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil (topic.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to create_topic.\n") , -1); } // Indicate that the subscriber is about to become ready FILE* readers_ready = ACE_OS::fopen (sub_ready_filename, ACE_TEXT("w")); if (readers_ready == 0) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) ERROR: Unable to create subscriber ready file.\n") , -1); } ACE_OS::fclose(readers_ready); // Check if the publisher is up and running ACE_stat stats; while (ACE_OS::stat (pub_ready_filename, &stats) == -1) { ACE_Time_Value small_time(0,250000); ACE_OS::sleep (small_time); } for (int count = 1; count <= sub_reinit_itr; count++) { if (verbose) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) Reinitializing subscriber.\n")); } // Create the subscriber and attach to the corresponding // transport. DDS::Subscriber_var sub = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, DDS::SubscriberListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil (sub.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to create_subscriber.\n") , -1); } // Create the Datareaders DDS::DataReaderQos dr_qos; sub->get_default_datareader_qos (dr_qos); DDS::DataReader_var dr = sub->create_datareader(topic.in (), dr_qos, DDS::DataReaderListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil (dr.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) create_datareader failed.\n") , -1); } // This is where a speed-bump should be. while (true) { ::DDS::InstanceHandleSeq handles; dr->get_matched_publications (handles); if (handles.length() > 0) { break; } ACE_Time_Value small_time (0,250000); ACE_OS::sleep (small_time); } // Add sleep to let the fully_associted message arrive datawriter // before remove_associations is called upon delete_datareader, // otherwise the datawriter will encounter bit lookup timeout upon // fully associated. ACE_Time_Value small_time (0,250000); ACE_OS::sleep (small_time); if (verbose) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) *** Destroying Subscriber\n")); } // Delete data reader sub->delete_datareader(dr.in()); // Delete subscriber participant->delete_subscriber(sub.in()); dr = DDS::DataReader::_nil (); sub = DDS::Subscriber::_nil(); } if (!CORBA::is_nil (participant.in ())) { participant->delete_contained_entities(); } if (!CORBA::is_nil (dpf.in ())) { dpf->delete_participant(participant.in ()); } TheServiceParticipant->shutdown (); // Indicate that the subscriber is done FILE* readers_completed = ACE_OS::fopen (sub_finished_filename, ACE_TEXT("w")); if (readers_completed == 0) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) ERROR: Unable to create subscriber completed file.\n") , -1); } ACE_OS::fclose(readers_completed); } catch (CORBA::Exception& e) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Exception caught in main (): %C (%C).\n" , e._name (), e._rep_id ()) , -1); } return 0; }
int do_reader(DDS::DomainParticipant_var participant, DDS::Topic_var topic, bool toggle) { // Create Subscriber DDS::Subscriber_var subscriber = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (!subscriber) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("ERROR: %N:%l: do_reader() -") ACE_TEXT(" create_subscriber failed!\n")), -1); } DDS::DataReaderQos qos; subscriber->get_default_datareader_qos(qos); qos.user_data.value.length(3); qos.user_data.value[0] = 0; qos.user_data.value[1] = 0; qos.user_data.value[2] = 0; qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS; if (toggle) { ACE_DEBUG((LM_DEBUG, "Creating reader\n")); DDS::DataReader_var reader = subscriber->create_datareader(topic, qos, 0, OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (!reader) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("ERROR: %N:%l: do_reader() -") ACE_TEXT(" create_datareader failed!\n")), -1); } ACE_OS::sleep(SLEEP_SHORT); // Go away. ACE_DEBUG((LM_DEBUG, "Deleting reader\n")); subscriber->delete_datareader(reader); ACE_OS::sleep(SLEEP_SHORT); // Come back. ACE_DEBUG((LM_DEBUG, "Creating reader\n")); reader = subscriber->create_datareader(topic, qos, 0, OpenDDS::DCPS::DEFAULT_STATUS_MASK); ACE_OS::sleep(SLEEP_SHORT); return 0; } else { struct Listener : public DDS::DataReaderListener { size_t found, lost; Listener() : found(0), lost(0) { } virtual void on_requested_deadline_missed (::DDS::DataReader_ptr, const ::DDS::RequestedDeadlineMissedStatus &) { } virtual void on_requested_incompatible_qos (::DDS::DataReader_ptr, const ::DDS::RequestedIncompatibleQosStatus &) { } virtual void on_sample_rejected (::DDS::DataReader_ptr, const ::DDS::SampleRejectedStatus &) { } virtual void on_liveliness_changed (::DDS::DataReader_ptr, const ::DDS::LivelinessChangedStatus &) { } virtual void on_data_available (::DDS::DataReader_ptr) { } virtual void on_subscription_matched (::DDS::DataReader_ptr, const ::DDS::SubscriptionMatchedStatus & status) { if (status.current_count_change > 0) { ACE_DEBUG((LM_DEBUG, "Reader found writer\n")); ++found; } if (status.current_count_change < 0) { ACE_DEBUG((LM_DEBUG, "Reader lost writer\n")); ++lost; } } virtual void on_sample_lost (::DDS::DataReader_ptr, const ::DDS::SampleLostStatus &) { } } listener; // Create DataReader DDS::DataReader_var reader = subscriber->create_datareader(topic, qos, &listener, OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (!reader) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("ERROR: %N:%l: do_reader() -") ACE_TEXT(" create_datareader failed!\n")), -1); } ACE_OS::sleep(SLEEP_LONG); if (listener.found == 2 && listener.lost == 1) { reader->set_listener(0, OpenDDS::DCPS::DEFAULT_STATUS_MASK); return 0; } return -1; } }
int ACE_TMAIN(int argc, ACE_TCHAR** argv) { parse_args(argc, argv); ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t) -> SUBSCRIBER STARTED\n"))); ::CORBA::Long sec = deadline_msec / 1000; ::CORBA::ULong remainder_msec = (deadline_msec - 1000*sec); ::CORBA::ULong nanosec = remainder_msec * 1000000; DDS::Duration_t const DEADLINE_PERIOD = { sec, nanosec }; bool deadline_used = DEADLINE_PERIOD.sec > 0 || DEADLINE_PERIOD.nanosec > 0; try { DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); SubscriberListenerImpl * subscriberListener = new SubscriberListenerImpl(received_samples, missed_samples); DDS::SubscriberListener_var subscriberListener_var = subscriberListener; // Create Participant DDS::DomainParticipant_var participant = dpf->create_participant(42, PARTICIPANT_QOS_DEFAULT, DDS::DomainParticipantListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(participant.in())) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" create_participant failed!\n")), 1); ACE_Time_Value delay_between_cycles(0, delay_between_cycles_msec * 1000); bool expected_samples_received = false; int i = 0; do { ++i; ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t) -> Subscriber cycle %d\n"), i)); // Create Subscriber DDS::Subscriber_var subscriber = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, subscriberListener, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(subscriber.in())) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" create_subscriber failed!\n")), 2); // Register Type (FooType) FooTypeSupport_var ts = new FooTypeSupportImpl; if (ts->register_type(participant.in(), "") != DDS::RETCODE_OK) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" register_type failed!\n")), 5); // Create Topic (FooTopic) DDS::Topic_var topic = participant->create_topic("FooTopic", ts->get_type_name(), TOPIC_QOS_DEFAULT, DDS::TopicListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); DDS::TopicDescription_ptr topic_used = topic.in(); DDS::ContentFilteredTopic_ptr cft = 0; if (use_cft) { // Topic name must be unique. ACE_CString topic_name = "FooTopic-Filtered-" + toStr(i); cft = participant->create_contentfilteredtopic(topic_name.c_str(), topic, "key > 0", DDS::StringSeq()); if (CORBA::is_nil(cft)) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" create_contentfilteredtopic failed!\n")), 8); topic_used = cft; } if (CORBA::is_nil(topic.in())) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" create_topic failed!\n")), 6); // Create DataReader DDS::DataReaderQos reader_qos; subscriber->get_default_datareader_qos(reader_qos); reader_qos.history.kind = DDS::KEEP_ALL_HISTORY_QOS; if (deadline_used) { reader_qos.deadline.period.sec = DEADLINE_PERIOD.sec; reader_qos.deadline.period.nanosec = DEADLINE_PERIOD.nanosec; } DDS::DataReader_var reader = subscriber->create_datareader(topic_used, reader_qos, DDS::DataReaderListener::_nil(), ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(reader.in())) ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l: main()") ACE_TEXT(" create_datareader failed!\n")), 7); ACE_Time_Value sample_count_sleep(0, sample_count_sleep_msec * 1000); std::size_t sample_count; std::size_t sample_count_start = subscriberListener->samples_processed(); do { ACE_OS::sleep(sample_count_sleep); sample_count = subscriberListener->samples_processed(); expected_samples_received = sample_count >= expected_samples; // ACE_DEBUG((LM_DEBUG, "(%P|%t) sample_count = %d\n", sample_count)); } while (!expected_samples_received && (sample_count - sample_count_start) < samples_per_cycle); subscriber->delete_datareader(reader.in()); if (use_cft) CORBA::release(cft); participant->delete_subscriber(subscriber.in()); ACE_OS::sleep(delay_between_cycles); } while (!expected_samples_received); participant->delete_contained_entities(); dpf->delete_participant(participant.in()); TheServiceParticipant->shutdown(); ACE_DEBUG ((LM_INFO, ACE_TEXT("INFO: %d samples received\n"), subscriberListener->received_samples())); if (deadline_used) ACE_DEBUG ((LM_INFO, ACE_TEXT("INFO: deadline missed %d times\n"), subscriberListener->missed_samples())); } catch (const CORBA::Exception& e) { e._tao_print_exception("caught in main()"); return 9; } ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t) <- SUBSCRIBER FINISHED\n"))); return 0; }
int main (int argc, char *argv[]) { try { DDS::DomainParticipantFactory_var dpf; DDS::DomainParticipant_var participant; dpf = TheParticipantFactoryWithArgs(argc, argv); if( parse_args(argc, argv) != 0) return 1; participant = dpf->create_participant(411, PARTICIPANT_QOS_DEFAULT, DDS::DomainParticipantListener::_nil()); if (CORBA::is_nil (participant.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) create_participant failed.\n") , -1); } MessageTypeSupportImpl* mts_servant = new MessageTypeSupportImpl(); PortableServer::ServantBase_var safe_servant = mts_servant; if (DDS::RETCODE_OK != mts_servant->register_type(participant.in (), "")) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to register the MessageTypeTypeSupport.\n") , -1); } CORBA::String_var type_name = mts_servant->get_type_name (); DDS::TopicQos topic_qos; participant->get_default_topic_qos(topic_qos); DDS::Topic_var topic = participant->create_topic("Movie Discussion List", type_name.in (), topic_qos, DDS::TopicListener::_nil()); if (CORBA::is_nil (topic.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to create_topic.\n") , -1); } // Initialize the transport TAO::DCPS::TransportImpl_rch tcp_impl = TheTransportFactory->create_transport_impl (TCP_IMPL_ID, ::TAO::DCPS::AUTO_CONFIG); // Indicate that the subscriber is about to become ready FILE* readers_ready = ACE_OS::fopen (sub_ready_filename, "w"); if (readers_ready == 0) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) ERROR Unable to create subscriber ready file.\n") , -1); } ACE_OS::fclose(readers_ready); // Check if the publisher is up and running ACE_stat stats; while (ACE_OS::stat (pub_ready_filename, &stats) == -1) { ACE_Time_Value small(0,250000); ACE_OS::sleep (small); } for (int count = 1; count <= sub_reinit_itr; count++) { if (verbose) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) Reinitializing subscriber.\n")); } // Create the subscriber and attach to the corresponding // transport. DDS::Subscriber_var sub = participant->create_subscriber(SUBSCRIBER_QOS_DEFAULT, DDS::SubscriberListener::_nil()); if (CORBA::is_nil (sub.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to create_subscriber.\n") , -1); } // Attach the subscriber to the transport. TAO::DCPS::SubscriberImpl* sub_impl = ::TAO::DCPS::reference_to_servant < TAO::DCPS::SubscriberImpl, DDS::Subscriber_ptr> (sub.in ()); if (0 == sub_impl) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to obtain subscriber servant.\n") , -1); } TAO::DCPS::AttachStatus status = sub_impl->attach_transport(tcp_impl.in()); if (status != TAO::DCPS::ATTACH_OK) { std::string status_str; switch (status) { case TAO::DCPS::ATTACH_BAD_TRANSPORT: status_str = "ATTACH_BAD_TRANSPORT"; break; case TAO::DCPS::ATTACH_ERROR: status_str = "ATTACH_ERROR"; break; case TAO::DCPS::ATTACH_INCOMPATIBLE_QOS: status_str = "ATTACH_INCOMPATIBLE_QOS"; break; default: status_str = "Unknown Status"; break; } ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Failed to attach to the transport. " "Status == %s.\n" , status_str.c_str()) , -1); } // Create the Datareaders DDS::DataReaderQos dr_qos; sub->get_default_datareader_qos (dr_qos); DDS::DataReader_var dr = sub->create_datareader(topic.in (), dr_qos, DDS::DataReaderListener::_nil()); if (CORBA::is_nil (dr.in ())) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) create_datareader failed.\n") , -1); } { ACE_OS::sleep (2); // This is where a speed-bump should be. } if (verbose) { ACE_DEBUG ((LM_DEBUG, "(%P|%t) *** Destroying Subscriber\n")); } // Delete data reader sub->delete_datareader(dr.in()); // Delete subscriber participant->delete_subscriber(sub.in()); sub = DDS::Subscriber::_nil(); } if (!CORBA::is_nil (participant.in ())) { participant->delete_contained_entities(); } if (!CORBA::is_nil (dpf.in ())) { dpf->delete_participant(participant.in ()); } TheTransportFactory->release(); TheServiceParticipant->shutdown (); // Indicate that the subscriber is done FILE* readers_completed = ACE_OS::fopen (sub_finished_filename, "w"); if (readers_completed == 0) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) ERROR Unable to create subscriber completed file.\n") , -1); } ACE_OS::fclose(readers_completed); } catch (CORBA::Exception& e) { ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) Exception caught in main (): %s (%s).\n" , e._name (), e._rep_id ()) , -1); } return 0; }