1 #ifndef CONN___NETCACHE_RW__HPP
2 #define CONN___NETCACHE_RW__HPP
3
4 /* $Id: netcache_rw.hpp 574016 2018-11-05 16:55:15Z sadyrovr $
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: Maxim Didenko, Dmitry Kazimirov
30 *
31 * File Description:
32 * Net cache client API.
33 *
34 */
35
36 /// @file netcache_rw.hpp
37 /// NetCache client specs.
38 ///
39
40 #include "netservice_api_impl.hpp"
41 #include "netcache_params.hpp"
42
43 #include <connect/ncbi_conn_reader_writer.hpp>
44
45 #include <util/transmissionrw.hpp>
46
47 #include <limits>
48
49 BEGIN_NCBI_SCOPE
50
51
52 /** @addtogroup NetCacheClient
53 *
54 * @{
55 */
56
57 #if SIZEOF_SIZE_T < 8
CheckBlobSize(Uint8 blob_size)58 inline size_t CheckBlobSize(Uint8 blob_size)
59 {
60 if (blob_size > std::numeric_limits<std::size_t>::max()) {
61 NCBI_THROW(CNetCacheException, eBlobClipped, "Blob is too big");
62 }
63 return (size_t) blob_size;
64 }
65 #else
66 #define CheckBlobSize(blob_size) ((size_t) (blob_size))
67 #endif
68
69
70 class NCBI_XCONNECT_EXPORT CNetCacheReader : public IReader
71 {
72 public:
73 CNetCacheReader(SNetCacheAPIImpl* impl,
74 const string& blob_id,
75 CNetServer::SExecResult& exec_result,
76 size_t* blob_size_ptr,
77 const CNetCacheAPIParameters* parameters);
78
79 virtual ~CNetCacheReader();
80
81 virtual ERW_Result Read(void* buf, size_t count,
82 size_t* bytes_read_ptr = 0);
83
84 virtual ERW_Result PendingCount(size_t* count);
85
86 virtual void Close();
87
GetBlobID() const88 const string& GetBlobID() const {return m_BlobID;}
89
GetBlobSize() const90 Uint8 GetBlobSize() const {return m_BlobSize;}
91
Eof() const92 bool Eof() const {return m_BlobBytesToRead == 0;}
93
94 private:
95 void SocketRead(void* buf, size_t count, size_t* bytes_read);
96
97 string m_BlobID;
98
99 CNetServerConnection m_Connection;
100 Uint8 m_BlobSize;
101 Uint8 m_BlobBytesToRead; // Remaining number of bytes to be read
102
103 CFileIO m_CacheFile;
104 bool m_CachingEnabled;
105 };
106
107 ///////////////////////////////////////////////////////////////////////////////
108 //
109
110 enum ENetCacheResponseType {
111 eNetCache_Wait,
112 eICache_NoWait,
113 };
114
115 class NCBI_XCONNECT_EXPORT CNetCacheWriter : public IEmbeddedStreamWriter
116 {
117 public:
118 CNetCacheWriter(SNetCacheAPIImpl* impl,
119 string* blob_id,
120 const string& key,
121 ENetCacheResponseType response_type,
122 const CNetCacheAPIParameters* parameters);
123
124 virtual ~CNetCacheWriter();
125
126 virtual ERW_Result Write(const void* buf,
127 size_t count,
128 size_t* bytes_written = 0);
129
130 /// Flush pending data (if any) down to the output device.
131 virtual ERW_Result Flush(void);
132
133 virtual void Close();
134 virtual void Abort();
135
136 void WriteBufferAndClose(const char* buf_ptr, size_t buf_size);
137
GetResponseType() const138 ENetCacheResponseType GetResponseType() const {return m_ResponseType;}
139
GetBlobID() const140 const string& GetBlobID() const {return m_BlobID;}
GetKey() const141 const string& GetKey() const {return m_Key;}
142
SetBlobID(const string & blob_id)143 void SetBlobID(const string& blob_id) {m_BlobID = blob_id;}
144
145 private:
IsConnectionOpen()146 bool IsConnectionOpen() { return m_TransmissionWriter.get() != NULL; }
147 void ResetWriters();
148 void AbortConnection();
149 EIO_Status TransmitImpl(const char* buf, size_t count);
150 void Transmit(const void* buf, size_t count, size_t* bytes_written);
151
152 void EstablishConnection();
153 void UploadCacheFile();
154
155 CNetServerConnection m_Connection;
156 unique_ptr<CSocketReaderWriter> m_SocketReaderWriter;
157 unique_ptr<CTransmissionWriter> m_TransmissionWriter;
158 ENetCacheResponseType m_ResponseType;
159
160 CNetCacheAPI m_NetCacheAPI;
161 string m_BlobID;
162 string m_Key; // Only for ICache mode.
163 const CNetCacheAPIParameters* m_Parameters;
164
165 CFileIO m_CacheFile;
166 bool m_CachingEnabled;
167 };
168
169 /* @} */
170
171
172 END_NCBI_SCOPE
173
174 #endif /* CONN___NETCACHE_RW__HPP */
175