int run_test(int argc, ACE_TCHAR* argv[]) { DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); Publisher_var pub = dp->create_publisher(PUBLISHER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); DomainParticipant_var dp2 = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); Subscriber_var sub = dp2->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); TransportRegistry& treg = *TheTransportRegistry; if (!treg.get_config("t1").is_nil()) { treg.bind_config("t1", pub); treg.bind_config("t2", sub); } const bool passed = run_multitopic_test(pub, sub); dp->delete_contained_entities(); dpf->delete_participant(dp); dp2->delete_contained_entities(); dpf->delete_participant(dp2); return passed ? 0 : 1; }
int run_test(int argc, ACE_TCHAR *argv[]) { DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); MessageTypeSupport_var ts = new MessageTypeSupportImpl; ts->register_type(dp, ""); Publisher_var pub = dp->create_publisher(PUBLISHER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); TransportImpl_rch pub_tport = TheTransportFactory->create_transport_impl(1, AUTO_CONFIG); PublisherImpl* pub_impl = dynamic_cast<PublisherImpl*>(pub.in()); pub_impl->attach_transport(pub_tport.in()); Subscriber_var sub = dp->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); TransportImpl_rch sub_tport = TheTransportFactory->create_transport_impl(2, AUTO_CONFIG); SubscriberImpl* sub_impl = dynamic_cast<SubscriberImpl*>(sub.in()); sub_impl->attach_transport(sub_tport.in()); bool passed = run_sorting_test(dp, ts, pub, sub); passed &= run_filtering_test(dp, ts, pub, sub); dp->delete_contained_entities(); dpf->delete_participant(dp); return passed ? 0 : 1; }
int run_test(int argc, ACE_TCHAR *argv[]) { using namespace DDS; using namespace OpenDDS::DCPS; WaitSet_var ws = new WaitSet; DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); Messenger::MessageTypeSupport_var ts = new Messenger::MessageTypeSupportImpl; ts->register_type(dp, ts->get_type_name()); Topic_var topic = dp->create_topic("MyTopic", ts->get_type_name(), TOPIC_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); Publisher_var pub = dp->create_publisher(PUBLISHER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); TransportImpl_rch pub_tport = TheTransportFactory->create_transport_impl(1, AUTO_CONFIG); PublisherImpl* pub_impl = dynamic_cast<PublisherImpl*> (pub.in()); pub_impl->attach_transport(pub_tport.in()); DataWriter_var dw = pub->create_datawriter(topic, DATAWRITER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); StatusCondition_var cond = dw->get_statuscondition(); cond->set_enabled_statuses(OFFERED_INCOMPATIBLE_QOS_STATUS); ws->attach_condition(cond); Subscriber_var sub = dp->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); TransportImpl_rch sub_tport = TheTransportFactory->create_transport_impl(2, AUTO_CONFIG); SubscriberImpl* sub_impl = dynamic_cast<SubscriberImpl*> (sub.in()); sub_impl->attach_transport(sub_tport.in()); DataReaderQos dr_qos; sub->get_default_datareader_qos(dr_qos); dr_qos.durability.kind = PERSISTENT_DURABILITY_QOS; Waiter w(ws); w.activate(); DataReader_var dr = sub->create_datareader(topic, dr_qos, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); w.wait(); bool passed = (w.result() == RETCODE_OK); ws->detach_condition(cond); dp->delete_contained_entities(); dpf->delete_participant(dp); return passed ? 0 : 1; }
int run_test(int argc, ACE_TCHAR *argv[]) { DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); MessageTypeSupport_var ts = new MessageTypeSupportImpl; ts->register_type(dp, ""); Publisher_var pub = dp->create_publisher(PUBLISHER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); Subscriber_var sub = dp->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); bool passed = run_sorting_test(dp, ts, pub, sub); passed &= run_filtering_test(dp, ts, pub, sub); passed &= run_change_parameter_test(dp, ts, pub, sub); dp->delete_contained_entities(); dpf->delete_participant(dp); return passed ? 0 : 1; }
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) { int ret = 1; try { using namespace DDS; DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); ret = run_test_next_instance(dp); ret += run_test_instance(dp); dpf->delete_participant(dp); TheServiceParticipant->shutdown(); ACE_Thread_Manager::instance()->wait(); } catch (const CORBA::BAD_PARAM& ex) { ex._tao_print_exception("Exception caught in GuardConditionTest.cpp:"); return 1; } return ret; }
int OSPL_MAIN ( int argc, char *argv[]) { /* Generic DDS entities */ DomainParticipantFactory_var dpf; DomainParticipant_var parentDP; ExtDomainParticipant_var participant; Topic_var chatMessageTopic; Topic_var nameServiceTopic; TopicDescription_var namedMessageTopic; Subscriber_var chatSubscriber; DataReader_var parentReader; /* Type-specific DDS entities */ ChatMessageTypeSupport_var chatMessageTS; NameServiceTypeSupport_var nameServiceTS; NamedMessageTypeSupport_var namedMessageTS; NamedMessageDataReader_var chatAdmin; NamedMessageSeq_var msgSeq = new NamedMessageSeq(); SampleInfoSeq_var infoSeq = new SampleInfoSeq(); /* QosPolicy holders */ TopicQos reliable_topic_qos; TopicQos setting_topic_qos; SubscriberQos sub_qos; DDS::StringSeq parameterList; /* DDS Identifiers */ DomainId_t domain = DOMAIN_ID_DEFAULT; ReturnCode_t status; /* Others */ bool terminated = false; const char * partitionName = "ChatRoom"; char * chatMessageTypeName = NULL; char * nameServiceTypeName = NULL; char * namedMessageTypeName = NULL; #ifdef USE_NANOSLEEP struct timespec sleeptime; struct timespec remtime; #endif /* Options: MessageBoard [ownID] */ /* Messages having owner ownID will be ignored */ parameterList.length(1); if (argc > 1) { parameterList[0] = DDS::string_dup(argv[1]); } else { parameterList[0] = "0"; } /* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings. */ dpf = DomainParticipantFactory::get_instance(); checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance"); parentDP = dpf->create_participant ( domain, PARTICIPANT_QOS_DEFAULT, NULL, STATUS_MASK_NONE); checkHandle(parentDP.in(), "DDS::DomainParticipantFactory::create_participant"); /* Narrow the normal participant to its extended representative */ participant = ExtDomainParticipantImpl::_narrow(parentDP.in()); checkHandle(participant.in(), "DDS::ExtDomainParticipant::_narrow"); /* Register the required datatype for ChatMessage. */ chatMessageTS = new ChatMessageTypeSupport(); checkHandle(chatMessageTS.in(), "new ChatMessageTypeSupport"); chatMessageTypeName = chatMessageTS->get_type_name(); status = chatMessageTS->register_type( participant.in(), chatMessageTypeName); checkStatus(status, "Chat::ChatMessageTypeSupport::register_type"); /* Register the required datatype for NameService. */ nameServiceTS = new NameServiceTypeSupport(); checkHandle(nameServiceTS.in(), "new NameServiceTypeSupport"); nameServiceTypeName = nameServiceTS->get_type_name(); status = nameServiceTS->register_type( participant.in(), nameServiceTypeName); checkStatus(status, "Chat::NameServiceTypeSupport::register_type"); /* Register the required datatype for NamedMessage. */ namedMessageTS = new NamedMessageTypeSupport(); checkHandle(namedMessageTS.in(), "new NamedMessageTypeSupport"); namedMessageTypeName = namedMessageTS->get_type_name(); status = namedMessageTS->register_type( participant.in(), namedMessageTypeName); checkStatus(status, "Chat::NamedMessageTypeSupport::register_type"); /* Set the ReliabilityQosPolicy to RELIABLE. */ status = participant->get_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos"); reliable_topic_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS; /* Make the tailored QoS the new default. */ status = participant->set_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos"); /* Use the changed policy when defining the ChatMessage topic */ chatMessageTopic = participant->create_topic( "Chat_ChatMessage", chatMessageTypeName, reliable_topic_qos, NULL, STATUS_MASK_NONE); checkHandle(chatMessageTopic.in(), "DDS::DomainParticipant::create_topic (ChatMessage)"); /* Set the DurabilityQosPolicy to TRANSIENT. */ status = participant->get_default_topic_qos(setting_topic_qos); checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos"); setting_topic_qos.durability.kind = DDS::TRANSIENT_DURABILITY_QOS; /* Create the NameService Topic. */ nameServiceTopic = participant->create_topic( "Chat_NameService", nameServiceTypeName, setting_topic_qos, NULL, STATUS_MASK_NONE); checkHandle(nameServiceTopic.in(), "DDS::DomainParticipant::create_topic"); /* Create a multitopic that substitutes the userID with its corresponding userName. */ namedMessageTopic = participant->create_simulated_multitopic( "Chat_NamedMessage", namedMessageTypeName, "SELECT userID, name AS userName, index, content " "FROM Chat_NameService NATURAL JOIN Chat_ChatMessage WHERE userID <> %0", parameterList); checkHandle(namedMessageTopic.in(), "DDS::ExtDomainParticipant::create_simulated_multitopic"); /* Adapt the default SubscriberQos to read from the "ChatRoom" Partition. */ status = participant->get_default_subscriber_qos (sub_qos); checkStatus(status, "DDS::DomainParticipant::get_default_subscriber_qos"); sub_qos.partition.name.length(1); sub_qos.partition.name[0] = partitionName; /* Create a Subscriber for the MessageBoard application. */ chatSubscriber = participant->create_subscriber(sub_qos, NULL, STATUS_MASK_NONE); checkHandle(chatSubscriber.in(), "DDS::DomainParticipant::create_subscriber"); /* Create a DataReader for the NamedMessage Topic (using the appropriate QoS). */ parentReader = chatSubscriber->create_datareader( namedMessageTopic.in(), DATAREADER_QOS_USE_TOPIC_QOS, NULL, STATUS_MASK_NONE); checkHandle(parentReader.in(), "DDS::Subscriber::create_datareader"); /* Narrow the abstract parent into its typed representative. */ chatAdmin = Chat::NamedMessageDataReader::_narrow(parentReader.in()); checkHandle(chatAdmin.in(), "Chat::NamedMessageDataReader::_narrow"); /* Print a message that the MessageBoard has opened. */ cout << "MessageBoard has opened: send a ChatMessage with userID = -1 to close it...." << endl << endl; while (!terminated) { /* Note: using read does not remove the samples from unregistered instances from the DataReader. This means that the DataRase would use more and more resources. That's why we use take here instead. */ status = chatAdmin->take( msgSeq, infoSeq, LENGTH_UNLIMITED, ANY_SAMPLE_STATE, ANY_VIEW_STATE, ALIVE_INSTANCE_STATE ); checkStatus(status, "Chat::NamedMessageDataReader::take"); for (DDS::ULong i = 0; i < msgSeq->length(); i++) { NamedMessage *msg = &(msgSeq[i]); if (msg->userID == TERMINATION_MESSAGE) { cout << "Termination message received: exiting..." << endl; terminated = true; } else { cout << msg->userName << ": " << msg->content << endl; } fflush(stdout); } status = chatAdmin->return_loan(msgSeq, infoSeq); checkStatus(status, "Chat::ChatMessageDataReader::return_loan"); /* Sleep for some amount of time, as not to consume too much CPU cycles. */ #ifdef USE_NANOSLEEP sleeptime.tv_sec = 0; sleeptime.tv_nsec = 100000000; nanosleep(&sleeptime, &remtime); #elif defined _WIN32 Sleep(100); #else usleep(100000); #endif } /* Remove the DataReader */ status = chatSubscriber->delete_datareader(chatAdmin.in()); checkStatus(status, "DDS::Subscriber::delete_datareader"); /* Remove the Subscriber. */ status = participant->delete_subscriber(chatSubscriber.in()); checkStatus(status, "DDS::DomainParticipant::delete_subscriber"); /* Remove the Topics. */ status = participant->delete_simulated_multitopic(namedMessageTopic.in()); checkStatus(status, "DDS::ExtDomainParticipant::delete_simulated_multitopic"); status = participant->delete_topic(nameServiceTopic.in()); checkStatus(status, "DDS::DomainParticipant::delete_topic (nameServiceTopic)"); status = participant->delete_topic(chatMessageTopic.in()); checkStatus(status, "DDS::DomainParticipant::delete_topic (chatMessageTopic)"); /* De-allocate the type-names. */ DDS::string_free(namedMessageTypeName); DDS::string_free(nameServiceTypeName); DDS::string_free(chatMessageTypeName); /* Remove the DomainParticipant. */ status = dpf->delete_participant(participant.in()); checkStatus(status, "DDS::DomainParticipantFactory::delete_participant"); return 0; }
int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { bool ok = false; DomainParticipantFactory_var dpf; DomainParticipant_var dp_sub, dp_pub; try { dpf = TheParticipantFactoryWithArgs(argc, argv); dp_sub = dpf->create_participant(9, PARTICIPANT_QOS_DEFAULT, 0, DEFAULT_STATUS_MASK); if (!dp_sub) { ACE_DEBUG((LM_ERROR, "ERROR: %P could not create Sub Domain Participant\n")); } else { { // New scope. ACE_Arg_Shifter shifter (argc, argv); while (shifter.is_anything_left ()) { const ACE_TCHAR* x = shifter.get_the_parameter (ACE_TEXT("-value_base")); if (x != NULL) { TestConfig::set (ACE_OS::atoi (x)); } shifter.consume_arg (); } } DomainParticipantQos dp_qos; dpf->get_default_participant_qos(dp_qos); set_qos(dp_qos.user_data.value, TestConfig::PARTICIPANT_USER_DATA()); dp_pub = dpf->create_participant(9, dp_qos, 0, DEFAULT_STATUS_MASK); if (!dp_pub) { ACE_DEBUG((LM_ERROR, "ERROR: %P could not create Domain Participant 2\n")); } else { ok = run_test(dp_sub, dp_pub); if (!ok) { ACE_DEBUG((LM_ERROR, "ERROR: %P from run_test\n")); return -1; } } } } catch (const std::exception& e) { ACE_DEBUG((LM_ERROR, "ERROR: %P Exception thrown: %C\n", e.what())); return -2; } catch (const CORBA::Exception& e) { e._tao_print_exception("ERROR: %P Exception thrown:"); return -2; } catch (const OpenDDS::DCPS::Transport::Exception&) { ACE_DEBUG((LM_ERROR, "ERROR: %P Transport exception thrown\n")); return -2; } catch (...) { ACE_DEBUG((LM_ERROR, "ERROR: %P unknown exception thrown\n")); return -2; } ACE_DEBUG((LM_INFO, "%P Cleaning up test\n")); cleanup(dpf, dp_sub); ACE_OS::sleep(2); cleanup(dpf, dp_pub); TheServiceParticipant->shutdown(); ACE_Thread_Manager::instance()->wait(); return ok ? EXIT_SUCCESS : EXIT_FAILURE; }
int run_test(int argc, ACE_TCHAR *argv[]) { using namespace DDS; using namespace OpenDDS::DCPS; using namespace Messenger; WaitSet_var ws = new WaitSet; DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv); DomainParticipant_var dp = dpf->create_participant(23, PARTICIPANT_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); MessageTypeSupport_var ts = new MessageTypeSupportImpl; // leave type name not speficied would register it with _interface_repository_id ts->register_type(dp, ""); CORBA::String_var type_name = ts->get_type_name(); Topic_var topic = dp->create_topic("MyTopic", type_name, TOPIC_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); Publisher_var pub = dp->create_publisher(PUBLISHER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); DataWriter_var dw = pub->create_datawriter(topic, DATAWRITER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); Subscriber_var sub = dp->create_subscriber(SUBSCRIBER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); DataReader_var dr = sub->create_datareader(topic, DATAREADER_QOS_DEFAULT, 0, ::OpenDDS::DCPS::DEFAULT_STATUS_MASK); StatusCondition_var dw_sc = dw->get_statuscondition(); dw_sc->set_enabled_statuses(PUBLICATION_MATCHED_STATUS); ws->attach_condition(dw_sc); Duration_t infinite = {DURATION_INFINITE_SEC, DURATION_INFINITE_NSEC}; ConditionSeq active; ws->wait(active, infinite); ws->detach_condition(dw_sc); ReturnCode_t ret = RETCODE_OK; MessageDataWriter_var mdw = MessageDataWriter::_narrow(dw); Message msg = {0}; for (int i(0); i < 12; ++i) { ++msg.subject_id; ret = mdw->write(msg, HANDLE_NIL); if (ret != RETCODE_OK) return ret; } ReadCondition_var dr_rc = dr->create_readcondition(NOT_READ_SAMPLE_STATE, NEW_VIEW_STATE, ALIVE_INSTANCE_STATE); ReadCondition_var dr_rc2 = dr->create_readcondition(ANY_SAMPLE_STATE, ANY_VIEW_STATE, NOT_ALIVE_DISPOSED_INSTANCE_STATE); ws->attach_condition(dr_rc); ws->attach_condition(dr_rc2); MessageDataReader_var mdr = MessageDataReader::_narrow(dr); bool passed = true, done = false; while (!done) { ret = ws->wait(active, infinite); if (ret != RETCODE_OK) { passed = false; break; } cout << "wait returned" << endl; for (CORBA::ULong i(0); i < active.length(); ++i) { if (active[i] == dr_rc) { // To test both take_w_condition and // take_next_instance_w_condition, we'll limit the "take" to 3 // samples and then use take_next_instance_w_condition. MessageSeq data; SampleInfoSeq info; ret = mdr->take_w_condition(data, info, 3, dr_rc); if (ret != RETCODE_OK && ret != RETCODE_NO_DATA) { cout << "ERROR: take_w_condition returned " << ret << endl; passed = false; done = true; } if (ret == RETCODE_OK) { received_data(data, mdw, msg); } cout << "testing take_next_instance_w_condition" << endl; InstanceHandle_t handle = HANDLE_NIL; while (true) { ret = mdr->take_next_instance_w_condition(data, info, 1, handle, dr_rc); if (ret == RETCODE_NO_DATA) break; if (ret != RETCODE_OK) { cout << "ERROR: take_next_instance_w_condition returned " << ret << endl; passed = false; done = true; } received_data(data, mdw, msg); handle = info[info.length() - 1].instance_handle; } } else if (active[i] == dr_rc2) { cout << "an instance has been disposed, exiting" << endl; done = true; } } } ws->detach_condition(dr_rc); dr->delete_readcondition(dr_rc); ws->detach_condition(dr_rc2); // Instead of: dr->delete_readcondition(dr_rc2); dr->delete_contained_entities(); dp->delete_contained_entities(); dpf->delete_participant(dp); return passed ? 0 : 1; }
int OSPL_MAIN ( int argc, char *argv[]) { /* Generic DDS entities */ DomainParticipantFactory_var dpf; DomainParticipant_var participant; Topic_var chatMessageTopic; Subscriber_var chatSubscriber; DataReader_ptr parentReader; /* Type-specific DDS entities */ ChatMessageTypeSupport_var chatMessageTS; ChatMessageDataReader_var chatAdmin; ChatMessageSeq_var msgSeq = new ChatMessageSeq(); SampleInfoSeq_var infoSeq = new SampleInfoSeq(); /* QosPolicy holders */ TopicQos reliable_topic_qos; SubscriberQos sub_qos; DDS::StringSeq parameterList; /* DDS Identifiers */ DomainId_t domain = 0; ReturnCode_t status; /* Others */ bool terminated = false; const char * partitionName = "ChatRoom1"; char * chatMessageTypeName = NULL; /* Options: MessageBoard [ownID] */ /* Messages having owner ownID will be ignored */ parameterList.length(1); if (argc > 1) { parameterList[0] = string_dup(argv[1]); } else { parameterList[0] = "0"; } /* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings. */ dpf = DomainParticipantFactory::get_instance(); checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance"); participant = dpf->create_participant ( domain, PARTICIPANT_QOS_DEFAULT, NULL, STATUS_MASK_NONE); checkHandle(participant, "DDS::DomainParticipantFactory::create_participant"); /* Register the required datatype for ChatMessage. */ chatMessageTS = new ChatMessageTypeSupport(); checkHandle(chatMessageTS.in(), "new ChatMessageTypeSupport"); chatMessageTypeName = chatMessageTS->get_type_name(); status = chatMessageTS->register_type( participant.in(), chatMessageTypeName); checkStatus(status, "NetworkPartitionsData::ChatMessageTypeSupport::register_type"); /* Set the ReliabilityQosPolicy to RELIABLE. */ status = participant->get_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos"); reliable_topic_qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS; /* Make the tailored QoS the new default. */ status = participant->set_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos"); /* Use the changed policy when defining the ChatMessage topic */ chatMessageTopic = participant->create_topic( "Chat_ChatMessage", chatMessageTypeName, reliable_topic_qos, NULL, STATUS_MASK_NONE); checkHandle(chatMessageTopic.in(), "DDS::DomainParticipant::create_topic (ChatMessage)"); /* Adapt the default SubscriberQos to read from the "ChatRoom1" Partition. */ status = participant->get_default_subscriber_qos (sub_qos); checkStatus(status, "DDS::DomainParticipant::get_default_subscriber_qos"); sub_qos.partition.name.length(1); sub_qos.partition.name[0] = partitionName; /* Create a Subscriber for the MessageBoard application. */ chatSubscriber = participant->create_subscriber(sub_qos, NULL, STATUS_MASK_NONE); checkHandle(chatSubscriber.in(), "DDS::DomainParticipant::create_subscriber"); /* Create a DataReader for the NamedMessage Topic (using the appropriate QoS). */ parentReader = chatSubscriber->create_datareader( chatMessageTopic.in(), DATAREADER_QOS_USE_TOPIC_QOS, NULL, STATUS_MASK_NONE); checkHandle(parentReader, "DDS::Subscriber::create_datareader"); /* Narrow the abstract parent into its typed representative. */ chatAdmin = ChatMessageDataReader::_narrow(parentReader); checkHandle(chatAdmin.in(), "NetworkPartitionsData::ChatMessageDataReader::_narrow"); /* Print a message that the MessageBoard has opened. */ cout << "MessageBoard has opened: send a ChatMessage with userID = -1 to close it...." << endl << endl; while (!terminated) { /* Note: using read does not remove the samples from unregistered instances from the DataReader. This means that the DataRase would use more and more resources. That's why we use take here instead. */ status = chatAdmin->take( msgSeq, infoSeq, LENGTH_UNLIMITED, ANY_SAMPLE_STATE, ANY_VIEW_STATE, ALIVE_INSTANCE_STATE ); checkStatus(status, "NetworkPartitionsData::ChatMessageDataReader::take"); for (ULong i = 0; i < msgSeq->length(); i++) { ChatMessage *msg = &(msgSeq[i]); if (msg->userID == TERMINATION_MESSAGE) { cout << "Termination message received: exiting..." << endl; terminated = true; } else { cout << msg->userID << ": " << msg->content << endl; } } status = chatAdmin->return_loan(msgSeq, infoSeq); checkStatus(status, "NetworkPartitionsData::ChatMessageDataReader::return_loan"); /* Sleep for some amount of time, so as not to consume too many CPU cycles. */ sleep(1); } /* Remove the DataReader */ status = chatSubscriber->delete_datareader(chatAdmin.in()); checkStatus(status, "DDS::Subscriber::delete_datareader"); /* Remove the Subscriber. */ status = participant->delete_subscriber(chatSubscriber.in()); checkStatus(status, "DDS::DomainParticipant::delete_subscriber"); /* Remove the Topic. */ status = participant->delete_topic(chatMessageTopic.in()); checkStatus(status, "DDS::DomainParticipant::delete_topic (chatMessageTopic)"); /* De-allocate the type-names. */ string_free(chatMessageTypeName); /* Remove the DomainParticipant. */ status = dpf->delete_participant(participant.in()); checkStatus(status, "DDS::DomainParticipantFactory::delete_participant"); return 0; }
int OSPL_MAIN ( int argc, char *argv[]) { /* Generic DDS entities */ DomainParticipantFactory_var dpf; DomainParticipant_var participant; Topic_var chatMessageTopic; Publisher_var chatPublisher; DataWriter_ptr parentWriter; /* QosPolicy holders */ TopicQos reliable_topic_qos; PublisherQos pub_qos; DataWriterQos dw_qos; /* DDS Identifiers */ DomainId_t domain = 0; InstanceHandle_t userHandle; ReturnCode_t status; /* Type-specific DDS entities */ ChatMessageTypeSupport_var chatMessageTS; ChatMessageDataWriter_var talker; /* Sample definitions */ ChatMessage *msg; /* Example on Heap */ /* Others */ int ownID = 1; int i; const char *partitionName = "ChatRoom1"; char *chatMessageTypeName = NULL; char buf[MAX_MSG_LEN]; #ifdef INTEGRITY #ifdef CHATTER_QUIT ownID = -1; #else ownID = 1; #endif #else /* Options: Chatter [ownID] */ if (argc > 1) { ownID = atoi(argv[1]); } #endif /* Create a DomainParticipantFactory and a DomainParticipant (using Default QoS settings. */ dpf = DomainParticipantFactory::get_instance (); checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance"); participant = dpf->create_participant(domain, PARTICIPANT_QOS_DEFAULT, NULL, STATUS_MASK_NONE); checkHandle(participant.in(), "DDS::DomainParticipantFactory::create_participant"); /* Register the required datatype for ChatMessage. */ chatMessageTS = new ChatMessageTypeSupport(); checkHandle(chatMessageTS.in(), "new ChatMessageTypeSupport"); chatMessageTypeName = chatMessageTS->get_type_name(); status = chatMessageTS->register_type( participant.in(), chatMessageTypeName); checkStatus(status, "NetworkPartitionsData::ChatMessageTypeSupport::register_type"); /* Set the ReliabilityQosPolicy to RELIABLE. */ status = participant->get_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos"); reliable_topic_qos.reliability.kind = RELIABLE_RELIABILITY_QOS; /* Make the tailored QoS the new default. */ status = participant->set_default_topic_qos(reliable_topic_qos); checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos"); /* Use the changed policy when defining the ChatMessage topic */ chatMessageTopic = participant->create_topic( "Chat_ChatMessage", chatMessageTypeName, reliable_topic_qos, NULL, STATUS_MASK_NONE); checkHandle(chatMessageTopic.in(), "DDS::DomainParticipant::create_topic (ChatMessage)"); /* Adapt the default PublisherQos to write into the "ChatRoom1" Partition. */ status = participant->get_default_publisher_qos (pub_qos); checkStatus(status, "DDS::DomainParticipant::get_default_publisher_qos"); pub_qos.partition.name.length(1); pub_qos.partition.name[0] = partitionName; /* Create a Publisher for the chatter application. */ chatPublisher = participant->create_publisher(pub_qos, NULL, STATUS_MASK_NONE); checkHandle(chatPublisher.in(), "DDS::DomainParticipant::create_publisher"); /* Create a DataWriter for the ChatMessage Topic (using the appropriate QoS). */ parentWriter = chatPublisher->create_datawriter( chatMessageTopic.in(), DATAWRITER_QOS_USE_TOPIC_QOS, NULL, STATUS_MASK_NONE); checkHandle(parentWriter, "DDS::Publisher::create_datawriter (chatMessage)"); /* Narrow the abstract parent into its typed representative. */ talker = ChatMessageDataWriter::_narrow(parentWriter); checkHandle(talker.in(), "NetworkPartitionsData::ChatMessageDataWriter::_narrow"); /* Initialize the chat messages on Heap. */ msg = new ChatMessage(); checkHandle(msg, "new ChatMessage"); msg->userID = ownID; msg->index = 0; if (ownID == TERMINATION_MESSAGE) { snprintf(buf, MAX_MSG_LEN, "Termination message."); } else { snprintf(buf, MAX_MSG_LEN, "Hi there, I will send you %d more messages.", NUM_MSG); } msg->content = string_dup(buf); cout << "Writing message: \"" << msg->content << "\"" << endl; /* Register a chat message for this user (pre-allocating resources for it!!) */ userHandle = talker->register_instance(*msg); /* Write a message using the pre-generated instance handle. */ status = talker->write(*msg, userHandle); checkStatus(status, "NetworkPartitionsData::ChatMessageDataWriter::write"); sleep (1); /* do not run so fast! */ /* Write any number of messages, re-using the existing string-buffer: no leak!!. */ for (i = 1; i <= NUM_MSG && ownID != TERMINATION_MESSAGE; i++) { msg->index = i; snprintf(buf, MAX_MSG_LEN, "Message no. %d", i); msg->content = string_dup(buf); cout << "Writing message: \"" << msg->content << "\"" << endl; status = talker->write(*msg, userHandle); checkStatus(status, "NetworkPartitionsData::ChatMessageDataWriter::write"); sleep (1); /* do not run so fast! */ } /* Leave the room by disposing and unregistering the message instance. */ status = talker->dispose(*msg, userHandle); checkStatus(status, "NetworkPartitionsData::ChatMessageDataWriter::dispose"); status = talker->unregister_instance(*msg, userHandle); checkStatus(status, "NetworkPartitionsData::ChatMessageDataWriter::unregister_instance"); /* Release the data-samples. */ delete msg; // msg allocated on heap: explicit de-allocation required!! /* Remove the DataWriters */ status = chatPublisher->delete_datawriter( talker.in() ); checkStatus(status, "DDS::Publisher::delete_datawriter (talker)"); /* Remove the Publisher. */ status = participant->delete_publisher( chatPublisher.in() ); checkStatus(status, "DDS::DomainParticipant::delete_publisher"); status = participant->delete_topic( chatMessageTopic.in() ); checkStatus(status, "DDS::DomainParticipant::delete_topic (chatMessageTopic)"); /* Remove the type-names. */ string_free(chatMessageTypeName); /* Remove the DomainParticipant. */ status = dpf->delete_participant( participant.in() ); checkStatus(status, "DDS::DomainParticipantFactory::delete_participant"); cout << "Completed Chatter example" << endl; return 0; }