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.
 
   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.
 
  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
 
  18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  21 #include "stg_client.h"
 
  26 #include "stg/locker.h"
 
  27 #include "stg/common.h"
 
  32 using STG::RLM::Client;
 
  34 using STG::RLM::RESULT;
 
  38 Client* stgClient = NULL;
 
  45         explicit Impl(const std::string& address);
 
  48         bool stop() { return m_conn ? m_conn->stop() : true; }
 
  50         RESULT request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs);
 
  53         std::string m_address;
 
  54         boost::scoped_ptr<Conn> m_conn;
 
  56         pthread_mutex_t m_mutex;
 
  57         pthread_cond_t m_cond;
 
  61         static bool callback(void* data, const RESULT& result)
 
  63             Impl& impl = *static_cast<Impl*>(data);
 
  64             STG_LOCKER lock(impl.m_mutex);
 
  65             impl.m_result = result;
 
  67             pthread_cond_signal(&impl.m_cond);
 
  72 Client::Impl::Impl(const std::string& address)
 
  77         m_conn.reset(new Conn(m_address, &Impl::callback, this));
 
  79     catch (const std::runtime_error& ex)
 
  81         RadLog("Connection error: %s.", ex.what());
 
  83     pthread_mutex_init(&m_mutex, NULL);
 
  84     pthread_cond_init(&m_cond, NULL);
 
  90     pthread_cond_destroy(&m_cond);
 
  91     pthread_mutex_destroy(&m_mutex);
 
  94 RESULT Client::Impl::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
 
  96     STG_LOCKER lock(m_mutex);
 
  97     if (!m_conn || !m_conn->connected())
 
  98         m_conn.reset(new Conn(m_address, &Impl::callback, this));
 
  99     if (!m_conn->connected())
 
 100         throw Conn::Error("Failed to create connection to '" + m_address + "'.");
 
 103     m_conn->request(type, userName, password, pairs);
 
 105     clock_gettime(CLOCK_REALTIME, &ts);
 
 108     while (!m_done && res == 0)
 
 109         res = pthread_cond_timedwait(&m_cond, &m_mutex, &ts);
 
 111         throw Conn::Error("Request failed.");
 
 115 Client::Client(const std::string& address)
 
 116     : m_impl(new Impl(address))
 
 126     return m_impl->stop();
 
 129 RESULT Client::request(REQUEST_TYPE type, const std::string& userName, const std::string& password, const PAIRS& pairs)
 
 131     return m_impl->request(type, userName, password, pairs);
 
 134 Client* Client::get()
 
 139 bool Client::configure(const std::string& address)
 
 141     if ( stgClient != NULL )
 
 142         return stgClient->configure(address);
 
 144         stgClient = new Client(address);
 
 146     } catch (const std::exception& ex) {
 
 147         RadLog("Client configuration error: %s.", ex.what());