Qore ConnectionProvider Module Reference  1.7.1
PollingConnectionMonitor.qc.dox.h
1 // -*- mode: c++; indent-tabs-mode: nil -*-
2 // Qore PollingConnectionMonitor class definition
3 
4 /* PollingConnectionMonitor.qc Copyright 2016 - 2022 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 
33 
34 public:
35 
36 
37 protected:
39  Mutex m();
40 
42  hash<string, hash<PollInfo>> cache;
43 
45  int tid;
46 
48  *Logger logger;
49 
51  bool autostart = True;
52 
55 
58 
60  const IO_ADD = "add";
61 
63  const IO_UPDATE = "update";
64 
66  const IO_REMOVE = "remove";
67 
69  const IO_QUIT = "quit";
70 
72  const IO_QUIT_REMOVE_ALL = "quit-remove-all";
73 
75  const DefaultPingTimeout = 30s;
76 
78  const DefaultPingRepeat = 1m;
79 
80 public:
81 
82 private:
84  File sem_write;
85 
87  ReadOnlyFile sem_read;
88 
90  hash<SocketPollInfo> sem_info;
91 
93  Counter mcnt();
94 
96  Queue cmdq();
97 
98 public:
99 
101  constructor(*Logger logger);
102 
103 
106 
107 
109  setLogger(Logger logger);
110 
111 
114 
115 
117  bool getAutostart();
118 
119 
121 
123  start();
124 
125 
127  stop();
128 
129 
132 
133 
135  bool running();
136 
137 
139 
145  add(AbstractConnection conn, *hash<auto> other);
146 
147 
149 
154  addOrUpdate(AbstractConnection conn, *hash<auto> other);
155 
156 
158 
166  bool removeConnection(string name);
167 
168 
170 
176  removeConnectionEx(string name);
177 
178 
179  logArgs(int lvl, string msg, auto args);
180 
181 
182  log(int lvl, string msg);
183 
184 
185 protected:
186  startIntern();
187 public:
188 
189 
190 protected:
191  int stopIntern();
192 public:
193 
194 
195 protected:
196  ioThread();
197 public:
198 
199 
201 protected:
202  handlePingSuccess(string name, date delta, *bool oldok);
203 public:
204 
205 
207 protected:
208  handlePingFailed(string name, date delta, hash<ExceptionInfo> ex);
209 public:
210 
211 
213 protected:
214  handlePingTimeout(string name, date delta);
215 public:
216 
217 
219 protected:
220  handlePingTimeoutIntern(string name, date delta);
221 public:
222 
223 
225 protected:
226  restartPing(string name, *bool force_restart);
227 public:
228 
229 
230 protected:
231  *bool processCommands();
232 public:
233 
234 
235 protected:
236  sendCmd(string cmd);
237 public:
238 
239 };
240 };
241 
242 // private symbols
243 namespace Priv {
244 hashdecl PollInfo {
245  // poll start timestamp
246  date start;
247 
248  # connection
249  AbstractConnection conn;
250 
251  # poll operation
252  AbstractPollOperation spop;
253 
254  # other data stored alongside the connection
255  *hash<auto> other;
256 }
257 };
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:57
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:84
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:66
int tid
I/O thread TID.
Definition: PollingConnectionMonitor.qc.dox.h:45
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:63
ReadOnlyFile sem_read
File to read in the I/O thread.
Definition: PollingConnectionMonitor.qc.dox.h:87
*Logger logger
Logger.
Definition: PollingConnectionMonitor.qc.dox.h:48
const IO_ADD
I/O thread command: add.
Definition: PollingConnectionMonitor.qc.dox.h:60
bool autostart
Autostart monitoring thread with first connection.
Definition: PollingConnectionMonitor.qc.dox.h:51
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:72
hash< SocketPollInfo > sem_info
Semaphore descriptor info.
Definition: PollingConnectionMonitor.qc.dox.h:90
const IO_QUIT
I/O thread command: quit.
Definition: PollingConnectionMonitor.qc.dox.h:69
setLogger(Logger logger)
Sets the logger.
hash< string, hash< PollInfo > > cache
Connection cache; unique name -> connection object wrapper.
Definition: PollingConnectionMonitor.qc.dox.h:42
date ping_timeout
ping timeout duration
Definition: PollingConnectionMonitor.qc.dox.h:54
constructor(*Logger logger)
Creates the object.
const DefaultPingTimeout
Default ping timeout duration.
Definition: PollingConnectionMonitor.qc.dox.h:75
const DefaultPingRepeat
Default ping repeat duration.
Definition: PollingConnectionMonitor.qc.dox.h:78
bool running()
Returns True if the I/O thread is running.
const True
date date(date dt)
The ConnectionProvider namespace.
Definition: AbstractConnection.qc.dox.h:28