-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
69 lines (59 loc) · 1.71 KB
/
main.cpp
File metadata and controls
69 lines (59 loc) · 1.71 KB
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
#include "main.hpp"
/**
@file main.cpp
@brief Entry point of the IRC server program
*/
/**
@brief Pointer to the global IRC server instance
This pointer is initialized to NULL and is later assigned a new Server object
by the main() function. It is used in the sigHandler() function to clean up
the Server object when the program is terminated by a signal.
*/
Server *g_ircserver = NULL;
void handleUserInput();
/**
@brief Signal handler for the IRC server program
This function is registered as the signal handler for the SIGINT signal, which is
sent to the program when the user presses CTRL-C or the program is terminated
by the operating system. It checks if the global g_ircserver pointer is not NULL,
deletes all channels stored in the server's channel list, calls the Server object's
destructor, and exits the program with a status of 0. If g_ircserver is NULL,
it simply exits the program with a status of 0.
@param sig The signal that triggered the signal handler
*/
void sigHandler(int sig)
{
signal(sig, sigHandler);
if (g_ircserver != NULL)
g_ircserver->freeResources();
delete g_ircserver;
exit(0);
}
int main(int argc, const char **argv)
{
if (argc != 3)
{
std::cerr << "./ircserv [port number] [password]\n";
return 1; // Return non-zero to indicate failure
}
// Create server object
Server *server;
try
{
server = new Server(argv[1], argv[2]);
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
exit(1);
}
g_ircserver = server;
// Register signal handler for SIGINT
signal(SIGINT, sigHandler);
// Launch server
server->launchServer();
// Free resources and delete server object
server->freeResources();
delete server;
return 0; // Return zero to indicate success
}