]> git.stg.codes - stg.git/blob - stargazer/services_impl.cpp
620a6ecbe9e2c620ac327bb27014352f5186f62b
[stg.git] / stargazer / services_impl.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 "services_impl.h"
22
23 #include "stg/admin.h"
24 #include "stg/admin_conf.h"
25 #include "stg/store.h"
26 #include "stg/common.h"
27
28 #include <algorithm>
29 #include <cassert>
30
31 using STG::ServicesImpl;
32
33 //-----------------------------------------------------------------------------
34 ServicesImpl::ServicesImpl(Store * st)
35     : store(st),
36       WriteServLog(Logger::get()),
37       searchDescriptors(),
38       handle(0)
39 {
40 Read();
41 }
42 //-----------------------------------------------------------------------------
43 int ServicesImpl::Add(const ServiceConf & service, const Admin * admin)
44 {
45 std::lock_guard<std::mutex> lock(mutex);
46 const auto priv = admin->GetPriv();
47
48 if (!priv->serviceChg)
49     {
50     std::string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied.";
51     strError = "Access denied.";
52     WriteServLog(s.c_str());
53     return -1;
54     }
55
56 iterator si(std::find(data.begin(), data.end(), service));
57
58 if (si != data.end())
59     {
60     strError = "Service \'" + service.name + "\' cannot not be added. Service already exist.";
61     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
62
63     return -1;
64     }
65
66 data.push_back(service);
67
68 if (store->AddService(service.name) == 0)
69     {
70     WriteServLog("%s Service \'%s\' added.",
71                  admin->GetLogStr().c_str(), service.name.c_str());
72     return 0;
73     }
74
75 strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
76 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
77
78 return -1;
79 }
80 //-----------------------------------------------------------------------------
81 int ServicesImpl::Del(const std::string & name, const Admin * admin)
82 {
83 std::lock_guard<std::mutex> lock(mutex);
84 const auto priv = admin->GetPriv();
85
86 if (!priv->serviceChg)
87     {
88     std::string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied.";
89     strError = "Access denied.";
90     WriteServLog(s.c_str());
91     return -1;
92     }
93
94 iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
95
96 if (si == data.end())
97     {
98     strError = "Service \'" + name + "\' cannot be deleted. Service does not exist.";
99     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
100     return -1;
101     }
102
103 std::map<int, const_iterator>::iterator csi;
104 csi = searchDescriptors.begin();
105 while (csi != searchDescriptors.end())
106     {
107     if (csi->second == si)
108         (csi->second)++;
109     ++csi;
110     }
111
112 data.erase(si);
113 if (store->DelService(name) < 0)
114     {
115     strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
116     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
117
118     return -1;
119     }
120
121 WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
122 return 0;
123 }
124 //-----------------------------------------------------------------------------
125 int ServicesImpl::Change(const ServiceConf & service, const Admin * admin)
126 {
127 std::lock_guard<std::mutex> lock(mutex);
128 const auto priv = admin->GetPriv();
129
130 if (!priv->serviceChg)
131     {
132     std::string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied.";
133     strError = "Access denied.";
134     WriteServLog(s.c_str());
135     return -1;
136     }
137
138 iterator si(std::find(data.begin(), data.end(), service));
139
140 if (si == data.end())
141     {
142     strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist.";
143     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
144     return -1;
145     }
146
147 printfd(__FILE__, "Old cost = %f, old pay day = %d\n", si->cost, (unsigned)si->payDay);
148 *si = service;
149 printfd(__FILE__, "New cost = %f, New pay day = %d\n", si->cost, (unsigned)si->payDay);
150 if (store->SaveService(service))
151     {
152     WriteServLog("Cannot write service %s.", service.name.c_str());
153     WriteServLog("%s", store->GetStrError().c_str());
154     return -1;
155     }
156
157 WriteServLog("%s Service \'%s\' changed.",
158              admin->GetLogStr().c_str(), service.name.c_str());
159
160 return 0;
161 }
162 //-----------------------------------------------------------------------------
163 bool ServicesImpl::Read()
164 {
165 std::lock_guard<std::mutex> lock(mutex);
166 std::vector<std::string> servicesList;
167 if (store->GetServicesList(&servicesList) < 0)
168     {
169     WriteServLog(store->GetStrError().c_str());
170     return true;
171     }
172
173 for (size_t i = 0; i < servicesList.size(); i++)
174     {
175     ServiceConf service;
176
177     if (store->RestoreService(&service, servicesList[i]))
178         {
179         WriteServLog(store->GetStrError().c_str());
180         return true;
181         }
182
183     data.push_back(service);
184     }
185 return false;
186 }
187 //-----------------------------------------------------------------------------
188 bool ServicesImpl::Find(const std::string & name, ServiceConf * service) const
189 {
190 assert(service != NULL && "Pointer to service is not null");
191
192 std::lock_guard<std::mutex> lock(mutex);
193 if (data.empty())
194     return true;
195
196 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
197
198 if (si != data.end())
199     {
200     *service = *si;
201     return false;
202     }
203
204 return true;
205 }
206 //-----------------------------------------------------------------------------
207 bool ServicesImpl::Find(const std::string & name, ServiceConfOpt * service) const
208 {
209 assert(service != NULL && "Pointer to service is not null");
210
211 std::lock_guard<std::mutex> lock(mutex);
212 if (data.empty())
213     return true;
214
215 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
216
217 if (si != data.end())
218     {
219     *service = *si;
220     return false;
221     }
222
223 return true;
224 }
225 //-----------------------------------------------------------------------------
226 bool ServicesImpl::Exists(const std::string & name) const
227 {
228 std::lock_guard<std::mutex> lock(mutex);
229 if (data.empty())
230     {
231     printfd(__FILE__, "No services in the system!\n");
232     return true;
233     }
234
235 const_iterator si(std::find(data.begin(), data.end(), ServiceConf(name)));
236
237 if (si != data.end())
238     return true;
239
240 return false;
241 }
242 //-----------------------------------------------------------------------------
243 int ServicesImpl::OpenSearch() const
244 {
245 std::lock_guard<std::mutex> lock(mutex);
246 handle++;
247 searchDescriptors[handle] = data.begin();
248 return handle;
249 }
250 //-----------------------------------------------------------------------------
251 int ServicesImpl::SearchNext(int h, ServiceConf * service) const
252 {
253 std::lock_guard<std::mutex> lock(mutex);
254 if (searchDescriptors.find(h) == searchDescriptors.end())
255     {
256     WriteServLog("SERVICES. Incorrect search handle.");
257     return -1;
258     }
259
260 if (searchDescriptors[h] == data.end())
261     return -1;
262
263 *service = *searchDescriptors[h]++;
264
265 return 0;
266 }
267 //-----------------------------------------------------------------------------
268 int ServicesImpl::CloseSearch(int h) const
269 {
270 std::lock_guard<std::mutex> lock(mutex);
271 if (searchDescriptors.find(h) != searchDescriptors.end())
272     {
273     searchDescriptors.erase(searchDescriptors.find(h));
274     return 0;
275     }
276
277 WriteServLog("SERVICES. Incorrect search handle.");
278 return -1;
279 }
280 //-----------------------------------------------------------------------------