]> git.stg.codes - stg.git/blob - projects/stargazer/tariffs_impl.cpp
Fix stargazer compilation errors
[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/stg_locker.h"
36 #include "stg/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       store(st),
47       WriteServLog(GetStgLogger()),
48       strError(),
49       noTariff(NO_TARIFF_NAME)
50 {
51 pthread_mutex_init(&mutex, NULL);
52 ReadTariffs();
53 }
54 //-----------------------------------------------------------------------------
55 TARIFFS_IMPL::~TARIFFS_IMPL()
56 {
57 pthread_mutex_destroy(&mutex);
58 }
59 //-----------------------------------------------------------------------------
60 int TARIFFS_IMPL::ReadTariffs()
61 {
62 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
63
64 vector<string> tariffsList;
65 if (store->GetTariffsList(&tariffsList))
66     {
67     WriteServLog("Cannot get tariffs list.");
68     WriteServLog("%s", store->GetStrError().c_str());
69     }
70
71 int tariffsNum = tariffsList.size();
72
73 for (int i = 0; i < tariffsNum; i++)
74     {
75     TARIFF_DATA td;
76     if (store->RestoreTariff(&td, tariffsList[i]))
77         {
78         WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
79         WriteServLog("%s", store->GetStrError().c_str());
80         return -1;
81         }
82     tariffs.push_back(TARIFF_IMPL(td));
83     }
84
85 return 0;
86 }
87 //-----------------------------------------------------------------------------
88 int TARIFFS_IMPL::GetTariffsNum() const
89 {
90 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
91 return tariffs.size();
92 }
93 //-----------------------------------------------------------------------------
94 const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
95 {
96 if (name == NO_TARIFF_NAME)
97     return &noTariff;
98
99 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
100 list<TARIFF_IMPL>::const_iterator ti;
101 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
102
103 if (ti != tariffs.end())
104     return &(*ti);
105
106 return NULL;
107 }
108 //-----------------------------------------------------------------------------
109 int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN * admin)
110 {
111 const PRIV * priv = admin->GetPriv();
112
113 if (!priv->tariffChg)
114     {
115     string s = admin->GetLogStr() + " Change tariff \'"
116                + td.tariffConf.name + "\'. Access denied.";
117     strError = "Access denied.";
118     WriteServLog(s.c_str());
119     return -1;
120     }
121
122 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
123
124 list<TARIFF_IMPL>::iterator ti;
125 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     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 string & name, const ADMIN * admin)
150 {
151 const PRIV * priv = admin->GetPriv();
152
153 if (!priv->tariffChg)
154     {
155     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 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
163
164 list<TARIFF_IMPL>::iterator ti;
165 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
166
167 if (ti == tariffs.end())
168     {
169     strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
170     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
171     return -1;
172     }
173
174 if (store->DelTariff(name))
175     {
176     WriteServLog("Cannot delete tariff %s.", name.c_str());
177     WriteServLog("%s", store->GetStrError().c_str());
178     return -1;
179     }
180
181 tariffs.erase(ti);
182
183 WriteServLog("%s Tariff \'%s\' deleted.",
184              admin->GetLogStr().c_str(),
185              name.c_str());
186 return 0;
187 }
188 //-----------------------------------------------------------------------------
189 int TARIFFS_IMPL::Add(const string & name, const ADMIN * admin)
190 {
191 const PRIV * priv = admin->GetPriv();
192
193 if (!priv->tariffChg)
194     {
195     string s = admin->GetLogStr() + " Add tariff \'"
196                + name + "\'. Access denied.";
197     strError = "Access denied.";
198     WriteServLog(s.c_str());
199     return -1;
200     }
201
202 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
203
204 list<TARIFF_IMPL>::iterator ti;
205 ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
206
207 if (ti != tariffs.end())
208     {
209     strError = "Tariff \'" + name + "\' cannot be added. Tariff alredy exist.";
210     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
211     return -1;
212     }
213
214 tariffs.push_back(TARIFF_IMPL(name));
215
216 if (store->AddTariff(name) < 0)
217     {
218     strError = "Tariff " + name + " adding error. " + store->GetStrError();
219     WriteServLog(strError.c_str());
220     return -1;
221     }
222
223 WriteServLog("%s Tariff \'%s\' added.",
224                  admin->GetLogStr().c_str(), name.c_str());
225
226 return 0;
227 }
228 //-----------------------------------------------------------------------------
229 void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
230 {
231 assert(tdl != NULL && "Tariffs data list is not null");
232 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
233
234 std::list<TARIFF_IMPL>::const_iterator it = tariffs.begin();
235 for (; it != tariffs.end(); ++it)
236     {
237     tdl->push_back(it->GetTariffData());
238     }
239 }
240 //-----------------------------------------------------------------------------