Go to the documentation of this file.00001 #include <QApplication>
00002 #include "utilities/message_event.h"
00003 #include "utilities/message_subscriber.h"
00004 #include <variant_topic_tools/MessageType.h>
00005
00006 namespace utilities
00007 {
00008 MessageSubscriber::MessageSubscriber(QObject* parent, const ros::NodeHandle& nh)
00009 : QObject(parent), nh_(nh), queue_size_(100)
00010 {
00011 }
00012
00013 MessageSubscriber::~MessageSubscriber() {}
00014
00015 const ros::NodeHandle& MessageSubscriber::getNodeHandle() const { return nh_; }
00016
00017 const QString& MessageSubscriber::getTopic() const { return topic_; }
00018
00019 void MessageSubscriber::setTopic(const QString& topic)
00020 {
00021 if (topic != topic_)
00022 {
00023 topic_ = topic;
00024 if (subscriber_)
00025 {
00026 unsubscribe();
00027 subscribe();
00028 }
00029 }
00030 }
00031
00032 void MessageSubscriber::setQueueSize(size_t queue_size)
00033 {
00034 if (queue_size != queue_size_)
00035 {
00036 queue_size_ = queue_size;
00037 if (subscriber_)
00038 {
00039 unsubscribe();
00040 subscribe();
00041 }
00042 }
00043 }
00044
00045 size_t MessageSubscriber::getQueueSize() const { return queue_size_; }
00046
00047 size_t MessageSubscriber::getNumPublishers() const
00048 {
00049 return subscriber_.getNumPublishers();
00050 }
00051
00052 bool MessageSubscriber::isValid() const { return subscriber_; }
00053
00054 bool MessageSubscriber::event(QEvent* event)
00055 {
00056 if (event->type() == MessageEvent::Type)
00057 {
00058 MessageEvent* message_event = static_cast<MessageEvent*>(event);
00059 emit messageReceived(message_event->getTopic(), message_event->getMessage());
00060 return true;
00061 }
00062 return QObject::event(event);
00063 }
00064
00065 void MessageSubscriber::subscribe()
00066 {
00067 variant_topic_tools::MessageType type;
00068
00069 subscriber_ =
00070 type.subscribe(nh_, topic_.toStdString(), queue_size_,
00071 boost::bind(&MessageSubscriber::callback, this, _1, _2));
00072 if (subscriber_)
00073 {
00074 emit subscribed(topic_);
00075 }
00076 }
00077
00078 void MessageSubscriber::unsubscribe()
00079 {
00080 if (subscriber_)
00081 {
00082 subscriber_.shutdown();
00083 QApplication::removePostedEvents(this, MessageEvent::Type);
00084 emit unsubscribed(topic_);
00085 }
00086 }
00087
00088 void MessageSubscriber::callback(
00089 const variant_topic_tools::MessageVariant& variant,
00090 const ros::Time& receiptTime)
00091 {
00092 Message message;
00093 message.setReceiptTime(receiptTime);
00094 message.setVariant(variant);
00095 MessageEvent* messageEvent = new MessageEvent(topic_, message);
00096 QApplication::postEvent(this, messageEvent);
00097 }
00098
00099 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
00100 void MessageSubscriber::connectNotify(const QMetaMethod& signal)
00101 {
00102 if (signal == QMetaMethod::fromSignal(&MessageSubscriber::messageReceived) &&
00103 !subscriber_)
00104 {
00105 subscribe();
00106 }
00107 }
00108
00109 void MessageSubscriber::disconnectNotify(const QMetaMethod& signal)
00110 {
00111 if (!receivers(QMetaObject::normalizedSignature(
00112 SIGNAL(messageReceived(const QString&, const Message&)))))
00113 {
00114 if (subscriber_)
00115 {
00116 unsubscribe();
00117 }
00118 emit aboutToBeDestroyed();
00119 deleteLater();
00120 }
00121 }
00122 #else
00123 void MessageSubscriber::connectNotify(const char* signal)
00124 {
00125 if ((QByteArray(signal) ==
00126 QMetaObject::normalizedSignature(
00127 SIGNAL(messageReceived(const QString&, const Message&)))) &&
00128 !subscriber_)
00129 {
00130 subscribe();
00131 }
00132 }
00133
00134 void MessageSubscriber::disconnectNotify(const char* signal)
00135 {
00136 if (!receivers(QMetaObject::normalizedSignature(
00137 SIGNAL(messageReceived(const QString&, const Message&)))))
00138 {
00139 if (subscriber_)
00140 {
00141 unsubscribe();
00142 }
00143 emit aboutToBeDestroyed();
00144 deleteLater();
00145 }
00146 }
00147 #endif
00148 }