1 #ifndef NETSTORAGE_SERVER__HPP
2 #define NETSTORAGE_SERVER__HPP
3 
4 /*  $Id: nst_server.hpp 515877 2016-10-06 14:29:20Z satskyse $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors:  Denis Vakatov
30  *
31  * File Description: NetStorage threaded server
32  *
33  */
34 
35 #include <string>
36 #include <connect/services/compound_id.hpp>
37 #include <connect/server.hpp>
38 
39 
40 #include "nst_server_parameters.hpp"
41 #include "nst_precise_time.hpp"
42 #include "nst_alert.hpp"
43 #include "nst_database.hpp"
44 #include "nst_service_parameters.hpp"
45 #include "nst_clients.hpp"
46 #include "nst_users.hpp"
47 
48 
49 BEGIN_NCBI_SCOPE
50 
51 
52 class CNetStorageServiceThread;
53 
54 
55 class CNetStorageServer : public CServer
56 {
57 public:
58     CNetStorageServer();
59     virtual ~CNetStorageServer();
60 
61     void AddDefaultListener(IServer_ConnectionFactory *  factory);
62     CJsonNode SetParameters(const SNetStorageServerParameters &  new_params,
63                             bool                                 reconfigure);
64 
65     virtual bool ShutdownRequested(void);
66     void SetShutdownFlag(int signum = 0);
67 
IsLog() const68     const bool &  IsLog() const
69     { return m_Log; }
IsLogTimingNSTAPI() const70     bool  IsLogTimingNSTAPI() const
71     { return m_LogTimingNSTAPI; }
IsLogTimingClientSocket() const72     bool  IsLogTimingClientSocket() const
73     { return m_LogTimingClientSocket; }
74 
GetNetworkTimeout(void) const75     unsigned int  GetNetworkTimeout(void) const
76     { return m_NetworkTimeout; }
GetPort() const77     unsigned short  GetPort() const
78     { return m_Port; }
GetHostNetAddr() const79     unsigned int  GetHostNetAddr() const
80     { return m_HostNetAddr; }
GetStartTime(void) const81     const CNSTPreciseTime &  GetStartTime(void) const
82     { return m_StartTime; }
GetSessionID(void) const83     string  GetSessionID(void) const
84     { return m_SessionID; }
SaveCommandLine(const string & cmd_line)85     void SaveCommandLine(const string &  cmd_line)
86     { m_CommandLine = cmd_line; }
GetCommandLine(void) const87     string GetCommandLine(void) const
88     { return m_CommandLine; }
GetCompoundIDPool(void) const89     CCompoundIDPool GetCompoundIDPool(void) const
90     { return m_CompoundIDPool; }
GetRAMConfigFileChecksum(void) const91     string GetRAMConfigFileChecksum(void) const
92     { return m_RAMConfigFileChecksum; }
GetDiskConfigFileChecksum(void) const93     string GetDiskConfigFileChecksum(void) const
94     { return m_DiskConfigFileChecksum; }
SetDiskConfigFileChecksum(const string & checksum)95     void SetDiskConfigFileChecksum(const string &  checksum)
96     { m_DiskConfigFileChecksum = checksum; }
SetRAMConfigFileChecksum(const string & checksum)97     void SetRAMConfigFileChecksum(const string &  checksum)
98     { m_RAMConfigFileChecksum = checksum; }
GetClientRegistry(void)99     CNSTClientRegistry &  GetClientRegistry(void)
100     { return m_ClientRegistry; }
GetUserCache(void)101     CNSTUserCache &  GetUserCache(void)
102     { return m_UserCache; }
SetAnybodyCanReconfigure(bool val)103     void SetAnybodyCanReconfigure(bool  val)
104     { m_AnybodyCanReconfigure = val; }
AnybodyCanReconfigure(void) const105     bool AnybodyCanReconfigure(void) const
106     { return m_AnybodyCanReconfigure; }
GetBackendConfiguration(void) const107     CJsonNode GetBackendConfiguration(void) const
108     { return m_BackendConfiguration; }
SetBackendConfiguration(const CJsonNode & conf)109     void SetBackendConfiguration(const CJsonNode &  conf)
110     { m_BackendConfiguration = conf; }
111 
112     void UpdateBackendConfiguration(const IRegistry &  reg,
113                                     vector<string> &  config_warnings);
114     CJsonNode GetBackendConfDiff(const CJsonNode &  conf) const;
115 
116     bool IsAdminClientName(const string &  name) const;
117 
118     enum EAlertAckResult AcknowledgeAlert(const string &  id,
119                                           const string &  user);
120     enum EAlertAckResult AcknowledgeAlert(EAlertType  alert_type,
121                                           const string &  user);
122     void RegisterAlert(EAlertType  alert_type,
123                        const string &  messge);
124     CJsonNode SerializeAlerts(void) const;
125     CNSTDatabase &  GetDb(void);
126 
127     // Metainfo support
128     EServiceMetadataPresence InMetadataServices(const string &  service) const;
129     bool GetServiceTTL(const string &            service,
130                        TNSTDBValue<CTimeSpan> &  ttl) const;
131     bool GetServiceProlongOnRead(
132                     const string &  service,
133                     const TNSTDBValue<Int8> &  individual_obj_ttl,
134                     TNSTDBValue<CTimeSpan> &  prolong_on_read) const;
135     bool GetServiceProlongOnWrite(
136                     const string &  service,
137                     const TNSTDBValue<Int8> &  individual_obj_ttl,
138                     TNSTDBValue<CTimeSpan> &  prolong_on_write) const;
139     bool GetServiceProperties(const string &  service,
140                               CNSTServiceProperties &  service_props) const;
141     CJsonNode ReadMetadataConfiguration(const IRegistry &  reg);
142     CJsonNode SerializeMetadataInfo(void) const;
143 
144     void RunServiceThread(void);
145     void StopServiceThread(void);
146 
147     static CNetStorageServer *  GetInstance(void);
148 
149 protected:
150     virtual void Exit();
151 
152 private:
153     unsigned short              m_Port;
154     unsigned int                m_HostNetAddr;
155     mutable bool                m_Shutdown;
156     int                         m_SigNum;
157     bool                        m_Log;
158     bool                        m_LogTimingNSTAPI;
159     bool                        m_LogTimingClientSocket;
160     string                      m_SessionID;
161     unsigned int                m_NetworkTimeout;
162     string                      m_DataPath;
163     CNSTPreciseTime             m_StartTime;
164     string                      m_CommandLine;
165     CCompoundIDPool             m_CompoundIDPool;
166     CNSTAlerts                  m_Alerts;
167     auto_ptr<CNSTDatabase>      m_Db;   // Access to NST attributes DB
168 
169     static CNetStorageServer *  sm_netstorage_server;
170 
171     // Administrators
172     mutable CFastMutex          m_AdminClientNamesLock;
173     set<string>                 m_AdminClientNames;
174 
175     // Metadata services support
176     CNSTServiceRegistry         m_MetadataServiceRegistry;
177 
178     string                      m_RAMConfigFileChecksum;
179     string                      m_DiskConfigFileChecksum;
180 
181     CRef<CNetStorageServiceThread>
182                                 m_ServiceThread;
183 
184     CNSTClientRegistry          m_ClientRegistry;
185     CNSTUserCache               m_UserCache;
186 
187     bool                        m_AnybodyCanReconfigure;
188 
189     CJsonNode                   m_BackendConfiguration;
190 
191     // Filetrack API keys support - decrypt may fail
192 private:
193     bool                        m_NeedDecryptCacheReset;
194     CNSTPreciseTime             m_LastDecryptCacheReset;
195 
196 public:
197     void  ResetDecryptCacheIfNeed(void);
198     void  RegisterNetStorageAPIDecryptError(const string &  message);
199     void  ReportNetStorageAPIDecryptSuccess(void);
200 
201     void  CheckStartAfterCrash(void);
202     string  CreateCrashFlagFile(void);
203     string  RemoveCrashFlagFile(void);
204 
205 private:
206     string  x_GenerateGUID(void) const;
207     set<string> x_GetAdminClientNames(const string &  client_names);
208     CJsonNode  x_diffInJson(const vector<string> &  added,
209                             const vector<string> &  deleted) const;
210     string  x_GetCrashFlagFileName(void) const;
211     bool    x_DoesCrashFlagFileExist(void) const;
212 };
213 
214 
215 END_NCBI_SCOPE
216 
217 #endif
218 
219