-
Notifications
You must be signed in to change notification settings - Fork 0
/
CliApp.cpp
268 lines (238 loc) · 6.8 KB
/
CliApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* linc is not cyberoam v1.0
* Copyright (C) 2002 Mayur Naik <[email protected]>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License, with the
* following exceptions:
* 1) Any existing copyright or authorship information in any given source
* file must remain intact. If you modify a source file, a notice to that
* effect must be appended to the authorship information in the source file.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* In short you can not change/modify/remove the information above, if you do
* redistribute this program or any of its modified form, it must be free and
* there is NO WARRANTY, the author can not be held responsible for any kind
* of damage caused by this program.
*
* This source file is part of linc $Name: rel-1-2 $
*
* $Log: CliApp.cpp,v $
* Revision 1.5 2003/04/01 11:13:34 mrnk
* Fixed crash when /home/mayur was not defined. Added initscript.
*
* Revision 1.4 2002/12/17 17:46:39 mrnk
* dup()ing on fd 0,1,2 instead of closing.
*
* Revision 1.3 2002/06/22 11:33:18 mrnk
* Documented class CliApp
*
* Revision 1.2 2002/06/19 11:59:49 mrnk
* Added CVS keywords to all source files
*
*/
extern "C" {
#include <signal.h>
}
#include <stdlib.h>
#include <Cfg.h>
#include <CliApp.h>
#include <ConnState.h>
#include <ActiveState.h>
#include <InactiveState.h>
#include <DiscState.h>
#include <DeInitState.h>
//! the polling interval (i.e. interval for sending keepalive messages
#define POLLINTERVAL 120
//! the period spent in inactive state
#define NOTRUNNINTERVAL 300
//! period to wait after the server denies login
#define DENY_TMO 300
//! time to wait for server responses
#define RECV_TMO 10
//! system-wide cfg file
#define SYS_RCFILE "/etc/lincrc"
//! per user cfg file
#define USER_RCFILE ".lincrc"
//! initialize the instance pointer
CliApp *CliApp::c_poInstance = NULL;
//! singleton initializer method
CliApp *CliApp::Instance(const CmdLine& rkoCmd)
{
if (c_poInstance != NULL)
throw runtime_error("instance of CliApp exists");
c_poInstance = new CliApp(rkoCmd);
return c_poInstance;
}
//! singleton access method
CliApp *CliApp::Instance(void)
{
if (c_poInstance == NULL)
throw runtime_error("no previous instance of CliApp");
else
return c_poInstance;
}
//! constructor
CliApp::CliApp (const CmdLine& rkoCmd) : m_oCmd(rkoCmd)
{
/** if the user has specified debug mode, initialize the LogStream
* accordingly */
if (m_oCmd.DebugMode())
m_poLogStream = new LogStream(PACKAGE, LOG_DAEMON, true);
else
m_poLogStream = new LogStream(PACKAGE, LOG_DAEMON);
//! ok, we can log now
*m_poLogStream << ends << LOG_DEBUG
<< "CliApp constructor called." << endl;
//! try to get our configuration
m_poCfgFile = NULL;
try
{
const char *pcHome = getenv("HOME");
string ostrHomeDir("");
if (NULL != pcHome)
{
ostrHomeDir = pcHome;
}
string ostrHomeRCFile = ostrHomeDir + "/" + USER_RCFILE;
//! first from the per user config file
m_poCfgFile = new CfgFile(ostrHomeRCFile);
}
catch (runtime_error& rErr)
{
*m_poLogStream << ends << LOG_CRIT
<< rErr.what() << endl;
try
{
//! fall back to the system-wide config file
m_poCfgFile = new CfgFile(SYS_RCFILE);
}
catch (runtime_error& rErr)
{
*m_poLogStream << ends << LOG_CRIT
<< rErr.what() << endl;
//! we can't proceed without configuration
exit(-1);
}
}
try
{
//! get our configuration from the config file
Cfg oCfg(*m_poCfgFile);
//! initialize the UDP server proxy
m_poSrvPxy = new SrvPxy(oCfg.GetSrvAddr(), oCfg.GetSrvPort()
, RECV_TMO);
//! initialize application states
m_oMapIDToState[EXIT] = NULL;
m_oMapIDToState[ERROR] = NULL;
m_oMapIDToState[CONN] = new ConnState(
LoginMsg(oCfg.GetUserName()
, oCfg.GetAuthInfo()
, oCfg.GetHWAddr()), POLLINTERVAL, DENY_TMO);
m_oMapIDToState[ACTIVE] = new ActiveState(
ContinueMsg(oCfg.GetUserName())
, POLLINTERVAL);
m_oMapIDToState[DISC] = new DiscState(
LogoutMsg(oCfg.GetUserName()));
m_oMapIDToState[DEINIT] = new DeInitState(
LogoutMsg(oCfg.GetUserName()));
m_oMapIDToState[INACTIVE] = new InActiveState(NOTRUNNINTERVAL);
}
catch (runtime_error& rErr)
{
//! if any of that failed, we can NOT proceed
*m_poLogStream << ends << LOG_CRIT
<< rErr.what() << endl;
exit(-1);
}
//! set signal handlers
signal(SIGUSR1, CliApp::SignalHandler);
signal(SIGUSR2, CliApp::SignalHandler);
signal(SIGINT, CliApp::SignalHandler);
signal(SIGTERM, CliApp::SignalHandler);
signal(SIGHUP, SIG_IGN);
}
int32_t CliApp::Run (void)
{
//! enter the initial state
m_poState = m_oMapIDToState[CONN];
//! yay
*m_poLogStream << ends << LOG_DEBUG
<< "CliApp running." << endl;
//! unless there is an error or we are asked to exit,
while (m_poState)
{
//! run the current state logic
m_poState->Logic();
}
return 0;
}
//! state transition
void CliApp::StateTrans(STATE_ID_en enNewState)
{
m_poState = m_oMapIDToState[enNewState];
}
//! destructor
CliApp::~CliApp (void)
{
*m_poLogStream << ends << LOG_DEBUG
<< "CliApp destructor called." << endl;
//! delete all application states
for (map<STATE_ID_en, CliState *>::iterator
oMapIter = m_oMapIDToState.begin();
oMapIter != m_oMapIDToState.end();
oMapIter++
)
if (oMapIter->second)
{
delete oMapIter->second;
oMapIter->second = NULL;
}
//! clean the UDP server proxy
delete m_poSrvPxy;
m_poSrvPxy = NULL;
//! delete the application logger
delete m_poLogStream;
m_poLogStream = NULL;
//! delete the configuration file
delete m_poCfgFile;
m_poCfgFile = NULL;
}
//! singleton destructor
void CliApp::Destroy (void)
{
if (c_poInstance)
{
delete c_poInstance;
c_poInstance = NULL;
}
}
//! signal handler
void CliApp::SignalHandler(int32_t i32Sig)
{
switch (i32Sig)
{
case SIGUSR1:
Instance()->StateTrans(CONN);
break;
case SIGUSR2:
Instance()->StateTrans(DISC);
break;
case SIGINT:
Instance()->StateTrans(DEINIT);
break;
case SIGTERM:
Instance()->StateTrans(DEINIT);
break;
default:
Instance()->StateTrans(ERROR);
}
}