template_queue.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 #ifndef TEMPLATE_QUEUE_H
3 #define TEMPLATE_QUEUE_H
4 
5 #include <queue>
6 #include <thread>
7 #include <thread>
8 #include <iostream>
9 #include <mutex>
10 #include <condition_variable>
11 
12 template<typename T>
13 class Queue
14 {
15 public:
16 
23  {
24  int retVal = 0;
25  std::unique_lock<std::mutex> mlock(mutex_);
26  retVal = queue_.size();
27  return (retVal);
28  }
29 
30  bool isQueueEmpty()
31  {
32  bool retVal = false;
33  std::unique_lock<std::mutex> mlock(mutex_);
34  retVal = queue_.empty();
35  return (retVal);
36  }
37 
38  bool waitForIncomingObject(int timeOutInMs, const std::vector<std::string>& datagram_keywords)
39  {
40  std::unique_lock<std::mutex> mlock(mutex_);
41  bool ret = true;
42  typename std::list<T>::iterator datagram_found;
43  while (findFirstByKeyword(datagram_keywords, datagram_found) == false && (ret == true))
44  {
45  ret = (cond_.wait_for(mlock, std::chrono::milliseconds(timeOutInMs)) == std::cv_status::no_timeout);
46  }
47  return (ret);
48  }
49 
50  T pop(const std::vector<std::string>& datagram_keywords) // T pop(const std::vector<std::string>& datagram_keywords = {})
51  {
52  std::unique_lock<std::mutex> mlock(mutex_);
53  typename std::list<T>::iterator datagram_found;
54  while (findFirstByKeyword(datagram_keywords, datagram_found) == false)
55  {
56  cond_.wait(mlock);
57  }
58  T item = *datagram_found;
59  queue_.erase(datagram_found);
60  return item;
61  }
62 
63  void push(const T &item)
64  {
65  {
66  std::unique_lock<std::mutex> mlock(mutex_);
67  queue_.push_back(item);
68  }
69  cond_.notify_all(); // cond_.notify_one();
70  }
71 
72 
73 protected:
74 
75  bool findFirstByKeyword(const std::vector<std::string>& keywords, typename std::list<T>::iterator & iter)
76  {
77  iter = queue_.begin();
78  if(keywords.empty())
79  {
80  return !queue_.empty();
81  }
82  for( ; iter != queue_.end(); iter++)
83  {
84  std::vector<unsigned char>& datagram = iter->data();
85  uint32_t cola_b_start = 0x02020202;
86  uint8_t* datagram_keyword_start = 0;
87  int datagram_keyword_maxlen = (int)datagram.size();
88  int commandIdOffset = 1;
89  if(datagram.size() > 12 && memcmp(datagram.data(),&cola_b_start, sizeof(cola_b_start)) == 0)
90  {
91  commandIdOffset = 8; // command id behind 0x02020202 + { 4 byte payload length }
92  datagram_keyword_start = datagram.data() + 12; // 0x02020202 + { 4 byte payload length } + { 4 byte command id incl. space }
93  datagram_keyword_maxlen -= 12;
94  }
95  else if(datagram.size() > 5)
96  {
97  commandIdOffset = 1;
98  datagram_keyword_start = datagram.data() + 5; // 0x02 + { 4 byte command id incl. space }
99  datagram_keyword_maxlen -= 5;
100  }
101  else
102  continue;
103 
104  for(int keyword_idx = 0; keyword_idx < keywords.size(); keyword_idx++)
105  {
106  const std::string& keyword = keywords[keyword_idx];
107  if(keyword.size() <= datagram_keyword_maxlen && memcmp(datagram_keyword_start, keyword.data(), keyword.size()) == 0)
108  {
109  // ROS_DEBUG_STREAM("Queue::findFirstByKeyword(): keyword_start=\"" << std::string((char*)datagram_keyword_start, keyword.size()) << "\", keyword=\"" << keyword << "\"");
110  return true;
111  }
112  }
113 
114  // keyword not found.
115  // Check for possible sFA command as an error reply
116  std::string errorIdentifier = "sFA";
117  if (datagram.size() >= (commandIdOffset + errorIdentifier.length()))
118  {
119  const char* errorIdentifierPtr = errorIdentifier.c_str();
120  const char* cmpPtr = (const char *)(datagram.data() + commandIdOffset);
121  if (memcmp(cmpPtr, errorIdentifierPtr, errorIdentifier.length() )== 0)
122  {
123  ROS_DEBUG_STREAM("Queue::findFirstByKeyword(): error identifier sFA found in datagram");
124  return true;
125  }
126  }
127  }
128  return false; // keyword not found, iter == queue_.end()
129  }
130 
131  std::list<T> queue_;
132  std::mutex mutex_;
133  std::condition_variable cond_;
134 };
135 
136 #endif
Queue::findFirstByKeyword
bool findFirstByKeyword(const std::vector< std::string > &keywords, typename std::list< T >::iterator &iter)
Definition: template_queue.h:75
Queue::getNumberOfEntriesInQueue
int getNumberOfEntriesInQueue()
get number of entries in queue
Definition: template_queue.h:22
Queue::cond_
std::condition_variable cond_
Definition: template_queue.h:133
Queue::pop
T pop(const std::vector< std::string > &datagram_keywords)
Definition: template_queue.h:50
Queue::mutex_
std::mutex mutex_
Definition: template_queue.h:132
Queue::waitForIncomingObject
bool waitForIncomingObject(int timeOutInMs, const std::vector< std::string > &datagram_keywords)
Definition: template_queue.h:38
ROS_DEBUG_STREAM
#define ROS_DEBUG_STREAM(args)
Definition: sick_scan_logging.h:113
Queue
Definition: template_queue.h:13
sick_scan_base.h
sick_scan_timing_dump_analyser.keyword
keyword
Definition: sick_scan_timing_dump_analyser.py:18
Queue::isQueueEmpty
bool isQueueEmpty()
Definition: template_queue.h:30
Queue::queue_
std::list< T > queue_
Definition: template_queue.h:131
Queue::push
void push(const T &item)
Definition: template_queue.h:63


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12