VSM C++ SDK
Vehicle Specific Modules SDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
text_stream_filter.h
Go to the documentation of this file.
1 // Copyright (c) 2018, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
8 #ifndef _UGCS_VSM_TEXT_STREAM_FILTER_H_
9 #define _UGCS_VSM_TEXT_STREAM_FILTER_H_
10 
11 #include <ugcs/vsm/vsm.h>
12 
13 #include <ugcs/vsm/regex.h>
14 
15 namespace ugcs {
16 namespace vsm {
17 
19 class Text_stream_filter: public std::enable_shared_from_this<Text_stream_filter> {
21 
22 public:
24  constexpr static size_t MAX_HISTORY_LINES = 10;
26  constexpr static size_t MAX_LINE_LENGTH = 512;
28  constexpr static size_t MAX_READ = 64;
30  typedef std::vector<std::string> Lines_list;
36  typedef Callback_proxy<bool, regex::smatch *, Lines_list *, Io_result> Match_handler;
41  typedef Callback_proxy<bool, const std::string *> Line_handler;
43  typedef uint32_t Entry_handle;
44 
46  DEFINE_CALLBACK_BUILDER(Make_match_handler,
47  (regex::smatch *, Text_stream_filter::Lines_list *, Io_result),
48  (nullptr, nullptr, Io_result::OK));
49 
51  DEFINE_CALLBACK_BUILDER(Make_line_handler, (const std::string *), (nullptr));
52 
54  Text_stream_filter(Io_stream::Ref stream,
56  size_t max_read = MAX_READ);
57 
59  void
60  Enable();
61 
63  void
64  Disable(bool close_stream = true);
65 
82  Entry_handle
83  Add_entry(const regex::regex &re, Match_handler handler,
84  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
85  size_t ctx_lines_before = 0, size_t ctx_lines_after = 0);
86 
88  void
89  Set_line_handler(Line_handler handler);
90 
91 private:
92  Io_stream::Ref stream;
94 
96  class Entry {
97  public:
98  Entry_handle handle;
100  regex::regex re;
102  Match_handler handler;
104  regex::smatch match;
106  Lines_list lines;
108  size_t ctx_lines_before, ctx_lines_after;
110  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero();
112  Timer_processor::Timer::Ptr timer = nullptr;
113 
114  Entry(Entry_handle handle, const regex::regex &re, Match_handler handler,
115  size_t ctx_lines_before, size_t ctx_lines_after):
116  handle(handle), re(re), handler(handler),
117  ctx_lines_before(ctx_lines_before), ctx_lines_after(ctx_lines_after)
118  {}
119 
120  ~Entry();
121  };
122 
124  std::string line_buf;
126  std::list<std::string> lines_history;
128  Entry_handle cur_handle = 1;
130  std::map<Entry_handle, Entry> entries;
132  bool cr_received = false;
134  Entry *cur_entry = nullptr;
136  size_t ctx_lines_after;
138  Line_handler line_handler;
140  Operation_waiter read_op_waiter;
142  bool read_is_scheduled = false;
144  size_t max_read;
145 
146  bool
147  Timeout_handler(Entry *e);
148 
150  void
151  On_data_received(Io_buffer::Ptr buf, Io_result result);
152 
154  void
155  On_char_received(int c);
156 
158  void
159  On_line_received(const std::string &line);
160 
162  void
163  Schedule_read();
164 
165  void
166  Reset_state(Io_result);
167 
168  void
169  Fire_entry(Entry &e, bool timed_out = false);
170 
172  Entry *
173  Match_entry(const std::string &line);
174 };
175 
176 } /* namespace vsm */
177 } /* namespace ugcs */
178 
179 #endif /* _UGCS_VSM_TEXT_STREAM_FILTER_H_ */
std::vector< std::string > Lines_list
List of accumulated lines.
Definition: text_stream_filter.h:30
static constexpr size_t MAX_LINE_LENGTH
Maximal number of characters in one line which is supported.
Definition: text_stream_filter.h:26
#define DEFINE_CALLBACK_BUILDER(__name, __types, __values)
Define callback builder function.
Definition: callback.h:42
static constexpr size_t MAX_READ
Maximal number of bytes to read at once.
Definition: text_stream_filter.h:28
static constexpr size_t MAX_HISTORY_LINES
Maximal number of lines saved before the matched line.
Definition: text_stream_filter.h:24
Io_result
Result of I/O operation.
Definition: io_stream.h:37
void Set_line_handler(Line_handler handler)
Set handler for pre-filtering all received lines.
Entry_handle Add_entry(const regex::regex &re, Match_handler handler, std::chrono::milliseconds timeout=std::chrono::milliseconds::zero(), size_t ctx_lines_before=0, size_t ctx_lines_after=0)
Add matching entry to the filter.
Class for convenient filtering of a text stream using regular expressions.
Definition: text_stream_filter.h:19
Abstract I/O stream interface.
Definition: io_stream.h:66
Operation completed successfully.
Request execution context.
Definition: request_context.h:24
void Enable()
Enable the filter.
Helper class for proxying callback invocation.
Definition: callback.h:694
std::shared_ptr< Text_stream_filter > Ptr
Pointer type.
Definition: text_stream_filter.h:20
uint32_t Entry_handle
Manipulation handle for an entry.
Definition: text_stream_filter.h:43
void Disable(bool close_stream=true)
Disable the filter.
std::shared_ptr< Io_buffer > Ptr
Pointer type.
Definition: io_buffer.h:34
#define DEFINE_COMMON_CLASS(__class_name,...)
Use this macro to define some common attributes for a class.
Definition: utils.h:25
std::shared_ptr< Timer > Ptr
Pointer type.
Definition: timer_processor.h:46
Callback_proxy< bool, const std::string * > Line_handler
Received line handler.
Definition: text_stream_filter.h:41
Class for synchronizing with request execution.
Definition: operation_waiter.h:24
Main include file for VSM SDK.