Example #1
0
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]){
  try
    {
      DDS::DomainParticipantFactory_var dpf =
        TheParticipantFactoryWithArgs(argc, argv);

      DDS::DomainParticipant_var participant =
        dpf->create_participant(311,
                                PARTICIPANT_QOS_DEFAULT,
                                DDS::DomainParticipantListener::_nil(),
                                ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (participant.in ())) {
        cerr << "create_participant failed." << endl;
        return 1;
      }

      MessageTypeSupportImpl* servant = new MessageTypeSupportImpl();

      if (DDS::RETCODE_OK != servant->register_type(participant.in (), "")) {
        cerr << "register_type failed." << endl;
        exit(1);
      }

      CORBA::String_var type_name = 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 ())) {
        cerr << "create_topic failed." << endl;
        exit(1);
      }

      DDS::PublisherQos pub_qos;
      participant->get_default_publisher_qos (pub_qos);

      pub_qos.partition.name.length (1);
      pub_qos.partition.name[0] = PARTITION_A;

      DDS::Publisher_var pub =
        participant->create_publisher(pub_qos, DDS::PublisherListener::_nil(),
                                      ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (pub.in ())) {
        cerr << "create_publisher failed." << endl;
        exit(1);
      }

      // ----------------------------------------------
      // Create DataWriter which is belongs to PARTITION_A
      DDS::DataWriter_var dw =
        pub->create_datawriter (topic.in (),
                                DATAWRITER_QOS_DEFAULT,
                                DDS::DataWriterListener::_nil (),
                                ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

      int const max_attempts = 15;
      int attempts = 1;

      // ----------------------------------------------
      // Wait for first DataReader that belongs to PARTITION_A too,
      // then write samples.

      // cache handle for first reader.
      ::DDS::InstanceHandle_t handle = -1;
      {
        std::auto_ptr<Writer> writer (new Writer (dw.in ()));

        cout << "Pub waiting for match on A partition." << std::endl;
        if (OpenDDS::Model::WriterSync::wait_match(dw)) {
          cerr << "Error waiting for match on A partition" << std::endl;
          return 1;
        }
        while (attempts != max_attempts)
        {

          ::DDS::InstanceHandleSeq handles;
          dw->get_matched_subscriptions(handles);
          cout << "Pub matched " << handles.length() << " A subs." << std::endl;
          if (handles.length() == 1)
          {
            handle = handles[0];
            break;
          }
          else
            ACE_OS::sleep(1);
          ++attempts;
        }

        if (attempts == max_attempts)
        {
          cerr << "ERROR: failed to wait for first DataReader." << endl;
          exit (1);
        }

        writer->start ();
        writer->end ();
      }

      // ----------------------------------------------
      // Switch from PARTITION A to B, now the first DataReader belong to
      // PARTITION A should be disconnected and the second DataReader belong to
      // PARTITION B should be connected.

      pub_qos.partition.name[0] = PARTITION_B;
      if (pub->set_qos (pub_qos)!= ::DDS::RETCODE_OK)
      {
        cerr << "ERROR: DataWriter changed partition which should be compatible "
          << "but should disconnect with DataReaders" << endl;

        exit (1);
      }


      // ----------------------------------------------
      // Now DataWriter is in PARTITION B, the second DataReader in PARTITION B
      // should receive the messages.
      {
        std::auto_ptr<Writer> writer (new Writer (dw.in ()));

        cout << "Pub waiting for match on B partition." << std::endl;
        if (OpenDDS::Model::WriterSync::wait_match(dw)) {
          cerr << "Error waiting for match on B partition" << std::endl;
          return 1;
        }
        attempts = 1;
        while (attempts != max_attempts)
        {
          ::DDS::InstanceHandleSeq handles;
          dw->get_matched_subscriptions(handles);
          cout << "Pub matched " << handles.length() << " B subs." << std::endl;
          if (handles.length() == 1 && handles[0] != handle)
            break;
          else
            ACE_OS::sleep(1);
          ++attempts;
        }

        if (attempts == max_attempts)
        {
          cerr << "ERROR: subscriptions failed to match." << endl;
          exit (1);
        }

        writer->start ();
        writer->end ();
      }

      // ----------------------------------------------
      // Wait for first reader to switch from PARTITION A to B so
      // both two readers will receive the messages.
      {
        std::auto_ptr<Writer> writer (new Writer (dw.in ()));

        attempts = 1;
        while (attempts != max_attempts)
        {
          ::DDS::InstanceHandleSeq handles;

          dw->get_matched_subscriptions(handles);
          if (handles.length() == 2)
            break;
          else
            ACE_OS::sleep(1);

          ++ attempts;
        }

        if (attempts == max_attempts)
        {
          cerr << "ERROR: failed to wait for DataReader partition switch." << endl;
          exit (1);
        }

        writer->start ();
        writer->end ();
      }

      // ----------------------------------------------
      // Now wait for subscriber exit.
      {
        attempts = 1;
        while (attempts != max_attempts)
        {
          ::DDS::InstanceHandleSeq handles;

          dw->get_matched_subscriptions(handles);
          if (handles.length() == 0)
            break;
          else
            ACE_OS::sleep(1);

          ++ attempts;
        }
      }

      participant->delete_contained_entities();
      dpf->delete_participant(participant.in ());
  }
  catch (CORBA::Exception& e)
  {
    cerr << "PUB: Exception caught in main.cpp:" << endl
         << e << endl;
    exit(1);
  }
  TheServiceParticipant->shutdown ();

  return 0;
}
Example #2
0
int ACE_TMAIN(int argc, ACE_TCHAR *argv[]) {
  try
    {
      ACE_DEBUG ((LM_DEBUG, "(%P|%t) publisher main\n"));

      DDS::DomainParticipantFactory_var dpf =
        TheParticipantFactoryWithArgs(argc, argv);

      if (parse_args (argc, argv) == -1) {
        return -1;
      }

      DDS::DomainParticipantQos partQos;
      dpf->get_default_participant_qos(partQos);

      // set up user data in DP qos
      CORBA::ULong part_user_data_len
        = static_cast<CORBA::ULong>(ACE_OS::strlen (PART_USER_DATA));
      partQos.user_data.value.length (part_user_data_len);
      partQos.user_data.value.replace (part_user_data_len,
                                       part_user_data_len,
                                       reinterpret_cast<CORBA::Octet*>(PART_USER_DATA));

      DDS::DomainParticipant_var participant =
        dpf->create_participant(411,
                                partQos,
                                DDS::DomainParticipantListener::_nil(),
                                ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (participant.in ())) {
        cerr << "publisher: create_participant failed." << endl;
        return 1;
      }

      ::Messenger::MessageTypeSupport_var ts = new ::Messenger::MessageTypeSupportImpl();

      if (DDS::RETCODE_OK != ts->register_type(participant.in (), "Messenger")) {
        cerr << "publisher: register_type failed." << endl;
        exit(1);
      }

      CORBA::String_var type_name = ts->get_type_name ();

      DDS::TopicQos topic_qos;
      participant->get_default_topic_qos(topic_qos);

      // set up topic data in topic qos
      CORBA::ULong topic_data_len = static_cast<CORBA::ULong>(ACE_OS::strlen (TOPIC_DATA));
      topic_qos.topic_data.value.length (topic_data_len);
      topic_qos.topic_data.value.replace (topic_data_len, topic_data_len, reinterpret_cast<CORBA::Octet*>(TOPIC_DATA));

      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 ())) {
        cerr << "publisher: create_topic failed." << endl;
        exit(1);
      }

      DDS::PublisherQos pub_qos;
      participant->get_default_publisher_qos (pub_qos);

      // set up group data in group qos
      CORBA::ULong group_data_len = static_cast<CORBA::ULong> (ACE_OS::strlen (GROUP_DATA));
      pub_qos.group_data.value.length (group_data_len);
      pub_qos.group_data.value.replace (group_data_len, group_data_len, reinterpret_cast<CORBA::Octet*>(GROUP_DATA));

      DDS::Publisher_var pub =
        participant->create_publisher(pub_qos,
        DDS::PublisherListener::_nil(),
        ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (pub.in ())) {
        cerr << "publisher: create_publisher failed." << endl;
        exit(1);
      }

      // Create the datawriter
      DDS::DataWriterQos dw_qos;
      pub->get_default_datawriter_qos (dw_qos);
      dw_qos.durability.kind = DDS::TRANSIENT_LOCAL_DURABILITY_QOS;
      dw_qos.reliability.kind  = ::DDS::RELIABLE_RELIABILITY_QOS;
      dw_qos.resource_limits.max_samples_per_instance = 1000;
      dw_qos.history.kind  = ::DDS::KEEP_ALL_HISTORY_QOS;

      // set up user data in DW qos
      CORBA::ULong dw_user_data_len = static_cast<CORBA::ULong>(ACE_OS::strlen (DW_USER_DATA));
      dw_qos.user_data.value.length (dw_user_data_len);
      dw_qos.user_data.value.replace (dw_user_data_len,
                                      dw_user_data_len,
                                      reinterpret_cast<CORBA::Octet*>(DW_USER_DATA));

      DDS::DataWriter_var dw =
        pub->create_datawriter(topic.in (),
                               dw_qos,
                               DDS::DataWriterListener::_nil(),
                               ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (dw.in ())) {
        cerr << "publisher: create_datawriter failed." << endl;
        exit(1);
      }

      // wait for Monitor 1 done
      FILE* fp = ACE_OS::fopen (synch_fname, ACE_TEXT("r"));
      int i = 0;
      while (fp == 0 &&  i < 15)
      {
        ACE_DEBUG ((LM_DEBUG,
          ACE_TEXT("(%P|%t) waiting monitor1 done ...\n")));
        ACE_OS::sleep (1);
        ++ i;
        fp = ACE_OS::fopen (synch_fname, ACE_TEXT("r"));
      }

      if (fp != 0)
        ACE_OS::fclose (fp);

      // Now change the changeable qos. The second monitor should get the updated qos from BIT.

      part_user_data_len = static_cast<CORBA::ULong>(ACE_OS::strlen (UPDATED_PART_USER_DATA));
      partQos.user_data.value.length (part_user_data_len);
      partQos.user_data.value.replace (part_user_data_len,
                                       part_user_data_len,
                                       reinterpret_cast<CORBA::Octet*>(UPDATED_PART_USER_DATA));
      participant->set_qos (partQos);

      dw_user_data_len = static_cast<CORBA::ULong>(ACE_OS::strlen (UPDATED_DW_USER_DATA));
      dw_qos.user_data.value.length (dw_user_data_len);
      dw_qos.user_data.value.replace (dw_user_data_len,
                                      dw_user_data_len,
                                      reinterpret_cast<CORBA::Octet*>(UPDATED_DW_USER_DATA));
      dw->set_qos (dw_qos);

      group_data_len = static_cast<CORBA::ULong> (ACE_OS::strlen (UPDATED_GROUP_DATA));
      pub_qos.group_data.value.length (group_data_len);
      pub_qos.group_data.value.replace (group_data_len,
                                        group_data_len,
                                        reinterpret_cast<CORBA::Octet*>(UPDATED_GROUP_DATA));
      pub->set_qos (pub_qos);

      topic_data_len = static_cast<CORBA::ULong>(ACE_OS::strlen (UPDATED_TOPIC_DATA));
      topic_qos.topic_data.value.length (topic_data_len);
      topic_qos.topic_data.value.replace (topic_data_len,
                                          topic_data_len,
                                          reinterpret_cast<CORBA::Octet*>(UPDATED_TOPIC_DATA));
      topic->set_qos (topic_qos);

      Writer* writer = new Writer(dw.in());

      writer->start ();
      while ( !writer->is_finished()) {
        ACE_Time_Value small_time(0,250000);
        ACE_OS::sleep (small_time);
      }

      // Cleanup
      writer->end ();
      delete writer;
      participant->delete_contained_entities();
      dpf->delete_participant(participant.in ());
      TheServiceParticipant->shutdown ();
  }
  catch (CORBA::Exception& e)
    {
       cerr << "publisher: PUB: Exception caught in main.cpp:" << endl
         << e << endl;
      exit(1);
    }

  return 0;
}