Validate RSSI instance to allow user defined RSSI source (issue #5842) (#5846)
[opentx.git] / companion / src / simulation / filteredtextbuffer.h
blob893f44e40ee534012c0a05a59d1688ef09530598
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef FILTEREDTEXTBUFFER_H
22 #define FILTEREDTEXTBUFFER_H
24 #include <QBuffer>
25 #include <QRegularExpression>
26 #include <QReadWriteLock>
27 #include <QTimer>
30 * FIFOBufferDevice implements a thread-safe, asynchronous, buffered, FIFO I/O device based on QBuffer (which is a QIODevice).
31 * Data is removed from the beginning after each read operation (read(), readAll(), readLine(), etc).
32 * Also unlike QBuffer, it will NOT grow in an unlmited fashion. Even if never read, size is constrained to dataBufferMaxSize.
33 * Default maximum buffer size 20KB.
35 class FIFOBufferDevice : public QBuffer
37 Q_OBJECT
39 public:
40 explicit FIFOBufferDevice(QObject * parent = Q_NULLPTR);
42 qint64 getDataBufferMaxSize() const;
43 void setDataBufferMaxSize(qint64 size);
45 // reimplemented from QIODevice for efficiency
46 qint64 readLine(char * data, qint64 maxSize);
47 QByteArray readLine(qint64 maxSize = 0);
49 signals:
50 void bufferOverflow(qint64 len); // len is overflow size, <= 0 means overflow has cleared
52 protected:
53 qint64 trimData(qint64 len);
54 virtual qint64 writeData(const char * data, qint64 len);
55 virtual qint64 readData(char * data, qint64 len);
57 QReadWriteLock m_dataRWLock;
58 qint64 m_dataBufferMaxSize; // [bytes] output buffer limit (FIFO).
59 bool m_hasOverflow;
64 * FilteredTextBuffer implements a FIFOBufferDevice which can, optionally, have a line filter applied to it.
65 * If no filter is set, it acts exactly like its parent class.
66 * If a filter is set, incoming data is buffered until a full line (\n terminated) is available. The line is then
67 * compared against the filter, and either added to the normal output buffer or discarded.
68 * If a full line is not found after a specified timeout (1500ms by default) then any data in the input buffer is
69 * flushed anyway. The same is true if the input buffer overflows (max. 5KB by default).
70 * Another FIFOBufferDevice is used as the input buffer.
72 class FilteredTextBuffer : public FIFOBufferDevice
74 Q_OBJECT
76 public:
77 explicit FilteredTextBuffer(QObject * parent = Q_NULLPTR);
78 ~FilteredTextBuffer();
80 qint64 getInputBufferMaxSize() const;
81 quint32 getInputBufferTimeout() const;
83 public slots:
84 // input buffer will be flushed if grows > size bytes.
85 void setInputBufferMaxSize(qint64 size);
86 // how often to flush the input buffer when less than whole lines are present.
87 void setInputBufferTimeout(quint32 ms);
88 void setLineFilterExpr(const QRegularExpression & expr);
89 void setLineFilterEnabled(bool enable);
90 void setLineFilterExclusive(bool exclusive);
91 void setLineFilter(bool enable, bool exclusive, const QRegularExpression & expr);
93 signals:
94 // these are used internally to toggle the input buffer timeout timer (we use signals for thread safety)
95 void timerStop();
96 void timerStart();
98 protected:
99 qint64 writeDataSuper(const char * data, qint64 len = -1);
100 virtual qint64 writeData(const char * data, qint64 len);
101 void flushInputBuffer();
102 void closeInputBuffer();
103 bool openInputBuffer();
104 void processInputBuffer();
105 void onInputBufferWrite(qint64);
106 void onInputBufferOverflow(const qint64 len);
108 FIFOBufferDevice * m_inBuffer;
109 QTimer * m_bufferFlushTimer;
110 QRegularExpression m_lineFilter;
111 qint64 m_inBuffMaxSize; // [bytes]
112 quint32 m_inBuffFlushTimeout; // [ms]
113 bool m_lineFilterEnable;
114 bool m_lineFilterExclusive;
118 #endif // FILTEREDTEXTBUFFER_H