]> git.stg.codes - stg.git/blob - projects/stargazer/corps_impl.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / stargazer / corps_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  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
19  */
20
21 #include "corps_impl.h"
22
23 #include "stg/admin.h"
24 #include "stg/common.h"
25
26 #include <cerrno>
27 #include <cassert>
28 #include <algorithm>
29
30 //-----------------------------------------------------------------------------
31 CORPORATIONS_IMPL::CORPORATIONS_IMPL(STORE * st)
32     : CORPORATIONS(),
33       data(),
34       store(st),
35       WriteServLog(GetStgLogger()),
36       searchDescriptors(),
37       handle(0),
38       mutex(),
39       strError()
40 {
41 pthread_mutex_init(&mutex, NULL);
42 Read();
43 }
44 //-----------------------------------------------------------------------------
45 int CORPORATIONS_IMPL::Add(const CORP_CONF & corp, const ADMIN * admin)
46 {
47 STG_LOCKER lock(&mutex);
48 const PRIV * priv = admin->GetPriv();
49
50 if (!priv->corpChg)
51     {
52     std::string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied.";
53     strError = "Access denied.";
54     WriteServLog(s.c_str());
55     return -1;
56     }
57
58 crp_iter si(find(data.begin(), data.end(), corp));
59
60 if (si != data.end())
61     {
62     strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist.";
63     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
64
65     return -1;
66     }
67
68 data.push_back(corp);
69
70 if (store->AddCorp(corp.name) == 0)
71     {
72     WriteServLog("%s Corporation \'%s\' added.",
73                  admin->GetLogStr().c_str(), corp.name.c_str());
74     return 0;
75     }
76
77 strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError();
78 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
79
80 return -1;
81 }
82 //-----------------------------------------------------------------------------
83 int CORPORATIONS_IMPL::Del(const std::string & name, const ADMIN * admin)
84 {
85 STG_LOCKER lock(&mutex);
86 const PRIV * priv = admin->GetPriv();
87
88 if (!priv->corpChg)
89     {
90     std::string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied.";
91     strError = "Access denied.";
92     WriteServLog(s.c_str());
93     return -1;
94     }
95
96 crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
97
98 if (si == data.end())
99     {
100     strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist.";
101     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
102     return -1;
103     }
104
105 std::map<int, const_crp_iter>::iterator csi;
106 csi = searchDescriptors.begin();
107 while (csi != searchDescriptors.end())
108     {
109     if (csi->second == si)
110         (csi->second)++;
111     ++csi;
112     }
113
114 data.remove(*si);
115 if (store->DelCorp(name) < 0)
116     {
117     strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError();
118     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
119
120     return -1;
121     }
122
123 WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str());
124 return 0;
125 }
126 //-----------------------------------------------------------------------------
127 int CORPORATIONS_IMPL::Change(const CORP_CONF & corp, const ADMIN * admin)
128 {
129 STG_LOCKER lock(&mutex);
130 const PRIV * priv = admin->GetPriv();
131
132 if (!priv->corpChg)
133     {
134     std::string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied.";
135     strError = "Access denied.";
136     WriteServLog(s.c_str());
137     return -1;
138     }
139
140 crp_iter si(find(data.begin(), data.end(), corp));
141
142 if (si == data.end())
143     {
144     strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist.";
145     WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
146     return -1;
147     }
148
149 *si = corp;
150 if (store->SaveCorp(corp))
151     {
152     WriteServLog("Cannot write corporation %s.", corp.name.c_str());
153     WriteServLog("%s", store->GetStrError().c_str());
154     return -1;
155     }
156
157 WriteServLog("%s Corporation \'%s\' changed.",
158              admin->GetLogStr().c_str(), corp.name.c_str());
159
160 return 0;
161 }
162 //-----------------------------------------------------------------------------
163 bool CORPORATIONS_IMPL::Read()
164 {
165 STG_LOCKER lock(&mutex);
166 std::vector<std::string> corpsList;
167 if (store->GetCorpsList(&corpsList) < 0)
168     {
169     WriteServLog(store->GetStrError().c_str());
170     return true;
171     }
172
173 for (size_t i = 0; i < corpsList.size(); i++)
174     {
175     CORP_CONF corp;
176
177     if (store->RestoreCorp(&corp, corpsList[i]))
178         {
179         WriteServLog(store->GetStrError().c_str());
180         return true;
181         }
182
183     data.push_back(corp);
184     }
185 return false;
186 }
187 //-----------------------------------------------------------------------------
188 bool CORPORATIONS_IMPL::Find(const std::string & name, CORP_CONF * corp)
189 {
190 assert(corp != NULL && "Pointer to corporation is not null");
191
192 STG_LOCKER lock(&mutex);
193 if (data.empty())
194     return false;
195
196 crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
197
198 if (si != data.end())
199     {
200     *corp = *si;
201     return false;
202     }
203
204 return true;
205 }
206 //-----------------------------------------------------------------------------
207 bool CORPORATIONS_IMPL::Exists(const std::string & name) const
208 {
209 STG_LOCKER lock(&mutex);
210 if (data.empty())
211     {
212     printfd(__FILE__, "no admin in system!\n");
213     return true;
214     }
215
216 const_crp_iter si(find(data.begin(), data.end(), CORP_CONF(name)));
217
218 if (si != data.end())
219     return true;
220
221 return false;
222 }
223 //-----------------------------------------------------------------------------
224 int CORPORATIONS_IMPL::OpenSearch() const
225 {
226 STG_LOCKER lock(&mutex);
227 handle++;
228 searchDescriptors[handle] = data.begin();
229 return handle;
230 }
231 //-----------------------------------------------------------------------------
232 int CORPORATIONS_IMPL::SearchNext(int h, CORP_CONF * corp) const
233 {
234 STG_LOCKER lock(&mutex);
235 if (searchDescriptors.find(h) == searchDescriptors.end())
236     {
237     WriteServLog("CORPORATIONS. Incorrect search handle.");
238     return -1;
239     }
240
241 if (searchDescriptors[h] == data.end())
242     return -1;
243
244 *corp = *searchDescriptors[h]++;
245
246 return 0;
247 }
248 //-----------------------------------------------------------------------------
249 int CORPORATIONS_IMPL::CloseSearch(int h) const
250 {
251 STG_LOCKER lock(&mutex);
252 if (searchDescriptors.find(h) != searchDescriptors.end())
253     {
254     searchDescriptors.erase(searchDescriptors.find(h));
255     return 0;
256     }
257
258 WriteServLog("CORPORATIONS. Incorrect search handle.");
259 return -1;
260 }
261 //-----------------------------------------------------------------------------