コード例 #1
0
ファイル: publisher.cpp プロジェクト: DOCGroup/DDS_Test
int
main (int argc, char *argv[])
{
  // Try to set real-time scheduling class. Requires login as
  // superuser or administrator.
  //set_rt ();
  
  int status = 0;

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

      // let the Service_Participant (in above line) strip out
      // -DCPSxxx parameters and then get application specific parameters.
      status = parse_args (argc, argv);
      
      if (0 != status)
        {
          return status;
        }

      ::DDS::DomainParticipant_var dp = 
        dpf->create_participant (TEST_DOMAIN, 
                                 PARTICIPANT_QOS_DEFAULT, 
                                 ::DDS::DomainParticipantListener::_nil ());

      if (CORBA::is_nil (dp.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%P|%t ERROR: create_participant ")
                             ACE_TEXT ("failed.\n")),
                            1);
        }

      DDS::ReturnCode_t ret_code = ::DDS::RETCODE_OK;

      BytesTypeSupportImpl* bytes_ts_servant =
        new BytesTypeSupportImpl ();
        
      PortableServer::ServantBase_var safe_servant =
        bytes_ts_servant;

      BytesTypeSupport_var bytes_ts = 
        TAO::DCPS::servant_to_reference_2<BytesTypeSupport> (
          bytes_ts_servant);
      
      ret_code = bytes_ts->register_type (dp.in (), TEST_TYPE);

      if (::DDS::RETCODE_OK != ret_code)
        {
          ACE_ERROR_RETURN ((LM_ERROR, 
                             ACE_TEXT ("%P|%t ERROR: Failed to register ")
                             ACE_TEXT ("type support.")
                             ACE_TEXT ("for data size %d\n"),
                             DATA_SIZE),
                            1);
        }

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

      topic_qos.resource_limits.max_samples_per_instance =
        MAX_SAMPLES_PER_INSTANCE;
      topic_qos.resource_limits.max_instances = MAX_INSTANCES;
      topic_qos.resource_limits.max_samples = MAX_SAMPLES;

      if (isReliable)
        {
      
          topic_qos.reliability.kind = ::DDS::RELIABLE_RELIABILITY_QOS;
          topic_qos.reliability.max_blocking_time.sec = max_mili_sec_blocking / 1000;
      
          topic_qos.reliability.max_blocking_time.nanosec = 
            (max_mili_sec_blocking % 1000) * 1000*1000;
        }
      else
        {
          topic_qos.reliability.kind = ::DDS::BEST_EFFORT_RELIABILITY_QOS;          
        }

      if (QoS_KEEP_ALL)
        {
          topic_qos.history.kind = ::DDS::KEEP_ALL_HISTORY_QOS;
        }

      ::DDS::Topic_var topic = 
      dp->create_topic (test_topic_name.c_str (), 
                            TEST_TYPE, 
                            topic_qos, 
                            ::DDS::TopicListener::_nil ());

      ACE_DEBUG ((LM_DEBUG, "(%P|%t)The current topic is %s\n",
         test_topic_name.c_str()));
     
      if (CORBA::is_nil (topic.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%P|%t ERROR: ")
                             ACE_TEXT ("create_topic failed.\n")),
                            1);
        }

      // Create the publisher
      ::DDS::Publisher_var pub =
        dp->create_publisher (PUBLISHER_QOS_DEFAULT,
                              ::DDS::PublisherListener::_nil ());
      
      if (CORBA::is_nil (pub.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%P|%t ERROR: ")
                             ACE_TEXT ("create_publisher failed.\n")),
                            1);
        }

      // Initialize the transport
      if (0 != ::init_writer_tranport ())
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT("%P|%t ERROR: ")
                           ACE_TEXT ("init_transport failed!\n")),
                          1);
      }

      // Attach the publisher to the transport.
      ::TAO::DCPS::PublisherImpl* pub_impl =
        ::TAO::DCPS::reference_to_servant< ::TAO::DCPS::PublisherImpl,
                                           ::DDS::Publisher_ptr> (
          pub.in ());

      if (0 == pub_impl)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%P|%t ERROR: Failed ")
                             ACE_TEXT ("to obtain servant ")
                             ACE_TEXT ("::TAO::DCPS::PublisherImpl\n")),
                            1);
        }

      TAO::DCPS::AttachStatus attach_status =
        pub_impl->attach_transport (writer_transport_impl.in ());

      if (attach_status != TAO::DCPS::ATTACH_OK)
        {
          // We failed to attach to the transport for some reason.
          std::string status_str;

          switch (attach_status)
            {
              case TAO::DCPS::ATTACH_BAD_TRANSPORT:
                status_str = "ATTACH_BAD_TRANSPORT";
                break;
              case TAO::DCPS::ATTACH_ERROR:
                status_str = "ATTACH_ERROR";
                break;
              case TAO::DCPS::ATTACH_INCOMPATIBLE_QOS:
                status_str = "ATTACH_INCOMPATIBLE_QOS";
                break;
              default:
                status_str = "Unknown Status";
                break;
            }

          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("%P|%t ERROR: Failed to ")
                             ACE_TEXT ("attach to the transport. ")
                             ACE_TEXT ("AttachStatus == %s\n"),
                             status_str.c_str()),
                            1);
        }

      // Create the datawriters
      ::DDS::DataWriterQos dw_qos;
      pub->get_default_datawriter_qos (dw_qos);
      pub->copy_from_topic_qos (dw_qos, topic_qos);

      ::DDS::DataWriter_var * dws =
        new ::DDS::DataWriter_var[num_datawriters];

      // Create one or multiple datawriters belonging to the same 
      // publisher.
      for (int k = 0; k < num_datawriters; ++k)
        {
          dws[k] = pub->create_datawriter (topic.in () ,
                                          dw_qos,
                                          ::DDS::DataWriterListener::_nil ());

          if (CORBA::is_nil (dws[k].in ()))
            {
              ACE_ERROR_RETURN ((LM_ERROR,
                                ACE_TEXT ("%P|%t ERROR: ")
                                ACE_TEXT ("create_datawriter failed.\n")),
                                1);
            }
        }

      Writer** writers = new Writer* [num_datawriters] ;

      // If we're using UDP then oversend the number of samples
      if (usingUdp)
      {
        // Using the precedence here from NDDS publisher for determining
        // oversampling value.
        STATS_SAMPLES = (STATS_SAMPLES + PRIMER_SAMPLES) * OVERSAMPLEMULTIPLIER;
      }

      for (int p = 0; p < num_datawriters; ++p)
        {
          writers[p] = new Writer (dws[p].in (),
                                   pub_output_file.c_str (),
                                   PRIMER_SAMPLES,
                                   STATS_SAMPLES,
                                   DATA_SIZE,
                                   num_datareaders,
                                   id + p); 
          writers[p]->start ();
        }


      bool writers_finished = false;

      while (!writers_finished)
        {
          ACE_Guard<ACE_Recursive_Thread_Mutex> just_me (done_lock_);
          
          // wait for a writer to signal so we done spin
          // waiting to see if the publisher is done.
          done_condition_.wait ();
          writers_finished = true;
          
          for (int m = 0; m < num_datawriters; ++m)
            {
              writers_finished =
                writers_finished && writers[m]->is_finished ();
            }
        }
        
      ACE_OS::sleep (1);
        
      // Clean up publisher objects
      pub->delete_contained_entities ();

      delete [] dws;

      for (int q = 0; q < num_datawriters; ++q)
        {
          delete writers[q];
        }

      delete [] writers;

      dp->delete_publisher (pub.in ());     
      dp->delete_topic (topic.in ());      
      dpf->delete_participant (dp.in ());

      ACE_OS::sleep (2);
      TheTransportFactory->release ();

      TheServiceParticipant->shutdown (); 

      writer_transport_impl = 0;
    }
  catch (CORBA::Exception &ex)
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception caught in main.cpp:");
      return 1;
    }

  return status;
}
コード例 #2
0
ファイル: publisher.cpp プロジェクト: Fantasticer/OpenDDS
int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{

  int status = 0;

  try
    {
      ACE_DEBUG((LM_INFO,"(%P|%t) %T publisher main\n"));

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

      // let the Service_Participant (in above line) strip out -DCPSxxx parameters
      // and then get application specific parameters.
      status = parse_args (argc, argv);
      if (status)
        return status;

      ::DDS::DomainParticipant_var dp =
        dpf->create_participant(TEST_DOMAIN,
                                PARTICIPANT_QOS_DEFAULT,
                                ::DDS::DomainParticipantListener::_nil(),
                                ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (dp.in() ))
      {
        ACE_ERROR ((LM_ERROR,
                   ACE_TEXT("(%P|%t) ERROR: create_participant failed.\n")));
        return 1 ;
      }

      ::Xyz::Pt128TypeSupport_var pt128ts;
      ::Xyz::Pt512TypeSupport_var pt512ts;
      ::Xyz::Pt2048TypeSupport_var pt2048ts;
      ::Xyz::Pt8192TypeSupport_var pt8192ts;

      // Register the type supports
      switch (DATA_SIZE)
      {
      case 128:
        {
          pt128ts = new ::Xyz::Pt128TypeSupportImpl();

          if (::DDS::RETCODE_OK != pt128ts->register_type(dp.in(), TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%P|%t) ERROR: Failed to register the Pt128TypeSupport.")));
              return 1;
            }
        }
        break;
      case 512:
        {
          pt512ts = new ::Xyz::Pt512TypeSupportImpl();

          if (::DDS::RETCODE_OK != pt512ts->register_type(dp.in(), TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%P|%t) ERROR: Failed to register the Pt512TypeSupport.")));
              return 1;
            }
        }
        break;
      case 2048:
        {
          pt2048ts = new ::Xyz::Pt2048TypeSupportImpl();

          if (::DDS::RETCODE_OK != pt2048ts->register_type(dp.in(), TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%P|%t) ERROR: Failed to register the Pt2048TypeSupport.")));
              return 1;
            }
        }
        break;
      case 8192:
        {
          pt8192ts = new ::Xyz::Pt8192TypeSupportImpl();

          if (::DDS::RETCODE_OK != pt8192ts->register_type(dp.in(), TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT ("(%P|%t) ERROR: Failed to register the Pt8192TypeSupport.")));
              return 1;
            }
        }
      };



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

      topic_qos.resource_limits.max_samples_per_instance =
            MAX_SAMPLES_PER_INSTANCE;
      topic_qos.resource_limits.max_instances = MAX_INSTANCES;
      topic_qos.resource_limits.max_samples = MAX_SAMPLES;

      topic_qos.history.kind = ::DDS::KEEP_ALL_HISTORY_QOS;

      ::DDS::Topic_var topic =
        dp->create_topic (TEST_TOPIC,
                          TEST_TYPE,
                          topic_qos,
                          ::DDS::TopicListener::_nil(),
                          ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
      if (CORBA::is_nil (topic.in() ))
      {
        return 1 ;
      }

      // Create the publisher
      ::DDS::Publisher_var pub =
        dp->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("(%P|%t) ERROR: create_publisher failed.\n")),
                          1);
      }

      // Create the datawriters
      ::DDS::DataWriterQos dw_qos;
      pub->get_default_datawriter_qos (dw_qos);
      pub->copy_from_topic_qos (dw_qos, topic_qos);

      ::DDS::DataWriter_var * dws = new ::DDS::DataWriter_var[num_datawriters];

      // Create one or multiple datawriters belonging to the same
      // publisher.
      for (int k = 0; k < num_datawriters; k ++)
      {
        dws[k] = pub->create_datawriter(topic.in() ,
                                        dw_qos,
                                        ::DDS::DataWriterListener::_nil(),
                                        ::OpenDDS::DCPS::DEFAULT_STATUS_MASK);

        if (CORBA::is_nil (dws[k].in ()))
        {
          ACE_ERROR ((LM_ERROR,
                     ACE_TEXT("(%P|%t) ERROR: create_datawriter failed.\n")));
          return 1 ;
        }
      }


      Writer** writers = new Writer* [num_datawriters] ;

      for (int p = 0; p < num_datawriters; p ++)
      {
        writers[p] = new Writer(dws[p].in (),
                                NUM_SAMPLES,
                                DATA_SIZE,
                                id + p,
                                num_datareaders,
                                throttle_factor);
        writers[p]->start ();
      }


      bool writers_finished = false;

      while ( !writers_finished )
        {
          ACE_Guard<ACE_Recursive_Thread_Mutex> just_me(done_lock_);
          // wait for a writer to signal so we dont spin
          // waiting to see if the publisher is done.
          //ACE_Time_Value timeout(5,0);
          done_condition_.wait();
          writers_finished = true;
          for (int m = 0; m < num_datawriters; m ++)
            {
              writers_finished = writers_finished && writers[m]->is_finished();
            }
        }
      ACE_OS::sleep(10);

      // Clean up publisher objects
      pub->delete_contained_entities() ;

      delete [] dws;
      for (int q = 0; q < num_datawriters; q ++)
      {
        delete writers[q];
      }

      delete [] writers;

      dp->delete_publisher(pub.in ());

      dp->delete_topic(topic.in ());
      dpf->delete_participant(dp.in ());

      TheServiceParticipant->shutdown ();

    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught in main.cpp:");
      return 1;
    }

  return status;
}
コード例 #3
0
ファイル: publisher.cpp プロジェクト: svn2github/OpenDDS
int main (int argc, char *argv[])
{

  int status = 0;

  try
    {
      ACE_DEBUG((LM_INFO," %P|%t %T publisher main\n"));

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

      // let the Service_Participant (in above line) strip out -DCPSxxx parameters
      // and then get application specific parameters.
      status = parse_args (argc, argv);
      if (status)
        return status;

      ::DDS::DomainParticipant_var dp =
        dpf->create_participant(TEST_DOMAIN,
                                PARTICIPANT_QOS_DEFAULT,
                                ::DDS::DomainParticipantListener::_nil());
      if (CORBA::is_nil (dp.in() ))
      {
        ACE_ERROR ((LM_ERROR,
                   ACE_TEXT(" %P|%t ERROR: create_participant failed.\n")));
        return 1 ;
      }

      // Register the type supports
      switch (DATA_SIZE)
      {
      case 128:
        {
          ::Xyz::Pt128TypeSupportImpl* pt128ts_servant = new ::Xyz::Pt128TypeSupportImpl();
          TAO::DCPS::LocalObject_var safe_servant = pt128ts_servant;

          ::Xyz::Pt128TypeSupport_var pt128ts =
            TAO::DCPS::servant_to_reference (pt128ts_servant);

          if (::DDS::RETCODE_OK != pt128ts->register_type(dp.in() , TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT (" %P|%t ERROR: Failed to register the Pt128TypeSupport.")));
              return 1;
            }
        }
        break;
      case 512:
        {
          ::Xyz::Pt512TypeSupportImpl* pt512ts_servant = new ::Xyz::Pt512TypeSupportImpl();
          TAO::DCPS::LocalObject_var safe_servant = pt512ts_servant;

          ::Xyz::Pt512TypeSupport_var pt512ts =
            TAO::DCPS::servant_to_reference (pt512ts_servant);

          if (::DDS::RETCODE_OK != pt512ts->register_type(dp.in() , TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT (" %P|%t ERROR: Failed to register the Pt512TypeSupport.")));
              return 1;
            }
        }
        break;
      case 2048:
        {
          ::Xyz::Pt2048TypeSupportImpl* pt2048ts_servant = new ::Xyz::Pt2048TypeSupportImpl();
          TAO::DCPS::LocalObject_var safe_servant = pt2048ts_servant;

          ::Xyz::Pt2048TypeSupport_var pt2048ts =
            TAO::DCPS::servant_to_reference (pt2048ts_servant);

          if (::DDS::RETCODE_OK != pt2048ts->register_type(dp.in() , TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT (" %P|%t ERROR: Failed to register the Pt2048TypeSupport.")));
              return 1;
            }
        }
        break;
      case 8192:
        {
          ::Xyz::Pt8192TypeSupportImpl* pt8192ts_servant = new ::Xyz::Pt8192TypeSupportImpl();
          TAO::DCPS::LocalObject_var safe_servant = pt8192ts_servant;

          ::Xyz::Pt8192TypeSupport_var pt8192ts =
            TAO::DCPS::servant_to_reference (pt8192ts_servant);

          if (::DDS::RETCODE_OK != pt8192ts->register_type(dp.in() , TEST_TYPE))
            {
              ACE_ERROR ((LM_ERROR,
                          ACE_TEXT (" %P|%t ERROR: Failed to register the Pt8192TypeSupport.")));
              return 1;
            }
        }
      };



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

      topic_qos.resource_limits.max_samples_per_instance =
            MAX_SAMPLES_PER_INSTANCE;
      topic_qos.resource_limits.max_instances = MAX_INSTANCES;
      topic_qos.resource_limits.max_samples = MAX_SAMPLES;

      topic_qos.reliability.kind = ::DDS::RELIABLE_RELIABILITY_QOS;
      topic_qos.reliability.max_blocking_time.sec = max_mili_sec_blocking / 1000;
      topic_qos.reliability.max_blocking_time.nanosec =
                                   (max_mili_sec_blocking % 1000) * 1000*1000;
      topic_qos.history.kind = ::DDS::KEEP_ALL_HISTORY_QOS;

      ::DDS::Topic_var topic =
        dp->create_topic (TEST_TOPIC,
                          TEST_TYPE,
                          topic_qos,
                          ::DDS::TopicListener::_nil());
      if (CORBA::is_nil (topic.in() ))
      {
        return 1 ;
      }

      // Create the publisher
      ::DDS::Publisher_var pub =
        dp->create_publisher(PUBLISHER_QOS_DEFAULT,
                             ::DDS::PublisherListener::_nil());
      if (CORBA::is_nil (pub.in() ))
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT(" %P|%t ERROR: create_publisher failed.\n")),
                          1);
      }

      // Initialize the transport
      if (0 != ::init_writer_tranport() )
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                           ACE_TEXT(" %P|%t ERROR: init_transport failed!\n")),
                           1);
      }

      // Attach the publisher to the transport.
      TAO::DCPS::PublisherImpl* pub_impl
        = TAO::DCPS::reference_to_servant<TAO::DCPS::PublisherImpl> (pub.in());

      if (0 == pub_impl)
      {
        ACE_ERROR_RETURN ((LM_ERROR,
                          ACE_TEXT(" %P|%t ERROR: Failed to obtain servant ::TAO::DCPS::PublisherImpl\n")),
                          1);
      }

      TAO::DCPS::AttachStatus attach_status =
        pub_impl->attach_transport(writer_transport_impl.in());

      if (attach_status != TAO::DCPS::ATTACH_OK)
        {
          // We failed to attach to the transport for some reason.
          ACE_TString status_str;

          switch (attach_status)
            {
              case TAO::DCPS::ATTACH_BAD_TRANSPORT:
                status_str = "ATTACH_BAD_TRANSPORT";
                break;
              case TAO::DCPS::ATTACH_ERROR:
                status_str = "ATTACH_ERROR";
                break;
              case TAO::DCPS::ATTACH_INCOMPATIBLE_QOS:
                status_str = "ATTACH_INCOMPATIBLE_QOS";
                break;
              default:
                status_str = "Unknown Status";
                break;
            }

          ACE_ERROR_RETURN ((LM_ERROR,
                            ACE_TEXT(" %P|%t ERROR: Failed to attach to the transport. ")
                            ACE_TEXT("AttachStatus == %s\n"),
                            status_str.c_str()),
                            1);
        }

      // Create the datawriters
      ::DDS::DataWriterQos dw_qos;
      pub->get_default_datawriter_qos (dw_qos);
      pub->copy_from_topic_qos (dw_qos, topic_qos);

      ::DDS::DataWriter_var * dws = new ::DDS::DataWriter_var[num_datawriters];

      // Create one or multiple datawriters belonging to the same
      // publisher.
      for (int k = 0; k < num_datawriters; k ++)
      {
        dws[k] = pub->create_datawriter(topic.in() ,
                                        dw_qos,
                                        ::DDS::DataWriterListener::_nil());

        if (CORBA::is_nil (dws[k].in ()))
        {
          ACE_ERROR ((LM_ERROR,
                     ACE_TEXT(" %P|%t ERROR: create_datawriter failed.\n")));
          return 1 ;
        }
      }

      Writer** writers = new Writer* [num_datawriters] ;

      for (int p = 0; p < num_datawriters; p ++)
      {
        writers[p] = new Writer(dws[p].in (),
                                NUM_SAMPLES,
                                DATA_SIZE,
                                num_datareaders,
                                id + p);
        writers[p]->start ();
      }


      bool writers_finished = false;

      while ( !writers_finished )
        {
          ACE_Guard<ACE_Recursive_Thread_Mutex> just_me(done_lock_);
          // wait for a writer to signal so we done spin
          // waiting to see if the publisher is done.
          //ACE_Time_Value timeout(5,0);
          done_condition_.wait();
          writers_finished = true;
          for (int m = 0; m < num_datawriters; m ++)
            {
              writers_finished = writers_finished && writers[m]->is_finished();
            }
        }

      // Clean up publisher objects
      pub->delete_contained_entities() ;

      delete [] dws;

      for (int q = 0; q < num_datawriters; q ++)
      {
        delete writers[q];
      }

      delete [] writers;

      dp->delete_publisher(pub.in());

      dp->delete_topic(topic.in());
      dpf->delete_participant(dp.in());

      TheTransportFactory->release();
      TheServiceParticipant->shutdown ();

      writer_transport_impl = 0;
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("Exception caught in main.cpp:");
      return 1;
    }

  return status;
}