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