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