MIRA
ChannelSubscriber.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef Q_MOC_RUN
49 #include <boost/shared_ptr.hpp>
50 #endif
51 
52 #include <utils/Bind.h>
53 
54 #include <fw/AbstractChannel.h>
56 
57 #include <fw/Status.h>
58 
59 
60 #ifndef _MIRA_CHANNELSUBSCRIBER_H_
61 #define _MIRA_CHANNELSUBSCRIBER_H_
62 
63 namespace mira {
64 
66 
67 // forward declaration
68 template<typename T>
69 class ConcreteChannel;
70 
72 
81 template<typename T, typename ChannelRead>
83 {
84 public:
85 
87 
88 public:
89 
91  boost::function<void (ChannelRead)> invokeFn,
92  DiagnosticsModulePtr diagnosticsModule = NULL) :
93  AbstractChannelSubscriber(dispatcher,diagnosticsModule),
94  mChannel(NULL),
95  mInvokeFn(invokeFn),
96  mLastTimestamp(Time::invalid()) {}
97 
99  AbstractChannelPtr getChannel() { return mChannel; }
100 
105  void attachChannel(ChannelType* channel) {
106  assert(mChannel==NULL);
107  mChannel=channel;
108  }
109 
113  virtual void detachChannel() {
114  mChannel=NULL;
115  }
116 
117 
118  // implements IRunnable
119  virtual void run(DispatcherThread* dispatcher)
120  {
121  if(!mEnabled)
122  return;
123 
124  // prevents this Runnable instance from being destroyed while we are in here
125  IRunnablePtr This = this->shared_from_this();
126 
127  assert(mChannel!=NULL);
128  std::string channelid = mChannel->getID();
129 
130  try {
131 
132  // read the latest data from the Channel and call the invoke
133  // function while securing that it was not already invoked with
134  // the same data (timestamp)
135  auto r = mChannel->read();
136  Time n = r.getTimestamp();
137  if (mLastTimestamp != n)
138  {
139  mLastTimestamp = n;
140  mInvokeFn(r);
141  }
142 
144  mDiagnosticsModule->ok(channelid);
145  }
146  catch(Exception& ex) {
147  if (!mDiagnosticsModule || mDiagnosticsModule->error(channelid,
148  ex.message(), "Exception while invoking subscriber")) {
149  MIRA_LOG_EXCEPTION(ERROR,ex) << "Exception:\n";
150  }
151  }
152  catch(std::exception& ex) {
153  if (!mDiagnosticsModule || mDiagnosticsModule->error(channelid,
154  ex.what(), "Exception while invoking subscriber")) {
155  MIRA_LOG_EXCEPTION(ERROR,ex) << "Exception:\n";
156  }
157  }
158  }
159 
160 
161 private:
162 
163  ChannelType* mChannel;
164  boost::function<void (ChannelRead)> mInvokeFn;
165  Time mLastTimestamp;
166 };
167 
169 
170 }
171 
172 #endif
ChannelSubscriber(DispatcherThread *dispatcher, boost::function< void(ChannelRead)> invokeFn, DiagnosticsModulePtr diagnosticsModule=NULL)
Definition: ChannelSubscriber.h:90
AbstractChannelPtr getChannel()
Returns the channel this subscriber is attached to.
Definition: ChannelSubscriber.h:99
Base class for all channel subscribers.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void attachChannel(ChannelType *channel)
Attaches this subscriber to the specified channel.
Definition: ChannelSubscriber.h:105
Definition: ChannelReadWrite.h:67
An object that allows read access to data of a channel.
Definition: ChannelReadWrite.h:494
ConcreteChannel< T > ChannelType
Definition: ChannelSubscriber.h:86
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
virtual void run(DispatcherThread *dispatcher)
Called from dispatcher thread this runnable is attached to, whenever the runnable should be executed...
Definition: ChannelSubscriber.h:119
Definition: AbstractChannel.h:70
std::string message() const MIRA_NOEXCEPT_OR_NOTHROW
Similar to what().
bool mEnabled
Is the subscriber enabled.
Definition: AbstractChannelSubscriber.h:117
DiagnosticsModulePtr mDiagnosticsModule
Definition: Runnable.h:105
boost::shared_ptr< IRunnable > IRunnablePtr
a runnable pointer
Definition: Runnable.h:63
Base class for exceptions.
Definition: Exception.h:199
Class that can be used whenever you want to have ONE thread where several handlers are assigned to...
Definition: DispatcherThread.h:131
Wrapper for boost/bind.
#define MIRA_LOG_EXCEPTION(level, ex)
Log the specified exception, including all information that the exception object carries.
Definition: LoggingAux.h:107
virtual void detachChannel()
Detaches this subscriber from the channel.
Definition: ChannelSubscriber.h:113
bool error(const std::string &category, const std::string &message, const std::string &trText="")
Signal an error in a category.
Definition: AbstractChannelSubscriber.h:70
Definition: LoggingCore.h:75
Base class for all framework channels.
Implements AbstractChannelSubscriber for a concrete data type.
Definition: ChannelSubscriber.h:82
Base class for modules that want to use diagnostics and set the current status.
Definition: Status.h:138
Status and status management classes used for diagnostics.
void ok(const std::string &category="")
Signal that a category contains no more errors.