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 "services_impl.h"
23 #include "stg/admin.h"
24 #include "stg/admin_conf.h"
25 #include "stg/store.h"
26 #include "stg/common.h"
31 using STG::ServicesImpl;
33 //-----------------------------------------------------------------------------
34 ServicesImpl::ServicesImpl(Store * st)
36 WriteServLog(Logger::get()),
42 //-----------------------------------------------------------------------------
43 int ServicesImpl::Add(const ServiceConf & service, const Admin * admin)
45 std::lock_guard<std::mutex> lock(mutex);
46 const auto priv = admin->GetPriv();
48 if (!priv->serviceChg)
50 std::string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied.";
51 strError = "Access denied.";
52 WriteServLog(s.c_str());
56 iterator si(std::find(data.begin(), data.end(), service));
60 strError = "Service \'" + service.name + "\' cannot not be added. Service already exist.";
61 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
66 data.push_back(service);
68 if (store->AddService(service.name) == 0)
70 WriteServLog("%s Service \'%s\' added.",
71 admin->GetLogStr().c_str(), service.name.c_str());
75 strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
76 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
80 //-----------------------------------------------------------------------------
81 int ServicesImpl::Del(const std::string & name, const Admin * admin)
83 std::lock_guard<std::mutex> lock(mutex);
84 const auto priv = admin->GetPriv();
86 if (!priv->serviceChg)
88 std::string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied.";
89 strError = "Access denied.";
90 WriteServLog(s.c_str());
94 iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
98 strError = "Service \'" + name + "\' cannot be deleted. Service does not exist.";
99 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
103 std::map<int, const_iterator>::iterator csi;
104 csi = searchDescriptors.begin();
105 while (csi != searchDescriptors.end())
107 if (csi->second == si)
113 if (store->DelService(name) < 0)
115 strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
116 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
121 WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
124 //-----------------------------------------------------------------------------
125 int ServicesImpl::Change(const ServiceConf & service, const Admin * admin)
127 std::lock_guard<std::mutex> lock(mutex);
128 const auto priv = admin->GetPriv();
130 if (!priv->serviceChg)
132 std::string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied.";
133 strError = "Access denied.";
134 WriteServLog(s.c_str());
138 iterator si(std::find(data.begin(), data.end(), service));
140 if (si == data.end())
142 strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist.";
143 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
147 printfd(__FILE__, "Old cost = %f, old pay day = %d\n", si->cost, (unsigned)si->payDay);
149 printfd(__FILE__, "New cost = %f, New pay day = %d\n", si->cost, (unsigned)si->payDay);
150 if (store->SaveService(service))
152 WriteServLog("Cannot write service %s.", service.name.c_str());
153 WriteServLog("%s", store->GetStrError().c_str());
157 WriteServLog("%s Service \'%s\' changed.",
158 admin->GetLogStr().c_str(), service.name.c_str());
162 //-----------------------------------------------------------------------------
163 bool ServicesImpl::Read()
165 std::lock_guard<std::mutex> lock(mutex);
166 std::vector<std::string> servicesList;
167 if (store->GetServicesList(&servicesList) < 0)
169 WriteServLog(store->GetStrError().c_str());
173 for (size_t i = 0; i < servicesList.size(); i++)
177 if (store->RestoreService(&service, servicesList[i]))
179 WriteServLog(store->GetStrError().c_str());
183 data.push_back(service);
187 //-----------------------------------------------------------------------------
188 bool ServicesImpl::Find(const std::string & name, ServiceConf * service) const
190 assert(service != NULL && "Pointer to service is not null");
192 std::lock_guard<std::mutex> lock(mutex);
196 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
198 if (si != data.end())
206 //-----------------------------------------------------------------------------
207 bool ServicesImpl::Find(const std::string & name, ServiceConfOpt * service) const
209 assert(service != NULL && "Pointer to service is not null");
211 std::lock_guard<std::mutex> lock(mutex);
215 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
217 if (si != data.end())
225 //-----------------------------------------------------------------------------
226 bool ServicesImpl::Exists(const std::string & name) const
228 std::lock_guard<std::mutex> lock(mutex);
231 printfd(__FILE__, "No services in the system!\n");
235 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
237 if (si != data.end())
242 //-----------------------------------------------------------------------------
243 int ServicesImpl::OpenSearch() const
245 std::lock_guard<std::mutex> lock(mutex);
247 searchDescriptors[handle] = data.begin();
250 //-----------------------------------------------------------------------------
251 int ServicesImpl::SearchNext(int h, ServiceConf * service) const
253 std::lock_guard<std::mutex> lock(mutex);
254 if (searchDescriptors.find(h) == searchDescriptors.end())
256 WriteServLog("SERVICES. Incorrect search handle.");
260 if (searchDescriptors[h] == data.end())
263 *service = *searchDescriptors[h]++;
267 //-----------------------------------------------------------------------------
268 int ServicesImpl::CloseSearch(int h) const
270 std::lock_guard<std::mutex> lock(mutex);
271 if (searchDescriptors.find(h) != searchDescriptors.end())
273 searchDescriptors.erase(searchDescriptors.find(h));
277 WriteServLog("SERVICES. Incorrect search handle.");
280 //-----------------------------------------------------------------------------