00001
#ifndef _DDS_PUBLISHER_H
00002
#define _DDS_PUBLISHER_H
00003
00004
#include <DDSHelper.h>
00005
#include <dds/DCPS/PublisherImpl.h>
00006
00007
namespace ddsnc{
00008
00020
template<
class DWVAR>
00021 class DDSPublisher :
public ::ddsnc::
DDSHelper{
00022
private:
00023 DDS::Publisher_var
pub;
00024 OpenDDS::DCPS::PublisherImpl *
pub_impl;
00025 DDS::DataWriter_var
dw;
00026 DWVAR
dataWriter;
00027 DDS::InstanceHandle_t
handler;
00028
00029 int attachToTransport()
00030 {
00031 OpenDDS::DCPS::AttachStatus status =
00032
pub_impl->attach_transport(transport_impl.in());
00033
if (status != OpenDDS::DCPS::ATTACH_OK) {
00034 std::string status_str;
00035
switch (status) {
00036
case OpenDDS::DCPS::ATTACH_BAD_TRANSPORT:
00037 status_str =
"ATTACH_BAD_TRANSPORT";
00038
break;
00039
case OpenDDS::DCPS::ATTACH_ERROR:
00040 status_str =
"ATTACH_ERROR";
00041
break;
00042
case OpenDDS::DCPS::ATTACH_INCOMPATIBLE_QOS:
00043 status_str =
"ATTACH_INCOMPATIBLE_QOS";
00044
break;
00045
default:
00046 status_str =
"Unknown Status";
00047
break;
00048 }
00049 ::std::cerr <<
"Failed to attach to the transport. Status == "
00050 << status_str.c_str() << ::std::endl;
00051
return 1;
00052 }
00053
return 0;
00054
00055 }
00056
00065 void initialize()
00066 {
00067 std::cerr<<
"DDSPublisher::initialize()"<<std::endl;
00068
createParticipant();
00069
if (CORBA::is_nil (participant.in()))
00070 std::cerr <<
"Participant is nil" << std::endl;
00071
00072
if(partitionName!=
NULL){
00073 participant->get_default_publisher_qos(
pubQos);
00074
pubQos.partition.name.length(1);
00075
pubQos.partition.name[0]=CORBA::string_dup(partitionName);
00076 }
00077
initializeTransport();
00078
createPublisher();
00079
00080
pub->get_default_datawriter_qos (
dwQos);
00081
dwQos.reliability.kind = ::DDS::RELIABLE_RELIABILITY_QOS;
00082
dwQos.reliability.max_blocking_time.sec = 1;
00083
dwQos.history.kind = ::DDS::KEEP_LAST_HISTORY_QOS;
00084
dwQos.history.depth = 100;
00085
00086
dwQos.durability.kind = ::DDS::TRANSIENT_LOCAL_DURABILITY_QOS;
00087
dwQos.durability.service_cleanup_delay.sec = 10;
00088 }
00089
00096 template <
class DW>
void initializeDataWriter()
00097 {
00098
dw =
pub->create_datawriter(topic.in(),
00099
dwQos, DDS::DataWriterListener::_nil());
00100
if(CORBA::is_nil(
dw.in())){
00101 std::cerr <<
"create datawriter failed" << std::endl;
00102 }
00103
dataWriter = DW::_narrow(
dw.in());
00104 }
00105
00111 int createPublisher()
00112 {
00113 std::cerr <<
"DDSPublisher::createPublisher" << std::endl;
00114
00115
if(partitionName==
NULL){
00116
pub = participant->create_publisher(PUBLISHER_QOS_DEFAULT,
00117 DDS::PublisherListener::_nil());
00118 std::cerr <<
"Creating Publisher with default Qos" << std::endl;
00119 }
00120
00121
else{
00122
pub = participant->create_publisher(
pubQos,
00123 DDS::PublisherListener::_nil());
00124 std::cerr <<
"Creating Publisher with partition " << partitionName
00125 << std::endl;
00126 }
00127
00128
if(CORBA::is_nil(
pub.in())){
00129 std::cerr <<
"create publisher failed" << std::endl;
00130
return 1;
00131 }
00132
00133
pub_impl= dynamic_cast<OpenDDS::DCPS::PublisherImpl*>(
pub.in());
00134
if(
pub_impl ==
NULL){
00135 std::cerr <<
"Failed to obtain publisher servant" << std::endl;
00136
return 1;
00137 }
00138
return attachToTransport();
00139 }
00140
00141
protected:
00142 DDS::PublisherQos
pubQos;
00143
00144
00145
public:
00146 DDS::DataWriterQos
dwQos;
00163 DDSPublisher(
const char *channelName):
00164 ::ddsnc::
DDSHelper(
channelName)
00165 {
00166
initialize();
00167 }
00168
00169
00185
template <
class D,
class DW,
class TSV,
class TSI>
00186 void publishData(D data)
00187 {
00188
if(initialized==
false){
00189
00190 TSV ts;
00191 ts =
new TSI();
00192
if(DDS::RETCODE_OK != ts->register_type(participant.in(),
""))
00193 std::cerr <<
"register_type failed" << std::endl;
00194
00195
initializeTopic(ts->get_type_name());
00196
if(CORBA::is_nil(topic.in()))
00197 std::cerr<<
"Topic is nil" << std::endl;
00198 initializeDataWriter<DW>();
00199
handler =
dataWriter->_cxx_register(data);
00200 initialized=
true;
00201 }
00202
dataWriter->write(data,
handler);
00203 }
00204
00205 ~DDSPublisher()
00206 {
00207 }
00208
00209
00210 };
00211 }
00212
00213 #define ACS_NEW_DDS_PUBLISHER(publisher_p, idlStruct, channelName) \
00214
ddsnc::DDSPublisher<idlStruct##DataWriter_var> *publisher_p = 0; \
00215
publisher_p = new ddsnc::DDSPublisher<idlStruct##DataWriter_var>(channelName);
00216
00217 #define PUBLISH_DATA(publisher_p, idlStruct, message) \
00218
{ \
00219
publisher_p->publishData<idlStruct, idlStruct##DataWriter, \
00220
idlStruct##TypeSupport_var, idlStruct##TypeSupportImpl> (message); \
00221
}
00222
#endif