]> git.stg.codes - stg.git/blob - projects/stargazer/tariffs_impl.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / 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 std::list<TARIFF_IMPL>::const_iterator ti;
103 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
104
105 if (ti != tariffs.end())
106     return &(*ti);
107
108 return NULL;
109 }
110 //-----------------------------------------------------------------------------
111 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
112 {
113 const PRIV * priv = admin->GetPriv();
114
115 if (!priv->tariffChg)
116     {
117     std::string s = admin->GetLogStr() + " Change tariff \'"
118                + td.tariffConf.name + "\'. Access denied.";
119     strError = "Access denied.";
120     WriteServLog(s.c_str());
121     return -1;
122     }
123
124 STG_LOCKER lock(&mutex);
125
126 std::list<TARIFF_IMPL>::iterator ti;
127 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
128
129 if (ti == tariffs.end())
130     {
131     strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
132     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
133     return -1;
134     }
135
136 *ti = td;
137
138 if (store->SaveTariff(td, td.tariffConf.name))
139     {
140     std::string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
141     WriteServLog(error.c_str());
142     return -1;
143     }
144
145 WriteServLog("%s Tariff \'%s\' changed.",
146              admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
147
148 return 0;
149 }
150 //-----------------------------------------------------------------------------
151 int TARIFFS_IMPL::Del(const std::string & name, const ADMIN * admin)
152 {
153 const PRIV * priv = admin->GetPriv();
154
155 if (!priv->tariffChg)
156     {
157     std::string s = admin->GetLogStr() + " Delete tariff \'"
158                + name + "\'. Access denied.";
159     strError = "Access denied.";
160     WriteServLog(s.c_str());
161     return -1;
162     }
163
164 TARIFF_DATA td;
165
166     {
167     STG_LOCKER lock(&mutex);
168
169     std::list<TARIFF_IMPL>::iterator ti;
170     ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
171
172     if (ti == tariffs.end())
173         {
174         strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
175         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
176         return -1;
177         }
178
179     if (store->DelTariff(name))
180         {
181         WriteServLog("Cannot delete tariff %s.", name.c_str());
182         WriteServLog("%s", store->GetStrError().c_str());
183         return -1;
184         }
185
186     td = ti->GetTariffData();
187
188     tariffs.erase(ti);
189     }
190
191 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onDelNotifiers.begin();
192 while (ni != onDelNotifiers.end())
193     {
194     (*ni)->Notify(td);
195     ++ni;
196     }
197
198 WriteServLog("%s Tariff \'%s\' deleted.",
199              admin->GetLogStr().c_str(),
200              name.c_str());
201 return 0;
202 }
203 //-----------------------------------------------------------------------------
204 int TARIFFS_IMPL::Add(const std::string & name, const ADMIN * admin)
205 {
206 const PRIV * priv = admin->GetPriv();
207
208 if (!priv->tariffChg)
209     {
210     std::string s = admin->GetLogStr() + " Add tariff \'"
211                + name + "\'. Access denied.";
212     strError = "Access denied.";
213     WriteServLog(s.c_str());
214     return -1;
215     }
216
217     {
218     STG_LOCKER lock(&mutex);
219
220     std::list<TARIFF_IMPL>::iterator ti;
221     ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
222
223     if (ti != tariffs.end())
224         {
225         strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
226         WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
227         return -1;
228         }
229
230     tariffs.push_back(TARIFF_IMPL(name));
231     }
232
233 if (store->AddTariff(name) < 0)
234     {
235     strError = "Tariff " + name + " adding error. " + store->GetStrError();
236     WriteServLog(strError.c_str());
237     return -1;
238     }
239
240 // Fire all "on add" notifiers
241 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onAddNotifiers.begin();
242 while (ni != onAddNotifiers.end())
243     {
244     (*ni)->Notify(tariffs.back().GetTariffData());
245     ++ni;
246     }
247
248 WriteServLog("%s Tariff \'%s\' added.",
249                  admin->GetLogStr().c_str(), name.c_str());
250
251 return 0;
252 }
253 //-----------------------------------------------------------------------------
254 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl) const
255 {
256 assert(tdl != NULL && "Tariffs data list is not null");
257 STG_LOCKER lock(&mutex);
258
259 Tariffs::const_iterator it = tariffs.begin();
260 for (; it != tariffs.end(); ++it)
261     {
262     tdl->push_back(it->GetTariffData());
263     }
264 }
265 //-----------------------------------------------------------------------------
266 void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
267 {
268 STG_LOCKER lock(&mutex);
269 onAddNotifiers.insert(n);
270 }
271 //-----------------------------------------------------------------------------
272 void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
273 {
274 STG_LOCKER lock(&mutex);
275 onAddNotifiers.erase(n);
276 }
277 //-----------------------------------------------------------------------------
278 void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
279 {
280 STG_LOCKER lock(&mutex);
281 onDelNotifiers.insert(n);
282 }
283 //-----------------------------------------------------------------------------
284 void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
285 {
286 STG_LOCKER lock(&mutex);
287 onDelNotifiers.erase(n);
288 }
289 //-----------------------------------------------------------------------------