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 #include "TopicRecorder.h"
24 
26 
27 namespace armarx
28 {
29 
30  GenericTopicSubscriber::GenericTopicSubscriber(TopicRecorderComponentPtr recorder, const std::string& topicName, IceUtil::Time startTimestamp, float maxFrequency) :
31  topicName(topicName),
32  recorder(recorder),
33  startTimestamp(startTimestamp),
34  maxFrequency(maxFrequency)
35  {
36 
37  }
38 
39  void GenericTopicSubscriber::getData(std::queue<TopicUtil::TopicData>& data)
40  {
41  std::unique_lock lock(queueMutex);
42  data.swap(dataQueue);
43  }
44 
45  bool GenericTopicSubscriber::ice_invoke(const std::vector<Ice::Byte>& inParams, std::vector<Ice::Byte>& outParams, const Ice::Current& current)
46  {
47  std::unique_lock lock(queueMutex);
48  auto now = TimeUtil::GetTime();
49 
50  if (!checkTimestamp(current.operation, now))
51  {
52  return true;
53  }
54 
55 
56  dataQueue.emplace(topicName, now - startTimestamp, current.operation, inParams);
57  if (recorder)
58  {
59  recorder->wakeUp();
60  }
61  return true;
62  }
63 
64  bool GenericTopicSubscriber::checkTimestamp(const std::string& operationName, const IceUtil::Time& timestamp)
65  {
66  if (maxFrequency > 0)
67  {
68  auto it = functionCallTimestamps.find(operationName);
69  if (it != functionCallTimestamps.end())
70  {
71  if ((timestamp - it->second).toSecondsDouble() < 1.0 / maxFrequency)
72  {
73  return false;
74  }
75  else
76  {
77  it->second = timestamp;
78  }
79  }
80  else
81  {
82  functionCallTimestamps[operationName] = timestamp;
83  }
84 
85  }
86  return true;
87  }
88 
89 } // namespace armarx
90 
91 
92 
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:45
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:39
armarx::GenericTopicSubscriber::GenericTopicSubscriber
GenericTopicSubscriber(TopicRecorderComponentPtr recorder, const std::string &topicName, IceUtil::Time startTimestamp, float maxFrequency=-1.0f)
Definition: GenericTopicSubscriber.cpp:30
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28