57#ifndef DISABLE_REST_API
58 ,
public Poco::Net::HTTPRequestHandler
62 LogDB(
const std::string& name,
int argc,
const char*
const* argv,
const std::string& prefix );
66 static std::shared_ptr<LogDB>
init_logdb(
int argc,
const char*
const* argv,
const std::string& prefix =
"logdb-" );
71 inline std::shared_ptr<DebugStream> log()
76 void run(
bool async );
77#ifndef DISABLE_REST_API
78 virtual void handleRequest( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp )
override;
79 void onWebSocketSession( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp );
80 void handleOverload(Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp);
88 virtual void evfinish()
override;
89 virtual void evprepare()
override;
90 void onCheckBuffer( ev::timer& t,
int revents );
91 void onActivate( ev::async& watcher,
int revents ) ;
92 void addLog(
Log* log,
const std::string& txt );
93 void log2File(
Log* log,
const std::string& txt );
95 size_t getCountOfRecords(
const std::string& logname =
"" );
96 size_t getFirstOfOldRecord(
size_t maxnum );
99 static std::string qEscapeString(
const std::string& s );
101#ifndef DISABLE_REST_API
102 Poco::JSON::Object::Ptr respError( Poco::Net::HTTPServerResponse& resp, Poco::Net::HTTPResponse::HTTPStatus s,
const std::string& message );
103 Poco::JSON::Object::Ptr httpGetRequest( Poco::Net::HTTPServerResponse& resp,
const std::string& cmd,
const Poco::URI::QueryParameters& p );
104 Poco::JSON::Object::Ptr
httpGetList( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
105 Poco::JSON::Object::Ptr httpGetLogs( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
106 Poco::JSON::Object::Ptr httpGetCount( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
107 Poco::JSON::Object::Ptr httpGetStatus( Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
108 Poco::JSON::Object::Ptr httpDownload( Poco::Net::HTTPServerRequest& req, Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
109 Poco::JSON::Object::Ptr httpLogControl(std::ostream& out, Poco::Net::HTTPServerRequest& req,
110 Poco::Net::HTTPServerResponse& resp,
const std::string& logname,
const Poco::URI::QueryParameters& params );
112 void httpWebSocketPage( std::ostream& out, Poco::Net::HTTPServerRequest& req,
113 Poco::Net::HTTPServerResponse& resp,
const Poco::URI::QueryParameters& p );
114 void httpWebSocketConnectPage( Poco::Net::HTTPServerRequest& req,
115 Poco::Net::HTTPServerResponse& resp,
const std::string& logname,
const Poco::URI::QueryParameters& p );
117 bool supportsGzip( Poco::Net::HTTPServerRequest& request );
121 static std::string qLast(
const std::string& p );
124 static std::string qDate(
const std::string& p,
const char sep =
'-');
126 std::shared_ptr<LogWebSocket> newWebSocket(Poco::Net::HTTPServerRequest* req, Poco::Net::HTTPServerResponse* resp,
127 const std::string& logname,
const Poco::URI::QueryParameters& p );
128 void delWebSocket( std::shared_ptr<LogWebSocket>&
ws );
132 std::unique_ptr<SQLiteInterface> db;
137 bool activate = {
false };
138 std::chrono::steady_clock::time_point startTime;
140 typedef std::queue<std::string> QueryBuffer;
142 size_t qbufSize = { 1000 };
144 ev::timer flushBufferTimer;
145 double tmFlushBuffer_sec = { 1.0 };
149 size_t maxdbRecords = { 200 * 1000 };
150 size_t numOverflow = { 0 };
155 void onTerminate( ev::sig& evsig,
int revents );
157 ev::async wsactivate;
159 std::shared_ptr<uniset::LogServer> logserv;
160 std::string logserv_host = {
""};
161 int logserv_port = {0};
172 std::string peername;
173 std::string description;
175 std::shared_ptr<DebugStream> dblog;
176 std::shared_ptr<DebugStream> logfile;
178 bool isConnected()
const;
180 void set( ev::dynamic_loop& loop );
181 void check( ev::timer& t,
int revents );
182 void event( ev::io& watcher,
int revents );
183 void read( ev::io& watcher );
184 void oncommand( ev::async& watcher,
int revents );
185 void write( ev::io& io );
188 typedef sigc::signal<void, Log*, const std::string&> ReadSignal;
189 ReadSignal signal_on_read();
191 void setCheckConnectionTime(
double sec );
192 void setReadBufSize(
size_t sz );
197 bool connect()
noexcept;
205 double checkConnection_sec = { 5.0 };
207 std::shared_ptr<UTCPStream> tcp;
208 std::vector<char> buf;
210 static const size_t reservsize = { 1000 };
214 std::queue<UTCPCore::Buffer*> wbuf;
217 std::vector<UTCPCore::Buffer*> cmdbuf;
221 std::vector< std::shared_ptr<Log> > logservers;
222 std::shared_ptr<DebugStream> dblog;
224#ifndef DISABLE_REST_API
225 std::shared_ptr<Poco::Net::HTTPServer> httpserv;
226 std::string httpHost = {
"" };
227 int httpPort = { 0 };
228 std::string httpCORS_allow = {
"*" };
229 std::string httpReplyAddr = {
"" };
230 std::string httpJsonContentType = {
"text/json; charset=UTF-8" };
231 std::string httpHtmlContentType = {
"text/html; charset=UTF-8" };
232 std::string utf8Code =
"UTF-8";
233 std::atomic<size_t> httpActiveRequests{0};
234 size_t httpMaxThreads = { 0 };
235 size_t httpMaxQueued = { 0 };
236 size_t httpMaxRequests = { 10 };
237 static constexpr size_t reservedForMonitoring = 2;
239 double wsHeartbeatTime_sec = { 3.0 };
240 double wsSendTime_sec = { 0.5 };
241 size_t wsMaxSend = { 200 };
242 double wsBackpressureTime_sec = { 15.0 };
243 size_t wsQueueBytesLimit = { 2 * 1024 * 1024 };
244 size_t wsFrameBytesLimit = { 64 * 1024 };
245 bool httpEnabledLogControl = {
false };
246 bool httpEnabledDownload = {
false };
247 double wsPongTimeout_sec = { 10.0 };
248 double wsMaxLifetime_sec = { 0 };
250 std::string wsPageTemplate =
"";
260 public Poco::Net::WebSocket
264 Poco::Net::HTTPServerResponse* resp,
265 std::shared_ptr<Log>& log );
270 std::shared_ptr<DebugStream> dblog;
273 void set( ev::dynamic_loop& loop );
275 void send( ev::timer& t,
int revents );
276 void ping( ev::timer& t,
int revents );
277 void add(
Log* log,
const std::string& txt );
280 void waitCompletion();
283 void setHearbeatTime(
const double& sec );
284 void setSendPeriod(
const double& sec );
285 void setMaxSendCount(
size_t val );
286 void setBackpressureTimeout(
const double& sec );
287 void setPendingNotice(
const std::string& msg );
288 void setQueueBytesLimit(
size_t bytes );
289 void setMaxFrameBytes(
size_t bytes );
290 void setPongTimeout(
const double& sec );
291 void setMaxLifetime(
const double& sec );
294 void read( ev::io& w,
int revents );
295 void checkPongTimeout( ev::timer& t,
int revents );
297 void enqueueMessage(
const std::string& msg );
298 void buildFramesFromMessages();
300 void logQueueStats(
const std::string& reason );
302 void handleBackpressure();
305 double send_sec = { 0.5 };
306 size_t maxsend = { 200 };
309 double ping_sec = { 3.0 };
312 ev::timer iopongcheck;
313 double pongTimeout_sec = { 10.0 };
314 std::atomic_bool waitingPong = {
false };
315 std::chrono::steady_clock::time_point lastPingSent;
316 std::chrono::steady_clock::time_point sessionStart;
317 double maxLifetime_sec = { 0 };
319 std::mutex finishmut;
320 std::condition_variable finish;
322 std::atomic_bool cancelled = {
false };
324 sigc::connection con;
326 Poco::Net::HTTPServerRequest* req;
327 Poco::Net::HTTPServerResponse* resp;
330 std::queue<UTCPCore::Buffer*> wbuf;
331 std::deque<std::string> msgQueue;
332 size_t queuedBytes = { 0 };
333 size_t queueBytesLimit = { 2 * 1024 * 1024 };
334 size_t maxFrameBytes = { 64 * 1024 };
335 std::chrono::steady_clock::time_point lastDiag;
336 size_t lostByOverflow = { 0 };
337 size_t backpressureCount = { 0 };
338 std::chrono::steady_clock::time_point backpressureStart;
339 bool backpressureActive = {
false };
340 double backpressureTimeout_sec = { 5.0 };
341 std::string pendingNotice;
343 std::unique_ptr<Poco::ObjectPool<uniset::UTCPCore::Buffer>> bufPool;
344 size_t bufPoolCapacity = { 256 };
345 size_t bufPoolPeak = { 2000 };
347 std::shared_ptr<Log> log;
360 logdb->delWebSocket(
ws);
365 std::shared_ptr<LogWebSocket>
ws;
371 std::list<std::shared_ptr<LogWebSocket>> wsocks;
373 size_t maxwsocks = { 50 };
376 public Poco::Net::HTTPRequestHandlerFactory
382 virtual Poco::Net::HTTPRequestHandler* createRequestHandler(
const Poco::Net::HTTPServerRequest& req )
override;