]> git.stg.codes - stg.git/blob - projects/sgauthstress/main.cpp
Authorization protocol stress test utility
[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 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31
32 #include <csignal>
33 #include <cstdio>
34 #include <cstring>
35 #include <iostream>
36 #include <vector>
37
38 #include "stg/ia.h"
39 #include "stg/common.h"
40 #include "web.h"
41 #include "settings_impl.h"
42
43 int mes;
44 char infoText[256];
45 char messageText[256];
46
47 const int winKOI = 0;
48
49 IA_CLIENT_PROT * clnp;
50 WEB * web = NULL;
51
52 using namespace std;
53
54 time_t stgTime;
55
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 void Usage()
60 {
61 printf("sgauth <path_to_config>\n");
62 }
63 //-----------------------------------------------------------------------------
64 void SetDirName(const vector<string> & dn, void *)
65 {
66 for (int j = 0; j < DIR_NUM; j++)
67     {
68     if (winKOI)
69         {
70         string dir;
71         KOIToWin(dn[j], &dir);
72         if (web)
73             web->SetDirName(dir, j);
74         }
75     else
76         {
77         if (web)
78             web->SetDirName(dn[j], j);
79         }
80     }
81 }
82 //-----------------------------------------------------------------------------
83 void StatUpdate(const LOADSTAT & ls, void *)
84 {
85 if (web)
86     web->UpdateStat(ls);
87 }
88 //-----------------------------------------------------------------------------
89 void StatusChanged(int, void *)
90 {
91 }
92 //-----------------------------------------------------------------------------
93 void ShowMessage(const string & message, int i, int, int, void *)
94 {
95 if (web)
96     web->AddMessage(message, i);
97 }
98 //-----------------------------------------------------------------------------
99 void ShowError(const string & message, int, void *)
100 {
101 if (web)
102      web->AddMessage(message, 0);
103 }
104 //-----------------------------------------------------------------------------
105 void CatchUSR1(int)
106 {
107 if (clnp->GetAuthorized())
108     {
109     cout << "Connect" << endl;
110     clnp->Connect();
111     }
112 }
113 //-----------------------------------------------------------------------------
114 void CatchUSR2(int)
115 {
116 cout << "Disconnect" << endl;
117 clnp->Disconnect();
118 }
119 //-----------------------------------------------------------------------------
120 void CatchTERM(int)
121 {
122 cout << "Terminated" << endl;
123 clnp->Disconnect();
124 sleep(2);
125 exit(0);
126 }
127 //-----------------------------------------------------------------------------
128 static void SetSignalHandlers()
129 {
130 struct sigaction newsa, oldsa;
131 sigset_t sigmask;
132
133 sigemptyset(&sigmask);
134 sigaddset(&sigmask, SIGTERM);
135 newsa.sa_handler = CatchTERM;
136 newsa.sa_mask = sigmask;
137 newsa.sa_flags = 0;
138 sigaction(SIGTERM, &newsa, &oldsa);
139
140 sigemptyset(&sigmask);
141 sigaddset(&sigmask, SIGINT);
142 newsa.sa_handler = CatchTERM;
143 newsa.sa_mask = sigmask;
144 newsa.sa_flags = 0;
145 sigaction(SIGINT, &newsa, &oldsa);
146
147 sigemptyset(&sigmask);
148 sigaddset(&sigmask, SIGUSR1);
149 newsa.sa_handler = CatchUSR1;
150 newsa.sa_mask = sigmask;
151 newsa.sa_flags = 0;
152 sigaction(SIGUSR1, &newsa, &oldsa);
153
154 sigemptyset(&sigmask);
155 sigaddset(&sigmask, SIGUSR2);
156 newsa.sa_handler = CatchUSR2;
157 newsa.sa_mask = sigmask;
158 newsa.sa_flags = 0;
159 sigaction(SIGUSR2, &newsa, &oldsa);
160
161 return;
162 }
163 //-----------------------------------------------------------------------------
164 int main(int argc, char *argv[])
165 {
166 SETTINGS_IMPL settings;
167
168 if (argc == 2)
169     {
170     settings.SetConfFile(argv[1]);
171     }
172 else
173     {
174     // Usage
175     }
176
177 if (settings.ReadSettings())
178     {
179     printf("ReadSettingsError\n");
180     printf("%s\n", settings.GetStrError().c_str());
181     exit(-1);
182     }
183 settings.Print();
184
185 if (settings.GetDaemon())
186     {
187     switch (fork())
188         {
189         case -1:
190             exit(1);
191             break;
192
193         case 0:
194             setsid();
195             break;
196
197         default:
198             exit(0);
199             break;
200         }
201     }
202
203 clnp = new IA_CLIENT_PROT(settings.GetServerName(), settings.GetServerPort(), settings.GetLocalPort());
204
205 if (!settings.GetNoWeb())
206     {
207     web = new WEB();
208     web->SetRefreshPagePeriod(settings.GetRefreshPeriod());
209     web->SetListenAddr(settings.GetListenWebIP());
210     web->Start();
211     }
212
213 clnp->SetLogin(settings.GetLogin());
214 clnp->SetPassword(settings.GetPassword());
215
216 clnp->SetStatusChangedCb(StatusChanged, NULL);
217 clnp->SetInfoCb(ShowMessage, NULL);
218 clnp->SetErrorCb(ShowError, NULL);
219 clnp->SetDirNameCb(SetDirName, NULL);
220 clnp->SetStatChangedCb(StatUpdate, NULL);
221 clnp->SetReconnect(settings.GetReconnect());
222
223 clnp->Start();
224
225 SetSignalHandlers();
226
227 #ifdef LINUX
228 for (int i = 1; i < argc; i++)
229     memset(argv[i], 0, strlen(argv[i]));
230
231 if(argc > 1)
232     strcpy(argv[1], "Connecting...");
233 #endif
234
235 #ifdef FREEBSD
236 setproctitle("Connecting...");
237 #endif
238 clnp->Connect();
239
240 while (1)
241     {
242     usleep(200000);
243
244     char state[20];
245
246     if (clnp->GetAuthorized())
247         {
248         if (settings.GetShowPid())
249             sprintf(state, "On %d", getpid());
250         else
251             strcpy(state, "Online");
252         }
253     else
254         {
255         if (settings.GetShowPid())
256             sprintf(state, "Off %d", getpid());
257         else
258             strcpy(state, "Offline");
259         }
260
261     #ifdef LINUX
262     for (int i = 1; i < argc; i++)
263         memset(argv[i], 0, strlen(argv[i]));
264     if(argc > 1)
265         strcpy(argv[1], state);
266     #endif
267
268     #ifdef FREEBSD
269     setproctitle(state);
270     #endif
271
272     #ifdef FREEBSD_5
273     setproctitle(state);
274     #endif
275     }
276
277 return 0;
278 }
279 //-----------------------------------------------------------------------------