1 #ifndef PSGS_OSGCONNECTION__HPP
2 #define PSGS_OSGCONNECTION__HPP
3 
4 /*  $Id: osg_connection.hpp 629837 2021-04-22 12:47:49Z 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  * Authors: Eugene Vasilchenko
30  *
31  * File Description: class for communication with OSG
32  *
33  */
34 
35 #include <corelib/ncbiobj.hpp>
36 #include <corelib/ncbimtx.hpp>
37 #include <corelib/ncbi_pool_balancer.hpp>
38 #include <list>
39 
40 BEGIN_NCBI_NAMESPACE;
41 
42 class CNcbiRegistry;
43 class CArgs;
44 class CConn_IOStream;
45 
46 BEGIN_NAMESPACE(objects);
47 
48 class CID2_Request;
49 class CID2_Request_Packet;
50 class CID2_Reply;
51 
52 END_NAMESPACE(objects);
53 
54 BEGIN_NAMESPACE(psg);
55 BEGIN_NAMESPACE(osg);
56 
57 USING_SCOPE(objects);
58 
59 class COSGConnectionPool;
60 class COSGServiceMapper;
61 
62 class COSGConnection : public CObject
63 {
64 public:
65     virtual ~COSGConnection();
66 
GetConnectionID() const67     size_t GetConnectionID() const
68         {
69             return m_ConnectionID;
70         }
71 
GetNextRequestSerialNumber() const72     int GetNextRequestSerialNumber() const
73         {
74             return m_RequestCount;
75         }
AllocateRequestSerialNumber()76     int AllocateRequestSerialNumber()
77         {
78             return m_RequestCount++;
79         }
80 
InitRequestWasSent() const81     bool InitRequestWasSent() const
82         {
83             return m_InitRequestWasSent;
84         }
85 
86     void SendRequestPacket(const CID2_Request_Packet& packet);
87     CRef<CID2_Reply> ReceiveReply();
88 
89     static CRef<CID2_Request> MakeInitRequest();
90     CRef<CID2_Request_Packet> MakeInitRequestPacket();
91 
92     double UpdateTimestamp();
93 
94     void AcceptFeedback(int feedback);
95 
96 protected:
97     friend class COSGConnectionPool;
98 
99     COSGConnection(size_t connection_id);
100     COSGConnection(size_t connection_id,
101                    unique_ptr<CConn_IOStream>&& stream);
102 
103 private:
104     size_t m_ConnectionID;
105     CRef<COSGConnectionPool> m_RemoveFrom;
106     string m_ServiceName;
107     TSvrRef m_ServerInfo;
108     unique_ptr<CConn_IOStream> m_Stream;
109     int m_RequestCount;
110     bool m_InitRequestWasSent;
111     CStopWatch m_Timestamp;
112 };
113 
114 
115 class COSGConnectionPool : public CObject
116 {
117 public:
118     COSGConnectionPool();
119     virtual ~COSGConnectionPool();
120 
121     void AppParseArgs(const CArgs& args);
122     void LoadConfig(const CNcbiRegistry& registry, string section = string());
123     void SetLogging(EDiagSev severity);
124 
GetMaxConnectionCount() const125     size_t GetMaxConnectionCount() const {
126         return size_t(m_MaxConnectionCount);
127     }
GetRetryCount() const128     size_t GetRetryCount() const {
129         return size_t(m_RetryCount);
130     }
131 
GetEnabledCDD() const132     bool GetEnabledCDD() const {
133         return m_EnabledCDD;
134     }
GetEnabledSNP() const135     bool GetEnabledSNP() const {
136         return m_EnabledSNP;
137     }
GetEnabledWGS() const138     bool GetEnabledWGS() const {
139         return m_EnabledWGS;
140     }
141 
GetCDDRetryTimeout() const142     double GetCDDRetryTimeout() const {
143         return m_CDDRetryTimeout;
144     }
145 
146     CRef<COSGConnection> AllocateConnection();
147     void ReleaseConnection(CRef<COSGConnection>& conn);
148 
149 protected:
150     friend class COSGConnection;
151 
152     void RemoveConnection(COSGConnection& conn);
153 
154     void x_OpenConnection(COSGConnection& conn);
155     TSvrRef x_GetServer();
156 
157 private:
158     string m_ServiceName;
159     int m_MaxConnectionCount;
160     double m_ExpirationTimeout;
161     double m_ReadTimeout;
162     double m_CDDRetryTimeout;
163     int m_RetryCount;
164     bool m_EnabledCDD;
165     bool m_EnabledSNP;
166     bool m_EnabledWGS;
167     CMutex m_Mutex;
168     CSemaphore m_WaitConnectionSlot;
169     size_t m_NextConnectionID;
170     int m_ConnectionCount;
171     int m_ConnectFailureCount;
172     list<CRef<COSGConnection>> m_FreeConnections;
173     CRef<COSGServiceMapper> m_Mapper;
174     CRef<CPoolBalancer>  m_Balancer;
175     unique_ptr<CDeadline>  m_NonresolutionRetryDeadline;
176 };
177 
178 
179 END_NAMESPACE(osg);
180 END_NAMESPACE(psg);
181 END_NCBI_NAMESPACE;
182 
183 #endif  // PSGS_OSGCONNECTION__HPP
184