opensplice_dds_broker.cpp
Go to the documentation of this file.
00001 
00023 #include <iostream>
00024 #include "opensplice_dds_broker/opensplice_dds_broker.h"
00025 
00026 PLUGINLIB_EXPORT_CLASS(opensplice_dds_broker::OpenSpliceDDSBroker, micros_swarm::CommInterface)
00027 
00028 namespace opensplice_dds_broker{
00029 
00030     OpenSpliceDDSBroker::OpenSpliceDDSBroker()
00031     {
00032         packet_publisher_.reset(new opensplice_dds_broker::Publisher("micros_swarm_framework_topic"));
00033         packet_subscriber_.reset(new opensplice_dds_broker::Subscriber("micros_swarm_framework_topic"));
00034     }
00035 
00036     void OpenSpliceDDSBroker::init(std::string name, const micros_swarm::PacketParser& parser)
00037     {
00038         name_ = name;
00039         parser_ = parser;
00040     }
00041 
00042     void OpenSpliceDDSBroker::broadcast(const std::vector<uint8_t>& msg_data)
00043     {
00044         opensplice_dds_broker::GSDFPacket dds_msg;
00045         unsigned int bufsize = msg_data.size();
00046         if(!msg_data.empty()) {
00047             dds_msg.data.replace(bufsize, bufsize, (char*)(&msg_data[0]), false);
00048             packet_publisher_->publish(dds_msg);
00049         }
00050     }
00051 
00052     void OpenSpliceDDSBroker::callback(const opensplice_dds_broker::GSDFPacket& dds_msg)
00053     {
00054         int msgLen = dds_msg.data.length();
00055         if (msgLen == 0) {
00056             std::cout<<"opensplice dds recv error."<<std::endl;
00057         }
00058         else {
00059             uint8_t* buf = (uint8_t*)malloc(sizeof(uint8_t)*msgLen);
00060             memcpy(buf, dds_msg.data.get_buffer(), msgLen);
00061             parser_.parse(buf, msgLen);
00062         }
00063     }
00064             
00065     void OpenSpliceDDSBroker::receive()
00066     {
00067         boost::function<void(const opensplice_dds_broker::GSDFPacket&)> func = boost::bind(&OpenSpliceDDSBroker::callback, this, _1);
00068         packet_subscriber_->subscribe(func);
00069     }
00070 };


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