void received_data(const Messenger::MessageSeq& data,
                   Messenger::MessageDataWriter_ptr mdw,
                   Messenger::Message& msg)
{
  for (CORBA::ULong j(0); j < data.length(); ++j) {
    cout << "took sample " << data[j].subject_id << endl;
    if (data[j].subject_id > 8) mdw->dispose(msg, DDS::HANDLE_NIL);
  }
}
Esempio n. 2
0
void DataReaderListenerImpl::on_data_available(DDS::DataReader_ptr reader)
throw(CORBA::SystemException)
{
  try {
    Messenger::MessageDataReader_var message_dr =
      Messenger::MessageDataReader::_narrow(reader);

    if (CORBA::is_nil(message_dr.in())) {
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("%T %N:%l: on_data_available()")
                 ACE_TEXT(" ERROR: _narrow failed!\n")));
      ACE_OS::exit(-1);
    }

    Messenger::MessageSeq messages;
    DDS::SampleInfoSeq info;

    DDS::ReturnCode_t error = message_dr->take(messages,
                                               info,
                                               DDS::LENGTH_UNLIMITED,
                                               DDS::ANY_SAMPLE_STATE,
                                               DDS::ANY_VIEW_STATE,
                                               DDS::ANY_INSTANCE_STATE);

    if (error == DDS::RETCODE_OK) {

      for (unsigned int i = 0; i < messages.length(); ++i) {
        const DDS::SampleInfo& si = info[i];
        if (si.valid_data) {
          const Messenger::Message& message = messages[i];

          // output for console to consume
          std::stringstream ss;
          ss << "Message: from writer " << message.process_id.in()
             << "->" << message.participant_id
             << "->" << message.writer_id
             << " sample_id = " << message.sample_id
             << " for reader=" << id_
             << std::endl;
          std::cerr << ss.str();
          // also track it in the log file
          ACE_DEBUG((LM_DEBUG,
                     ACE_TEXT("%T %N:%l: Message: process_id = %C ")
                     ACE_TEXT("participant_id = %d ")
                     ACE_TEXT("writer_id = %d ")
                     ACE_TEXT("sample_id = %d ")
                     ACE_TEXT("for reader = %C\n"),
                     message.process_id.in(),
                     message.participant_id,
                     message.writer_id,
                     message.sample_id,
                     id_.c_str()));

          for (CORBA::ULong i = 0; i < message.data.length(); ++i) {
            if (message.data[i] != i % 256) {
              std::cout << "ERROR: Bad data at index " << i << " writer_id "
                        << message.writer_id << " sample_id " << message.sample_id
                        << std::endl;
              break;
            }
          }
          if (!options_.no_validation) {
            std::string process_id(message.process_id.in());
            processes_[process_id][message.participant_id][message.writer_id].insert(message.sample_id);
          }

          ++num_samples_;

        } else if (si.instance_state == DDS::NOT_ALIVE_DISPOSED_INSTANCE_STATE) {
          ACE_DEBUG((LM_DEBUG, ACE_TEXT("%T %N:%l: INFO: instance is disposed\n")));

        } else if (si.instance_state == DDS::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE) {
          ACE_DEBUG((LM_DEBUG, ACE_TEXT("%T %N:%l: INFO: instance is unregistered\n")));

        } else {
          ACE_ERROR((LM_ERROR,
                     ACE_TEXT("%T %N:%l: on_data_available()")
                     ACE_TEXT(" ERROR: unknown instance state: %d\n"),
                     si.instance_state));
        }
      }
    } else {
      ACE_ERROR((LM_ERROR,
                 ACE_TEXT("%T %N:%l: on_data_available()")
                 ACE_TEXT(" ERROR: unexpected status: %d\n"),
                 error));
    }

  } catch (const CORBA::Exception& e) {
    e._tao_print_exception("Exception caught in on_data_available():");
    ACE_OS::exit(-1);
  }
}
void
SubscriberListenerImpl::on_data_on_readers(
  DDS::Subscriber_ptr subs)
{
  ::DDS::ReturnCode_t ret = subs->begin_access ();
  if (ret != ::DDS::RETCODE_OK) {
    ACE_ERROR((LM_ERROR,
                  ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                  ACE_TEXT(" ERROR: begin_access failed!\n")));
    ACE_OS::exit(-1);
  }

  DDS::SubscriberQos qos;
  ret = subs->get_qos (qos);
  if (ret != ::DDS::RETCODE_OK) {
    ACE_ERROR((LM_ERROR,
                  ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                  ACE_TEXT(" ERROR: get_qos failed!\n")));
    ACE_OS::exit(-1);
  }

  ::DDS::DataReaderSeq_var readers = new ::DDS::DataReaderSeq(100);

  ret = subs->get_datareaders(readers.inout(),
                              ::DDS::NOT_READ_SAMPLE_STATE,
                              ::DDS::ANY_VIEW_STATE,
                              ::DDS::ANY_INSTANCE_STATE) ;
  if (ret != ::DDS::RETCODE_OK) {
    ACE_ERROR((LM_ERROR,
                  ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                  ACE_TEXT(" ERROR: get_datareaders failed!\n")));
    ACE_OS::exit(-1);
  }

  CORBA::ULong len = readers->length ();

  if (qos.presentation.access_scope == ::DDS::GROUP_PRESENTATION_QOS) {
    // redirect datareader listener to receive DISPOSE and UNREGISTER notifications.
    if (len == 0) {
      if (subs->notify_datareaders () != ::DDS::RETCODE_OK) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: notify_datareaders failed!\n")));
        this->verify_result_ = false;
      }

      return;
    }

    if (len != num_messages * 4) {
      ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: get_datareaders returned %d readers!\n"),
                    len));
      this->verify_result_ = false;
    }

    for (CORBA::ULong i = 0; i < len; ++i) {
      Messenger::MessageDataReader_var message_dr =
        Messenger::MessageDataReader::_narrow(readers[i]);

      Messenger::MessageSeq msg;
      ::DDS::SampleInfoSeq si;
      ret = message_dr->take(msg, si,
              DDS::LENGTH_UNLIMITED,
              ::DDS::NOT_READ_SAMPLE_STATE,
              ::DDS::ANY_VIEW_STATE,
              ::DDS::ANY_INSTANCE_STATE) ;
      if (msg.length() != 1 || si.length() != 1) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: MessageSeq %d SampleInfoSeq %d!\n"),
                    msg.length(), si.length()));
        this->verify_result_ = false;
      }

      if (ret != DDS::RETCODE_OK) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: read failed!\n")));
        ACE_OS::exit(-1);
      }

      this->verify (msg[0], si[0], qos, false);
    }
  }
  else if (qos.presentation.access_scope == ::DDS::TOPIC_PRESENTATION_QOS) {
    // redirect datareader listener to receive DISPOSE and UNREGISTER notifications.
    if (len != 2) {
      if (subs->notify_datareaders () != ::DDS::RETCODE_OK) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: notify_datareaders failed!\n")));
        this->verify_result_ = false;
      }

      return;
    }


    for (CORBA::ULong i = 0; i < len; ++i) {
      Messenger::MessageDataReader_var message_dr =
        Messenger::MessageDataReader::_narrow(readers[i]);

      Messenger::MessageSeq msg;
      ::DDS::SampleInfoSeq si;
      ret = message_dr->take(msg, si,
              DDS::LENGTH_UNLIMITED,
              ::DDS::NOT_READ_SAMPLE_STATE,
              ::DDS::ANY_VIEW_STATE,
              ::DDS::ANY_INSTANCE_STATE) ;

      if (si[0].instance_state == DDS::NOT_ALIVE_DISPOSED_INSTANCE_STATE
         || si[0].instance_state == DDS::NOT_ALIVE_NO_WRITERS_INSTANCE_STATE)

      if (msg.length() != num_messages || si.length() != num_messages) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: MessageSeq %d SampleInfoSeq %d !\n"),
                    msg.length(), si.length()));
        this->verify_result_ = false;
      }

      if (ret != DDS::RETCODE_OK) {
        ACE_ERROR((LM_ERROR,
                    ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                    ACE_TEXT(" ERROR: read failed!\n")));
        ACE_OS::exit(-1);
      }

      CORBA::ULong num_samples = si.length();
      for (CORBA::ULong i = 0; i < num_samples; ++i) {
        this->verify (msg[i], si[i], qos, i == num_samples - 1 ? true : false);
      }
    }
  }
  else { //::DDS::INSTANCE_PRESENTATION_QOS
    subs->notify_datareaders ();
  }

  ret = subs->end_access ();
  if (ret != ::DDS::RETCODE_OK) {
    ACE_ERROR((LM_ERROR,
                  ACE_TEXT("%N:%l: SubscriberListenerImpl::on_data_on_readers()")
                  ACE_TEXT(" ERROR: end_access failed!\n")));
    ACE_OS::exit(-1);
  }

}