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 */
22 #include "GenericTopicSubscriber.h"
23 
25 
26 #include "TopicRecorder.h"
27 
28 namespace 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
IceInternal::Handle< TopicRecorderComponent >
GenericTopicSubscriber.h
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
TopicRecorder.h
armarx::GenericTopicSubscriber::ice_invoke
bool ice_invoke(const std::vector< Ice::Byte > &inParams, std::vector< Ice::Byte > &outParams, const Ice::Current &current) override
Definition: GenericTopicSubscriber.cpp:50
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
TimeUtil.h
armarx::GenericTopicSubscriber::getData
void getData(std::queue< TopicUtil::TopicData > &data)
Definition: GenericTopicSubscriber.cpp:43
armarx::GenericTopicSubscriber::GenericTopicSubscriber
GenericTopicSubscriber(TopicRecorderComponentPtr recorder, const std::string &topicName, IceUtil::Time startTimestamp, float maxFrequency=-1.0f)
Definition: GenericTopicSubscriber.cpp:31
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27