DDS可以不使用中心节点来发布、订阅,而是直接在配置文件里指定双方的信息:
点对点的静态配置peer.ini如下:
[common]
DCPSDefaultDiscovery=DEFAULT_STATIC
[topic/TheTopic]
type_name=Demo::Pos
max_message_size=300
[config/ConfigR]
transports=RtpsR
[transport/RtpsR]
transport_type=rtps_udp
use_multicast=0
local_address=127.0.0.1:21074
[config/ConfigW]
transports=RtpsW
[transport/RtpsW]
transport_type=rtps_udp
use_multicast=0
local_address=127.0.0.1:21075
[datawriterqos/ReliableWriter]
reliability.kind=RELIABLE
#reliability.max_blocking_time.sec=DURATION_INFINITE_SEC
durability.kind=TRANSIENT_LOCAL
history.kind=KEEP_ALL
history.depth=100
[datareaderqos/ReliableReader]
reliability.kind=RELIABLE
durability.kind=TRANSIENT_LOCAL
history.kind=KEEP_ALL
history.depth=100
[endpoint/Reader]
domain=42
participant=000000000001
entity=000001
type=reader
config=ConfigR
topic=TheTopic
datareaderqos=ReliableReader
[endpoint/Writer]
domain=42
participant=000000000002
entity=000002
type=writer
config=ConfigW
topic=TheTopic
datawriterqos=ReliableWriter
备注如下:
1)不要指定主题名字,opendds示例中指定了名字报错;
2)type_name按照自己的定义来填写;
3)QOS相关的是参考PDF文档填写的,但是除了 reliability.kind=RELIABLE 其他都没有起作用,都是自己代码控制的;
4)participant=000000000001 这个按照那个PDF文档上是12位的hex进制数据,GUID
5)entity=000001 按照文档是6位的hex进制数据
相关重点代码如下:
发布端创建参与者:
DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv);
//parse_args(argc, argv);
//
DDS::DomainParticipantQos dp_qos;
dpf->get_default_participant_qos(dp_qos);
dp_qos.user_data.value.length(6);
dp_qos.user_data.value[0] = 0x00;
dp_qos.user_data.value[1] = 0x00;
dp_qos.user_data.value[2] = 0x00;
dp_qos.user_data.value[3] = 0x00;
dp_qos.user_data.value[4] = 0x00;
dp_qos.user_data.value[5] = 0x02;
//
DDS::DomainParticipant_var participant =
dpf->create_participant(42, dp_qos,
//PARTICIPANT_QOS_DEFAULT,
DDS::DomainParticipantListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (participant.in ())) {
cerr << "create_participant failed." << endl;
exit(1);
}
创建写:
::DDS::ReturnCode_t ret;
DDS::DataWriterQos dw_qos;
ret = pub->get_default_datawriter_qos(dw_qos);
if (DDS::RETCODE_OK != ret) {
std::cerr << "Could not get default data writer QoS" << std::endl;
}
dw_qos.user_data.value.length(3);
dw_qos.user_data.value[0] = 0x00;
dw_qos.user_data.value[1] = 0x00;
dw_qos.user_data.value[2] = 0x02;
dw_qos.reliability.kind = DDS::ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
dw_qos.history.depth = 5;
dw_qos.history.kind = DDS::HistoryQosPolicyKind::KEEP_ALL_HISTORY_QOS;
dw_qos.durability.kind = DDS::DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
// Create the datawriter
DDS::DataWriter_var dw =
pub->create_datawriter(topic.in(), dw_qos,
//DATAWRITER_QOS_DEFAULT,
DDS::DataWriterListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil(dw.in())) {
cerr << "create_datawriter failed." << endl;
exit(1);
}
在订阅端创建参与者:
DDS::DomainParticipantFactory_var dpf = TheParticipantFactoryWithArgs(argc, argv);
//parse_args(argc, argv);
//
DDS::DomainParticipantQos dp_qos;
dpf->get_default_participant_qos(dp_qos);
dp_qos.user_data.value.length(6);
dp_qos.user_data.value[0] = 0x00;
dp_qos.user_data.value[1] = 0x00;
dp_qos.user_data.value[2] = 0x00;
dp_qos.user_data.value[3] = 0x00;
dp_qos.user_data.value[4] = 0x00;
dp_qos.user_data.value[5] = 0x01;
DDS::DomainParticipant_var participant =
dpf->create_participant(g_domainId, dp_qos,
DDS::DomainParticipantListener::_nil(),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (participant.in ())) {
cerr << "create_participant failed." << endl;
return 1 ;
}
创建读者:
//
::DDS::ReturnCode_t ret;
DDS::DataReaderQos reader_qos;
ret = sub->get_default_datareader_qos(reader_qos);
if (DDS::RETCODE_OK != ret) {
std::cerr << "Could not get default data reader QoS" << std::endl;
}
reader_qos.user_data.value.length(3);
reader_qos.user_data.value[0] = 0x00;
reader_qos.user_data.value[1] = 0x00;
reader_qos.user_data.value[2] = 0x01;
reader_qos.reliability.kind = DDS::ReliabilityQosPolicyKind::RELIABLE_RELIABILITY_QOS;
reader_qos.history.depth = 5;
reader_qos.history.kind = DDS::HistoryQosPolicyKind::KEEP_ALL_HISTORY_QOS;
reader_qos.durability.kind = DDS::DurabilityQosPolicyKind::TRANSIENT_LOCAL_DURABILITY_QOS;
// Create the Datareaders
DDS::DataReader_var dr = sub->create_datareader(topic.in (), reader_qos,
//DATAREADER_QOS_DEFAULT,
listener.in (),
::OpenDDS::DCPS::DEFAULT_STATUS_MASK);
if (CORBA::is_nil (dr.in ())) {
cerr << "create_datareader failed." << endl;
exit(1);
}
运行的命令:
./subscriber -DCPSConfigFile peer.ini
./publisher -DCPSConfigFile peer.ini