]> git.stg.codes - stg.git/blob - projects/sgauthstress/main.cpp
Initialize and fill proto with users in main
[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 "proto.h"
39 #include "user.h"
40
41 time_t stgTime;
42 bool running;
43
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 void Usage()
48 {
49 std::cout << "Usage:\n\nsgauth <path_to_config>" << std::endl;
50 }
51 //-----------------------------------------------------------------------------
52 void CatchTERM(int)
53 {
54 running = false;
55 }
56 //-----------------------------------------------------------------------------
57 static void SetSignalHandlers()
58 {
59 struct sigaction newsa, oldsa;
60 sigset_t sigmask;
61
62 sigemptyset(&sigmask);
63 sigaddset(&sigmask, SIGTERM);
64 newsa.sa_handler = CatchTERM;
65 newsa.sa_mask = sigmask;
66 newsa.sa_flags = 0;
67 sigaction(SIGTERM, &newsa, &oldsa);
68
69 sigemptyset(&sigmask);
70 sigaddset(&sigmask, SIGINT);
71 newsa.sa_handler = CatchTERM;
72 newsa.sa_mask = sigmask;
73 newsa.sa_flags = 0;
74 sigaction(SIGINT, &newsa, &oldsa);
75
76 return;
77 }
78 //-----------------------------------------------------------------------------
79 int main(int argc, char *argv[])
80 {
81 SETTINGS settings;
82
83 if (argc == 2)
84     {
85     settings.SetConfFile(argv[1]);
86     }
87
88 if (settings.ReadSettings())
89     {
90     std::cerr << "Failed to read settings: '"
91               << settings.GetStrError() << "'" << std::endl;
92     Usage();
93     return -1;
94     }
95
96 SetSignalHandlers();
97
98 PROTO proto(settings.GetServerName(),
99             settings.GetServerPort(),
100             settings.GetLocalPort(),
101             1);
102
103 STORE_LOADER storeLoader(settings.GetModulesPath(), settings.GetStoreModuleSettings());
104 if (storeLoader.Load())
105     {
106     std::cerr << "Failed to load storage plugin: '" << storeLoader.GetStrError() << "'" << std::endl;
107     return -1;
108     }
109
110 STORE * dataStore = storeLoader.GetStore();
111
112 std::vector<std::string> userList;
113 if (dataStore->GetUsersList(&userList))
114     {
115     std::cerr << "Failed to get user list: '" << dataStore->GetStrError() << "'" << std::endl;
116     return -1;
117     }
118
119 std::vector<std::string>::const_iterator it;
120 for (it = userList.begin(); it != userList.end(); ++it)
121     {
122     USER_CONF userConf;
123     if (dataStore->RestoreUserConf(&userConf, *it))
124         {
125         std::cerr << "Failed to read user conf: '" << dataStore->GetStrError() << "'" << std::endl;
126         return -1;
127         }
128     proto.AddUser(
129             USER(
130                 *it,
131                 userConf.password,
132                 userConf.ips[0].ip
133             )
134     );
135     }
136
137 std::cout << "Successfully loaded " << proto.UserCount() << " users" << std::endl;
138
139 running = true;
140 while (running)
141     {
142     usleep(200000);
143     }
144
145 storeLoader.Unload();
146
147 return 0;
148 }
149 //-----------------------------------------------------------------------------