// -*- C++ -*- // ============================================================================ /** * @file subscriber.cpp * * $Id$ * * */ // ============================================================================ #include "DataReaderListener.h" #include "QuoterTypeSupportImpl.h" #include #include #include #include #include #ifdef ACE_AS_STATIC_LIBS #include #include #include #endif #include #include "ace/Get_Opt.h" //using namespace Quoter; OpenDDS::DCPS::TransportIdType transport_impl_id = 1; int parse_args (int argc, ACE_TCHAR *argv[]) { ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("t:")); int c; while ((c = get_opts ()) != -1) { switch (c) { case 't': if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("udp")) == 0) { transport_impl_id = 2; } else if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("mcast")) == 0) { transport_impl_id = 3; } else if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("reliable_mcast")) == 0) { transport_impl_id = 4; } // test with DEFAULT_SIMPLE_TCP_ID. else if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("default_tcp")) == 0) { transport_impl_id = OpenDDS::DCPS::DEFAULT_SIMPLE_TCP_ID; } // test with DEFAULT_SIMPLE_UDP_ID. else if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("default_udp")) == 0) { transport_impl_id = OpenDDS::DCPS::DEFAULT_SIMPLE_UDP_ID; } else if (ACE_OS::strcmp (get_opts.opt_arg (), ACE_TEXT("default_mcast_sub")) == 0) { transport_impl_id = OpenDDS::DCPS::DEFAULT_SIMPLE_MCAST_SUB_ID; } break; case '?': default: ACE_ERROR_RETURN ((LM_ERROR, "usage: %s " "-t " "\n", argv [0]), -1); } } // Indicates sucessful parsing of the command line return 0; } int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { try { DDS::DomainParticipantFactory_var dpf; DDS::DomainParticipant_var participant; dpf = TheParticipantFactoryWithArgs(argc, argv); // To Do: Create the participant named 'participant'. // End: Create the participant named 'participant'. if (parse_args (argc, argv) == -1) { return -1; } QuoterTypeSupport_var qts = new QuoterTypeSupportImpl(); // To Do: Register the type using 'qts' from above. // End: Register the type using 'qts' from above. CORBA::String_var type_name = qts->get_type_name (); // To Do: Create the topic named 'topic' using the default topic QoS. // End: Create the topic named 'topic' using the default topic QoS. // Initialize the transport OpenDDS::DCPS::TransportImpl_rch transport_impl = TheTransportFactory->create_transport_impl (transport_impl_id, ::OpenDDS::DCPS::AUTO_CONFIG); // To Do: Create the subscriber named 'sub' using default QoS. // End: Create the subscriber named 'sub' using default QoS. // Attach the subscriber to the transport. OpenDDS::DCPS::SubscriberImpl* sub_impl = dynamic_cast (sub.in ()); if (0 == sub_impl) { cerr << "Failed to obtain subscriber servant\n" << endl; exit(1); } OpenDDS::DCPS::AttachStatus status = sub_impl->attach_transport(transport_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); } // activate the listener DDS::DataReaderListener_var listener (new DataReaderListenerImpl); DataReaderListenerImpl* listener_servant = dynamic_cast(listener.in()); if (CORBA::is_nil (listener.in ())) { cerr << "listener is nil." << endl; exit(1); } // To Do: Create the Datareader named 'dr' using the default QoS. // End: Create the Datareader named 'dr' using the default QoS. // Wait for 10 reads before exiting. int expected = 10; while (listener_servant->num_reads() < expected) { ACE_OS::sleep (1); } // To Do: Delete the participant's contained entities // End: Delete the participant's contained entities if (!CORBA::is_nil (dpf.in ())) { dpf->delete_participant(participant.in ()); } ACE_OS::sleep(2); TheTransportFactory->release(); TheServiceParticipant->shutdown (); } catch (CORBA::Exception& e) { cerr << "SUB: Exception caught in main ():" << endl << e << endl; return 1; } return 0; }