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 : Maksym Mamontov <stg@madf.info>
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"
50 volatile time_t stgTime = time(NULL);
52 int main(int argc, char **argv)
54 printfd(__FILE__, "Start\n");
56 STORE * fromStore = NULL;
57 STORE * toStore = NULL;
59 SETTINGS_IMPL * settings = NULL;
61 std::string modulePath;
63 MODULE_SETTINGS fromStoreSettings;
64 MODULE_SETTINGS toStoreSettings;
73 std::vector<STG_MSG_HDR> hdrs;
76 settings = new SETTINGS_IMPL(argv[1]);
78 settings = new SETTINGS_IMPL();
80 if (settings->ReadSettings())
82 printfd(__FILE__, "Error reading settings\n");
87 fromStoreSettings = settings->GetSourceStoreModuleSettings();
88 toStoreSettings = settings->GetDestStoreModuleSettings();
89 modulePath = settings->GetModulesPath();
91 std::string sourcePlugin(modulePath + "/mod_" + fromStoreSettings.moduleName + ".so");
92 std::string destPlugin(modulePath + "/mod_" + toStoreSettings.moduleName + ".so");
94 void * src_lh = dlopen(sourcePlugin.c_str(), RTLD_NOW);
97 printfd(__FILE__, "Source storage plugin loading failed: %s\n", dlerror());
102 void * dst_lh = dlopen(destPlugin.c_str(), RTLD_NOW);
105 printfd(__FILE__, "Destination storage plugin loading failed: %s\n", dlerror());
110 STORE * (*GetSourceStore)();
111 STORE * (*GetDestStore)();
112 GetSourceStore = (STORE * (*)())dlsym(src_lh, "GetStore");
115 printfd(__FILE__, "Source storage plugin loading failed. GetStore not found: %s\n", dlerror());
119 GetDestStore = (STORE * (*)())dlsym(dst_lh, "GetStore");
122 printfd(__FILE__, "Storage plugin (firebird) loading failed. GetStore not found: %s\n", dlerror());
127 fromStore = GetSourceStore();
128 toStore = GetDestStore();
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();
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))
142 printfd(__FILE__, "Error getting admins list: %s\n", fromStore->GetStrError().c_str());
148 if (toStore->GetAdminsList(&ready))
150 printfd(__FILE__, "Error getting admins list: %s\n", toStore->GetStrError().c_str());
157 std::vector<std::string>::const_iterator it;
158 for (it = entities.begin(); it != entities.end(); ++it)
160 printfd(__FILE__, "\t - %s\n", it->c_str());
161 if (find(ready.begin(), ready.end(), *it) == ready.end())
162 if (toStore->AddAdmin(*it))
164 printfd(__FILE__, "Error adding admin: %s\n", toStore->GetStrError().c_str());
170 if (fromStore->RestoreAdmin(&ac, *it))
172 printfd(__FILE__, "Error getting admin's confi: %s\n", fromStore->GetStrError().c_str());
179 if (toStore->SaveAdmin(ac))
181 printfd(__FILE__, "Error saving admin's conf: %s\n", toStore->GetStrError().c_str());
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))
194 printfd(__FILE__, "Error getting tariffs list: %s\n", fromStore->GetStrError().c_str());
200 if (toStore->GetTariffsList(&ready))
202 printfd(__FILE__, "Error getting tariffs list: %s\n", toStore->GetStrError().c_str());
209 for (it = entities.begin(); it != entities.end(); ++it)
211 printfd(__FILE__, "\t - %s\n", it->c_str());
212 if (find(ready.begin(), ready.end(), *it) == ready.end())
213 if (toStore->AddTariff(*it))
215 printfd(__FILE__, "Error adding tariff: %s\n", toStore->GetStrError().c_str());
221 if (fromStore->RestoreTariff(&td, *it))
223 printfd(__FILE__, "Error getting tariff's data: %s\n", fromStore->GetStrError().c_str());
229 if (toStore->SaveTariff(td, *it))
231 printfd(__FILE__, "Error saving tariff's data: %s\n", toStore->GetStrError().c_str());
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))
244 printfd(__FILE__, "Error getting service list: %s\n", fromStore->GetStrError().c_str());
250 if (toStore->GetServicesList(&ready))
252 printfd(__FILE__, "Error getting service list: %s\n", toStore->GetStrError().c_str());
259 for (it = entities.begin(); it != entities.end(); ++it)
261 printfd(__FILE__, "\t - %s\n", it->c_str());
262 if (find(ready.begin(), ready.end(), *it) == ready.end())
263 if (toStore->AddService(*it))
265 printfd(__FILE__, "Error adding service: %s\n", toStore->GetStrError().c_str());
271 if (fromStore->RestoreService(&sc, *it))
273 printfd(__FILE__, "Error getting service's data: %s\n", fromStore->GetStrError().c_str());
279 if (toStore->SaveService(sc))
281 printfd(__FILE__, "Error saving service's data: %s\n", toStore->GetStrError().c_str());
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))
294 printfd(__FILE__, "Error getting corporations list: %s\n", fromStore->GetStrError().c_str());
300 if (toStore->GetCorpsList(&ready))
302 printfd(__FILE__, "Error getting corporations list: %s\n", toStore->GetStrError().c_str());
309 for (it = entities.begin(); it != entities.end(); ++it)
311 printfd(__FILE__, "\t - %s\n", it->c_str());
312 if (find(ready.begin(), ready.end(), *it) == ready.end())
313 if (toStore->AddCorp(*it))
315 printfd(__FILE__, "Error adding corporation: %s\n", toStore->GetStrError().c_str());
321 if (fromStore->RestoreCorp(&cc, *it))
323 printfd(__FILE__, "Error getting corporation's data: %s\n", fromStore->GetStrError().c_str());
329 if (toStore->SaveCorp(cc))
331 printfd(__FILE__, "Error saving corporation's data: %s\n", toStore->GetStrError().c_str());
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))
344 printfd(__FILE__, "Error getting users list: %s\n", fromStore->GetStrError().c_str());
350 if (toStore->GetUsersList(&ready))
352 printfd(__FILE__, "Error getting users list: %s\n", toStore->GetStrError().c_str());
359 sort(ready.begin(), ready.end());
360 for (it = entities.begin(); it != entities.end(); ++it)
362 printfd(__FILE__, "\t - %s\n", it->c_str());
363 if (!binary_search(ready.begin(), ready.end(), *it)) {
364 if (toStore->AddUser(*it))
366 printfd(__FILE__, "Error adding user: %s\n", toStore->GetStrError().c_str());
373 printfd(__FILE__, "\t\t(adding passed)\n");
375 if (fromStore->RestoreUserConf(&uc, *it))
377 printfd(__FILE__, "Error getting user's conf: %s\n", fromStore->GetStrError().c_str());
383 if (fromStore->RestoreUserStat(&us, *it))
385 printfd(__FILE__, "Error getting user's stat: %s\n", fromStore->GetStrError().c_str());
391 if (toStore->SaveUserConf(uc, *it))
393 printfd(__FILE__, "Error saving user's conf: %s\n", toStore->GetStrError().c_str());
399 if (toStore->SaveUserStat(us, *it))
401 printfd(__FILE__, "Error saving user's stat: %s\n", toStore->GetStrError().c_str());
407 hdrs.erase(hdrs.begin(), hdrs.end());
408 if (fromStore->GetMessageHdrs(&hdrs, *it))
410 printfd(__FILE__, "Error getting user's messages: %s\n", fromStore->GetStrError().c_str());
416 std::vector<STG_MSG_HDR>::iterator mit;
417 for (mit = hdrs.begin(); mit != hdrs.end(); ++mit)
419 if (fromStore->GetMessage(mit->id, &msg, *it))
421 printfd(__FILE__, "Error getting message for a user: %s\n", fromStore->GetStrError().c_str());
427 printfd(__FILE__, "\t\t * %s\n", msg.text.c_str());
428 if (toStore->AddMessage(&msg, *it))
430 printfd(__FILE__, "Error adding message to a user: %s\n", toStore->GetStrError().c_str());
441 printfd(__FILE__, "Done\n");