publisher.cpp
Go to the documentation of this file.
00001 
00023 #include <string>
00024 #include <iostream>
00025 #include <sstream>
00026 #include <vector>
00027 #include "ccpp_dds_dcps.h"
00028 #include "opensplice_dds_broker/check_status.h"
00029 #include "opensplice_dds_broker/ccpp_GSDFPacket.h"
00030 #include "opensplice_dds_broker/example_main.h"
00031 #include "opensplice_dds_broker/publisher.h"
00032 
00033 using namespace DDS;
00034 
00035 namespace opensplice_dds_broker{
00036     
00037     Publisher::Publisher(const std::string& topic_name)
00038     {
00039         domain = 0;
00040         topic_name_ = topic_name.data();
00041         GSDFPacketTypeName = NULL;
00042         
00043         //Create a DomainParticipantFactory and a DomainParticipant, using Default QoS settings
00044         dpf = DomainParticipantFactory::get_instance ();
00045         checkHandle(dpf.in(), "DDS::DomainParticipantFactory::get_instance");
00046         participant = dpf->create_participant(domain, PARTICIPANT_QOS_DEFAULT, NULL, STATUS_MASK_NONE);
00047         checkHandle(participant.in(), "DDS::DomainParticipantFactory::create_participant");
00048 
00049         //Register the required datatype
00050         GSDFPacketTS = new GSDFPacketTypeSupport();
00051         checkHandle(GSDFPacketTS.in(), "new GSDFPacketTypeSupport");
00052         GSDFPacketTypeName = GSDFPacketTS->get_type_name();
00053         status = GSDFPacketTS->register_type(
00054             participant.in(),
00055             GSDFPacketTypeName);
00056         checkStatus(status, "micros_swarm_framework::GSDFPacketTypeSupport::register_type");
00057 
00058         //Set the ReliabilityQosPolicy to BEST_EFFORT_RELIABILITY
00059         status = participant->get_default_topic_qos(topic_qos);
00060         checkStatus(status, "DDS::DomainParticipant::get_default_topic_qos");
00061         topic_qos.reliability.kind = BEST_EFFORT_RELIABILITY_QOS;
00062         //topic_qos.reliability.kind = RELIABLE_RELIABILITY_QOS;
00063         topic_qos.durability_service.history_kind = KEEP_LAST_HISTORY_QOS;
00064         //topic_qos.durability_service.history_depth = 4000;
00065 
00066         //Make the tailored QoS the new default
00067         status = participant->set_default_topic_qos(topic_qos);
00068         checkStatus(status, "DDS::DomainParticipant::set_default_topic_qos");
00069 
00070         //Use the changed policy when defining the GSDFPacket topic
00071         GSDFPacketTopic = participant->create_topic(
00072             topic_name_,
00073             GSDFPacketTypeName,
00074             topic_qos,
00075             NULL,
00076             STATUS_MASK_NONE);
00077         checkHandle(GSDFPacketTopic.in(), "DDS::DomainParticipant::create_topic (GSDFPacket)");
00078 
00079         //Adapt the default PublisherQos to write into the "micros_swarm_framework_default_partion" Partition
00080         status = participant->get_default_publisher_qos (pub_qos);
00081         checkStatus(status, "DDS::DomainParticipant::get_default_publisher_qos");
00082         pub_qos.partition.name.length(1);
00083         pub_qos.partition.name[0] = "micros_swarm_framework_partion";
00084 
00085         //Create a Publisher for the application
00086         publisher_ = participant->create_publisher(pub_qos, NULL, STATUS_MASK_NONE);
00087         checkHandle(publisher_.in(), "DDS::DomainParticipant::create_publisher");
00088         
00089         status = publisher_->get_default_datawriter_qos(dw_qos);
00090         dw_qos.destination_order.kind = BY_SOURCE_TIMESTAMP_DESTINATIONORDER_QOS;
00091         dw_qos.history.kind = KEEP_ALL_HISTORY_QOS;
00092         //dw_qos.history.kind = KEEP_LAST_HISTORY_QOS;
00093         //dw_qos.durability.kind = TRANSIENT_LOCAL_DURABILITY_QOS;
00094         dw_qos.durability.kind = VOLATILE_DURABILITY_QOS;
00095 
00096         //Create a DataWriter for the GSDFPacket Topic (using the appropriate QoS)
00097         parentWriter = publisher_->create_datawriter(
00098             GSDFPacketTopic.in(),
00099             dw_qos,
00100             NULL,
00101             STATUS_MASK_NONE);
00102         checkHandle(parentWriter, "DDS::Publisher::create_datawriter (GSDFPacket)");
00103 
00104         //Narrow the abstract parent into its typed representative
00105         GSDFPacketDW = GSDFPacketDataWriter::_narrow(parentWriter);
00106         checkHandle(GSDFPacketDW.in(), "micros_swarm_framework::GSDFPacketDataWriter::_narrow");
00107     }
00108     
00109     void Publisher::publish(const GSDFPacket& packet)
00110     {
00111         status = GSDFPacketDW->write(packet, DDS::HANDLE_NIL);
00112         checkStatus(status, "micros_swarm_framework::GSDFPacketDataWriter::write");
00113     }
00114     
00115     Publisher::~Publisher()
00116     {
00117         //Remove the DataWriters 
00118         status = publisher_->delete_datawriter(GSDFPacketDW.in() );
00119         checkStatus(status, "DDS::Publisher::delete_datawriter (GSDFPacketDW)");
00120 
00121         //Remove the Publisher
00122         status = participant->delete_publisher(publisher_.in() );
00123         checkStatus(status, "DDS::DomainParticipant::delete_publisher");
00124 
00125         status = participant->delete_topic( GSDFPacketTopic.in() );
00126         checkStatus(status, "DDS::DomainParticipant::delete_topic (GSDFPacketTopic)");
00127 
00128         //Remove the type-names
00129         string_free(GSDFPacketTypeName);
00130 
00131         //Remove the DomainParticipant
00132         status = dpf->delete_participant( participant.in() );
00133         checkStatus(status, "DDS::DomainParticipantFactory::delete_participant");
00134 
00135         //cout << "Completed publisher" << endl;
00136     }
00137 };


opensplice_dds_broker
Author(s):
autogenerated on Thu Jun 6 2019 18:52:31