1 #ifndef OBJTOOLS__PUBSEQ_GATEWAY__PSG_CLIENT_IMPL_HPP
2 #define OBJTOOLS__PUBSEQ_GATEWAY__PSG_CLIENT_IMPL_HPP
3 
4 /*  $Id: psg_client_impl.hpp 628921 2021-04-07 18:46:41Z ivanov $
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  * Author: Rafael Sadyrov
30  *
31  */
32 
33 #include <objtools/pubseq_gateway/client/impl/misc.hpp>
34 #include <objtools/pubseq_gateway/client/psg_client.hpp>
35 
36 #ifdef HAVE_PSG_CLIENT
37 
38 #include "psg_client_transport.hpp"
39 
40 #include <corelib/reader_writer.hpp>
41 #include <corelib/rwstream.hpp>
42 
43 #include <unordered_map>
44 #include <mutex>
45 
46 BEGIN_NCBI_SCOPE
47 
48 struct SPSG_BlobReader : IReader
49 {
50     SPSG_BlobReader(SPSG_Reply::SItem::TTS* src);
51 
52     ERW_Result Read(void* buf, size_t count, size_t* bytes_read = 0);
53     ERW_Result PendingCount(size_t* count);
54 
55 private:
56     void CheckForNewChunks();
57     ERW_Result x_Read(void* buf, size_t count, size_t* bytes_read);
58 
59     SPSG_Reply::SItem::TTS* m_Src;
60     vector<SPSG_Chunk> m_Data;
61     size_t m_Chunk = 0;
62     size_t m_Index = 0;
63 };
64 
65 struct SPSG_RStream : private SPSG_BlobReader, private array<char, 64 * 1024>, public CRStream
66 {
SPSG_RStreamSPSG_RStream67     SPSG_RStream(SPSG_Reply::SItem::TTS* src) :
68         SPSG_BlobReader(src),
69         CRStream(this, size(), data())
70     {}
71 };
72 
73 struct CPSG_ReplyItem::SImpl
74 {
75     SPSG_Reply::SItem::TTS* item = nullptr;
76 };
77 
78 struct CPSG_Reply::SImpl
79 {
80     shared_ptr<SPSG_Reply> reply;
81     weak_ptr<CPSG_Reply> user_reply;
82 
83     shared_ptr<CPSG_ReplyItem> Create(SPSG_Reply::SItem::TTS* item_ts);
84 
85 private:
86     template <class TReplyItem>
87     TReplyItem* CreateImpl(TReplyItem* item, const vector<SPSG_Chunk>& chunks);
88 };
89 
90 struct CPSG_Queue::SImpl : CPSG_WaitingStack<shared_ptr<CPSG_Reply>>
91 {
92     SImpl(const string& service);
93 
94     bool SendRequest(shared_ptr<const CPSG_Request> request, const CDeadline& deadline);
RejectsRequestsCPSG_Queue::SImpl95     bool RejectsRequests() const { return m_Service.ioc.RejectsRequests(); }
96 
GetApiLockCPSG_Queue::SImpl97     static TApiLock GetApiLock() { return CService::GetMap(); }
98 
99 private:
100     class CService
101     {
102         // Have to use unique_ptr as some old compilers do not use move ctor of SPSG_IoCoordinator
103         using TMap = unordered_map<string, unique_ptr<SPSG_IoCoordinator>>;
104 
105         SPSG_IoCoordinator& GetIoC(const string& service);
106 
107         shared_ptr<TMap> m_Map;
108         static pair<mutex, weak_ptr<TMap>> sm_Instance;
109 
110     public:
111         SPSG_IoCoordinator& ioc;
112 
CService(const string & service)113         CService(const string& service) : m_Map(GetMap()), ioc(GetIoC(service)) {}
114 
115         static shared_ptr<TMap> GetMap();
116     };
117 
118     string x_GetAbsPathRef(shared_ptr<const CPSG_Request> user_request);
119 
120     CService m_Service;
121 };
122 
123 END_NCBI_SCOPE
124 
125 #endif
126 #endif
127