NCBI C++ ToolKit
request_ctx.cpp
Go to the documentation of this file.
00001 /*  $Id: request_ctx.cpp 51720 2011-10-26 17:00:23Z lavr $
00002  * ===========================================================================
00003  *
00004  *                            PUBLIC DOMAIN NOTICE
00005  *               National Center for Biotechnology Information
00006  *
00007  *  This software/database is a "United States Government Work" under the
00008  *  terms of the United States Copyright Act.  It was written as part of
00009  *  the author's official duties as a United States Government employee and
00010  *  thus cannot be copyrighted.  This software/database is freely available
00011  *  to the public for use. The National Library of Medicine and the U.S.
00012  *  Government have not placed any restriction on its use or reproduction.
00013  *
00014  *  Although all reasonable efforts have been taken to ensure the accuracy
00015  *  and reliability of the software and data, the NLM and the U.S.
00016  *  Government do not and cannot warrant the performance or results that
00017  *  may be obtained by using this software or data. The NLM and the U.S.
00018  *  Government disclaim all warranties, express or implied, including
00019  *  warranties of performance, merchantability or fitness for any particular
00020  *  purpose.
00021  *
00022  *  Please cite the author in any work or product based on this material.
00023  *
00024  * ===========================================================================
00025  *
00026  * Authors:  Aleksey Grichenko, Denis Vakatov
00027  *
00028  * File Description:
00029  *   Request context for diagnostic framework.
00030  *
00031  */
00032 
00033 
00034 #include <ncbi_pch.hpp>
00035 
00036 #include <corelib/request_ctx.hpp>
00037 
00038 BEGIN_NCBI_SCOPE
00039 
00040 
00041 CRequestContext::CRequestContext(void)
00042     : m_RequestID(0),
00043       m_AppState(eDiagAppState_NotSet),
00044       m_ReqStatus(0),
00045       m_ReqTimer(CStopWatch::eStop),
00046       m_BytesRd(0),
00047       m_BytesWr(0),
00048       m_PropSet(0),
00049       m_IsRunning(false),
00050       m_AutoIncOnPost(false)
00051 {
00052 }
00053 
00054 
00055 CRequestContext::~CRequestContext(void)
00056 {
00057 }
00058 
00059 
00060 CRequestContext::TCount CRequestContext::GetNextRequestID(void)
00061 {
00062     static CAtomicCounter s_RequestCount;
00063     return s_RequestCount.Add(1);
00064 }
00065 
00066 
00067 const string& CRequestContext::SetHitID(void)
00068 {
00069     SetHitID(GetDiagContext().GetNextHitID());
00070     return m_HitID;
00071 }
00072 
00073 
00074 const string& CRequestContext::SetSessionID(void)
00075 {
00076     CNcbiOstrstream oss;
00077     CDiagContext& ctx = GetDiagContext();
00078     oss << ctx.GetStringUID(ctx.UpdateUID()) << '_' << setw(4) << setfill('0')
00079         << GetRequestID() << "SID";
00080     SetSessionID(CNcbiOstrstreamToString(oss));
00081     return m_SessionID.GetOriginalString();
00082 }
00083 
00084 
00085 EDiagAppState CRequestContext::GetAppState(void) const
00086 {
00087     return m_AppState != eDiagAppState_NotSet
00088         ? m_AppState : GetDiagContext().GetGlobalAppState();
00089 }
00090 
00091 
00092 void CRequestContext::SetAppState(EDiagAppState state)
00093 {
00094     m_AppState = state;
00095 }
00096 
00097 
00098 void CRequestContext::Reset(void)
00099 {
00100     m_AppState = eDiagAppState_NotSet; // Use global AppState
00101     UnsetRequestID();
00102     UnsetClientIP();
00103     UnsetSessionID();
00104     UnsetHitID();
00105     UnsetRequestStatus();
00106     UnsetBytesRd();
00107     UnsetBytesWr();
00108     m_ReqTimer.Reset();
00109 }
00110 
00111 
00112 void CRequestContext::SetProperty(const string& name, const string& value)
00113 {
00114     m_Properties[name] = value;
00115 }
00116 
00117 
00118 const string& CRequestContext::GetProperty(const string& name) const
00119 {
00120     TProperties::const_iterator it = m_Properties.find(name);
00121     return it != m_Properties.end() ? it->second : kEmptyStr;
00122 }
00123 
00124 
00125 bool CRequestContext::IsSetProperty(const string& name) const
00126 {
00127     return m_Properties.find(name) != m_Properties.end();
00128 }
00129 
00130 
00131 void CRequestContext::UnsetProperty(const string& name)
00132 {
00133     m_Properties.erase(name);
00134 }
00135 
00136 
00137 static const char* kBadIP = "0.0.0.0";
00138 
00139 
00140 void CRequestContext::SetClientIP(const string& client)
00141 {
00142     x_SetProp(eProp_ClientIP);
00143 
00144     // Verify IP
00145     if ( !NStr::IsIPAddress(client) ) {
00146         m_ClientIP = kBadIP;
00147         ERR_POST("Bad client IP value: " << client);
00148         return;
00149     }
00150 
00151     m_ClientIP = client;
00152 }
00153 
00154 
00155 void CRequestContext::StartRequest(void)
00156 {
00157     UnsetRequestStatus();
00158     SetBytesRd(0);
00159     SetBytesWr(0);
00160     GetRequestTimer().Restart();
00161     m_IsRunning = true;
00162 }
00163 
00164 
00165 void CRequestContext::StopRequest(void)
00166 {
00167     Reset();
00168     m_IsRunning = false;
00169 }
00170 
00171 
00172 bool& CRequestContext::sx_GetDefaultAutoIncRequestIDOnPost(void)
00173 {
00174     static bool s_DefaultAutoIncRequestIDOnPostFlag = false;
00175     return s_DefaultAutoIncRequestIDOnPostFlag;
00176 }
00177 
00178 
00179 void CRequestContext::SetDefaultAutoIncRequestIDOnPost(bool enable)
00180 {
00181     sx_GetDefaultAutoIncRequestIDOnPost() = enable;
00182 }
00183 
00184 
00185 bool CRequestContext::GetDefaultAutoIncRequestIDOnPost(void)
00186 {
00187     return sx_GetDefaultAutoIncRequestIDOnPost();
00188 }
00189 
00190 
00191 END_NCBI_SCOPE
Modified on Wed May 23 13:26:07 2012 by modify_doxy.py rev. 337098