]> git.stg.codes - stg.git/blob - stargazer/tariffs_impl.cpp
Remove std::list from tariffs.
[stg.git] / stargazer / tariffs_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  *    Date: 07.11.2007
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 /*
26  $Revision: 1.9 $
27  $Date: 2010/10/07 18:43:21 $
28  $Author: faust $
29  */
30
31 #include <cassert>
32 #include <algorithm>
33 #include <vector>
34
35 #include "stg/locker.h"
36 #include "stg/logger.h"
37 #include "stg/store.h"
38 #include "stg/admin.h"
39 #include "tariffs_impl.h"
40
41 //-----------------------------------------------------------------------------
42 TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
43     : TARIFFS(),
44       tariffs(),
45       store(st),
46       WriteServLog(GetStgLogger()),
47       mutex(),
48       strError(),
49       noTariff(NO_TARIFF_NAME),
50       onAddNotifiers(),
51       onDelNotifiers()
52 {
53 pthread_mutex_init(&mutex, NULL);
54 ReadTariffs();
55 }
56 //-----------------------------------------------------------------------------
57 TARIFFS_IMPL::~TARIFFS_IMPL()
58 {
59 pthread_mutex_destroy(&mutex);
60 }
61 //-----------------------------------------------------------------------------
62 int TARIFFS_IMPL::ReadTariffs()
63 {
64 STG_LOCKER lock(&mutex);
65
66 std::vector<std::string> tariffsList;
67 if (store->GetTariffsList(&tariffsList))
68     {
69     WriteServLog("Cannot get tariffs list.");
70     WriteServLog("%s", store->GetStrError().c_str());
71     }
72
73 Tariffs::size_type tariffsNum = tariffsList.size();
74
75 for (Tariffs::size_type i = 0; i < tariffsNum; i++)
76     {
77     TARIFF_DATA td;
78     if (store->RestoreTariff(&td, tariffsList[i]))
79         {
80         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
81         WriteServLog("%s", store->GetStrError().c_str());
82         return -1;
83         }
84     tariffs.push_back(TARIFF_IMPL(td));
85     }
86
87 return 0;
88 }
89 //-----------------------------------------------------------------------------
90 size_t TARIFFS_IMPL::Count() const
91 {
92 STG_LOCKER lock(&mutex);
93 return tariffs.size();
94 }
95 //-----------------------------------------------------------------------------
96 const TARIFF * TARIFFS_IMPL::FindByName(const std::string & name) const
97 {
98 if (name == NO_TARIFF_NAME)
99     return &noTariff;
100
101 STG_LOCKER lock(&mutex);
102 const auto ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
103
104 if (ti != tariffs.end())
105     return &(*ti);
106
107 return NULL;
108 }
109 //-----------------------------------------------------------------------------
110 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
111 {
112 const PRIV * priv = admin->GetPriv();
113
114 if (!priv->tariffChg)
115     {
116     std::string s = admin->GetLogStr() + " Change tariff \'"
117                + td.tariffConf.name + "\'. Access denied.";
118     strError = "Access denied.";
119     WriteServLog(s.c_str());
120     return -1;
121     }
122
123 STG_LOCKER lock(&mutex);
124
125 auto ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
126
127 if (ti == tariffs.end())
128     {
129     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
130     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
131     return -1;
132     }
133
134 *ti = td;
135
136 if (store->SaveTariff(td, td.tariffConf.name))
137     {
138     std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
139     WriteServLog(error.c_str());
140     return -1;
141     }
142
143 WriteServLog("%s Tariff \'%s\' changed.",
144              admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
145
146 return 0;
147 }
148 //-----------------------------------------------------------------------------
149 int TARIFFS_IMPL::Del(const std::string & name, const ADMIN * admin)
150 {
151 const PRIV * priv = admin->GetPriv();
152
153 if (!priv->tariffChg)
154     {
155     std::string s = admin->GetLogStr() + " Delete tariff \'"
156                + name + "\'. Access denied.";
157     strError = "Access denied.";
158     WriteServLog(s.c_str());
159     return -1;
160     }
161
162 TARIFF_DATA td;
163
164     {
165     STG_LOCKER lock(&mutex);
166
167     const auto ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
168
169     if (ti == tariffs.end())
170         {
171         strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
172         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
173         return -1;
174         }
175
176     if (store->DelTariff(name))
177         {
178         WriteServLog("Cannot delete tariff %s.", name.c_str());
179         WriteServLog("%s", store->GetStrError().c_str());
180         return -1;
181         }
182
183     td = ti->GetTariffData();
184
185     tariffs.erase(ti);
186     }
187
188 auto ni = onDelNotifiers.begin();
189 while (ni != onDelNotifiers.end())
190     {
191     (*ni)->Notify(td);
192     ++ni;
193     }
194
195 WriteServLog("%s Tariff \'%s\' deleted.",
196              admin->GetLogStr().c_str(),
197              name.c_str());
198 return 0;
199 }
200 //-----------------------------------------------------------------------------
201 int TARIFFS_IMPL::Add(const std::string & name, const ADMIN * admin)
202 {
203 const PRIV * priv = admin->GetPriv();
204
205 if (!priv->tariffChg)
206     {
207     std::string s = admin->GetLogStr() + " Add tariff \'"
208                + name + "\'. Access denied.";
209     strError = "Access denied.";
210     WriteServLog(s.c_str());
211     return -1;
212     }
213
214     {
215     STG_LOCKER lock(&mutex);
216
217     const auto ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
218
219     if (ti != tariffs.end())
220         {
221         strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
222         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
223         return -1;
224         }
225
226     tariffs.push_back(TARIFF_IMPL(name));
227     }
228
229 if (store->AddTariff(name) < 0)
230     {
231     strError = "Tariff " + name + " adding error. " + store->GetStrError();
232     WriteServLog(strError.c_str());
233     return -1;
234     }
235
236 // Fire all "on add" notifiers
237 auto ni = onAddNotifiers.begin();
238 while (ni != onAddNotifiers.end())
239     {
240     (*ni)->Notify(tariffs.back().GetTariffData());
241     ++ni;
242     }
243
244 WriteServLog("%s Tariff \'%s\' added.",
245                  admin->GetLogStr().c_str(), name.c_str());
246
247 return 0;
248 }
249 //-----------------------------------------------------------------------------
250 void TARIFFS_IMPL::GetTariffsData(std::vector<TARIFF_DATA> * tdl) const
251 {
252 assert(tdl != NULL && "Tariffs data list is not null");
253 STG_LOCKER lock(&mutex);
254
255 auto it = tariffs.begin();
256 for (; it != tariffs.end(); ++it)
257     {
258     tdl->push_back(it->GetTariffData());
259     }
260 }
261 //-----------------------------------------------------------------------------
262 void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
263 {
264 STG_LOCKER lock(&mutex);
265 onAddNotifiers.insert(n);
266 }
267 //-----------------------------------------------------------------------------
268 void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
269 {
270 STG_LOCKER lock(&mutex);
271 onAddNotifiers.erase(n);
272 }
273 //-----------------------------------------------------------------------------
274 void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
275 {
276 STG_LOCKER lock(&mutex);
277 onDelNotifiers.insert(n);
278 }
279 //-----------------------------------------------------------------------------
280 void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
281 {
282 STG_LOCKER lock(&mutex);
283 onDelNotifiers.erase(n);
284 }
285 //-----------------------------------------------------------------------------