-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·80 lines (63 loc) · 1.86 KB
/
main.py
File metadata and controls
executable file
·80 lines (63 loc) · 1.86 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
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
import sys
import socket
import time
import os
#Own Libraries
from lib import *
os.chdir(os.path.dirname(__file__))
server = None
print("MissionControl Server (MIDaC V1)\n")
log = Logger("eventlog.log")
conf = ConfigHandler("config.conf", log)
log.logAndPrintMessage("Setting up server")
#Create Socket
try:
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind((conf.HOST, conf.PORT))
server.listen(conf.BACKLOG)
except socket.error:
if server:
server.close()
log.logAndPrintError("Could not open socket: "+`sys.exc_info()[1]`)
sys.exit()
#Create RSAL object, launch RSAL as subprocess and connect
rsal = RS(conf, log)
rsal.connect()
log.logAndPrintSuccess("RSAL Connected!")
log.logAndPrintSuccess("Server running!")
#Create client manager
clients = ClientManager(server, log, conf, rsal.LAO)
log.logAndPrintSuccess("Client Manager started!")
running = True
#main loop
while running:
try:
#run select on all clients
clients.update()
#perform handshake operations with clients that are in handshake
clients.handleHandshake()
#receive input from connnected clients and send it to rsal
control = clients.handleInput()
if control != None:
rsal.handleOutput(control)
#wait for half the steprate time to prevent uds complications
time.sleep(conf.SAMPLERATE/2)
#receive input from rsal and send it to all ready connected clients
rsal.request()
data = rsal.handleInput()
if data != None:
clients.handleOutput(data)
#wait for steprate time
time.sleep(conf.SAMPLERATE/2)
except KeyboardInterrupt:
log.logAndPrintWarning("Server manually stopped")
break
except:
log.logAndPrintError("Unexpected Exception catched")
#tidy up and close
server.close()
rsal.rsalProcss.terminate()
os.remove("uds_socket")
sys.exit()