$darkmode
Qore ConnectionProvider Module Reference 1.11
PollingConnectionMonitor.qc.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // Qore PollingConnectionMonitor class definition
3 
4 /* PollingConnectionMonitor.qc Copyright 2016 - 2023 Qore Technologies, s.r.o.
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23 */
24 
26 
28 namespace ConnectionProvider {
30 
32 class PollingConnectionMonitor : public LoggerWrapper {
33 
34 public:
36  const PollEventMap = {
37  SOCK_POLLIN: "read",
38  SOCK_POLLOUT: "write",
39  SOCK_POLLIN | SOCK_POLLOUT: "read and write",
40  };
41 
42 protected:
44  Mutex m();
45 
47  hash<string, hash<PollInfo>> cache;
48 
50  int tid;
51 
53  bool autostart = True;
54 
57 
60 
62  const IO_ADD = "add";
63 
65  const IO_UPDATE = "update";
66 
68  const IO_REMOVE = "remove";
69 
71  const IO_QUIT = "quit";
72 
74  const IO_QUIT_REMOVE_ALL = "quit-remove-all";
75 
77  const DefaultPingTimeout = 30s;
78 
80  const DefaultPingRepeat = 1m;
81 
82 public:
83 
84 private:
86  File sem_write;
87 
89  ReadOnlyFile sem_read;
90 
92  hash<SocketPollInfo> sem_info;
93 
95  Counter mcnt();
96 
98  Queue cmdq();
99 
100 public:
101 
103  constructor(*LoggerInterface logger) ;
104 
105 
108 
109 
112 
113 
115  bool getAutostart();
116 
117 
119 
121  start();
122 
123 
125  stop();
126 
127 
130 
131 
133  bool running();
134 
135 
137 
143  add(AbstractConnection conn, *hash<auto> other);
144 
145 
147 
152  addOrUpdate(AbstractConnection conn, *hash<auto> other);
153 
154 
156 
164  bool removeConnection(string name);
165 
166 
168 
174  removeConnectionEx(string name);
175 
176 
177 protected:
178  startIntern();
179 public:
180 
181 
182 protected:
183  int stopIntern();
184 public:
185 
186 
187 protected:
188  ioThread();
189 public:
190 
191 
193 protected:
194  handlePingSuccess(string name, date delta, *bool oldok);
195 public:
196 
197 
199 protected:
200  handlePingFailed(string name, date delta, hash<ExceptionInfo> ex);
201 public:
202 
203 
205 protected:
206  handlePingTimeout(string name, date delta);
207 public:
208 
209 
211 protected:
212  handlePingTimeoutIntern(string name, date delta);
213 public:
214 
215 
217 protected:
218  restartPing(string name, *bool force_restart);
219 public:
220 
221 
222 protected:
223  *bool processCommands();
224 public:
225 
226 
227 protected:
228  sendCmd(string cmd);
229 public:
230 
231 };
232 };
233 
234 // private symbols
235 namespace Priv {
236 hashdecl PollInfo {
237  // poll start timestamp
238  date start;
239 
240  # connection
241  AbstractConnection conn;
242 
243  # poll operation
244  AbstractPollOperation spop;
245 
246  # other data stored alongside the connection
247  *hash<auto> other;
248 }
249 
250 class RecursiveAutoLock {
251 
252 public:
253 protected:
254  Mutex m;
255 
256 public:
257 
258  constructor(Mutex m);
259 
260 
261  destructor();
262 
263 };
264 };
abstract base class for connections
Definition: AbstractConnection.qc.dox.h:34
Class for monitoring connection supporting the polling API.
Definition: PollingConnectionMonitor.qc.dox.h:32
bool removeConnection(string name)
Removes the given connection.
date ping_repeat
ping repeat duration
Definition: PollingConnectionMonitor.qc.dox.h:59
handlePingTimeout(string name, date delta)
Handles a poll timeout.
handlePingSuccess(string name, date delta, *bool oldok)
Handles a successful ping.
File sem_write
File to signal the I/O thread.
Definition: PollingConnectionMonitor.qc.dox.h:86
setAutostart(bool autostart=True)
Set autostart flag.
Mutex m()
Lock for atomic actions.
Queue cmdq()
I/O thread command queue.
handlePingFailed(string name, date delta, hash< ExceptionInfo > ex)
Handles a failed ping.
restartPing(string name, *bool force_restart)
Restarts a ping operation.
handlePingTimeoutIntern(string name, date delta)
Handles a ping timeout.
removeConnectionEx(string name)
Removes the given connection.
add(AbstractConnection conn, *hash< auto > other)
Adds a connection to be monitored.
const IO_REMOVE
I/O thread command: remove.
Definition: PollingConnectionMonitor.qc.dox.h:68
int tid
I/O thread TID.
Definition: PollingConnectionMonitor.qc.dox.h:50
addOrUpdate(AbstractConnection conn, *hash< auto > other)
Adds or updates an existing connection that is already being monitored.
destructor()
Stops the monitoring thread and destroys the object.
const IO_UPDATE
I/O thread command: update.
Definition: PollingConnectionMonitor.qc.dox.h:65
ReadOnlyFile sem_read
File to read in the I/O thread.
Definition: PollingConnectionMonitor.qc.dox.h:89
constructor(*LoggerInterface logger)
Creates the object.
const IO_ADD
I/O thread command: add.
Definition: PollingConnectionMonitor.qc.dox.h:62
bool autostart
Autostart monitoring thread with first connection.
Definition: PollingConnectionMonitor.qc.dox.h:53
stopClear()
Stops monitoring and clears all connections.
const IO_QUIT_REMOVE_ALL
I/O thread command: quit and remove all connection.
Definition: PollingConnectionMonitor.qc.dox.h:74
hash< SocketPollInfo > sem_info
Semaphore descriptor info.
Definition: PollingConnectionMonitor.qc.dox.h:92
const IO_QUIT
I/O thread command: quit.
Definition: PollingConnectionMonitor.qc.dox.h:71
hash< string, hash< PollInfo > > cache
Connection cache; unique name -> connection object wrapper.
Definition: PollingConnectionMonitor.qc.dox.h:47
date ping_timeout
ping timeout duration
Definition: PollingConnectionMonitor.qc.dox.h:56
const PollEventMap
Polling event map.
Definition: PollingConnectionMonitor.qc.dox.h:36
const DefaultPingTimeout
Default ping timeout duration.
Definition: PollingConnectionMonitor.qc.dox.h:77
const DefaultPingRepeat
Default ping repeat duration.
Definition: PollingConnectionMonitor.qc.dox.h:80
bool running()
Returns True if the I/O thread is running.
const True
date date(date dt)
const SOCK_POLLIN
const SOCK_POLLOUT
The ConnectionProvider namespace.
Definition: AbstractConnection.qc.dox.h:28