1 #ifndef NETSCHEDULE_QUEUE_VC__HPP
2 #define NETSCHEDULE_QUEUE_VC__HPP
3 
4 
5 /*  $Id: queue_vc.hpp 485542 2015-11-23 17:36:05Z satskyse $
6  * ===========================================================================
7  *
8  *                            PUBLIC DOMAIN NOTICE
9  *               National Center for Biotechnology Information
10  *
11  *  This software/database is a "United States Government Work" under the
12  *  terms of the United States Copyright Act.  It was written as part of
13  *  the author's official duties as a United States Government employee and
14  *  thus cannot be copyrighted.  This software/database is freely available
15  *  to the public for use. The National Library of Medicine and the U.S.
16  *  Government have not placed any restriction on its use or reproduction.
17  *
18  *  Although all reasonable efforts have been taken to ensure the accuracy
19  *  and reliability of the software and data, the NLM and the U.S.
20  *  Government do not and cannot warrant the performance or results that
21  *  may be obtained by using this software or data. The NLM and the U.S.
22  *  Government disclaim all warranties, express or implied, including
23  *  warranties of performance, merchantability or fitness for any particular
24  *  purpose.
25  *
26  *  Please cite the author in any work or product based on this material.
27  *
28  * ===========================================================================
29  *
30  * Authors:  Anatoliy Kuznetsov
31  *
32  * File Description:
33  *   Net schedule queue client version control
34  *
35  */
36 /// @file queue_vc.hpp
37 /// NetSchedule queue client version control
38 ///
39 /// @internal
40 
41 #include <corelib/version.hpp>
42 #include <corelib/ncbistr.hpp>
43 
44 BEGIN_NCBI_SCOPE
45 
46 /// Netschedule queue client info
47 ///
48 /// @internal
49 ///
50 struct CQueueClientInfo
51 {
52     string         client_name;
53     CVersionInfo   version_info;
54 
CQueueClientInfoCQueueClientInfo55     CQueueClientInfo() : version_info(-1,-1,-1)
56     {}
57 
CQueueClientInfoCQueueClientInfo58     CQueueClientInfo(const string& cname, const CVersionInfo& vinfo)
59         : client_name(cname), version_info(vinfo)
60     {}
61 };
62 
63 
64 /// All clients registered to connect
65 ///
66 /// @internal
67 ///
68 class CQueueClientInfoList
69 {
70 public:
CQueueClientInfoList()71     CQueueClientInfoList()
72     {}
73 
AddClientInfo(const CQueueClientInfo & cinfo)74     void AddClientInfo(const CQueueClientInfo& cinfo)
75     {
76         CWriteLockGuard     guard(m_Lock);
77         x_AddClientInfo_NoLock(cinfo);
78     }
79 
AddClientInfo(const string & program_name)80     void AddClientInfo(const string& program_name)
81     {
82         CWriteLockGuard     guard(m_Lock);
83         list<string>        programs;
84         CQueueClientInfo    program_info;
85 
86         NStr::Split(program_name, ";,", programs,
87                     NStr::fSplit_MergeDelimiters | NStr::fSplit_Truncate);
88         ITERATE(list<string>, it, programs) {
89             const string &  vstr = *it;
90             try {
91                 ParseVersionString(vstr, &program_info.client_name,
92                                          &program_info.version_info);
93                 NStr::TruncateSpacesInPlace(program_info.client_name);
94                 x_AddClientInfo_NoLock(program_info);
95             }
96             catch (CStringException&) {
97                 ERR_POST(Warning << "Error while parsing program "
98                                     "name '" << program_name <<
99                                     "'. Program string '" << vstr <<
100                                     "'" << " cannot be parsed "
101                                     "and will be ignored.");
102             }
103         }
104     }
105 
IsMatchingClient(const CQueueClientInfo & cinfo) const106     bool IsMatchingClient(const CQueueClientInfo& cinfo) const
107     {
108         CReadLockGuard guard(m_Lock);
109 
110         if (m_RegisteredClients.empty())
111             return true;
112 
113         ITERATE(vector<CQueueClientInfo>, it, m_RegisteredClients) {
114             if (NStr::CompareNocase(cinfo.client_name, it->client_name)==0) {
115                 if (it->version_info.IsAny())
116                     return true;
117 
118                 if (cinfo.version_info.IsUpCompatible(it->version_info))
119                     return true;
120             }
121         }
122         return false;
123     }
124 
IsConfigured() const125     bool IsConfigured() const
126     {
127         CReadLockGuard guard(m_Lock);
128         return !m_RegisteredClients.empty();
129     }
130 
Clear()131     void Clear()
132     {
133         CWriteLockGuard guard(m_Lock);
134         m_RegisteredClients.resize(0);
135     }
136 
Print(const char * sep=",") const137     string Print(const char* sep=",") const
138     {
139         string s;
140         ITERATE(vector<CQueueClientInfo>, it, m_RegisteredClients) {
141             if (!s.empty())
142                 s += sep;
143 
144             s += it->client_name + ' ' + it->version_info.Print();
145         }
146         return s;
147     }
148 
149 private:
x_AddClientInfo_NoLock(const CQueueClientInfo & cinfo)150     void x_AddClientInfo_NoLock(const CQueueClientInfo& cinfo)
151     {
152         m_RegisteredClients.push_back(cinfo);
153     }
154 
155 private:
156     vector<CQueueClientInfo>   m_RegisteredClients;
157     mutable CRWLock            m_Lock;
158 };
159 
160 
161 END_NCBI_SCOPE
162 
163 #endif /* NETSCHEDULE_QUEUE_VC__HPP */
164 
165