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.
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.
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
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/10/07 18:43:21 $
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"
43 //-----------------------------------------------------------------------------
44 TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
48 WriteServLog(GetStgLogger()),
50 noTariff(NO_TARIFF_NAME),
54 pthread_mutex_init(&mutex, NULL);
57 //-----------------------------------------------------------------------------
58 TARIFFS_IMPL::~TARIFFS_IMPL()
60 pthread_mutex_destroy(&mutex);
62 //-----------------------------------------------------------------------------
63 int TARIFFS_IMPL::ReadTariffs()
65 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
67 vector<string> tariffsList;
68 if (store->GetTariffsList(&tariffsList))
70 WriteServLog("Cannot get tariffs list.");
71 WriteServLog("%s", store->GetStrError().c_str());
74 int tariffsNum = tariffsList.size();
76 for (int i = 0; i < tariffsNum; i++)
79 if (store->RestoreTariff(&td, tariffsList[i]))
81 WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
82 WriteServLog("%s", store->GetStrError().c_str());
85 tariffs.push_back(TARIFF_IMPL(td));
90 //-----------------------------------------------------------------------------
91 size_t TARIFFS_IMPL::Count() const
93 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
94 return tariffs.size();
96 //-----------------------------------------------------------------------------
97 const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
99 if (name == NO_TARIFF_NAME)
102 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
103 list<TARIFF_IMPL>::const_iterator ti;
104 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
106 if (ti != tariffs.end())
111 //-----------------------------------------------------------------------------
112 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
114 const PRIV * priv = admin->GetPriv();
116 if (!priv->tariffChg)
118 string s = admin->GetLogStr() + " Change tariff \'"
119 + td.tariffConf.name + "\'. Access denied.";
120 strError = "Access denied.";
121 WriteServLog(s.c_str());
125 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
127 list<TARIFF_IMPL>::iterator ti;
128 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
130 if (ti == tariffs.end())
132 strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
133 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
139 if (store->SaveTariff(td, td.tariffConf.name))
141 string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
142 WriteServLog(error.c_str());
146 WriteServLog("%s Tariff \'%s\' changed.",
147 admin->GetLogStr().c_str(), td.tariffConf.name.c_str());
151 //-----------------------------------------------------------------------------
152 int TARIFFS_IMPL::Del(const string & name, const ADMIN * admin)
154 const PRIV * priv = admin->GetPriv();
156 if (!priv->tariffChg)
158 string s = admin->GetLogStr() + " Delete tariff \'"
159 + name + "\'. Access denied.";
160 strError = "Access denied.";
161 WriteServLog(s.c_str());
168 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
170 list<TARIFF_IMPL>::iterator ti;
171 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
173 if (ti == tariffs.end())
175 strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
176 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
180 if (store->DelTariff(name))
182 WriteServLog("Cannot delete tariff %s.", name.c_str());
183 WriteServLog("%s", store->GetStrError().c_str());
187 td = ti->GetTariffData();
192 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onDelNotifiers.begin();
193 while (ni != onDelNotifiers.end())
199 WriteServLog("%s Tariff \'%s\' deleted.",
200 admin->GetLogStr().c_str(),
204 //-----------------------------------------------------------------------------
205 int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
207 const PRIV * priv = admin->GetPriv();
209 if (!priv->tariffChg)
211 string s = admin->GetLogStr() + " Add tariff \'"
212 + name + "\'. Access denied.";
213 strError = "Access denied.";
214 WriteServLog(s.c_str());
219 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
221 list<TARIFF_IMPL>::iterator ti;
222 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
224 if (ti != tariffs.end())
226 strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
227 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
231 tariffs.push_back(TARIFF_IMPL(name));
234 if (store->AddTariff(name) < 0)
236 strError = "Tariff " + name + " adding error. " + store->GetStrError();
237 WriteServLog(strError.c_str());
241 // Fire all "on add" notifiers
242 std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onAddNotifiers.begin();
243 while (ni != onAddNotifiers.end())
245 (*ni)->Notify(tariffs.back().GetTariffData());
249 WriteServLog("%s Tariff \'%s\' added.",
250 admin->GetLogStr().c_str(), name.c_str());
254 //-----------------------------------------------------------------------------
255 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
257 assert(tdl != NULL && "Tariffs data list is not null");
258 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
260 std::list<TARIFF_IMPL>::const_iterator it = tariffs.begin();
261 for (; it != tariffs.end(); ++it)
263 tdl->push_back(it->GetTariffData());
266 //-----------------------------------------------------------------------------
267 void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
269 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
270 onAddNotifiers.insert(n);
272 //-----------------------------------------------------------------------------
273 void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
275 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
276 onAddNotifiers.erase(n);
278 //-----------------------------------------------------------------------------
279 void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
281 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
282 onDelNotifiers.insert(n);
284 //-----------------------------------------------------------------------------
285 void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
287 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
288 onDelNotifiers.erase(n);
290 //-----------------------------------------------------------------------------