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>
 
  25 #include "stg/admin.h"
 
  26 #include "stg/common.h"
 
  27 #include "services_impl.h"
 
  29 //-----------------------------------------------------------------------------
 
  30 SERVICES_IMPL::SERVICES_IMPL(STORE * st)
 
  34       WriteServLog(GetStgLogger()),
 
  40 pthread_mutex_init(&mutex, NULL);
 
  43 //-----------------------------------------------------------------------------
 
  44 int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin)
 
  46 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
  47 const PRIV * priv = admin->GetPriv();
 
  49 if (!priv->serviceChg)
 
  51     std::string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied.";
 
  52     strError = "Access denied.";
 
  53     WriteServLog(s.c_str());
 
  57 srv_iter si(find(data.begin(), data.end(), service));
 
  61     strError = "Service \'" + service.name + "\' cannot not be added. Service already exist.";
 
  62     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
 
  67 data.push_back(service);
 
  69 if (store->AddService(service.name) == 0)
 
  71     WriteServLog("%s Service \'%s\' added.",
 
  72                  admin->GetLogStr().c_str(), service.name.c_str());
 
  76 strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
 
  77 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
 
  81 //-----------------------------------------------------------------------------
 
  82 int SERVICES_IMPL::Del(const std::string & name, const ADMIN * admin)
 
  84 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
  85 const PRIV * priv = admin->GetPriv();
 
  87 if (!priv->serviceChg)
 
  89     std::string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied.";
 
  90     strError = "Access denied.";
 
  91     WriteServLog(s.c_str());
 
  95 srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
 
  99     strError = "Service \'" + name + "\' cannot be deleted. Service does not exist.";
 
 100     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
 
 104 std::map<int, const_srv_iter>::iterator csi;
 
 105 csi = searchDescriptors.begin();
 
 106 while (csi != searchDescriptors.end())
 
 108     if (csi->second == si)
 
 114 if (store->DelService(name) < 0)
 
 116     strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
 
 117     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
 
 122 WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
 
 125 //-----------------------------------------------------------------------------
 
 126 int SERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN * admin)
 
 128 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 129 const PRIV * priv = admin->GetPriv();
 
 131 if (!priv->serviceChg)
 
 133     std::string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied.";
 
 134     strError = "Access denied.";
 
 135     WriteServLog(s.c_str());
 
 139 srv_iter si(find(data.begin(), data.end(), service));
 
 141 if (si == data.end())
 
 143     strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist.";
 
 144     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
 
 149 if (store->SaveService(service))
 
 151     WriteServLog("Cannot write service %s.", service.name.c_str());
 
 152     WriteServLog("%s", store->GetStrError().c_str());
 
 156 WriteServLog("%s Service \'%s\' changed.",
 
 157              admin->GetLogStr().c_str(), service.name.c_str());
 
 161 //-----------------------------------------------------------------------------
 
 162 bool SERVICES_IMPL::Read()
 
 164 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 165 std::vector<std::string> servicesList;
 
 166 if (store->GetServicesList(&servicesList) < 0)
 
 168     WriteServLog(store->GetStrError().c_str());
 
 172 for (size_t i = 0; i < servicesList.size(); i++)
 
 174     SERVICE_CONF service;
 
 176     if (store->RestoreService(&service, servicesList[i]))
 
 178         WriteServLog(store->GetStrError().c_str());
 
 182     data.push_back(service);
 
 186 //-----------------------------------------------------------------------------
 
 187 bool SERVICES_IMPL::Find(const std::string & name, SERVICE_CONF * service)
 
 189 assert(service != NULL && "Pointer to service is not null");
 
 191 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 195 srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
 
 197 if (si != data.end())
 
 205 //-----------------------------------------------------------------------------
 
 206 bool SERVICES_IMPL::Exists(const std::string & name) const
 
 208 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 211     printfd(__FILE__, "no admin in system!\n");
 
 215 const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name)));
 
 217 if (si != data.end())
 
 222 //-----------------------------------------------------------------------------
 
 223 int SERVICES_IMPL::OpenSearch() const
 
 225 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 227 searchDescriptors[handle] = data.begin();
 
 230 //-----------------------------------------------------------------------------
 
 231 int SERVICES_IMPL::SearchNext(int h, SERVICE_CONF * service) const
 
 233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 234 if (searchDescriptors.find(h) == searchDescriptors.end())
 
 236     WriteServLog("SERVICES. Incorrect search handle.");
 
 240 if (searchDescriptors[h] == data.end())
 
 243 *service = *searchDescriptors[h]++;
 
 247 //-----------------------------------------------------------------------------
 
 248 int SERVICES_IMPL::CloseSearch(int h) const
 
 250 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 251 if (searchDescriptors.find(h) != searchDescriptors.end())
 
 253     searchDescriptors.erase(searchDescriptors.find(h));
 
 257 WriteServLog("SERVICES. Incorrect search handle.");
 
 260 //-----------------------------------------------------------------------------