// 头文件
#pragma once
#include <string>
#include <map>
#include "ndds/ndds_cpp.h"
#include "ndds/ndds_namespace_cpp.h"
using namespace DDS;
//#include "DDSData/DDS615Data.h"
//#include "DDSData/DDS615DataSupport.h"
//#include "dds/lz.h"
//#include "dds/lzSupport.h"
//using namespace LZ;
using namespace std;
class DDSDomainParticipant;
class DDSPublisher;
class DDSDataWriter;
class DDSTopic;
class MAINDataWriter;
class MAIN;
template <typename typeSupportT, typename typeDataWriter,typename typeData>
class MyTopicAndData
{
public:
MyTopicAndData(DDSDomainParticipant *participant, DDSPublisher *pPublisher)
{
m_pParticipant = participant;
m_pPublisher = pPublisher;
m_pDataWriter = NULL;
m_pTopic = NULL;
m_pMyDataWriter = NULL;
m_pDataInstance = NULL;
}
~MyTopicAndData()
{
}
bool Initialize(DDSDomainParticipant *participant, DDSPublisher *pPublisher,const char* strTopicName)
{
m_pParticipant = participant;
m_pPublisher = pPublisher;
m_pTopic = CreateTopic(strTopicName);
if ( NULL!= m_pTopic )
{
m_pDataWriter = CreateDataWriter(m_pTopic);
if ( NULL != m_pDataWriter)
{
m_pMyDataWriter = GetMyDataWriter(m_pDataWriter);
if ( NULL!= m_pMyDataWriter)
{
m_pDataInstance = typeSupportT::create_data();
if (NULL!= m_pDataInstance)
{
return true;
}
}
}
}
return true;
}
DDSTopic* CreateTopic(const char*strTopicName)
{
DDSTopic* pTopic = NULL;
if (NULL != m_pParticipant)
{
DDS_Duration_t period = { 0, 0 };
pTopic = m_pParticipant->find_topic(strTopicName, period);
if (NULL == pTopic)
{
ReturnCode_t retcode;
InstanceHandle_t instance_handle = HANDLE_NIL;
const char *type_name = NULL;
/* Register type before creating topic */
type_name = typeSupportT::get_type_name();
retcode = typeSupportT::register_type(
m_pParticipant, type_name);
if (retcode != RETCODE_OK) {
fprintf(stderr, "register_type error %d\n", retcode);
//publisher_shutdown(m_pParticipant);
return NULL;
}
//m_pParticipant->topic
/* To customize topic QoS, use
the configuration file USER_QOS_PROFILES.xml */
pTopic = m_pParticipant->create_topic(
strTopicName,
type_name, TOPIC_QOS_DEFAULT, NULL /* listener */,
STATUS_MASK_NONE);
if (pTopic == NULL) {
fprintf(stderr, "create_topic error\n");
//publisher_shutdown(m_pParticipant);
return NULL;
}
}
}
return pTopic;
}
DDSDataWriter* CreateDataWriter(DDSTopic* pTopic)
{
DDSDataWriter* pWrite = NULL;
/* To customize data writer QoS, use
the configuration file USER_QOS_PROFILES.xml */
pWrite = m_pPublisher->create_datawriter(
m_pTopic, DATAWRITER_QOS_DEFAULT, NULL /* listener */,
STATUS_MASK_NONE);
if (pWrite == NULL) {
fprintf(stderr, "create_datawriter error\n");
//publisher_shutdown(m_pParticipant);
return NULL;
}
return pWrite;
}
typeData* GetMemoryInstance()
{
if (NULL == m_pDataInstance)
{
m_pDataInstance = typeSupportT::create_data();
if (NULL == m_pDataInstance)
{
return NULL;
}
}
return m_pDataInstance;
}
typeData* GetDataMemory()
{
typeData* pDataMem = NULL;
pDataMem = typeSupportT::create_data();
if (NULL == pDataMem)
{
return NULL;
}
return pDataMem;
}
void DeleteDataMemory(typeData* pDataMem)
{
ReturnCode_t retcode = typeSupportT::delete_data(pDataMem);
if (retcode != RETCODE_OK)
{
printf( "DeleteDataMemory error.\n");
//return NULL;
}
//return pDataMem;
}
// 发送数据
bool SendData(typeData* pDataInstance)
{
if (NULL == m_pMyDataWriter ||
NULL == pDataInstance)
{
return false;
}
ReturnCode_t retcode;
InstanceHandle_t instance_handle = HANDLE_NIL;
retcode = m_pMyDataWriter->write(*pDataInstance, instance_handle);
if (retcode != RETCODE_OK) {
return false;
}
return true;
}
typeDataWriter* GetMyDataWriter(DDSDataWriter* pWrite)
{
typeDataWriter* pMyDataWriter = NULL;
if (NULL != m_pMyDataWriter)
{
return m_pMyDataWriter;
}
pMyDataWriter = typeDataWriter::narrow(pWrite);
if (pMyDataWriter == NULL) {
fprintf(stderr, "DataWriter narrow error\n");
//publisher_shutdown(m_pParticipant);
return NULL;
}
return pMyDataWriter;
}
MyTopicAndData()
{
m_pDataWriter = NULL;
m_pTopic = NULL;
m_pMyDataWriter = NULL;
m_pDataInstance = NULL;
}
public:
DDSTopic* m_pTopic;
DDSDataWriter* m_pDataWriter;
typeDataWriter* m_pMyDataWriter;
typeData* m_pDataInstance;
private:
DDSDomainParticipant * m_pParticipant;
DDSPublisher *m_pPublisher;
};
class DDSHelper;
typedef void (*FUNCPROCESSDATA)(int, void*,int);
template <typename typeSupportT, typename typeDataReader, typename typeDataSeq>
class DataSubScriberListener : public DataReaderListener {
public:
bool Init(DDSDomainParticipant *pParticipant, DDSSubscriber *subscriber,string strTopic)
{
m_pParticipant = pParticipant;
DDSTopic* pTopic = CreateTopic(strTopic.c_str());
// 888888888888888888888888888
m_pDDSReader = subscriber->create_datareader(
pTopic, DATAREADER_QOS_DEFAULT, this,
STATUS_MASK_ALL);
if (m_pDDSReader == NULL) {
printf("create_datareader error\n");
return false;
}
return true;
}
void AddCallBack(int nDataType, FUNCPROCESSDATA callback)
{
m_nDataType = nDataType;
m_Fcallback = callback;
}
DDSTopic* CreateTopic(const char*strTopicName)
{
DDSTopic* pTopic = NULL;
if (NULL != m_pParticipant)
{
DDS_Duration_t period = { 0, 0 };
pTopic = m_pParticipant->find_topic(strTopicName, period);
if (NULL == pTopic)
{
ReturnCode_t retcode;
//InstanceHandle_t instance_handle = HANDLE_NIL;
const char *type_name = NULL;
/* Register type before creating topic */
type_name = typeSupportT::get_type_name();
retcode = typeSupportT::register_type(
m_pParticipant, type_name);
if (retcode != RETCODE_OK) {
fprintf(stderr, "register_type error %d\n", retcode);
//publisher_shutdown(m_pParticipant);
return NULL;
}
//m_pParticipant->topic
/* To customize topic QoS, use
the configuration file USER_QOS_PROFILES.xml */
pTopic = m_pParticipant->create_topic(
strTopicName,
type_name, TOPIC_QOS_DEFAULT, NULL /* listener */,
STATUS_MASK_NONE);
if (pTopic == NULL) {
fprintf(stderr, "create_topic error\n");
//publisher_shutdown(m_pParticipant);
return NULL;
}
}
}
return pTopic;
}
virtual void on_requested_deadline_missed(
DataReader* /*reader*/,
const RequestedDeadlineMissedStatus& /*status*/) {}
virtual void on_requested_incompatible_qos(
DataReader* /*reader*/,
const RequestedIncompatibleQosStatus& /*status*/) {}
virtual void on_sample_rejected(
DataReader* /*reader*/,
const SampleRejectedStatus& /*status*/) {}
virtual void on_liveliness_changed(
DataReader* /*reader*/,
const LivelinessChangedStatus& /*status*/) {}
virtual void on_sample_lost(
DataReader* /*reader*/,
const SampleLostStatus& /*status*/) {}
virtual void on_subscription_matched(
DataReader* /*reader*/,
const SubscriptionMatchedStatus& /*status*/) {}
virtual void on_data_available(DataReader* reader)
{
typeDataReader *LZ_MAIN_reader = NULL;
typeDataSeq data_seq;
SampleInfoSeq info_seq;
ReturnCode_t retcode;
int i;
LZ_MAIN_reader = typeDataReader::narrow(reader);
if (LZ_MAIN_reader == NULL) {
printf("DataReader narrow error\n");
return;
}
retcode = LZ_MAIN_reader->take(
data_seq, info_seq, LENGTH_UNLIMITED,
ANY_SAMPLE_STATE, ANY_VIEW_STATE, ANY_INSTANCE_STATE);
if (retcode == RETCODE_NO_DATA) {
return;
}
else if (retcode != RETCODE_OK) {
printf("take error %d\n", retcode);
return;
}
//data_seq[0].head;
for (i = 0; i < data_seq.length(); ++i) {
if (info_seq[i].valid_data) {
m_Fcallback(m_nDataType, (&data_seq[i]), sizeof(data_seq[i]));
}
}
retcode = LZ_MAIN_reader->return_loan(data_seq, info_seq);
if (retcode != RETCODE_OK) {
printf("return loan error %d\n", retcode);
}
}
private:
int m_nDataType;
FUNCPROCESSDATA m_Fcallback;
DDSDataReader *m_pDDSReader;
DDSDomainParticipant *m_pParticipant;// = NULL;
};
//#include "dds/lz.h"
//#include "dds/lzSupport.h"
//#include "DDSData/DataTransmition.h"
//#include "DDSData/DataTransmitionSupport.h"
class DDSHelper
{
public:
~DDSHelper();
DDSHelper(int nDomainId);
int publisher_shutdown(
DDSDomainParticipant *participant);
// bool InitPublisher( string strTopicID);
// bool InitSubScriber(string strTopicID, FUNCPROCESSDATA callback, int nCallBackID = 1000);
private:
DDSHelper();
public:
DDSDomainParticipant *m_pParticipant;
DDSPublisher *m_pPublisher;
DDSSubscriber *m_pSubscriber;
};
//源文件
//#include "stdafx.h"
#include "DDSHelper.h"
#include <stdio.h>
#include <stdlib.h>
DDSHelper::DDSHelper()
{
}
DDSHelper::~DDSHelper()
{
if (NULL != m_pParticipant)
{
publisher_shutdown(m_pParticipant);
}
}
/* Delete all entities */
int DDSHelper::publisher_shutdown(
DDSDomainParticipant *participant)
{
DDS_ReturnCode_t retcode;
int status = 0;
if (participant != NULL) {
retcode = participant->delete_contained_entities();
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "delete_contained_entities error %d\n", retcode);
status = -1;
}
retcode = DDSTheParticipantFactory->delete_participant(participant);
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "delete_participant error %d\n", retcode);
status = -1;
}
}
/* RTI Connext provides finalize_instance() method on
domain participant factory for people who want to release memory used
by the participant factory. Uncomment the following block of code for
clean destruction of the singleton. */
/*
retcode = DDSDomainParticipantFactory::finalize_instance();
if (retcode != DDS_RETCODE_OK) {
fprintf(stderr, "finalize_instance error %d\n", retcode);
status = -1;
}
*/
return status;
}
DDSHelper::DDSHelper(int nDomainId) :m_pParticipant(NULL), m_pPublisher(NULL)
{
/* To customize participant QoS, use
the configuration file USER_QOS_PROFILES.xml */
m_pParticipant = TheParticipantFactory->create_participant(
nDomainId, PARTICIPANT_QOS_DEFAULT,
NULL /* listener */, STATUS_MASK_NONE);
if (m_pParticipant == NULL) {
printf("create_participant error\n");
publisher_shutdown(m_pParticipant);
return ;
}
/* To customize publisher QoS, use
the configuration file USER_QOS_PROFILES.xml */
m_pPublisher = m_pParticipant->create_publisher(
PUBLISHER_QOS_DEFAULT, NULL /* listener */, STATUS_MASK_NONE);
if (m_pPublisher == NULL) {
printf("create_publisher error\n");
//publisher_shutdown(participant);
return ;
}
m_pSubscriber = m_pParticipant->create_subscriber(
SUBSCRIBER_QOS_DEFAULT, NULL /* listener */, STATUS_MASK_NONE);
if (m_pSubscriber == NULL) {
printf("create_subscriber error\n");
//subscriber_shutdown(participant);
return ;
}
}