reader.h
Go to the documentation of this file.
1 #ifndef H_CAN_BUFFERED_READER
2 #define H_CAN_BUFFERED_READER
3 
5 #include <deque>
6 
7 #include <boost/thread/mutex.hpp>
8 #include <boost/thread/condition_variable.hpp>
9 #include <boost/chrono.hpp>
10 
11 namespace can{
12 
14  std::deque<can::Frame> buffer_;
15  boost::mutex mutex_;
16  boost::condition_variable cond_;
18  bool enabled_;
19  size_t max_len_;
20 
21  void trim(){
22  if(max_len_ > 0){
23  while(buffer_.size() > max_len_){
24  LOG("buffer overflow, discarded oldest message " /*<< tostring(buffer_.front())*/); // enable message printing
25  buffer_.pop_front();
26  }
27  }
28  }
29  void handleFrame(const can::Frame & msg){
30  boost::mutex::scoped_lock lock(mutex_);
31  if(enabled_){
32  buffer_.push_back(msg);
33  trim();
34  cond_.notify_one();
35  }else{
36  LOG("discarded message " /*<< tostring(msg)*/); // enable message printing
37  }
38  }
39 public:
42  bool before_;
43  public:
44  ScopedEnabler(BufferedReader &reader) : reader_(reader), before_(reader_.setEnabled(true)) {}
45  ~ScopedEnabler() { reader_.setEnabled(before_); }
46  };
47 
48  BufferedReader() : enabled_(true), max_len_(0) {}
49  BufferedReader(bool enable, size_t max_len = 0) : enabled_(enable), max_len_(max_len) {}
50 
51  void flush(){
52  boost::mutex::scoped_lock lock(mutex_);
53  buffer_.clear();
54  }
55  void setMaxLen(size_t max_len){
56  boost::mutex::scoped_lock lock(mutex_);
57  max_len_ = max_len;
58  trim();
59  }
60  bool isEnabled(){
61  boost::mutex::scoped_lock lock(mutex_);
62  return enabled_;
63  }
64  bool setEnabled(bool enabled){
65  boost::mutex::scoped_lock lock(mutex_);
66  bool before = enabled_;
67  enabled_ = enabled;
68  return before;
69  }
70  void enable(){
71  boost::mutex::scoped_lock lock(mutex_);
72  enabled_ = true;
73  }
74 
75  void disable(){
76  boost::mutex::scoped_lock lock(mutex_);
77  enabled_ = false;
78  }
79 
80  void listen(CommInterfaceSharedPtr interface){
81  boost::mutex::scoped_lock lock(mutex_);
82  listener_ = interface->createMsgListener(CommInterface::FrameDelegate(this, &BufferedReader::handleFrame));
83  buffer_.clear();
84  }
85  void listen(CommInterfaceSharedPtr interface, const Frame::Header& h){
86  boost::mutex::scoped_lock lock(mutex_);
87  listener_ = interface->createMsgListener(h, CommInterface::FrameDelegate(this, &BufferedReader::handleFrame));
88  buffer_.clear();
89  }
90 
91  template<typename DurationType> bool read(can::Frame * msg, const DurationType &duration){
92  return readUntil(msg, boost::chrono::high_resolution_clock::now() + duration);
93  }
94  bool readUntil(can::Frame * msg, boost::chrono::high_resolution_clock::time_point abs_time){
95  boost::mutex::scoped_lock lock(mutex_);
96 
97  while(buffer_.empty() && cond_.wait_until(lock,abs_time) != boost::cv_status::timeout)
98  {}
99 
100  if(buffer_.empty()){
101  return false;
102  }
103 
104  if(msg){
105  *msg = buffer_.front();
106  buffer_.pop_front();
107  }
108  return true;
109  }
110 
111 };
112 
113 } // namespace can
114 #endif
void listen(CommInterfaceSharedPtr interface, const Frame::Header &h)
Definition: reader.h:85
Definition: asio_base.h:11
void listen(CommInterfaceSharedPtr interface)
Definition: reader.h:80
bool readUntil(can::Frame *msg, boost::chrono::high_resolution_clock::time_point abs_time)
Definition: reader.h:94
ScopedEnabler(BufferedReader &reader)
Definition: reader.h:44
bool isEnabled()
Definition: reader.h:60
void setMaxLen(size_t max_len)
Definition: reader.h:55
boost::shared_ptr< CommInterface > CommInterfaceSharedPtr
Definition: interface.h:159
void handleFrame(const can::Frame &msg)
Definition: reader.h:29
CommInterface::FrameListenerConstSharedPtr listener_
Definition: reader.h:17
#define LOG(log)
Definition: interface.h:216
bool read(can::Frame *msg, const DurationType &duration)
Definition: reader.h:91
bool setEnabled(bool enabled)
Definition: reader.h:64
BufferedReader(bool enable, size_t max_len=0)
Definition: reader.h:49
size_t max_len_
Definition: reader.h:19
void disable()
Definition: reader.h:75
BufferedReader & reader_
Definition: reader.h:41
std::deque< can::Frame > buffer_
Definition: reader.h:14
FrameListener::ListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:130
boost::condition_variable cond_
Definition: reader.h:16
boost::mutex mutex_
Definition: reader.h:15


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Sat May 4 2019 02:40:41