]> git.stg.codes - stg.git/blob - projects/rlm_stg/stg_client.cpp
Moved all sync/async boundary code into the client.
[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/common.h"
27
28 #include <map>
29 #include <utility>
30
31 using STG::RLM::Client;
32 using STG::RLM::Conn;
33 using STG::RLM::RESULT;
34
35 namespace {
36
37 Client* stgClient = NULL;
38
39 }
40
41 class Client::Impl
42 {
43     public:
44         Impl(const std::string& address);
45         ~Impl();
46
47         bool stop() { return m_conn->stop(); }
48
49         RESULT request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs);
50
51     private:
52         std::string m_address;
53         boost::scoped_ptr<Conn> m_conn;
54
55         pthread_mutex_t m_mutex;
56         pthread_cond_t m_cond;
57         bool m_done;
58         RESULT m_result;
59         bool m_status;
60
61         static bool callback(void* data, const RESULT& result, bool status)
62         {
63             Impl& impl = *static_cast<Impl*>(data);
64             pthread_mutex_lock(&impl.m_mutex);
65             impl.m_result = result;
66             impl.m_status = status;
67             impl.m_done = true;
68             pthread_cond_signal(&impl.m_cond);
69             pthread_mutex_unlock(&impl.m_mutex);
70             return true;
71         }
72 };
73
74 Client::Impl::Impl(const std::string& address)
75     : m_address(address),
76       m_conn(new Conn(m_address, &Impl::callback, this))
77 {
78     pthread_mutex_init(&m_mutex, NULL);
79     pthread_cond_init(&m_cond, NULL);
80     m_done = false;
81 }
82
83 Client::Impl::~Impl()
84 {
85     pthread_cond_destroy(&m_cond);
86     pthread_mutex_destroy(&m_mutex);
87 }
88
89 RESULT Client::Impl::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
90 {
91     pthread_mutex_lock(&m_mutex);
92     if (!m_conn->connected())
93         m_conn.reset(new Conn(m_address, &Impl::callback, this));
94     if (!m_conn->connected())
95         throw Conn::Error("Failed to create connection to '" + m_address + "'.");
96
97     m_done = false;
98     m_conn->request(type, userName, password, pairs);
99     timespec ts;
100     clock_gettime(CLOCK_REALTIME, &ts);
101     ts.tv_sec += 5;
102     int res = 0;
103     while (!m_done && res == 0)
104         res = pthread_cond_timedwait(&m_cond, &m_mutex, &ts);
105     pthread_mutex_unlock(&m_mutex);
106     if (res != 0 || !m_status)
107         throw Conn::Error("Request failed.");
108     return m_result;
109 }
110
111 Client::Client(const std::string& address)
112     : m_impl(new Impl(address))
113 {
114 }
115
116 Client::~Client()
117 {
118 }
119
120 bool Client::stop()
121 {
122     return m_impl->stop();
123 }
124
125 RESULT Client::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
126 {
127     return m_impl->request(type, userName, password, pairs);
128 }
129
130 Client* Client::get()
131 {
132     return stgClient;
133 }
134
135 bool Client::configure(const std::string& address)
136 {
137     if ( stgClient != NULL )
138         return stgClient->configure(address);
139     try {
140         stgClient = new Client(address);
141         return true;
142     } catch (const std::exception& ex) {
143         RadLog("Client configuration error: %s.", ex.what());
144     }
145     return false;
146 }