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