forked from mipmop01/Beat-Station
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_controller.py
51 lines (42 loc) · 1.49 KB
/
server_controller.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
import subprocess
import socket
import urlparse
UDP_IP="127.0.0.1"
UDP_PORT=8019
sock = socket.socket( socket.AF_INET, # Internet
socket.SOCK_DGRAM ) # UDP
sock.bind( (UDP_IP,UDP_PORT) )
last_ticker_state = None
def handle_message(data, addr):
global last_ticker_state
params = urlparse.parse_qs(data)
print(data)
try:
if params["type"][0] == "log" and str(params["log"][0]) and str(params["message"][0]):
open(params["log"][0],"a+").write(params["message"][0]+"\n")
except IOError:
pass
except KeyError:
pass
try:
if params["type"][0] == "ticker_state" and str(params["message"][0]):
last_ticker_state = str(params["message"][0])
except KeyError:
pass
try:
if params["type"][0] == "startup" and last_ticker_state:
open("crashlog.txt","a+").write("Server exited, last ticker state was: "+last_ticker_state+"\n")
except KeyError:
pass
sock.settimeout(60*6) # 10 minute timeout
while True:
try:
data, addr = sock.recvfrom( 1024 ) # buffer size is 1024 bytes
handle_message(data,addr)
except socket.timeout:
# try to start the server again
print("Server timed out.. attempting restart.")
if last_ticker_state:
open("crashmsg.txt","a+").write("Server crashed, trying to reboot. last ticker state: "+last_ticker_state+"\n")
subprocess.call("killall -9 DreamDaemon")
subprocess.call("./start")