MessageSubscriberRegistry.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2015 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <QMetaType>
00020 
00021 #include "rqt_multiplot/MessageSubscriberRegistry.h"
00022 
00023 namespace rqt_multiplot {
00024 
00025 /*****************************************************************************/
00026 /* Constructors and Destructor                                               */
00027 /*****************************************************************************/
00028 
00029 MessageSubscriberRegistry::MessageSubscriberRegistry(QObject* parent, const
00030     ros::NodeHandle& nodeHandle) :
00031   MessageBroker(parent),
00032   nodeHandle_(nodeHandle) {
00033 }
00034 
00035 MessageSubscriberRegistry::~MessageSubscriberRegistry() {
00036 }
00037 
00038 /*****************************************************************************/
00039 /* Accessors                                                                 */
00040 /*****************************************************************************/
00041 
00042 const ros::NodeHandle& MessageSubscriberRegistry::getNodeHandle() const {
00043   return nodeHandle_;
00044 }
00045 
00046 /*****************************************************************************/
00047 /* Methods                                                                   */
00048 /*****************************************************************************/
00049 
00050 bool MessageSubscriberRegistry::subscribe(const QString& topic,
00051     QObject* receiver, const char* method, const PropertyMap& properties,
00052     Qt::ConnectionType type) {
00053   QMap<QString, MessageSubscriber*>::iterator it = subscribers_.find(topic);
00054   
00055   size_t queueSize = 100;
00056   if (properties.contains(MessageSubscriber::QueueSize))
00057     queueSize = properties[MessageSubscriber::QueueSize].toULongLong();
00058   
00059   if (it == subscribers_.end()) {
00060     it = subscribers_.insert(topic, new MessageSubscriber(this,
00061       getNodeHandle()));
00062     
00063     it.value()->setQueueSize(queueSize);    
00064     it.value()->setTopic(topic);    
00065     
00066     connect(it.value(), SIGNAL(aboutToBeDestroyed()), this,
00067       SLOT(subscriberAboutToBeDestroyed()));
00068   }
00069   else if (it.value()->getQueueSize() < queueSize)
00070     it.value()->setQueueSize(queueSize);
00071   
00072   return receiver->connect(it.value(), SIGNAL(messageReceived(const
00073     QString&, const Message&)), method, type);
00074 }
00075 
00076 bool MessageSubscriberRegistry::unsubscribe(const QString& topic, QObject*
00077     receiver, const char* method) {
00078   QMap<QString, MessageSubscriber*>::iterator it = subscribers_.find(topic);
00079     
00080   if (it != subscribers_.end()) {
00081     return it.value()->disconnect(SIGNAL(messageReceived(const QString&,
00082       const Message&)), receiver, method);
00083   }
00084   else
00085     return false;
00086 }
00087 
00088 /*****************************************************************************/
00089 /* Slots                                                                     */
00090 /*****************************************************************************/
00091 
00092 void MessageSubscriberRegistry::subscriberAboutToBeDestroyed() {
00093   for (QMap<QString, MessageSubscriber*>::iterator it = subscribers_.begin();
00094       it != subscribers_.end(); ++it) {
00095     if (it.value() == static_cast<MessageSubscriber*>(sender())) {
00096       subscribers_.erase(it);      
00097       break;
00098     }
00099   }
00100 }
00101   
00102 }


rqt_multiplot
Author(s): Ralf Kaestner
autogenerated on Thu Jun 6 2019 21:49:11