NCBI C++ ToolKit
srv_inlines.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef NETCACHE__SRV_INLINES__HPP
2 #define NETCACHE__SRV_INLINES__HPP
3 /* $Id: srv_inlines.hpp 83741 2018-09-14 12:35:21Z gouriano $
4  * ===========================================================================
5  *
6  * PUBLIC DOMAIN NOTICE
7  * National Center for Biotechnology Information
8  *
9  * This software/database is a "United States Government Work" under the
10  * terms of the United States Copyright Act. It was written as part of
11  * the author's official duties as a United States Government employee and
12  * thus cannot be copyrighted. This software/database is freely available
13  * to the public for use. The National Library of Medicine and the U.S.
14  * Government have not placed any restriction on its use or reproduction.
15  *
16  * Although all reasonable efforts have been taken to ensure the accuracy
17  * and reliability of the software and data, the NLM and the U.S.
18  * Government do not and cannot warrant the performance or results that
19  * may be obtained by using this software or data. The NLM and the U.S.
20  * Government disclaim all warranties, express or implied, including
21  * warranties of performance, merchantability or fitness for any particular
22  * purpose.
23  *
24  * Please cite the author in any work or product based on this material.
25  *
26  * ===========================================================================
27  *
28  * Authors: Pavel Ivanov
29  *
30  * File Description:
31  */
32 
33 
34 #include <corelib/ncbistr.hpp>
35 
36 
38 
39 
40 
44 
45 
46 
51  // Everything below is shutdown states
56 };
57 
59 
60 
61 
62 #ifdef NCBI_OS_LINUX
63 
64 inline int
66 {
67  return int(s_LastJiffyTime.tv_sec);
68 }
69 
70 inline CSrvTime
72 {
74  clock_gettime(CLOCK_REALTIME, &result);
75  return result;
76 }
77 
78 inline
80 {
81  tv_sec = 0;
82  tv_nsec = 0;
83 }
84 inline
86 {
87  tv_sec = sec;
88  tv_nsec = 0;
89 }
90 
91 inline time_t&
92 CSrvTime::Sec(void)
93 {
94  return tv_sec;
95 }
96 
97 inline time_t
98 CSrvTime::Sec(void) const
99 {
100  return tv_sec;
101 }
102 
103 inline long&
104 CSrvTime::NSec(void)
105 {
106  return tv_nsec;
107 }
108 
109 inline long
110 CSrvTime::NSec(void) const
111 {
112  return tv_nsec;
113 }
114 
115 inline Uint8
116 CSrvTime::AsUSec(void) const
117 {
118  return tv_sec * kUSecsPerSecond + tv_nsec / kNSecsPerUSec;
119 }
120 
121 inline int
122 CSrvTime::Compare(const CSrvTime& t) const
123 {
124  if (tv_sec < t.tv_sec)
125  return -1;
126  else if (tv_sec > t.tv_sec)
127  return 1;
128  else if (tv_nsec < t.tv_nsec)
129  return -1;
130  else if (tv_nsec > t.tv_nsec)
131  return 1;
132  return 0;
133 }
134 
135 inline CSrvTime&
137 {
138  tv_sec += t.tv_sec;
139  tv_nsec += t.tv_nsec;
140  if (tv_nsec >= kNSecsPerSecond) {
141  ++tv_sec;
142  tv_nsec -= kNSecsPerSecond;
143  }
144  return *this;
145 }
146 
147 inline CSrvTime&
149 {
150  tv_sec -= t.tv_sec;
151  if (tv_nsec >= t.tv_nsec) {
152  tv_nsec -= t.tv_nsec;
153  }
154  else {
155  --tv_sec;
156  tv_nsec += kNSecsPerSecond;
157  tv_nsec -= t.tv_nsec;
158  }
159  return *this;
160 }
161 
162 #else /* NCBI_OS_LINUX */
163 
164 inline int
166 {
167  return 0;
168 }
169 
170 inline CSrvTime
172 {
173  return CSrvTime();
174 }
175 
176 inline
178 {}
179 inline
181 {}
182 
183 inline time_t&
185 {
186  static time_t tt;
187  return tt;
188 }
189 
190 inline time_t
191 CSrvTime::Sec(void) const
192 {
193  return 0;
194 }
195 
196 inline long&
198 {
199  static long tt;
200  return tt;
201 }
202 
203 inline long
204 CSrvTime::NSec(void) const
205 {
206  return 0;
207 }
208 
209 inline Uint8
210 CSrvTime::AsUSec(void) const
211 {
212  return 0;
213 }
214 
215 inline int
217 {
218  return 0;
219 }
220 
221 inline CSrvTime&
223 {
224  return *this;
225 }
226 
227 inline CSrvTime&
229 {
230  return *this;
231 }
232 
233 #endif
234 
235 inline bool
237 {
238  return Compare(t) > 0;
239 }
240 
241 inline bool
243 {
244  return Compare(t) >= 0;
245 }
246 
247 inline bool
248 CSrvTime::operator< (const CSrvTime& t) const
249 {
250  return Compare(t) < 0;
251 }
252 
253 inline bool
254 CSrvTime::operator<= (const CSrvTime& t) const
255 {
256  return Compare(t) <= 0;
257 }
258 
259 
260 inline CSrvTime
262 {
263  return s_SrvStartTime;
264 }
265 
266 inline TSrvThreadNum
268 {
269  return s_MaxRunningThreads;
270 }
271 
272 inline bool
274 {
275  return s_SrvState == eSrvRunning;
276 }
277 
278 inline bool
280 {
282 }
283 
284 inline bool
286 {
288 }
289 
290 inline bool
292 {
294 }
295 
296 inline bool
298 {
299  return s_SrvState >= eSrvStopping;
300 }
301 
302 
303 inline const CSrvDiagMsg&
304 CSrvDiagMsg::operator<< (const char* str) const
305 {
306  return operator<< (CTempString(str));
307 }
308 
309 inline const CSrvDiagMsg&
310 CSrvDiagMsg::operator<< (const string& str) const
311 {
312  return operator<< (CTempString(str));
313 }
314 
315 inline const CSrvDiagMsg&
317 {
318  return operator<< (Int8(num));
319 }
320 
321 inline const CSrvDiagMsg&
323 {
324  return operator<< (Uint8(num));
325 }
326 
327 inline CSrvDiagMsg&
329 {
330  return PrintParam(name, Int8(value));
331 }
332 
333 inline CSrvDiagMsg&
335 {
336  return PrintParam(name, Uint8(value));
337 }
338 
339 
340 inline Uint4
342 {
343  return m_Priority;
344 }
345 
346 inline void
348 {
349  m_Priority = prty;
350 }
351 
352 inline CRequestContext*
354 {
355  return m_DiagCtx;
356 }
357 
358 
359 inline bool
361 {
362  return m_RdSize > m_RdPos;
363 }
364 
365 inline bool
367 {
368  return m_WrPos < m_WrSize;
369 }
370 
371 inline bool
373 {
374  if (m_RegError) {
375  x_PrintError();
376  return true;
377  }
378  return false;
379 }
380 
381 inline bool
383 {
384  return m_SockCanReadMore;
385 }
386 
387 inline bool
389 {
391 }
392 
393 inline bool
395 {
396  return NeedToClose() || HasError() || !CanHaveMoreRead();
397 }
398 
399 inline CSrvSocketTask&
401 {
402  WriteData(message.data(), message.size());
403  return *this;
404 }
405 
406 template <typename NumType>
407 inline CSrvSocketTask&
409 {
410  return WriteText(NStr::NumericToString(num));
411 }
412 
413 inline CSrvSocketTask&
415 {
416  return WriteText(b ? "true" : "false");
417 }
418 
419 inline void
421 {
422  m_FlushIsDone = false;
423  ACCESS_ONCE(m_NeedToFlush) = true;
424  SetRunnable();
425 }
426 
427 inline bool
429 {
430  return ACCESS_ONCE(m_FlushIsDone);
431 }
432 
433 inline bool
435 {
436  if (m_RdSize - m_RdPos < size) {
437  ReadToBuf();
438  if (m_RdSize - m_RdPos < size)
439  return false;
440  }
441  memcpy(buf, m_RdBuf + m_RdPos, size);
442  m_RdPos += size;
443  return true;
444 }
445 
446 template <typename NumType>
447 inline bool
449 {
450  return ReadData(num, sizeof(*num));
451 }
452 
453 inline bool
455 {
456  return m_ProxySrc || m_ProxyDst;
457 }
458 
459 inline bool
461 {
462  return m_ProxyHadError;
463 }
464 
465 inline void
467 {
468  x_CloseSocket(false);
469 }
470 
471 inline void
473 {
474  x_CloseSocket(true);
475 }
476 
477 
478 inline bool
480 {
481  return m_TransState == eState_Final;
482 }
483 
484 
485 inline bool
487 {
488  return m_TransFinished;
489 }
490 
491 
493 
494 #endif /* NETCACHE__SRV_INLINES__HPP */
Class used in all diagnostic logging.
Definition: srv_diag.hpp:73
const CSrvDiagMsg & operator<<(CTempString str) const
Converts input value to string and adds to started log message.
Definition: logging.cpp:1138
CSrvDiagMsg & PrintParam(CTempString name, CTempString value)
Adds parameter to "request-start" or "extra" message.
Definition: logging.cpp:1040
Task controlling a socket.
Uint2 m_WrPos
Position of current writing pointer in the write buffer.
bool IsProxyInProgress(void)
Check whether proxying started earlier is still in progress.
bool NeedToClose(void)
Checks if socket should be closed because of long inactivity or because server is in "hard" shutdown ...
void x_CloseSocket(bool do_abort)
Close or abort the socket – they have little difference, thus they joined in one method.
CSrvSocketTask * m_ProxySrc
Source task for proxying.
bool HasError(void)
Checks if socket has some error in it.
void AbortSocket(void)
Abort the socket, i.e.
CSrvSocketTask & WriteText(CTempString message)
Write text into socket.
CSrvSocketTask & WriteNumber(NumType num)
Write number into socket as string, i.e.
bool IsReadDataAvailable(void)
Checks if there's some data available for reading in internal buffers.
void CloseSocket(void)
Close the socket gracefully, i.e.
bool ProxyHadError(void)
Check whether proxying started earlier finished successfully or any of sockets had some error in it.
bool m_NeedToFlush
Flag showing that task needs to flush all write buffers.
bool m_NeedToClose
Flag showing that socket needs to be closed because of long inactivity.
char * m_RdBuf
Read buffer.
bool CanHaveMoreRead(void)
Checks if socket can ever have more data to read even though it may not have it at the moment.
bool m_ProxyHadError
Flag showing that last proxying operation finished with error.
bool m_SockCanReadMore
Flag showing that socket can have more reads, i.e. there was no EOF yet.
bool m_FlushIsDone
Flag showing that write buffers were flushed.
bool m_RegError
Flag showing if there's error pending on the socket.
bool ReadData(void *buf, Uint2 size)
Read from socket exactly the given data size.
CSrvSocketTask * m_ProxyDst
Destination task for proxying.
bool ReadToBuf(void)
Read from socket into internal buffer.
void RequestFlush(void)
Request flushing of all data saved in internal write buffers to socket.
Uint2 m_RdPos
Position of current reading in the read buffer, i.e.
bool ReadNumber(NumType *num)
Read from socket a number in native machine representation.
void WriteData(const void *buf, size_t size)
Write the exact amount of data into the socket.
bool IsWriteDataPending(void)
Checks if there's some data pending in write buffers and waiting to be sent to kernel.
Uint2 m_WrSize
Size of data in the write buffer waiting for writing.
Uint2 m_RdSize
Size of data available for reading in the read buffer.
bool NeedEarlyClose(void)
Checks if socket should be closed because of internal reasons (long inactivity or "hard" shutdown as ...
void x_PrintError(void)
Prints socket's error if there's any error pending on the socket.
CSrvSocketTask & WriteBool(bool b)
bool FlushIsDone(void)
Check if data flushing requested earlier is complete.
CRequestContext * GetDiagCtx(void)
Get current diagnostic context for the task.
CRequestContext * m_DiagCtx
Current diagnostic context for this task.
Definition: srv_tasks.hpp:216
void SetPriority(Uint4 prty)
Set and retrieve task's priority.
void SetRunnable(bool boost=false)
Set this task "runnable", i.e.
Definition: scheduler.cpp:618
Uint4 m_Priority
Task's priority.
Definition: srv_tasks.hpp:214
Uint4 GetPriority(void)
Class incorporating convenient methods to work with struct timespec.
Definition: srv_time.hpp:61
bool operator>(const CSrvTime &t) const
Compare object's value to another CSrvTime object's value.
int Compare(const CSrvTime &t) const
Compares object's value to another CSrvTime object's value.
CSrvTime(void)
bool operator<(const CSrvTime &t) const
static int CurSecs(void)
Current time in seconds since epoch (time_t).
bool operator>=(const CSrvTime &t) const
CSrvTime & operator-=(const CSrvTime &t)
Subtract value of other CSrvTime object from this one.
time_t & Sec(void)
Read/set number of seconds since epoch stored in the object.
CSrvTime & operator+=(const CSrvTime &t)
Add value of other CSrvTime object to this one.
bool operator<=(const CSrvTime &t) const
static CSrvTime Current(void)
Exact current time with precision up to nanoseconds.
Uint8 AsUSec(void) const
Converts object's value to microseconds since epoch.
long & NSec(void)
Read/set number of nanoseconds stored in the object.
bool IsTransFinished(void)
Check if transition completeness was consumed.
bool m_TransFinished
Flag showing that transition was already consumed.
Definition: srv_tasks.hpp:443
bool IsTransStateFinal(void)
Checks if "Final" state was already reached.
ETransState m_TransState
Current state of the task.
Definition: srv_tasks.hpp:410
static TSrvThreadNum GetMaxRunningThreads(void)
Returns maximum number of worker threads allowed ([task_server]/max_threads parameter in ini-file).
static CSrvTime GetStartTime(void)
Returns time when this server application was started (when Initialize() method was called).
static bool IsRunning(void)
Checks if TaskServer is running now, i.e.
static bool IsInShutdown(void)
Checks if TaskServer received request to shutdown.
static bool IsInSoftShutdown(void)
Checks if TaskServer currently is in "soft" shutdown phase when client-related operations still can p...
static bool IsInHardShutdown(void)
Checks if TaskServer currently is in "hard" shutdown phase when no operation is allowed to proceed un...
CTempString implements a light-weight string on top of a storage buffer whose lifetime management is ...
Definition: tempstr.hpp:65
The NCBI C++ standard methods for dealing with std::string.
static const char * str(char *buf, int n)
Definition: stats.c:84
int32_t Int4
4-byte (32-bit) signed integer
Definition: ncbitype.h:102
uint32_t Uint4
4-byte (32-bit) unsigned integer
Definition: ncbitype.h:103
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
int64_t Int8
8-byte (64-bit) signed integer
Definition: ncbitype.h:104
uint64_t Uint8
8-byte (64-bit) unsigned integer
Definition: ncbitype.h:105
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
const char * data(void) const
Return a pointer to the array represented.
Definition: tempstr.hpp:313
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
size_type size(void) const
Return the length of the represented array.
Definition: tempstr.hpp:327
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
char * buf
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
EIPRangeType t
Definition: ncbi_localip.c:101
EServerState
Definition: srv_inlines.hpp:47
@ eSrvInitialized
Definition: srv_inlines.hpp:49
@ eSrvRunning
Definition: srv_inlines.hpp:50
@ eSrvStopping
Definition: srv_inlines.hpp:54
@ eSrvNotInitialized
Definition: srv_inlines.hpp:48
@ eSrvStopped
Definition: srv_inlines.hpp:55
@ eSrvShuttingDownHard
Definition: srv_inlines.hpp:53
@ eSrvShuttingDownSoft
Definition: srv_inlines.hpp:52
CSrvTime s_SrvStartTime
Definition: time_man.cpp:51
bool IsServerStopping(void)
TSrvThreadNum s_MaxRunningThreads
Definition: threads_man.cpp:71
EServerState s_SrvState
Definition: server_core.cpp:68
CSrvTime s_LastJiffyTime
Definition: time_man.cpp:52
#define ACCESS_ONCE(x)
Purpose of this macro is to force compiler to access variable exactly at the place it's written (no m...
Definition: srv_sync.hpp:51
@ kUSecsPerSecond
Definition: srv_time.hpp:46
@ kNSecsPerSecond
Definition: srv_time.hpp:48
@ kNSecsPerUSec
Definition: srv_time.hpp:45
Uint2 TSrvThreadNum
Type for thread number in TaskServer.
Definition: task_server.hpp:42
else result
Definition: token2.c:20
Modified on Wed May 29 18:38:41 2024 by modify_doxy.py rev. 669887