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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
23 $Date: 2010/03/25 12:32:30 $
35 #include "stg/common.h"
36 #include "stg/store.h"
37 #include "stg/conffiles.h"
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"
48 #include "settings_impl.h"
52 volatile time_t stgTime = time(NULL);
54 int main(int argc, char **argv)
56 printfd(__FILE__, "Start\n");
58 STORE * fromStore = NULL;
59 STORE * toStore = NULL;
61 SETTINGS_IMPL * settings = NULL;
65 MODULE_SETTINGS fromStoreSettings;
66 MODULE_SETTINGS toStoreSettings;
75 vector<STG_MSG_HDR> hdrs;
78 settings = new SETTINGS_IMPL(argv[1]);
80 settings = new SETTINGS_IMPL();
82 if (settings->ReadSettings())
84 printfd(__FILE__, "Error reading settings\n");
89 fromStoreSettings = settings->GetSourceStoreModuleSettings();
90 toStoreSettings = settings->GetDestStoreModuleSettings();
91 modulePath = settings->GetModulesPath();
93 string sourcePlugin(modulePath + "/mod_" + fromStoreSettings.moduleName + ".so");
94 string destPlugin(modulePath + "/mod_" + toStoreSettings.moduleName + ".so");
96 void * src_lh = dlopen(sourcePlugin.c_str(), RTLD_NOW);
99 printfd(__FILE__, "Source storage plugin loading failed: %s\n", dlerror());
104 void * dst_lh = dlopen(destPlugin.c_str(), RTLD_NOW);
107 printfd(__FILE__, "Destination storage plugin loading failed: %s\n", dlerror());
112 STORE * (*GetSourceStore)();
113 STORE * (*GetDestStore)();
114 GetSourceStore = (STORE * (*)())dlsym(src_lh, "GetStore");
117 printfd(__FILE__, "Source storage plugin loading failed. GetStore not found: %s\n", dlerror());
121 GetDestStore = (STORE * (*)())dlsym(dst_lh, "GetStore");
124 printfd(__FILE__, "Storage plugin (firebird) loading failed. GetStore not found: %s\n", dlerror());
129 fromStore = GetSourceStore();
130 toStore = GetDestStore();
132 vector<string> entities;
133 vector<string> ready;
134 fromStore->SetSettings(fromStoreSettings);
135 fromStore->ParseSettings();
136 toStore->SetSettings(toStoreSettings);
137 toStore->ParseSettings();
139 printfd(__FILE__, "Importing admins:\n");
140 entities.erase(entities.begin(), entities.end());
141 ready.erase(ready.begin(), ready.end());
142 if (fromStore->GetAdminsList(&entities))
144 printfd(__FILE__, "Error getting admins list: %s\n", fromStore->GetStrError().c_str());
150 if (toStore->GetAdminsList(&ready))
152 printfd(__FILE__, "Error getting admins list: %s\n", toStore->GetStrError().c_str());
159 vector<string>::const_iterator it;
160 for (it = entities.begin(); it != entities.end(); ++it)
162 printfd(__FILE__, "\t - %s\n", it->c_str());
163 if (find(ready.begin(), ready.end(), *it) == ready.end())
164 if (toStore->AddAdmin(*it))
166 printfd(__FILE__, "Error adding admin: %s\n", toStore->GetStrError().c_str());
172 if (fromStore->RestoreAdmin(&ac, *it))
174 printfd(__FILE__, "Error getting admin's confi: %s\n", fromStore->GetStrError().c_str());
181 if (toStore->SaveAdmin(ac))
183 printfd(__FILE__, "Error saving admin's conf: %s\n", toStore->GetStrError().c_str());
191 printfd(__FILE__, "Importing tariffs:\n");
192 entities.erase(entities.begin(), entities.end());
193 ready.erase(ready.begin(), ready.end());
194 if (fromStore->GetTariffsList(&entities))
196 printfd(__FILE__, "Error getting tariffs list: %s\n", fromStore->GetStrError().c_str());
202 if (toStore->GetTariffsList(&ready))
204 printfd(__FILE__, "Error getting tariffs list: %s\n", toStore->GetStrError().c_str());
211 for (it = entities.begin(); it != entities.end(); ++it)
213 printfd(__FILE__, "\t - %s\n", it->c_str());
214 if (find(ready.begin(), ready.end(), *it) == ready.end())
215 if (toStore->AddTariff(*it))
217 printfd(__FILE__, "Error adding tariff: %s\n", toStore->GetStrError().c_str());
223 if (fromStore->RestoreTariff(&td, *it))
225 printfd(__FILE__, "Error getting tariff's data: %s\n", fromStore->GetStrError().c_str());
231 if (toStore->SaveTariff(td, *it))
233 printfd(__FILE__, "Error saving tariff's data: %s\n", toStore->GetStrError().c_str());
241 printfd(__FILE__, "Importing services:\n");
242 entities.erase(entities.begin(), entities.end());
243 ready.erase(ready.begin(), ready.end());
244 if (fromStore->GetServicesList(&entities))
246 printfd(__FILE__, "Error getting service list: %s\n", fromStore->GetStrError().c_str());
252 if (toStore->GetServicesList(&ready))
254 printfd(__FILE__, "Error getting service list: %s\n", toStore->GetStrError().c_str());
261 for (it = entities.begin(); it != entities.end(); ++it)
263 printfd(__FILE__, "\t - %s\n", it->c_str());
264 if (find(ready.begin(), ready.end(), *it) == ready.end())
265 if (toStore->AddService(*it))
267 printfd(__FILE__, "Error adding service: %s\n", toStore->GetStrError().c_str());
273 if (fromStore->RestoreService(&sc, *it))
275 printfd(__FILE__, "Error getting service's data: %s\n", fromStore->GetStrError().c_str());
281 if (toStore->SaveService(sc))
283 printfd(__FILE__, "Error saving service's data: %s\n", toStore->GetStrError().c_str());
291 printfd(__FILE__, "Importing corporations:\n");
292 entities.erase(entities.begin(), entities.end());
293 ready.erase(ready.begin(), ready.end());
294 if (fromStore->GetCorpsList(&entities))
296 printfd(__FILE__, "Error getting corporations list: %s\n", fromStore->GetStrError().c_str());
302 if (toStore->GetCorpsList(&ready))
304 printfd(__FILE__, "Error getting corporations list: %s\n", toStore->GetStrError().c_str());
311 for (it = entities.begin(); it != entities.end(); ++it)
313 printfd(__FILE__, "\t - %s\n", it->c_str());
314 if (find(ready.begin(), ready.end(), *it) == ready.end())
315 if (toStore->AddCorp(*it))
317 printfd(__FILE__, "Error adding corporation: %s\n", toStore->GetStrError().c_str());
323 if (fromStore->RestoreCorp(&cc, *it))
325 printfd(__FILE__, "Error getting corporation's data: %s\n", fromStore->GetStrError().c_str());
331 if (toStore->SaveCorp(cc))
333 printfd(__FILE__, "Error saving corporation's data: %s\n", toStore->GetStrError().c_str());
341 printfd(__FILE__, "Importing users:\n");
342 entities.erase(entities.begin(), entities.end());
343 ready.erase(ready.begin(), ready.end());
344 if (fromStore->GetUsersList(&entities))
346 printfd(__FILE__, "Error getting users list: %s\n", fromStore->GetStrError().c_str());
352 if (toStore->GetUsersList(&ready))
354 printfd(__FILE__, "Error getting users list: %s\n", toStore->GetStrError().c_str());
361 sort(ready.begin(), ready.end());
362 for (it = entities.begin(); it != entities.end(); ++it)
364 printfd(__FILE__, "\t - %s\n", it->c_str());
365 if (!binary_search(ready.begin(), ready.end(), *it)) {
366 if (toStore->AddUser(*it))
368 printfd(__FILE__, "Error adding user: %s\n", toStore->GetStrError().c_str());
375 printfd(__FILE__, "\t\t(adding passed)\n");
377 if (fromStore->RestoreUserConf(&uc, *it))
379 printfd(__FILE__, "Error getting user's conf: %s\n", fromStore->GetStrError().c_str());
385 if (fromStore->RestoreUserStat(&us, *it))
387 printfd(__FILE__, "Error getting user's stat: %s\n", fromStore->GetStrError().c_str());
393 if (toStore->SaveUserConf(uc, *it))
395 printfd(__FILE__, "Error saving user's conf: %s\n", toStore->GetStrError().c_str());
401 if (toStore->SaveUserStat(us, *it))
403 printfd(__FILE__, "Error saving user's stat: %s\n", toStore->GetStrError().c_str());
409 hdrs.erase(hdrs.begin(), hdrs.end());
410 if (fromStore->GetMessageHdrs(&hdrs, *it))
412 printfd(__FILE__, "Error getting user's messages: %s\n", fromStore->GetStrError().c_str());
418 vector<STG_MSG_HDR>::iterator mit;
419 for (mit = hdrs.begin(); mit != hdrs.end(); ++mit)
421 if (fromStore->GetMessage(mit->id, &msg, *it))
423 printfd(__FILE__, "Error getting message for a user: %s\n", fromStore->GetStrError().c_str());
429 printfd(__FILE__, "\t\t * %s\n", msg.text.c_str());
430 if (toStore->AddMessage(&msg, *it))
432 printfd(__FILE__, "Error adding message to a user: %s\n", toStore->GetStrError().c_str());
443 printfd(__FILE__, "Done\n");