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