]> git.stg.codes - stg.git/blob - projects/rlm_stg/stg_client.cpp
Declare unary ctors explicit.
[stg.git] / projects / rlm_stg / stg_client.cpp
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "stg_client.h"
22
23 #include "conn.h"
24 #include "radlog.h"
25
26 #include "stg/locker.h"
27 #include "stg/common.h"
28
29 #include <map>
30 #include <utility>
31
32 using STG::RLM::Client;
33 using STG::RLM::Conn;
34 using STG::RLM::RESULT;
35
36 namespace {
37
38 Client* stgClient = NULL;
39
40 }
41
42 class Client::Impl
43 {
44     public:
45         explicit Impl(const std::string& address);
46         ~Impl();
47
48         bool stop() { return m_conn ? m_conn->stop() : true; }
49
50         RESULT request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs);
51
52     private:
53         std::string m_address;
54         boost::scoped_ptr<Conn> m_conn;
55
56         pthread_mutex_t m_mutex;
57         pthread_cond_t m_cond;
58         bool m_done;
59         RESULT m_result;
60         bool m_status;
61
62         static bool callback(void* data, const RESULT& result, bool status)
63         {
64             Impl& impl = *static_cast<Impl*>(data);
65             STG_LOCKER lock(impl.m_mutex);
66             impl.m_result = result;
67             impl.m_status = status;
68             impl.m_done = true;
69             pthread_cond_signal(&impl.m_cond);
70             return true;
71         }
72 };
73
74 Client::Impl::Impl(const std::string& address)
75     : m_address(address)
76 {
77     try
78     {
79         m_conn.reset(new Conn(m_address, &Impl::callback, this));
80     }
81     catch (const std::runtime_error& ex)
82     {
83         RadLog("Connection error: %s.", ex.what());
84     }
85     pthread_mutex_init(&m_mutex, NULL);
86     pthread_cond_init(&m_cond, NULL);
87     m_done = false;
88 }
89
90 Client::Impl::~Impl()
91 {
92     pthread_cond_destroy(&m_cond);
93     pthread_mutex_destroy(&m_mutex);
94 }
95
96 RESULT Client::Impl::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
97 {
98     STG_LOCKER lock(m_mutex);
99     if (!m_conn || !m_conn->connected())
100         m_conn.reset(new Conn(m_address, &Impl::callback, this));
101     if (!m_conn->connected())
102         throw Conn::Error("Failed to create connection to '" + m_address + "'.");
103
104     m_done = false;
105     m_conn->request(type, userName, password, pairs);
106     timespec ts;
107     clock_gettime(CLOCK_REALTIME, &ts);
108     ts.tv_sec += 5;
109     int res = 0;
110     while (!m_done && res == 0)
111         res = pthread_cond_timedwait(&m_cond, &m_mutex, &ts);
112     if (res != 0 || !m_status)
113         throw Conn::Error("Request failed.");
114     return m_result;
115 }
116
117 Client::Client(const std::string& address)
118     : m_impl(new Impl(address))
119 {
120 }
121
122 Client::~Client()
123 {
124 }
125
126 bool Client::stop()
127 {
128     return m_impl->stop();
129 }
130
131 RESULT Client::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
132 {
133     return m_impl->request(type, userName, password, pairs);
134 }
135
136 Client* Client::get()
137 {
138     return stgClient;
139 }
140
141 bool Client::configure(const std::string& address)
142 {
143     if ( stgClient != NULL )
144         return stgClient->configure(address);
145     try {
146         stgClient = new Client(address);
147         return true;
148     } catch (const std::exception& ex) {
149         RadLog("Client configuration error: %s.", ex.what());
150     }
151     return false;
152 }