Exemplo n.º 1
0
int
OpenDDS::Model::WriterSync::wait_ack(const DDS::DataWriter_var& writer)
{
  DDS::ReturnCode_t stat;
  DDS::Duration_t timeout = { 30, 0 };
  if (DCPS_debug_level > 4) {
    ACE_DEBUG((LM_NOTICE, ACE_TEXT("WriterSync: waiting for acks\n")));
  }
  stat = writer->wait_for_acknowledgments(timeout);
  if ((stat != DDS::RETCODE_OK) && (stat != DDS::RETCODE_TIMEOUT)) {
    ACE_ERROR_RETURN((LM_ERROR,
                      ACE_TEXT("(%P|%t) ERROR: %N:%l: wait_ack() -")
                      ACE_TEXT(" wait_for_acknowledgments failed!\n")),
                     -1);
  }
  if (DCPS_debug_level > 4) {
    ACE_DEBUG((LM_NOTICE, ACE_TEXT("WriterSync: acks received\n")));
  }
  return 0;
}
Exemplo n.º 2
0
int
ParticipantTask::svc()
{
  try
  {
    ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)    -> PARTICIPANT STARTED\n")));

    DDS::DomainParticipantFactory_var dpf = TheParticipantFactory;
    DDS::DomainParticipant_var participant;
    DDS::Publisher_var publisher;
    DDS::DataWriter_var writer;
    FooDataWriter_var writer_i;
    DDS::StatusCondition_var cond;
    DDS::WaitSet_var ws = new DDS::WaitSet;

    { // Scope for guard to serialize creating Entities.
      GuardType guard(lock_);

      // Create Participant
      participant =
        dpf->create_participant(42,
                                PARTICIPANT_QOS_DEFAULT,
                                DDS::DomainParticipantListener::_nil(),
                                ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

#ifdef OPENDDS_SAFETY_PROFILE
      // RTPS cannot be shared
      char config_name[64], inst_name[64];
      ACE_OS::snprintf(config_name, 64, "cfg_%d", thread_index_);
      ACE_OS::snprintf(inst_name, 64, "rtps_%d", thread_index_);
      ++thread_index_;

      ACE_DEBUG((LM_INFO,
        "(%P|%t)    -> PARTICIPANT creating transport config %C\n",
        config_name));
      OpenDDS::DCPS::TransportConfig_rch config =
        TheTransportRegistry->create_config(config_name);
      OpenDDS::DCPS::TransportInst_rch inst =
        TheTransportRegistry->create_inst(inst_name, "rtps_udp");
      config->instances_.push_back(inst);
      TheTransportRegistry->bind_config(config_name, participant);
#endif

    } // End of lock scope.

    if (CORBA::is_nil(participant.in()))
      ACE_ERROR_RETURN((LM_ERROR,
                        ACE_TEXT("%N:%l: svc()")
                        ACE_TEXT(" create_participant failed!\n")), 1);

    {
      // Create Publisher
      publisher =
        participant->create_publisher(PUBLISHER_QOS_DEFAULT,
                                      DDS::PublisherListener::_nil(),
                                      ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

      if (CORBA::is_nil(publisher.in()))
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: svc()")
                          ACE_TEXT(" create_publisher failed!\n")), 1);


      // 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: svc()")
                          ACE_TEXT(" register_type failed!\n")), 1);

      // 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);

      if (CORBA::is_nil(topic.in()))
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: svc()")
                          ACE_TEXT(" create_topic failed!\n")), 1);

      // Create DataWriter
      DDS::DataWriterQos writer_qos;
      publisher->get_default_datawriter_qos(writer_qos);
#ifndef OPENDDS_NO_OWNERSHIP_PROFILE
      writer_qos.history.depth = samples_per_thread_;
#endif

      writer =
        publisher->create_datawriter(topic.in(),
                                     writer_qos,
                                     DDS::DataWriterListener::_nil(),
                                     ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

      if (CORBA::is_nil(writer.in()))
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: svc()")
                          ACE_TEXT(" create_datawriter failed!\n")), 1);

      writer_i = FooDataWriter::_narrow(writer);
      if (CORBA::is_nil(writer_i))
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: svc()")
                          ACE_TEXT(" _narrow failed!\n")), 1);

      // Block until Subscriber is available
      cond = writer->get_statuscondition();
      cond->set_enabled_statuses(DDS::PUBLICATION_MATCHED_STATUS);

      ws->attach_condition(cond);

      DDS::Duration_t timeout =
        { DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC };

      DDS::ConditionSeq conditions;
      DDS::PublicationMatchedStatus matches = {0, 0, 0, 0, 0};
      do
      {
        if (ws->wait(conditions, timeout) != DDS::RETCODE_OK)
          ACE_ERROR_RETURN((LM_ERROR,
                            ACE_TEXT("%N:%l: svc()")
                            ACE_TEXT(" wait failed!\n")), 1);

        if (writer->get_publication_matched_status(matches) != ::DDS::RETCODE_OK)
        {
          ACE_ERROR ((LM_ERROR,
            "(%P|%t) ERROR: failed to get publication matched status\n"));
          ACE_OS::exit (1);
        }
      }
      while (matches.current_count < 1);

      ws->detach_condition(cond);

      // The following is intentionally inefficient to stress various
      // pathways related to publication; we should be especially dull
      // and write only one sample at a time per writer.

      ProgressIndicator progress("(%P|%t)       PARTICIPANT %d%% (%d samples sent)\n",
                                 samples_per_thread_);

      for (std::size_t i = 0; i < samples_per_thread_; ++i)
      {
        Foo foo;
        foo.key = 3;
        DDS::InstanceHandle_t handle = writer_i->register_instance(foo);

        if (writer_i->write(foo, handle) != DDS::RETCODE_OK) {
          ACE_ERROR_RETURN((LM_ERROR,
                            ACE_TEXT("%N:%l: svc()")
                            ACE_TEXT(" write failed!\n")), 1);
        }
        ++progress;
      }

      DDS::Duration_t interval = { 30, 0};
      if( DDS::RETCODE_OK != writer->wait_for_acknowledgments( interval)) {
        ACE_ERROR_RETURN((LM_ERROR,
          ACE_TEXT("(%P:%t) ERROR: svc() - ")
          ACE_TEXT("timed out waiting for acks!\n")
        ), 1);
      }
    }

    // Clean-up!
    ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)       <- PUBLISHER PARTICIPANT DEL CONT ENTITIES\n")));
    participant->delete_contained_entities();
    ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)       <- PUBLISHER DELETE PARTICIPANT\n")));
    dpf->delete_participant(participant.in());
    ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)       <- PUBLISHER PARTICIPANT VARS GOING OUT OF SCOPE\n")));
  }
  catch (const CORBA::Exception& e)
  {
    e._tao_print_exception("caught in svc()");
    return 1;
  }

  ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)    <- PARTICIPANT FINISHED\n")));

  return 0;
}
Exemplo n.º 3
0
int
ParticipantTask::svc()
{
    try
    {
        ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)    -> PARTICIPANT STARTED\n")));

        ACE_Time_Value delay_between_pubs(0, this->delay_between_pubs_msec_ * 1000);

        DDS::DomainParticipantFactory_var dpf = TheParticipantFactory;
        // 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: svc()")
                              ACE_TEXT(" create_participant failed!\n")), 1);

        // Create Publisher
        DDS::Publisher_var publisher =
            participant->create_publisher(PUBLISHER_QOS_DEFAULT,
                                          DDS::PublisherListener::_nil(),
                                          ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

        if (CORBA::is_nil(publisher.in()))
            ACE_ERROR_RETURN((LM_ERROR,
                              ACE_TEXT("%N:%l: svc()")
                              ACE_TEXT(" create_publisher failed!\n")), 1);


        // 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: svc()")
                              ACE_TEXT(" register_type failed!\n")), 1);

        // 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);

        if (CORBA::is_nil(topic.in()))
            ACE_ERROR_RETURN((LM_ERROR,
                              ACE_TEXT("%N:%l: svc()")
                              ACE_TEXT(" create_topic failed!\n")), 1);

        // Create DataWriter
        DDS::DataWriterQos writer_qos;
        publisher->get_default_datawriter_qos(writer_qos);

        writer_qos.history.depth = samples_per_thread_;

        if (deadline_.sec > 0 || deadline_.nanosec > 0)
        {
            writer_qos.deadline.period.sec = deadline_.sec;
            writer_qos.deadline.period.nanosec = deadline_.nanosec;
        }

        DDS::DataWriter_var writer =
            publisher->create_datawriter(topic.in(),
                                         writer_qos,
                                         DDS::DataWriterListener::_nil(),
                                         ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

        if (CORBA::is_nil(writer.in()))
            ACE_ERROR_RETURN((LM_ERROR,
                              ACE_TEXT("%N:%l: svc()")
                              ACE_TEXT(" create_datawriter failed!\n")), 1);

        FooDataWriter_var writer_i = FooDataWriter::_narrow(writer);
        if (CORBA::is_nil(writer_i))
            ACE_ERROR_RETURN((LM_ERROR,
                              ACE_TEXT("%N:%l: svc()")
                              ACE_TEXT(" _narrow failed!\n")), 1);

        // Block until Subscriber is available
        DDS::StatusCondition_var cond = writer->get_statuscondition();
        cond->set_enabled_statuses(DDS::PUBLICATION_MATCHED_STATUS);

        DDS::WaitSet_var ws = new DDS::WaitSet;
        ws->attach_condition(cond);

        DDS::Duration_t timeout =
        { DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC };

        DDS::ConditionSeq conditions;
        DDS::PublicationMatchedStatus matches = {0, 0, 0, 0, 0};
        do
        {
            if (ws->wait(conditions, timeout) != DDS::RETCODE_OK)
                ACE_ERROR_RETURN((LM_ERROR,
                                  ACE_TEXT("%N:%l: svc()")
                                  ACE_TEXT(" wait failed!\n")), 1);

            if (writer->get_publication_matched_status(matches) != ::DDS::RETCODE_OK)
            {
                ACE_ERROR ((LM_ERROR,
                            "(%P|%t) ERROR: failed to get publication matched status\n"));
                ACE_OS::exit (1);
            }
        }
        while (matches.current_count < 1);

        ws->detach_condition(cond);

        // The following is intentionally inefficient to stress various
        // pathways related to publication; we should be especially dull
        // and write only one sample at a time per writer.

        ProgressIndicator progress("(%P|%t)       PARTICIPANT %d%% (%d samples sent)\n",
                                   samples_per_thread_);

        for (std::size_t i = 0; i < samples_per_thread_; ++i)
        {
            Foo foo;
            foo.key = 3;
            DDS::InstanceHandle_t handle = writer_i->register_instance(foo);

            if (writer_i->write(foo, handle) != DDS::RETCODE_OK)
                ACE_ERROR_RETURN((LM_ERROR,
                                  ACE_TEXT("%N:%l: svc()")
                                  ACE_TEXT(" write failed!\n")), 1);
            ++progress;
            ACE_OS::sleep(delay_between_pubs);
        }

        DDS::Duration_t interval = { 30, 0};
        if( DDS::RETCODE_OK != writer->wait_for_acknowledgments( interval)) {
            ACE_ERROR_RETURN((LM_ERROR,
                              ACE_TEXT("(%P:%t) ERROR: svc() - ")
                              ACE_TEXT("timed out waiting for acks!\n")
                             ), 1);
        }
        publisher->delete_datawriter(writer);

        // Clean-up!
        participant->delete_contained_entities();
        dpf->delete_participant(participant.in());
    }
    catch (const CORBA::Exception& e)
    {
        e._tao_print_exception("caught in svc()");
        return 1;
    }

    ACE_DEBUG((LM_INFO, ACE_TEXT("(%P|%t)    <- PARTICIPANT FINISHED\n")));

    return 0;
}
Exemplo n.º 4
0
int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  DDS::DomainParticipantFactory_var dpf;
  DDS::DomainParticipant_var participant;

  try {

    std::cout << "Starting publisher" << std::endl;
    {
      // Initialize DomainParticipantFactory
      dpf = TheParticipantFactoryWithArgs(argc, argv);

      std::cout << "Starting publisher with " << argc << " args" << std::endl;
      int error;
      if ((error = parse_args(argc, argv)) != 0) {
        return error;
      }

      // Create DomainParticipant
      participant = dpf->create_participant(4,
                                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 TypeSupport (Messenger::Message)
      Messenger::MessageTypeSupport_var mts =
        new Messenger::MessageTypeSupportImpl();

      if (mts->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
      CORBA::String_var type_name = mts->get_type_name();
      DDS::Topic_var topic =
        participant->create_topic("Movie Discussion List",
                                  type_name.in(),
                                  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 Publisher
      DDS::Publisher_var pub =
        participant->create_publisher(PUBLISHER_QOS_DEFAULT,
                                      DDS::PublisherListener::_nil(),
                                      OpenDDS::DCPS::DEFAULT_STATUS_MASK);

      if (CORBA::is_nil(pub.in())) {
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: main()")
                          ACE_TEXT(" ERROR: create_publisher failed!\n")),
                         -1);
      }

      DDS::DataWriterQos qos;
      pub->get_default_datawriter_qos(qos);
      if (dw_reliable()) {
        std::cout << "Reliable DataWriter" << std::endl;
        qos.history.kind = DDS::KEEP_ALL_HISTORY_QOS;
        qos.reliability.kind = DDS::RELIABLE_RELIABILITY_QOS;
      }

      // Create DataWriter
      DDS::DataWriter_var dw =
        pub->create_datawriter(topic.in(),
                               qos,
                               DDS::DataWriterListener::_nil(),
                               OpenDDS::DCPS::DEFAULT_STATUS_MASK);

      if (CORBA::is_nil(dw.in())) {
        ACE_ERROR_RETURN((LM_ERROR,
                          ACE_TEXT("%N:%l: main()")
                          ACE_TEXT(" ERROR: create_datawriter failed!\n")),
                         -1);
      }

      // Start writing threads
      std::cout << "Creating Writer" << std::endl;
      Writer* writer = new Writer(dw.in());
      std::cout << "Starting Writer" << std::endl;
      writer->start();

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

      std::cout << "Writer finished " << std::endl;
      writer->end();

      if (wait_for_acks) {
        std::cout << "Writer wait for ACKS" << std::endl;

        DDS::Duration_t timeout =
          { DDS::DURATION_INFINITE_SEC, DDS::DURATION_INFINITE_NSEC };
        dw->wait_for_acknowledgments(timeout);
      } else {
        // let any missed multicast/rtps messages get re-delivered
        ACE_Time_Value small_time(0, 250000);
        ACE_OS::sleep(small_time);
      }

      std::cout << "deleting DW" << std::endl;
      delete writer;
    }
    // Clean-up!
    participant->delete_contained_entities();
    dpf->delete_participant(participant.in());
    TheServiceParticipant->shutdown();

  } catch (const CORBA::Exception& e) {
    e._tao_print_exception("Exception caught in main():");
    ACE_OS::exit(-1);
  }

  return 0;
}