]> git.stg.codes - stg.git/blob - projects/sgauthstress/main.cpp
main.cpp cleaned up
[stg.git] / projects / sgauthstress / 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 : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21  /*
22  $Revision: 1.13 $
23  $Date: 2010/04/14 09:01:29 $
24  $Author: faust $
25  */
26
27 #include <unistd.h>
28
29 #include <csignal>
30 #include <iostream>
31
32 #include "stg/common.h"
33 #include "stg/store.h"
34 #include "stg/user_conf.h"
35
36 #include "settings.h"
37 #include "store_loader.h"
38 #include "user.h"
39
40 time_t stgTime;
41 bool running;
42
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 void Usage()
47 {
48 std::cout << "Usage:\n\nsgauth <path_to_config>" << std::endl;
49 }
50 //-----------------------------------------------------------------------------
51 void CatchTERM(int)
52 {
53 running = false;
54 }
55 //-----------------------------------------------------------------------------
56 static void SetSignalHandlers()
57 {
58 struct sigaction newsa, oldsa;
59 sigset_t sigmask;
60
61 sigemptyset(&sigmask);
62 sigaddset(&sigmask, SIGTERM);
63 newsa.sa_handler = CatchTERM;
64 newsa.sa_mask = sigmask;
65 newsa.sa_flags = 0;
66 sigaction(SIGTERM, &newsa, &oldsa);
67
68 sigemptyset(&sigmask);
69 sigaddset(&sigmask, SIGINT);
70 newsa.sa_handler = CatchTERM;
71 newsa.sa_mask = sigmask;
72 newsa.sa_flags = 0;
73 sigaction(SIGINT, &newsa, &oldsa);
74
75 return;
76 }
77 //-----------------------------------------------------------------------------
78 int main(int argc, char *argv[])
79 {
80 SETTINGS settings;
81
82 if (argc == 2)
83     {
84     settings.SetConfFile(argv[1]);
85     }
86
87 if (settings.ReadSettings())
88     {
89     std::cerr << "Failed to read settings: '"
90               << settings.GetStrError() << "'" << std::endl;
91     Usage();
92     return -1;
93     }
94
95 SetSignalHandlers();
96
97 STORE_LOADER storeLoader(settings.GetModulesPath(), settings.GetStoreModuleSettings());
98 if (storeLoader.Load())
99     {
100     std::cerr << "Failed to load storage plugin: '" << storeLoader.GetStrError() << "'" << std::endl;
101     return -1;
102     }
103
104 STORE * dataStore = storeLoader.GetStore();
105
106 std::vector<std::string> userList;
107 if (dataStore->GetUsersList(&userList))
108     {
109     std::cerr << "Failed to get user list: '" << dataStore->GetStrError() << "'" << std::endl;
110     return -1;
111     }
112
113 std::vector<USER> users;
114 std::vector<std::string>::const_iterator it;
115 for (it = userList.begin(); it != userList.end(); ++it)
116     {
117     USER_CONF userConf;
118     if (dataStore->RestoreUserConf(&userConf, *it))
119         {
120         std::cerr << "Failed to read user conf: '" << dataStore->GetStrError() << "'" << std::endl;
121         return -1;
122         }
123     users.push_back(
124             USER(
125                 settings.GetServerName(),
126                 settings.GetServerPort(),
127                 settings.GetLocalPort(),
128                 *it,
129                 userConf.password
130             )
131     );
132     }
133
134 std::cout << "Successfully loaded " << users.size() << " users" << std::endl;
135
136 running = true;
137 while (running)
138     {
139     usleep(200000);
140     }
141
142 storeLoader.Unload();
143
144 return 0;
145 }
146 //-----------------------------------------------------------------------------