]> git.stg.codes - stg.git/blob - projects/sgconv/main.cpp
Process services on daily basis.
[stg.git] / projects / sgconv / main.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  /*
22  $Revision: 1.11 $
23  $Date: 2010/03/25 12:32:30 $
24  $Author: faust $
25  */
26
27 #include <dlfcn.h>
28
29 #include <string>
30 #include <vector>
31 #include <iostream>
32 #include <ctime>
33 #include <algorithm>
34
35 #include "stg/common.h"
36 #include "stg/store.h"
37 #include "stg/conffiles.h"
38
39 #include "stg/user_stat.h"
40 #include "stg/user_conf.h"
41 #include "stg/corp_conf.h"
42 #include "stg/service_conf.h"
43 #include "stg/admin_conf.h"
44 #include "stg/tariff_conf.h"
45 #include "stg/settings.h"
46 #include "stg/message.h"
47
48 #include "settings_impl.h"
49
50 volatile time_t stgTime = time(NULL);
51
52 int main(int argc, char **argv)
53 {
54 printfd(__FILE__, "Start\n");
55
56 STORE * fromStore = NULL;
57 STORE * toStore = NULL;
58
59 SETTINGS_IMPL * settings = NULL;
60
61 std::string modulePath;
62
63 MODULE_SETTINGS fromStoreSettings;
64 MODULE_SETTINGS toStoreSettings;
65
66 ADMIN_CONF ac;
67 USER_CONF uc;
68 USER_STAT us;
69 STG_MSG msg;
70 TARIFF_DATA td;
71 CORP_CONF cc;
72 SERVICE_CONF sc;
73 std::vector<STG_MSG_HDR> hdrs;
74
75 if (argc == 2)
76     settings = new SETTINGS_IMPL(argv[1]);
77 else
78     settings = new SETTINGS_IMPL();
79
80 if (settings->ReadSettings())
81     {
82     printfd(__FILE__, "Error reading settings\n");
83     delete settings;
84     return -1;
85     }
86
87 fromStoreSettings = settings->GetSourceStoreModuleSettings();
88 toStoreSettings = settings->GetDestStoreModuleSettings();
89 modulePath = settings->GetModulesPath();
90
91 std::string sourcePlugin(modulePath + "/mod_" + fromStoreSettings.moduleName + ".so");
92 std::string destPlugin(modulePath + "/mod_" + toStoreSettings.moduleName + ".so");
93
94 void * src_lh = dlopen(sourcePlugin.c_str(), RTLD_NOW);
95 if (!src_lh)
96     {
97     printfd(__FILE__, "Source storage plugin loading failed: %s\n", dlerror());
98     delete settings;
99     return -1;
100     }
101
102 void * dst_lh = dlopen(destPlugin.c_str(), RTLD_NOW);
103 if (!dst_lh)
104     {
105     printfd(__FILE__, "Destination storage plugin loading failed: %s\n", dlerror());
106     delete settings;
107     return -1;
108     }
109
110 STORE * (*GetSourceStore)();
111 STORE * (*GetDestStore)();
112 GetSourceStore = (STORE * (*)())dlsym(src_lh, "GetStore");
113 if (!GetSourceStore)
114     {
115     printfd(__FILE__, "Source storage plugin loading failed. GetStore not found: %s\n", dlerror());
116     delete settings;
117     return -1;
118     }
119 GetDestStore = (STORE * (*)())dlsym(dst_lh, "GetStore");
120 if (!GetDestStore)
121     {
122     printfd(__FILE__, "Storage plugin (firebird) loading failed. GetStore not found: %s\n", dlerror());
123     delete settings;
124     return -1;
125     }
126
127 fromStore = GetSourceStore();
128 toStore = GetDestStore();
129
130 std::vector<std::string> entities;
131 std::vector<std::string> ready;
132 fromStore->SetSettings(fromStoreSettings);
133 fromStore->ParseSettings();
134 toStore->SetSettings(toStoreSettings);
135 toStore->ParseSettings();
136
137 printfd(__FILE__, "Importing admins:\n");
138 entities.erase(entities.begin(), entities.end());
139 ready.erase(ready.begin(), ready.end());
140 if (fromStore->GetAdminsList(&entities))
141     {
142     printfd(__FILE__, "Error getting admins list: %s\n", fromStore->GetStrError().c_str());
143     dlclose(src_lh);
144     dlclose(dst_lh);
145     delete settings;
146     return -1;
147     }
148 if (toStore->GetAdminsList(&ready))
149     {
150     printfd(__FILE__, "Error getting admins list: %s\n", toStore->GetStrError().c_str());
151     dlclose(src_lh);
152     dlclose(dst_lh);
153     delete settings;
154     return -1;
155     }
156
157 std::vector<std::string>::const_iterator it;
158 for (it = entities.begin(); it != entities.end(); ++it)
159     {
160     printfd(__FILE__, "\t - %s\n", it->c_str());
161     if (find(ready.begin(), ready.end(), *it) == ready.end())
162         if (toStore->AddAdmin(*it))
163             {
164             printfd(__FILE__, "Error adding admin: %s\n", toStore->GetStrError().c_str());
165             dlclose(src_lh);
166             dlclose(dst_lh);
167             delete settings;
168             return -1;
169             }
170     if (fromStore->RestoreAdmin(&ac, *it))
171         {
172         printfd(__FILE__, "Error getting admin's confi: %s\n", fromStore->GetStrError().c_str());
173         dlclose(src_lh);
174         dlclose(dst_lh);
175         delete settings;
176         return -1;
177         }
178     ac.login = *it;
179     if (toStore->SaveAdmin(ac))
180         {
181         printfd(__FILE__, "Error saving admin's conf: %s\n", toStore->GetStrError().c_str());
182         dlclose(src_lh);
183         dlclose(dst_lh);
184         delete settings;
185         return -1;
186         }
187     }
188
189 printfd(__FILE__, "Importing tariffs:\n");
190 entities.erase(entities.begin(), entities.end());
191 ready.erase(ready.begin(), ready.end());
192 if (fromStore->GetTariffsList(&entities))
193     {
194     printfd(__FILE__, "Error getting tariffs list: %s\n", fromStore->GetStrError().c_str());
195     dlclose(src_lh);
196     dlclose(dst_lh);
197     delete settings;
198     return -1;
199     }
200 if (toStore->GetTariffsList(&ready))
201     {
202     printfd(__FILE__, "Error getting tariffs list: %s\n", toStore->GetStrError().c_str());
203     dlclose(src_lh);
204     dlclose(dst_lh);
205     delete settings;
206     return -1;
207     }
208
209 for (it = entities.begin(); it != entities.end(); ++it)
210     {
211     printfd(__FILE__, "\t - %s\n", it->c_str());
212     if (find(ready.begin(), ready.end(), *it) == ready.end())
213         if (toStore->AddTariff(*it))
214             {
215             printfd(__FILE__, "Error adding tariff: %s\n", toStore->GetStrError().c_str());
216             dlclose(src_lh);
217             dlclose(dst_lh);
218             delete settings;
219             return -1;
220             }
221     if (fromStore->RestoreTariff(&td, *it))
222         {
223         printfd(__FILE__, "Error getting tariff's data: %s\n", fromStore->GetStrError().c_str());
224         dlclose(src_lh);
225         dlclose(dst_lh);
226         delete settings;
227         return -1;
228         }
229     if (toStore->SaveTariff(td, *it))
230         {
231         printfd(__FILE__, "Error saving tariff's data: %s\n", toStore->GetStrError().c_str());
232         dlclose(src_lh);
233         dlclose(dst_lh);
234         delete settings;
235         return -1;
236         }
237     }
238
239 printfd(__FILE__, "Importing services:\n");
240 entities.erase(entities.begin(), entities.end());
241 ready.erase(ready.begin(), ready.end());
242 if (fromStore->GetServicesList(&entities))
243     {
244     printfd(__FILE__, "Error getting service list: %s\n", fromStore->GetStrError().c_str());
245     dlclose(src_lh);
246     dlclose(dst_lh);
247     delete settings;
248     return -1;
249     }
250 if (toStore->GetServicesList(&ready))
251     {
252     printfd(__FILE__, "Error getting service list: %s\n", toStore->GetStrError().c_str());
253     dlclose(src_lh);
254     dlclose(dst_lh);
255     delete settings;
256     return -1;
257     }
258
259 for (it = entities.begin(); it != entities.end(); ++it)
260     {
261     printfd(__FILE__, "\t - %s\n", it->c_str());
262     if (find(ready.begin(), ready.end(), *it) == ready.end())
263         if (toStore->AddService(*it))
264             {
265             printfd(__FILE__, "Error adding service: %s\n", toStore->GetStrError().c_str());
266             dlclose(src_lh);
267             dlclose(dst_lh);
268             delete settings;
269             return -1;
270             }
271     if (fromStore->RestoreService(&sc, *it))
272         {
273         printfd(__FILE__, "Error getting service's data: %s\n", fromStore->GetStrError().c_str());
274         dlclose(src_lh);
275         dlclose(dst_lh);
276         delete settings;
277         return -1;
278         }
279     if (toStore->SaveService(sc))
280         {
281         printfd(__FILE__, "Error saving service's data: %s\n", toStore->GetStrError().c_str());
282         dlclose(src_lh);
283         dlclose(dst_lh);
284         delete settings;
285         return -1;
286         }
287     }
288
289 printfd(__FILE__, "Importing corporations:\n");
290 entities.erase(entities.begin(), entities.end());
291 ready.erase(ready.begin(), ready.end());
292 if (fromStore->GetCorpsList(&entities))
293     {
294     printfd(__FILE__, "Error getting corporations list: %s\n", fromStore->GetStrError().c_str());
295     dlclose(src_lh);
296     dlclose(dst_lh);
297     delete settings;
298     return -1;
299     }
300 if (toStore->GetCorpsList(&ready))
301     {
302     printfd(__FILE__, "Error getting corporations list: %s\n", toStore->GetStrError().c_str());
303     dlclose(src_lh);
304     dlclose(dst_lh);
305     delete settings;
306     return -1;
307     }
308
309 for (it = entities.begin(); it != entities.end(); ++it)
310     {
311     printfd(__FILE__, "\t - %s\n", it->c_str());
312     if (find(ready.begin(), ready.end(), *it) == ready.end())
313         if (toStore->AddCorp(*it))
314             {
315             printfd(__FILE__, "Error adding corporation: %s\n", toStore->GetStrError().c_str());
316             dlclose(src_lh);
317             dlclose(dst_lh);
318             delete settings;
319             return -1;
320             }
321     if (fromStore->RestoreCorp(&cc, *it))
322         {
323         printfd(__FILE__, "Error getting corporation's data: %s\n", fromStore->GetStrError().c_str());
324         dlclose(src_lh);
325         dlclose(dst_lh);
326         delete settings;
327         return -1;
328         }
329     if (toStore->SaveCorp(cc))
330         {
331         printfd(__FILE__, "Error saving corporation's data: %s\n", toStore->GetStrError().c_str());
332         dlclose(src_lh);
333         dlclose(dst_lh);
334         delete settings;
335         return -1;
336         }
337     }
338
339 printfd(__FILE__, "Importing users:\n");
340 entities.erase(entities.begin(), entities.end());
341 ready.erase(ready.begin(), ready.end());
342 if (fromStore->GetUsersList(&entities))
343     {
344     printfd(__FILE__, "Error getting users list: %s\n", fromStore->GetStrError().c_str());
345     dlclose(src_lh);
346     dlclose(dst_lh);
347     delete settings;
348     return -1;
349     }
350 if (toStore->GetUsersList(&ready))
351     {
352     printfd(__FILE__, "Error getting users list: %s\n", toStore->GetStrError().c_str());
353     dlclose(src_lh);
354     dlclose(dst_lh);
355     delete settings;
356     return -1;
357     }
358
359 sort(ready.begin(), ready.end());
360 for (it = entities.begin(); it != entities.end(); ++it)
361     {
362     printfd(__FILE__, "\t - %s\n", it->c_str());
363     if (!binary_search(ready.begin(), ready.end(), *it)) {
364         if (toStore->AddUser(*it))
365             {
366             printfd(__FILE__, "Error adding user: %s\n", toStore->GetStrError().c_str());
367             dlclose(src_lh);
368             dlclose(dst_lh);
369             delete settings;
370             return -1;
371             }
372     } else {
373         printfd(__FILE__, "\t\t(adding passed)\n");
374     }
375     if (fromStore->RestoreUserConf(&uc, *it))
376         {
377         printfd(__FILE__, "Error getting user's conf: %s\n", fromStore->GetStrError().c_str());
378         dlclose(src_lh);
379         dlclose(dst_lh);
380         delete settings;
381         return -1;
382         }
383     if (fromStore->RestoreUserStat(&us, *it))
384         {
385         printfd(__FILE__, "Error getting user's stat: %s\n", fromStore->GetStrError().c_str());
386         dlclose(src_lh);
387         dlclose(dst_lh);
388         delete settings;
389         return -1;
390         }
391     if (toStore->SaveUserConf(uc, *it))
392         {
393         printfd(__FILE__, "Error saving user's conf: %s\n", toStore->GetStrError().c_str());
394         dlclose(src_lh);
395         dlclose(dst_lh);
396         delete settings;
397         return -1;
398         }
399     if (toStore->SaveUserStat(us, *it))
400         {
401         printfd(__FILE__, "Error saving user's stat: %s\n", toStore->GetStrError().c_str());
402         dlclose(src_lh);
403         dlclose(dst_lh);
404         delete settings;
405         return -1;
406         }
407     hdrs.erase(hdrs.begin(), hdrs.end());
408     if (fromStore->GetMessageHdrs(&hdrs, *it))
409         {
410         printfd(__FILE__, "Error getting user's messages: %s\n", fromStore->GetStrError().c_str());
411         dlclose(src_lh);
412         dlclose(dst_lh);
413         delete settings;
414         return -1;
415         }
416     std::vector<STG_MSG_HDR>::iterator mit;
417     for (mit = hdrs.begin(); mit != hdrs.end(); ++mit)
418         {
419         if (fromStore->GetMessage(mit->id, &msg, *it))
420             {
421             printfd(__FILE__, "Error getting message for a user: %s\n", fromStore->GetStrError().c_str());
422             dlclose(src_lh);
423             dlclose(dst_lh);
424             delete settings;
425             return -1;
426             }
427         printfd(__FILE__, "\t\t * %s\n", msg.text.c_str());
428         if (toStore->AddMessage(&msg, *it))
429             {
430             printfd(__FILE__, "Error adding message to a user: %s\n", toStore->GetStrError().c_str());
431             dlclose(src_lh);
432             dlclose(dst_lh);
433             delete settings;
434             return -1;
435             }
436         }
437     }
438
439 dlclose(src_lh);
440 dlclose(dst_lh);
441 printfd(__FILE__, "Done\n");
442 delete settings;
443 return 0;
444 }