GenericTopicSubscriber.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX
17* @author Mirko Waechter( mirko.waechter at kit dot edu)
18* @date 2016
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
23
25
26#include "TopicRecorder.h"
27
28namespace armarx
29{
30
32 const std::string& topicName,
33 IceUtil::Time startTimestamp,
34 float maxFrequency) :
35 topicName(topicName),
36 recorder(recorder),
37 startTimestamp(startTimestamp),
38 maxFrequency(maxFrequency)
39 {
40 }
41
42 void
43 GenericTopicSubscriber::getData(std::queue<TopicUtil::TopicData>& data)
44 {
45 std::unique_lock lock(queueMutex);
46 data.swap(dataQueue);
47 }
48
49 bool
50 GenericTopicSubscriber::ice_invoke(const std::vector<Ice::Byte>& inParams,
51 std::vector<Ice::Byte>& outParams,
52 const Ice::Current& current)
53 {
54 std::unique_lock lock(queueMutex);
55 auto now = TimeUtil::GetTime();
56
57 if (!checkTimestamp(current.operation, now))
58 {
59 return true;
60 }
61
62
63 dataQueue.emplace(topicName, now - startTimestamp, current.operation, inParams);
64 if (recorder)
65 {
66 recorder->wakeUp();
67 }
68 return true;
69 }
70
71 bool
72 GenericTopicSubscriber::checkTimestamp(const std::string& operationName,
73 const IceUtil::Time& timestamp)
74 {
75 if (maxFrequency > 0)
76 {
77 auto it = functionCallTimestamps.find(operationName);
78 if (it != functionCallTimestamps.end())
79 {
80 if ((timestamp - it->second).toSecondsDouble() < 1.0 / maxFrequency)
81 {
82 return false;
83 }
84 else
85 {
86 it->second = timestamp;
87 }
88 }
89 else
90 {
91 functionCallTimestamps[operationName] = timestamp;
92 }
93 }
94 return true;
95 }
96
97} // namespace armarx
std::string timestamp()
void getData(std::queue< TopicUtil::TopicData > &data)
bool ice_invoke(const std::vector< Ice::Byte > &inParams, std::vector< Ice::Byte > &outParams, const Ice::Current &current) override
GenericTopicSubscriber(TopicRecorderComponentPtr recorder, const std::string &topicName, IceUtil::Time startTimestamp, float maxFrequency=-1.0f)
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< TopicRecorderComponent > TopicRecorderComponentPtr