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