/
server.py
executable file
·159 lines (124 loc) · 3.69 KB
/
server.py
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
#! /usr/bin/env python
import sys
import os
import signal
import logging
import logging.config
import asyncore
import time
import gc
from server.Configuration import configuration, ConfigurationError
from server.ChatServer import ChatServer
from server.TelnetServer import TelnetServer
from server.LocalDatabase import LocalDatabase
from server.MySQLDatabase import MySQLDatabase
def initialize_configuration():
global config
global configfilename
configfilename = "server.conf"
config = configuration()
print "Reading configuration from " + configfilename + "..."
ret = config.read(configfilename)
config.validate()
return ret
def save_configuration():
print "Saving configuration to " + configfilename + "..."
configfile = open(configfilename, "w")
config.write(configfile)
configfile.close()
def initialize_logger():
global logger
print "Reading logging config from logging.ini..."
logging.config.fileConfig("logging.ini")
logger = logging.getLogger("chattest.server")
logger.info("Server logger initialized!")
def initialize_database():
global database
type = config.get_database_type()
if(type == "local"):
database = LocalDatabase()
elif(type == "mysql"):
database = MySQLDatabase()
else:
logger.error("Unknown database type '" + type + "'")
return False
return database.connect()
def signal_handler(signum, frame):
if(signum == signal.SIGHUP):
logger.info("Caught SIGHUP, restarting...")
server.restart(database)
telnet.restart(server)
logger.info("Running garbage collector...")
gc.collect()
def initialize_signal_handlers():
logger.info("Initializing signal handlers...")
signal.signal(signal.SIGHUP, signal_handler)
def daemonize():
print "Daemonizing..."
try:
# first child
pid = os.fork()
if(pid == 0):
os.setsid()
#signal.signal(signal.SIGHUP, signal.SIG_IGN)
# second child
pid = os.fork()
if(pid == 0):
#os.chdir("/")
#os.umask(0)
pass
else:
os._exit(0)
else:
os._exit(0)
# close parent file descriptors
try:
maxfd = os.sysconf("SC_OPEN_MAX")
except(AttributeError, ValueError):
print "WARNING: No SC_OPEN_MAX, using 256"
maxfd = 256
try:
for fd in range(0, maxfd):
os.close(fd)
except OSError:
pass
os.open("/dev/null", os.O_RDONLY)
os.open("/dev/null", os.O_RDWR)
os.open("/dev/null", os.O_RDWR)
except OSError, err:
print "OS error: " + str(err)
def main():
global server
global telnet
# read the configuration
try:
if(not initialize_configuration()):
print "WARNING: Reading configuration failed"
save_configuration()
except ConfigurationError, err:
logger.critical(str(err))
sys.exit(1)
# daemonize
if(config.get_misc_daemon()):
daemonize()
# initialize the logger
initialize_logger()
config.dump()
# connect to the database
if(not initialize_database()):
sys.exit(1)
# create the servers
server = ChatServer(database)
telnet = TelnetServer(server)
# init the signal handlers
initialize_signal_handlers()
try:
logger.info("Entering event loop...")
asyncore.loop(1)
except KeyboardInterrupt, err:
server.quit()
telnet.quit()
save_configuration()
logger.info("Server terminated successfully")
if __name__ == "__main__":
main()