// Copyright Unpublished 2003-2013. All rights reserved under the copyright
// laws by Stauder Technologies. This material may be reproduced by or for
// the U.S. Government pursuant to the copyright license under the clause at
// DFARS 252.227-7013. Furnished to the U.S. Government with Government
// Purpose Rights as defined in DFARS 252.227-7013, and Government Rights as
// defined in DFARS 252.227.7022, which take precedence over any restrictive
// markings herein.
//
// EXPORT CONTROLLED INFORMATION
//
// This document contains technical information which may require an export
// control license or prior program management approval for distribution to
// non-U.S. entities. Violations may invoke criminal and civil penalties.
///////////////////////////////////////////////////////////////////////////////
#include "TrackCUDEvents.h"
#include "TrackEventSupport.h"
#define TRACK_GET_RESPONSE_TOPIC "TrackGetResponse"
#define QOS_LIBRARY_NAME "JTCWDDS_QOS"
#define QOS_PROFILE_NAME_TRACKEVENTS "TrackEvents"
namespace UnManagedC2PCC
{
TrackCUDEventsController::TrackCUDEventsController()
:m_TrackEventDataReader(NULL),
m_DDSSubscriber(NULL), m_bInitializedSuccessfully(false),
m_bStartedSuccessfully(false), m_TrackCUDEventsListener(NULL), m_TrackEventTopic(NULL),
m_TrackEventReader(NULL)
{
}
///
/// The method Init() initializes enumeration process related DDS structures
/// based on provided DDS Domain participant, publisher and subscriber which
/// are created by the parent DDS Manager class-> The method creates the reader
/// and writers for Track Enumeration topic-> The method returns true if all
/// registrations are successfull-> If method fails, it cleans up itself and
/// no additional clean up is necessary->
///
///
/// The "DDS::DomainParticipant ddsParticipant" parameter is provided by DDS Manager
/// class and is the DDS entity existing per process
///
///
/// The "DDS::Publisher ddsPublisher" parameter is provided by DDS Manager
/// class and is the DDS entity existing per process
///
///
/// The "DDS::Subscriber ddsSubscriberr" parameter is provided by DDS Manager
/// class and is the DDS entity existing per process
///
///
/// The error std::string& is generated if some calls to DDS fail-> The method can still return
/// successful result if the error is not fatal, see method summary for details->
///
///
/// bool "true" or "false" indicating success or failure of initialization process->
///
bool TrackCUDEventsController::Init(int domainId, std::string& strError)
{
bool bReturn = false;
DDS_DomainParticipantQos* participant_qos = new DDS_DomainParticipantQos();
DDS_ReturnCode_t retcode = DDS::DomainParticipantFactory::get_instance()->get_default_participant_qos(*participant_qos);
if ( retcode == DDS_RETCODE_OK )
{
participant_qos->transport_builtin.mask = DDS_TRANSPORTBUILTIN_UDPv4;
participant_qos->discovery.initial_peers.length(2);
const char* szOneLine = "builtin.udpv4://127.0.0.1";
const char* szTwoLine = "builtin.udpv4://239.225.0.1";
participant_qos->discovery.initial_peers.set_at(0, szOneLine);
participant_qos->discovery.initial_peers.set_at(1, szTwoLine);
m_DDSParticipant = TheParticipantFactory->create_participant(
domainId,
*participant_qos,
NULL /* listener */, DDS_STATUS_MASK_NONE);
if (m_DDSParticipant == NULL)
{
// This is a fatal error-> At this early initialization stage no clean up is necessary
strError += "TrackCUDEventsController::Init - create_participant error";
}
else
{
bReturn = CreateSubscriber(strError);
if (bReturn == true)
{
bReturn &= SetupTrackEvemtsDataReader(strError);
}
}
}
m_bInitializedSuccessfully = bReturn;
return bReturn;
}
bool TrackCUDEventsController::CreateSubscriber(std::string& strError)
{
bool bReturn = false;
// Create subscriber now-> The subscription services are crucial for the client
// because they are used both for command/response topics and for events
// To customize subscriber QoS, one could use the method get_default_subscriber_qos()
// first and then modify received QOS and call the method below with custom QOS
// m_DDSSubscriber = m_DDSParticipant->create_subscriber_with_profile(
// GWCLIENT_QOS_LIBRARY, DEFAULT_PROFILE, NULL /* listener */, DDS_STATUS_MASK_NONE);
DDS_SubscriberQos qos;
DDS_ReturnCode_t retcode = m_DDSParticipant->get_default_subscriber_qos(qos);
if ( retcode == DDS_RETCODE_OK )
{
m_DDSSubscriber = m_DDSParticipant->create_subscriber(qos, NULL, DDS_STATUS_MASK_NONE);
if (m_DDSSubscriber == NULL)
{
// This is a fatal error-> Create error std::string& and clean up already created structures
strError += "DDSManager::Init - create_subscriber error";
bReturn = false;
}
else
{
bReturn = true;
}
}
return(bReturn);
}
bool TrackCUDEventsController::SetupTrackEvemtsDataReader(std::string& strError)
{
bool bResult = false;
DDS_ReturnCode_t retcode;
/* Register the type before creating the pTopic */
retcode = JTCWDDS::TrackCUDEventTypeSupport::register_type(
m_DDSParticipant, JTCWDDS::TrackCUDEventTypeSupport::get_type_name() );
if ( retcode != DDS_RETCODE_OK )
{
printf( "register_type error %d\n", retcode );
}
else
{
//m_TrackResponseTopic = m_DDSParticipant->create_topic_with_profile(
// TRACK_GET_RESPONSE_TOPIC,
// JTCWDDS::TrackGetResponseTypeSupport::get_type_name(),
// GWCLIENT_QOS_LIBRARY,
// RESPONSE_TOPIC_PROFILE,
// NULL /* listener */,
// DDS_STATUS_MASK_NONE );
DDS_TopicQos qos;
retcode = m_DDSParticipant->get_default_topic_qos(qos);
if ( retcode == DDS_RETCODE_OK )
{
m_TrackEventTopic = m_DDSParticipant->create_topic(
TRACK_GET_RESPONSE_TOPIC,
JTCWDDS::TrackCUDEventTypeSupport::get_type_name(),
qos, NULL, DDS_STATUS_MASK_NONE);
if ( m_TrackEventTopic == NULL )
{
printf( "create_topic error\n" );
}
else
{
m_TrackCUDEventsListener = new TrackCUDEventsListener();
m_TrackEventReader = m_DDSSubscriber->create_datareader_with_profile(
m_TrackEventTopic, QOS_LIBRARY_NAME, QOS_PROFILE_NAME_TRACKEVENTS, m_TrackCUDEventsListener, DDS_STATUS_MASK_ALL );
if ( m_TrackEventReader == NULL )
{
printf( "create_datareader error\n" );
delete m_TrackCUDEventsListener;
}
else
{
bResult = true;
}
}
}
}
return(bResult);
}
///
/// shutdown ends the enumeration object->
///
void TrackCUDEventsController::shutdown()
{
if(m_TrackEventReader != NULL)
{
m_DDSParticipant->delete_datareader(m_TrackEventReader);
}
m_DDSParticipant->delete_topic(m_TrackEventTopic);
m_DDSParticipant->delete_subscriber(m_DDSSubscriber);
DDS::DomainParticipantFactory::get_instance()->delete_participant(m_DDSParticipant);
}//public void shutdown()
} // namespace JTCWDDS