1 #ifndef NETSTORAGE_CLIENTS__HPP 2 #define NETSTORAGE_CLIENTS__HPP 3 4 /* $Id: nst_clients.hpp 500203 2016-05-03 13:19:07Z 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: Sergey Satskiy 30 * 31 * File Description: 32 * NetStorage clients registry supporting facilities 33 * 34 */ 35 36 #include <string> 37 38 #include <corelib/ncbimtx.hpp> 39 #include <connect/services/json_over_uttp.hpp> 40 41 #include "nst_precise_time.hpp" 42 #include "nst_metadata_options.hpp" 43 44 45 46 BEGIN_NCBI_SCOPE 47 48 // Shared with the database wrapper code 49 const Int8 k_UndefinedClientID = -1; 50 51 52 // Note: access to the instances of this class is possible only via the client 53 // registry and the corresponding container access is always done under a lock. 54 // So it is safe to do any modifications in the members without any locks here. 55 class CNSTClient 56 { 57 public: 58 // Used for a bit mask to identify what kind of 59 // operations the client tried to perform 60 enum ENSTClientType { 61 eReader = 1, 62 eWriter = 2, 63 eAdministrator = 4 64 }; 65 66 public: 67 CNSTClient(); 68 GetRegistrationTime(void) const69 CNSTPreciseTime GetRegistrationTime(void) const 70 { return m_RegistrationTime; } GetLastAccess(void) const71 CNSTPreciseTime GetLastAccess(void) const 72 { return m_LastAccess; } RegisterSocketWriteError(void)73 void RegisterSocketWriteError(void) 74 { ++m_NumberOfSockErrors; } GetType(void) const75 unsigned int GetType(void) const 76 { return m_Type; } AppendType(unsigned int type_to_append)77 void AppendType(unsigned int type_to_append) 78 { m_Type |= type_to_append; } GetPeerAddress(void) const79 unsigned int GetPeerAddress(void) const 80 { return m_Addr; } SetApplication(const string & application)81 void SetApplication(const string & application) 82 { m_Application = application; } SetTicket(const string & ticket)83 void SetTicket(const string & ticket) 84 { m_Ticket = ticket; } SetService(const string & service)85 void SetService(const string & service) 86 { m_Service = service; } SetProtocolVersion(const string & protocol_version)87 void SetProtocolVersion(const string & protocol_version) 88 { m_ProtocolVersion = protocol_version; } SetProtocolVersionProvided(bool val)89 void SetProtocolVersionProvided(bool val ) 90 { m_ProtocolVersionProvided = val; } SetMetadataOption(EMetadataOption val)91 void SetMetadataOption(EMetadataOption val) 92 { m_MetadataOption = val; } SetPeerAddress(unsigned int peer_address)93 void SetPeerAddress(unsigned int peer_address) 94 { m_Addr = peer_address; } AddBytesWritten(size_t count)95 void AddBytesWritten(size_t count) 96 { m_NumberOfBytesWritten += count; } AddBytesRead(size_t count)97 void AddBytesRead(size_t count) 98 { m_NumberOfBytesRead += count; } AddBytesRelocated(size_t count)99 void AddBytesRelocated(size_t count) 100 { m_NumberOfBytesRelocated += count; } AddObjectsWritten(size_t count)101 void AddObjectsWritten(size_t count) 102 { m_NumberOfObjectsWritten += count; } AddObjectsRead(size_t count)103 void AddObjectsRead(size_t count) 104 { m_NumberOfObjectsRead += count; } AddObjectsRelocated(size_t count)105 void AddObjectsRelocated(size_t count) 106 { m_NumberOfObjectsRelocated += count; } AddObjectsDeleted(size_t count)107 void AddObjectsDeleted(size_t count) 108 { m_NumberOfObjectsDeleted += count; } 109 110 void Touch(void); 111 CJsonNode Serialize(void) const; 112 GetDBClientID(void) const113 Int8 GetDBClientID(void) const 114 { return m_DBClientID; } SetDBClientID(Int8 id)115 void SetDBClientID(Int8 id) 116 { m_DBClientID = id; } 117 118 private: 119 string m_Application; // Absolute exec path 120 string m_Ticket; // Optional auth ticket 121 string m_Service; // Optional service 122 EMetadataOption m_MetadataOption; // Metadata option after HELLO 123 string m_ProtocolVersion; 124 bool m_ProtocolVersionProvided; 125 126 unsigned int m_Type; // bit mask of ENSTClientType 127 unsigned int m_Addr; // Client peer address 128 129 CNSTPreciseTime m_RegistrationTime; 130 CNSTPreciseTime m_LastAccess; // The last time the client accessed 131 // netstorage 132 133 size_t m_NumberOfBytesWritten; 134 size_t m_NumberOfBytesRead; 135 size_t m_NumberOfBytesRelocated; 136 size_t m_NumberOfObjectsWritten; 137 size_t m_NumberOfObjectsRead; 138 size_t m_NumberOfObjectsRelocated; 139 size_t m_NumberOfObjectsDeleted; 140 141 size_t m_NumberOfSockErrors; 142 143 Int8 m_DBClientID; // Cached database value 144 145 string x_TypeAsString(void) const; 146 }; 147 148 149 150 class CNSTClientRegistry 151 { 152 public: 153 CNSTClientRegistry(); 154 155 size_t Size(void) const; 156 void Touch(const string & client, 157 const string & applications, 158 const string & ticket, 159 const string & service, 160 const string & protocol_version, 161 EMetadataOption metadataOption, 162 unsigned int peer_address); 163 void Touch(const string & client); 164 void RegisterSocketWriteError(const string & client); 165 void AppendType(const string & client, 166 unsigned int type_to_append); 167 void AddBytesWritten(const string & client, size_t count); 168 void AddBytesRead(const string & client, size_t count); 169 void AddBytesRelocated(const string & client, size_t count); 170 void AddObjectsWritten(const string & client, size_t count); 171 void AddObjectsRead(const string & client, size_t count); 172 void AddObjectsRelocated(const string & client, size_t count); 173 void AddObjectsDeleted(const string & client, size_t count); 174 175 CJsonNode Serialize(void) const; 176 177 Int8 GetDBClientID(const string & client) const; 178 void SetDBClientID(const string & client, Int8 id); 179 180 private: 181 typedef map< string, CNSTClient > TClients; 182 183 TClients m_Clients; // All the server clients 184 // netstorage knows about 185 mutable CMutex m_Lock; // Lock for the map 186 }; 187 188 189 190 END_NCBI_SCOPE 191 192 #endif /* NETSTORAGE_CLIENTS__HPP */ 193 194