1 | /* |
---|
2 | * Author and Copyright of this file and of the stellarium telescope feature: |
---|
3 | * Johannes Gajdosik, 2006 |
---|
4 | * |
---|
5 | * This program is free software; you can redistribute it and/or |
---|
6 | * modify it under the terms of the GNU General Public License |
---|
7 | * as published by the Free Software Foundation; either version 2 |
---|
8 | * of the License, or (at your option) any later version. |
---|
9 | * |
---|
10 | * This program is distributed in the hope that it will be useful, |
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
13 | * GNU General Public License for more details. |
---|
14 | * |
---|
15 | * You should have received a copy of the GNU General Public License |
---|
16 | * along with this program; if not, write to the Free Software |
---|
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "ServerBAO.hpp" |
---|
21 | #include "LogFile.hpp" |
---|
22 | |
---|
23 | #ifdef WIN32 |
---|
24 | #include "Socket.hpp" // winsock2 |
---|
25 | #else |
---|
26 | #include <signal.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #include <iostream> |
---|
30 | using namespace std; |
---|
31 | |
---|
32 | |
---|
33 | static volatile bool continue_looping = true; |
---|
34 | |
---|
35 | #ifdef WIN32 |
---|
36 | static |
---|
37 | BOOL signal_handler(DWORD fdwCtrlType) |
---|
38 | { |
---|
39 | switch (fdwCtrlType) |
---|
40 | { |
---|
41 | case CTRL_C_EVENT: |
---|
42 | case CTRL_BREAK_EVENT: |
---|
43 | case CTRL_CLOSE_EVENT: |
---|
44 | case CTRL_SHUTDOWN_EVENT: |
---|
45 | continue_looping = false; |
---|
46 | return TRUE; |
---|
47 | case CTRL_LOGOFF_EVENT: |
---|
48 | break; |
---|
49 | } |
---|
50 | return FALSE; |
---|
51 | } |
---|
52 | #else |
---|
53 | #include <signal.h> |
---|
54 | static |
---|
55 | void signal_handler(int signum) |
---|
56 | { |
---|
57 | switch (signum) |
---|
58 | { |
---|
59 | case SIGINT: |
---|
60 | case SIGQUIT: |
---|
61 | case SIGTERM: |
---|
62 | continue_looping = false; |
---|
63 | break; |
---|
64 | default: |
---|
65 | // just ignore |
---|
66 | break; |
---|
67 | } |
---|
68 | } |
---|
69 | #endif |
---|
70 | |
---|
71 | |
---|
72 | int main(int argc, char *argv[]) |
---|
73 | { |
---|
74 | cout << "This is " << argv[0] << ", built at " |
---|
75 | << __DATE__ << ", " << __TIME__ << endl; |
---|
76 | #ifdef WIN32 |
---|
77 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)signal_handler, TRUE)) |
---|
78 | { |
---|
79 | cout << "SetConsoleCtrlHandler failed" << endl; |
---|
80 | return 127; |
---|
81 | } |
---|
82 | WSADATA wsaData; |
---|
83 | if (WSAStartup(0x202, &wsaData) != 0) |
---|
84 | { |
---|
85 | cout << "WSAStartup failed" << endl; |
---|
86 | return 127; |
---|
87 | } |
---|
88 | #else |
---|
89 | // SIGPIPE is normal operation when we send while the other side |
---|
90 | // has already closed the socket. We must ignore it: |
---|
91 | signal(SIGPIPE, SIG_IGN); |
---|
92 | signal(SIGINT, signal_handler); |
---|
93 | signal(SIGTERM, signal_handler); |
---|
94 | signal(SIGQUIT, signal_handler); |
---|
95 | // maybe the user wants to continue after SIGHUP ? |
---|
96 | //signal(SIGHUP,signal_handler); |
---|
97 | #endif |
---|
98 | |
---|
99 | int port; |
---|
100 | if ((argc < 2 || argc > 4) || |
---|
101 | 1 != sscanf(argv[1], "%d", &port) || |
---|
102 | port < 0 || port > 0xFFFF) |
---|
103 | { |
---|
104 | cout << "Usage: " << argv[0] << " port [ignored_arg logfile]" << endl; |
---|
105 | return 126; |
---|
106 | } |
---|
107 | if (argc > 3) |
---|
108 | { |
---|
109 | SetLogFile(argv[3]); |
---|
110 | *log_file << Now() << "This is " << argv[0] << ", built on " |
---|
111 | << __DATE__ << ", " << __TIME__ << endl; |
---|
112 | } |
---|
113 | ServerDummy server(port); |
---|
114 | while (continue_looping) |
---|
115 | { |
---|
116 | server.step(10000); |
---|
117 | } |
---|
118 | |
---|
119 | #ifdef WIN32 |
---|
120 | WSACleanup(); |
---|
121 | #endif |
---|
122 | *log_file << Now() << "bye." << endl; |
---|
123 | return 0; |
---|
124 | } |
---|