1 #ifndef NETCACHE__SRV_INLINES__HPP
2 #define NETCACHE__SRV_INLINES__HPP
3 /* $Id: srv_inlines.hpp 570772 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
37 BEGIN_NCBI_SCOPE
38
39
40
41 extern CSrvTime s_SrvStartTime;
42 extern CSrvTime s_LastJiffyTime;
43 extern TSrvThreadNum s_MaxRunningThreads;
44
45
46
47 enum EServerState {
48 eSrvNotInitialized,
49 eSrvInitialized,
50 eSrvRunning,
51 // Everything below is shutdown states
52 eSrvShuttingDownSoft,
53 eSrvShuttingDownHard,
54 eSrvStopping,
55 eSrvStopped
56 };
57
58 extern EServerState s_SrvState;
59
60
61
62 #ifdef NCBI_OS_LINUX
63
64 inline int
CurSecs(void)65 CSrvTime::CurSecs(void)
66 {
67 return int(s_LastJiffyTime.tv_sec);
68 }
69
70 inline CSrvTime
Current(void)71 CSrvTime::Current(void)
72 {
73 CSrvTime result;
74 clock_gettime(CLOCK_REALTIME, &result);
75 return result;
76 }
77
78 inline
CSrvTime(void)79 CSrvTime::CSrvTime(void)
80 {
81 tv_sec = 0;
82 tv_nsec = 0;
83 }
84 inline
CSrvTime(Uint8 sec)85 CSrvTime::CSrvTime(Uint8 sec)
86 {
87 tv_sec = sec;
88 tv_nsec = 0;
89 }
90
91 inline time_t&
Sec(void)92 CSrvTime::Sec(void)
93 {
94 return tv_sec;
95 }
96
97 inline time_t
Sec(void) const98 CSrvTime::Sec(void) const
99 {
100 return tv_sec;
101 }
102
103 inline long&
NSec(void)104 CSrvTime::NSec(void)
105 {
106 return tv_nsec;
107 }
108
109 inline long
NSec(void) const110 CSrvTime::NSec(void) const
111 {
112 return tv_nsec;
113 }
114
115 inline Uint8
AsUSec(void) const116 CSrvTime::AsUSec(void) const
117 {
118 return tv_sec * kUSecsPerSecond + tv_nsec / kNSecsPerUSec;
119 }
120
121 inline int
Compare(const CSrvTime & t) const122 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&
operator +=(const CSrvTime & t)136 CSrvTime::operator+= (const CSrvTime& t)
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&
operator -=(const CSrvTime & t)148 CSrvTime::operator-= (const CSrvTime& t)
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
CurSecs(void)165 CSrvTime::CurSecs(void)
166 {
167 return 0;
168 }
169
170 inline CSrvTime
Current(void)171 CSrvTime::Current(void)
172 {
173 return CSrvTime();
174 }
175
176 inline
CSrvTime(void)177 CSrvTime::CSrvTime(void)
178 {}
179 inline
CSrvTime(Uint8)180 CSrvTime::CSrvTime(Uint8)
181 {}
182
183 inline time_t&
Sec(void)184 CSrvTime::Sec(void)
185 {
186 static time_t tt;
187 return tt;
188 }
189
190 inline time_t
Sec(void) const191 CSrvTime::Sec(void) const
192 {
193 return 0;
194 }
195
196 inline long&
NSec(void)197 CSrvTime::NSec(void)
198 {
199 static long tt;
200 return tt;
201 }
202
203 inline long
NSec(void) const204 CSrvTime::NSec(void) const
205 {
206 return 0;
207 }
208
209 inline Uint8
AsUSec(void) const210 CSrvTime::AsUSec(void) const
211 {
212 return 0;
213 }
214
215 inline int
Compare(const CSrvTime & t) const216 CSrvTime::Compare(const CSrvTime& t) const
217 {
218 return 0;
219 }
220
221 inline CSrvTime&
operator +=(const CSrvTime & t)222 CSrvTime::operator+= (const CSrvTime& t)
223 {
224 return *this;
225 }
226
227 inline CSrvTime&
operator -=(const CSrvTime & t)228 CSrvTime::operator-= (const CSrvTime& t)
229 {
230 return *this;
231 }
232
233 #endif
234
235 inline bool
operator >(const CSrvTime & t) const236 CSrvTime::operator> (const CSrvTime& t) const
237 {
238 return Compare(t) > 0;
239 }
240
241 inline bool
operator >=(const CSrvTime & t) const242 CSrvTime::operator>= (const CSrvTime& t) const
243 {
244 return Compare(t) >= 0;
245 }
246
247 inline bool
operator <(const CSrvTime & t) const248 CSrvTime::operator< (const CSrvTime& t) const
249 {
250 return Compare(t) < 0;
251 }
252
253 inline bool
operator <=(const CSrvTime & t) const254 CSrvTime::operator<= (const CSrvTime& t) const
255 {
256 return Compare(t) <= 0;
257 }
258
259
260 inline CSrvTime
GetStartTime(void)261 CTaskServer::GetStartTime(void)
262 {
263 return s_SrvStartTime;
264 }
265
266 inline TSrvThreadNum
GetMaxRunningThreads(void)267 CTaskServer::GetMaxRunningThreads(void)
268 {
269 return s_MaxRunningThreads;
270 }
271
272 inline bool
IsRunning(void)273 CTaskServer::IsRunning(void)
274 {
275 return s_SrvState == eSrvRunning;
276 }
277
278 inline bool
IsInShutdown(void)279 CTaskServer::IsInShutdown(void)
280 {
281 return s_SrvState >= eSrvShuttingDownSoft;
282 }
283
284 inline bool
IsInSoftShutdown(void)285 CTaskServer::IsInSoftShutdown(void)
286 {
287 return s_SrvState == eSrvShuttingDownSoft;
288 }
289
290 inline bool
IsInHardShutdown(void)291 CTaskServer::IsInHardShutdown(void)
292 {
293 return s_SrvState >= eSrvShuttingDownHard;
294 }
295
296 inline bool
IsServerStopping(void)297 IsServerStopping(void)
298 {
299 return s_SrvState >= eSrvStopping;
300 }
301
302
303 inline const CSrvDiagMsg&
operator <<(const char * str) const304 CSrvDiagMsg::operator<< (const char* str) const
305 {
306 return operator<< (CTempString(str));
307 }
308
309 inline const CSrvDiagMsg&
operator <<(const string & str) const310 CSrvDiagMsg::operator<< (const string& str) const
311 {
312 return operator<< (CTempString(str));
313 }
314
315 inline const CSrvDiagMsg&
operator <<(Int4 num) const316 CSrvDiagMsg::operator<< (Int4 num) const
317 {
318 return operator<< (Int8(num));
319 }
320
321 inline const CSrvDiagMsg&
operator <<(Uint4 num) const322 CSrvDiagMsg::operator<< (Uint4 num) const
323 {
324 return operator<< (Uint8(num));
325 }
326
327 inline CSrvDiagMsg&
PrintParam(CTempString name,Int4 value)328 CSrvDiagMsg::PrintParam(CTempString name, Int4 value)
329 {
330 return PrintParam(name, Int8(value));
331 }
332
333 inline CSrvDiagMsg&
PrintParam(CTempString name,Uint4 value)334 CSrvDiagMsg::PrintParam(CTempString name, Uint4 value)
335 {
336 return PrintParam(name, Uint8(value));
337 }
338
339
340 inline Uint4
GetPriority(void)341 CSrvTask::GetPriority(void)
342 {
343 return m_Priority;
344 }
345
346 inline void
SetPriority(Uint4 prty)347 CSrvTask::SetPriority(Uint4 prty)
348 {
349 m_Priority = prty;
350 }
351
352 inline CRequestContext*
GetDiagCtx(void)353 CSrvTask::GetDiagCtx(void)
354 {
355 return m_DiagCtx;
356 }
357
358
359 inline bool
IsReadDataAvailable(void)360 CSrvSocketTask::IsReadDataAvailable(void)
361 {
362 return m_RdSize > m_RdPos;
363 }
364
365 inline bool
IsWriteDataPending(void)366 CSrvSocketTask::IsWriteDataPending(void)
367 {
368 return m_WrPos < m_WrSize;
369 }
370
371 inline bool
HasError(void)372 CSrvSocketTask::HasError(void)
373 {
374 if (m_RegError) {
375 x_PrintError();
376 return true;
377 }
378 return false;
379 }
380
381 inline bool
CanHaveMoreRead(void)382 CSrvSocketTask::CanHaveMoreRead(void)
383 {
384 return m_SockCanReadMore;
385 }
386
387 inline bool
NeedToClose(void)388 CSrvSocketTask::NeedToClose(void)
389 {
390 return m_NeedToClose || CTaskServer::IsInHardShutdown();
391 }
392
393 inline bool
NeedEarlyClose(void)394 CSrvSocketTask::NeedEarlyClose(void)
395 {
396 return NeedToClose() || HasError() || !CanHaveMoreRead();
397 }
398
399 inline CSrvSocketTask&
WriteText(CTempString message)400 CSrvSocketTask::WriteText(CTempString message)
401 {
402 WriteData(message.data(), message.size());
403 return *this;
404 }
405
406 template <typename NumType>
407 inline CSrvSocketTask&
WriteNumber(NumType num)408 CSrvSocketTask::WriteNumber(NumType num)
409 {
410 return WriteText(NStr::NumericToString(num));
411 }
412
413 inline CSrvSocketTask&
WriteBool(bool b)414 CSrvSocketTask::WriteBool(bool b)
415 {
416 return WriteText(b ? "true" : "false");
417 }
418
419 inline void
RequestFlush(void)420 CSrvSocketTask::RequestFlush(void)
421 {
422 m_FlushIsDone = false;
423 ACCESS_ONCE(m_NeedToFlush) = true;
424 SetRunnable();
425 }
426
427 inline bool
FlushIsDone(void)428 CSrvSocketTask::FlushIsDone(void)
429 {
430 return ACCESS_ONCE(m_FlushIsDone);
431 }
432
433 inline bool
ReadData(void * buf,Uint2 size)434 CSrvSocketTask::ReadData(void* buf, Uint2 size)
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
ReadNumber(NumType * num)448 CSrvSocketTask::ReadNumber(NumType* num)
449 {
450 return ReadData(num, sizeof(*num));
451 }
452
453 inline bool
IsProxyInProgress(void)454 CSrvSocketTask::IsProxyInProgress(void)
455 {
456 return m_ProxySrc || m_ProxyDst;
457 }
458
459 inline bool
ProxyHadError(void)460 CSrvSocketTask::ProxyHadError(void)
461 {
462 return m_ProxyHadError;
463 }
464
465 inline void
CloseSocket(void)466 CSrvSocketTask::CloseSocket(void)
467 {
468 x_CloseSocket(false);
469 }
470
471 inline void
AbortSocket(void)472 CSrvSocketTask::AbortSocket(void)
473 {
474 x_CloseSocket(true);
475 }
476
477
478 inline bool
IsTransStateFinal(void)479 CSrvTransitionTask::IsTransStateFinal(void)
480 {
481 return m_TransState == eState_Final;
482 }
483
484
485 inline bool
IsTransFinished(void)486 CSrvTransConsumer::IsTransFinished(void)
487 {
488 return m_TransFinished;
489 }
490
491
492 END_NCBI_SCOPE
493
494 #endif /* NETCACHE__SRV_INLINES__HPP */
495