]> git.stg.codes - stg.git/blob - projects/sgauthstress/main.cpp
Timeout fixed, proto starting and stopping added
[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             1000);
102
103 if (!proto.Start())
104     {
105     std::cerr << "Failed to start listening thread: '" << proto.GetStrError() << "'" << std::endl;
106     return -1;
107     }
108
109 STORE_LOADER storeLoader(settings.GetModulesPath(), settings.GetStoreModuleSettings());
110 if (storeLoader.Load())
111     {
112     std::cerr << "Failed to load storage plugin: '" << storeLoader.GetStrError() << "'" << std::endl;
113     return -1;
114     }
115
116 STORE * dataStore = storeLoader.GetStore();
117
118 std::vector<std::string> userList;
119 if (dataStore->GetUsersList(&userList))
120     {
121     std::cerr << "Failed to get user list: '" << dataStore->GetStrError() << "'" << std::endl;
122     return -1;
123     }
124
125 std::vector<std::string>::const_iterator it;
126 for (it = userList.begin(); it != userList.end(); ++it)
127     {
128     USER_CONF userConf;
129     if (dataStore->RestoreUserConf(&userConf, *it))
130         {
131         std::cerr << "Failed to read user conf: '" << dataStore->GetStrError() << "'" << std::endl;
132         return -1;
133         }
134     proto.AddUser(
135             USER(
136                 *it,
137                 userConf.password,
138                 userConf.ips[0].ip
139             ),
140             true
141     );
142     }
143
144 std::cout << "Successfully loaded " << proto.UserCount() << " users" << std::endl;
145
146 running = true;
147 while (running)
148     {
149     usleep(200000);
150     }
151
152 proto.Stop();
153
154 storeLoader.Unload();
155
156 return 0;
157 }
158 //-----------------------------------------------------------------------------