VSM C++ SDK
Vehicle Specific Modules SDK
text_stream_filter.h
Go to the documentation of this file.
1 // Copyright (c) 2014, Smart Projects Holdings Ltd
2 // All rights reserved.
3 // See LICENSE file for license details.
4 
8 #ifndef TEXT_STREAM_FILTER_H_
9 #define 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 public:
23  constexpr static size_t MAX_HISTORY_LINES = 10;
25  constexpr static size_t MAX_LINE_LENGTH = 512;
27  constexpr static size_t MAX_READ = 64;
29  typedef std::vector<std::string> Lines_list;
35  typedef Callback_proxy<bool, regex::smatch *, Lines_list *, Io_result> Match_handler;
40  typedef Callback_proxy<bool, const std::string *> Line_handler;
42  typedef unsigned long Entry_handle;
43 
45  DEFINE_CALLBACK_BUILDER(Make_match_handler,
46  (regex::smatch *, Text_stream_filter::Lines_list *, Io_result),
47  (nullptr, nullptr, Io_result::OK));
48 
50  DEFINE_CALLBACK_BUILDER(Make_line_handler, (const std::string *), (nullptr));
51 
53  Text_stream_filter(Io_stream::Ref stream,
55  size_t max_read = MAX_READ);
56 
58  void
59  Enable();
60 
62  void
63  Disable(bool close_stream = true);
64 
81  Entry_handle
82  Add_entry(const regex::regex &re, Match_handler handler,
83  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero(),
84  size_t ctx_lines_before = 0, size_t ctx_lines_after = 0);
85 
87  void
88  Set_line_handler(Line_handler handler);
89 
90 private:
91  Io_stream::Ref stream;
93 
95  class Entry {
96  public:
97  Entry_handle handle;
99  regex::regex re;
101  Match_handler handler;
103  regex::smatch match;
105  Lines_list lines;
107  size_t ctx_lines_before, ctx_lines_after;
109  std::chrono::milliseconds timeout = std::chrono::milliseconds::zero();
111  Timer_processor::Timer::Ptr timer = nullptr;
112 
113  Entry(Entry_handle handle, const regex::regex &re, Match_handler handler,
114  size_t ctx_lines_before, size_t ctx_lines_after):
115  handle(handle), re(re), handler(handler),
116  ctx_lines_before(ctx_lines_before), ctx_lines_after(ctx_lines_after)
117  {}
118 
119  ~Entry();
120  };
121 
123  std::string line_buf;
125  std::list<std::string> lines_history;
127  Entry_handle cur_handle = 1;
129  std::map<Entry_handle, Entry> entries;
131  bool cr_received = false;
133  Entry *cur_entry = nullptr;
135  size_t ctx_lines_after;
137  Line_handler line_handler;
139  Operation_waiter read_op_waiter;
141  bool read_is_scheduled = false;
143  size_t max_read;
144 
145  bool
146  Timeout_handler(Entry *e);
147 
149  void
150  On_data_received(Io_buffer::Ptr buf, Io_result result);
151 
153  void
154  On_char_received(int c);
155 
157  void
158  On_line_received(const std::string &line);
159 
161  void
162  Schedule_read();
163 
164  void
165  Reset_state(Io_result);
166 
167  void
168  Fire_entry(Entry &e, bool timed_out = false);
169 
171  Entry *
172  Match_entry(const std::string &line);
173 };
174 
175 } /* namespace vsm */
176 } /* namespace ugcs */
177 
178 #endif /* TEXT_STREAM_FILTER_H_ */
UGCS root namespace.
Definition: android-linux/ugcs/vsm/platform_sockets.h:27
std::vector< std::string > Lines_list
List of accumulated lines.
Definition: text_stream_filter.h:29
static constexpr size_t MAX_LINE_LENGTH
Maximal number of characters in one line which is supported.
Definition: text_stream_filter.h:25
#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:27
unsigned long Entry_handle
Manipulation handle for an entry.
Definition: text_stream_filter.h:42
STL namespace.
static constexpr size_t MAX_HISTORY_LINES
Maximal number of lines saved before the matched line.
Definition: text_stream_filter.h:23
Io_result
Result of I/O operation.
Definition: io_stream.h:25
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:54
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:691
std::shared_ptr< Text_stream_filter > Ptr
Pointer type.
Definition: text_stream_filter.h:20
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
Class for synchronizing with request execution.
Definition: operation_waiter.h:24
Main include file for VSM SDK.