int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { DDS::DomainParticipantFactory_var dpf; DDS::DomainParticipant_var participant; dpf = TheParticipantFactoryWithArgs(argc, argv); participant = dpf->create_participant(411, PARTICIPANT_QOS_DEFAULT, DDS::DomainParticipantListener::_nil()); if (CORBA::is_nil (participant.in ())) { cerr << "create_participant failed." << endl; return 1 ; } Messenger::MessageTypeSupportImpl* mts_servant = new Messenger::MessageTypeSupportImpl; if (DDS::RETCODE_OK != mts_servant->register_type(participant.in (), "")) { cerr << "Failed to register the MessageTypeTypeSupport." << endl; exit(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 ())) { cerr << "Failed to create_topic." << endl; exit(1); } // Initialize the transport OpenDDS::DCPS::TransportImpl_rch tcp_impl = TheTransportFactory->create_transport_impl ( transport_impl_id, ::OpenDDS::DCPS::AUTO_CONFIG); // Create the first subscriber belongs to PARTITION A DDS::SubscriberQos sub_qos1; participant->get_default_subscriber_qos (sub_qos1); sub_qos1.partition.name.length (1); sub_qos1.partition.name[0] = PARTITION_A; DDS::Subscriber_var sub1 = participant->create_subscriber (sub_qos1, DDS::SubscriberListener::_nil()); if (CORBA::is_nil (sub1.in ())) { cerr << "Failed to create_subscriber." << endl; exit(1); } // Create the second subscriber belongs to PARTITION B DDS::SubscriberQos sub_qos2; participant->get_default_subscriber_qos (sub_qos2); sub_qos2.partition.name.length (1); sub_qos2.partition.name[0] = PARTITION_B; DDS::Subscriber_var sub2 = participant->create_subscriber (sub_qos2, DDS::SubscriberListener::_nil()); if (CORBA::is_nil (sub2.in ())) { cerr << "Failed to create_subscriber." << endl; exit(1); } // Attach the subscribers to the transport. OpenDDS::DCPS::SubscriberImpl* sub_impl1 = dynamic_cast<OpenDDS::DCPS::SubscriberImpl*> (sub1.in ()); if (0 == sub_impl1) { cerr << "Failed to obtain subscriber1 servant \n" << endl; exit(1); } OpenDDS::DCPS::SubscriberImpl* sub_impl2 = dynamic_cast<OpenDDS::DCPS::SubscriberImpl*> (sub2.in ()); if (0 == sub_impl2) { cerr << "Failed to obtain subscriber2 servant \n" << endl; exit(1); } OpenDDS::DCPS::AttachStatus status = sub_impl1->attach_transport(tcp_impl.in()); if (status != OpenDDS::DCPS::ATTACH_OK) { std::string status_str; switch (status) { case OpenDDS::DCPS::ATTACH_BAD_TRANSPORT: status_str = "ATTACH_BAD_TRANSPORT"; break; case OpenDDS::DCPS::ATTACH_ERROR: status_str = "ATTACH_ERROR"; break; case OpenDDS::DCPS::ATTACH_INCOMPATIBLE_QOS: status_str = "ATTACH_INCOMPATIBLE_QOS"; break; default: status_str = "Unknown Status"; break; } cerr << "Failed to attach to the transport. Status == " << status_str.c_str() << endl; exit(1); } status = sub_impl2->attach_transport(tcp_impl.in()); if (status != OpenDDS::DCPS::ATTACH_OK) { std::string status_str; switch (status) { case OpenDDS::DCPS::ATTACH_BAD_TRANSPORT: status_str = "ATTACH_BAD_TRANSPORT"; break; case OpenDDS::DCPS::ATTACH_ERROR: status_str = "ATTACH_ERROR"; break; case OpenDDS::DCPS::ATTACH_INCOMPATIBLE_QOS: status_str = "ATTACH_INCOMPATIBLE_QOS"; break; default: status_str = "Unknown Status"; break; } cerr << "Failed to attach to the transport. Status == " << status_str.c_str() << endl; exit(1); } DDS::DataReaderQos dr_qos; sub1->get_default_datareader_qos (dr_qos); // Create first DataReader with listener. DDS::DataReaderListener_var listener1 (new DataReaderListenerImpl); if (CORBA::is_nil (listener1.in ())) { cerr << "ERROR: listener1 is nil." << endl; exit(1); } DDS::DataReader_var dr1 = sub1->create_datareader (topic.in (), dr_qos, listener1.in ()); // Create second DataReader with listener. DDS::DataReaderListener_var listener2 (new DataReaderListenerImpl); if (CORBA::is_nil (listener2.in ())) { cerr << "ERROR: listener2 is nil." << endl; exit(1); } DDS::DataReader_var dr2 = sub2->create_datareader (topic.in (), dr_qos, listener2.in ()); if (CORBA::is_nil (dr1.in ()) || CORBA::is_nil (dr2.in ())) { cerr << "ERROR: create_datareader failed." << endl; exit(1); } DataReaderListenerImpl* listener_servant1 = dynamic_cast<DataReaderListenerImpl*>(listener1.in()); DataReaderListenerImpl* listener_servant2 = dynamic_cast<DataReaderListenerImpl*>(listener2.in()); int expected = 10; // Writer of PARTITION A -> Reader of PARTITION A while ( listener_servant1->num_reads() < expected) { ACE_OS::sleep (1); } // Writer switch from PARTITION A to B -> Reader of PARTITION B while ( listener_servant2->num_reads() < expected) { ACE_OS::sleep (1); } // ---------------------------------------------- // Now switch first reader/subscriber from A to B // and it should be connected with DataWriter. sub_qos1.partition.name[0] = PARTITION_B; if (sub1->set_qos (sub_qos1) != ::DDS::RETCODE_OK) { cerr << "ERROR: failed to set partition" << endl; exit (1); } // Continue receive 10 more messages each. expected = 20; while ( listener_servant1->num_reads() < expected) { ACE_OS::sleep (1); } while ( listener_servant2->num_reads() < expected) { ACE_OS::sleep (1); } if (listener_servant1->num_reads() > expected || listener_servant2->num_reads() > expected) { cerr << "ERROR: received more than excepted messages" << endl; exit (1); } // Now shutdown 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 (); } catch (CORBA::Exception& e) { cerr << "SUB: Exception caught in main ():" << endl << e << endl; return 1; } return 0; }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { try { // Initialize DomainParticipantFactory DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); int error; if ((error = parse_args(argc, argv)) != 0) { return error; } // Create DomainParticipant DDS::DomainParticipant_var 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, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_participant() failed!\n")), -1); } // Register Type (Messenger::Message) Messenger::MessageTypeSupport_var ts = new Messenger::MessageTypeSupportImpl(); if (ts->register_type(participant.in(), "") != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: register_type() failed!\n")), -1); } // Create Topic (Movie Discussion List) DDS::Topic_var topic = participant->create_topic("Movie Discussion List", ts->get_type_name(), TOPIC_QOS_DEFAULT, DDS::TopicListener::_nil(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(topic.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_topic() failed!\n")), -1); } ::DDS::SubscriberQos subscriber_qos; participant->get_default_subscriber_qos (subscriber_qos); subscriber_qos.presentation.access_scope = (::DDS::PresentationQosPolicyAccessScopeKind)acess_scope; subscriber_qos.presentation.coherent_access = true; subscriber_qos.presentation.ordered_access = true; SubscriberListenerImpl* subscriber_listener_svt = new SubscriberListenerImpl(); DDS::SubscriberListener_var subscriber_listener(subscriber_listener_svt); // Create Subscriber DDS::Subscriber_var sub = participant->create_subscriber(subscriber_qos, subscriber_listener.in(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(sub.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_subscriber() failed!\n")), -1); } // Initialize Transport OpenDDS::DCPS::TransportImpl_rch transport_impl = TheTransportFactory->create_transport_impl(transport_impl_id, OpenDDS::DCPS::AUTO_CONFIG); OpenDDS::DCPS::AttachStatus status = transport_impl->attach(sub.in()); if (status != OpenDDS::DCPS::ATTACH_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: attach() failed!\n")), -1); } // Create DataReader DataReaderListenerImpl* listener_svt1 = new DataReaderListenerImpl("DataReader1"); DataReaderListenerImpl* listener_svt2 = new DataReaderListenerImpl("DataReader2"); DDS::DataReaderListener_var listener1(listener_svt1); DDS::DataReaderListener_var listener2(listener_svt2); ::DDS::DataReaderQos readerQos; sub->get_default_datareader_qos( readerQos); readerQos.history.kind = ::DDS::KEEP_ALL_HISTORY_QOS; readerQos.resource_limits.max_samples_per_instance = ::DDS::LENGTH_UNLIMITED; DDS::DataReader_var reader1 = sub->create_datareader(topic.in(), readerQos, listener1.in(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(reader1.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_datareader() failed!\n")), -1); } DDS::DataReader_var reader2 = sub->create_datareader(topic.in(), readerQos, listener2.in(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(reader2.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_datareader() failed!\n")), -1); } // Block until Publisher completes DDS::StatusCondition_var condition1 = reader1->get_statuscondition(); DDS::StatusCondition_var condition2 = reader2->get_statuscondition(); condition1->set_enabled_statuses(DDS::SUBSCRIPTION_MATCHED_STATUS); condition2->set_enabled_statuses(DDS::SUBSCRIPTION_MATCHED_STATUS); DDS::WaitSet_var ws1 = new DDS::WaitSet; DDS::WaitSet_var ws2 = new DDS::WaitSet; ws1->attach_condition(condition1); ws2->attach_condition(condition2); DDS::Duration_t timeout = { DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC }; DDS::ConditionSeq conditions1; DDS::ConditionSeq conditions2; DDS::SubscriptionMatchedStatus matches1 = { 0, 0, 0, 0, 0 }; DDS::SubscriptionMatchedStatus matches2 = { 0, 0, 0, 0, 0 }; do { if (matches1.current_count == 0 && ws1->wait(conditions1, timeout) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: wait() failed!\n")), -1); } if (matches2.current_count == 0 && ws2->wait(conditions2, timeout) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: wait() failed!\n")), -1); } if (reader1->get_subscription_matched_status(matches1) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: get_subscription_matched_status() failed!\n")), -1); } if (reader2->get_subscription_matched_status(matches2) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: get_subscription_matched_status() failed!\n")), -1); } } while (matches1.current_count > 0 && matches2.current_count > 0); if (! subscriber_listener_svt->verify_result () || ! listener_svt1->verify_result() || ! listener_svt2->verify_result()) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: failed to verify message!\n")), -1); } ws1->detach_condition(condition1); ws2->detach_condition(condition2); // Clean-up! participant->delete_contained_entities(); dpf->delete_participant(participant.in()); TheTransportFactory->release(); TheServiceParticipant->shutdown(); } catch (const CORBA::Exception& e) { e._tao_print_exception("Exception caught in main():"); return -1; } return 0; }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { try { // Initialize DomainParticipantFactory DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DDS::DomainParticipant_var participant; bool result1, result2; int error; if ((error = parse_args(argc, argv)) != 0) { ACE_DEBUG((LM_ERROR, "(%P|%t) Parsing error, returning %d\n", error)); return error; } { // Scope of entities // Create DomainParticipant 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, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_participant() failed!\n")), -1); } // Register Type (Messenger::Message) Messenger::MessageTypeSupport_var ts = new Messenger::MessageTypeSupportImpl(); if (ts->register_type(participant.in(), "") != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: register_type() failed!\n")), -1); } // Create Topic (Movie Discussion List) DDS::Topic_var topic = participant->create_topic("Movie Discussion List", ts->get_type_name(), TOPIC_QOS_DEFAULT, DDS::TopicListener::_nil(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(topic.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_topic() failed!\n")), -1); } // Create Subscriber 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, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_subscriber() failed!\n")), -1); } // Create DataReader DataReaderListenerImpl* listener_svt1 = new DataReaderListenerImpl("DataReader1"); DataReaderListenerImpl* listener_svt2 = new DataReaderListenerImpl("DataReader2"); DDS::DataReaderListener_var listener1(listener_svt1); DDS::DataReaderListener_var listener2(listener_svt2); ::DDS::DataReaderQos dr_qos; sub->get_default_datareader_qos (dr_qos); dr_qos.ownership.kind = ::DDS::EXCLUSIVE_OWNERSHIP_QOS; dr_qos.deadline.period.sec = deadline.sec; dr_qos.deadline.period.nanosec = deadline.nanosec; dr_qos.liveliness.lease_duration.sec = liveliness.sec; dr_qos.liveliness.lease_duration.nanosec = liveliness.nanosec; DDS::DataReader_var reader1 = sub->create_datareader(topic.in(), dr_qos, listener1.in(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(reader1.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_datareader() failed!\n")), -1); } DDS::DataReader_var reader2 = sub->create_datareader(topic.in(), dr_qos, listener2.in(), OpenDDS::DCPS::DEFAULT_STATUS_MASK); if (CORBA::is_nil(reader2.in())) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: create_datareader() failed!\n")), -1); } // Block until Publisher completes DDS::StatusCondition_var condition1 = reader1->get_statuscondition(); DDS::StatusCondition_var condition2 = reader2->get_statuscondition(); condition1->set_enabled_statuses(DDS::SUBSCRIPTION_MATCHED_STATUS); condition2->set_enabled_statuses(DDS::SUBSCRIPTION_MATCHED_STATUS); DDS::WaitSet_var ws = new DDS::WaitSet; ws->attach_condition(condition1); ws->attach_condition(condition2); DDS::Duration_t timeout = { DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC }; DDS::ConditionSeq conditions; DDS::SubscriptionMatchedStatus matches1 = { 0, 0, 0, 0, 0 }; DDS::SubscriptionMatchedStatus matches2 = { 0, 0, 0, 0, 0 }; while (true) { if (reader1->get_subscription_matched_status(matches1) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: get_subscription_matched_status() failed!\n")), -1); } if (reader2->get_subscription_matched_status(matches2) != DDS::RETCODE_OK) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: get_subscription_matched_status() failed!\n")), -1); } if ((matches1.current_count == 0 && matches1.total_count > 0) || (matches2.current_count == 0 && matches2.total_count > 0)) { break; } DDS::ReturnCode_t wait_status = ws->wait(conditions, timeout); if (wait_status != DDS::RETCODE_OK) { std::cerr << "ERROR: Subscriber failed during waiting on wait set with return code " << wait_status << std::endl; } } ws->detach_condition(condition1); ws->detach_condition(condition2); result1 = listener_svt1->verify_result(); result2 = listener_svt2->verify_result(); } // Scope of entities // Clean-up! participant->delete_contained_entities(); dpf->delete_participant(participant.in()); TheServiceParticipant->shutdown(); if (result1 == false || result2 == false) { ACE_ERROR_RETURN((LM_ERROR, ACE_TEXT("%N:%l main()") ACE_TEXT(" ERROR: failed to verify message!\n")), -2); } } catch (const CORBA::Exception& e) { e._tao_print_exception("Exception caught in main():"); return -1; } return 0; }