Fixed shutdown time, added sync route, ...

This commit is contained in:
PhilipTrauner 2016-03-08 08:47:28 +01:00
parent 1dfaaa59e3
commit 4ac7af1097

View file

@ -10,6 +10,7 @@ from .Broadcast import Broadcast
from time import strftime from time import strftime
from time import time from time import time
import json import json
import os
class Info(Routing.ServerRoute): class Info(Routing.ServerRoute):
@ -63,13 +64,13 @@ class Handler(Server.Handler):
SUBLIME = 1 SUBLIME = 1
WALLABY = 2 WALLABY = 2
def setup(self, routes, broadcast, last_stop_time): def setup(self, routes, broadcast, last_stop):
Logging.info("Handler for '%s:%d' initalised." % (self.sock.address, self.sock.port)) Logging.info("Handler for '%s:%d' initalised." % (self.sock.address, self.sock.port))
self.routes = Routing.create_routes(routes)
self.cached_routes = {} self.cached_routes = {}
self.broadcast = broadcast self.broadcast = broadcast
self.last_stop_time = last_stop_time self.last_stop = last_stop
self.channel = None self.channel = None
self.routes = Routing.create_routes(routes, self)
self.start_time = time() self.start_time = time()
@ -104,6 +105,8 @@ INFO_PATH = "server.info"
config = Config.Config() config = Config.Config()
config.add(Config.Option("server_address", ("127.0.0.1", 3077))) config.add(Config.Option("server_address", ("127.0.0.1", 3077)))
config.add(Config.Option("debug", True, validator=lambda x: True if True or False else False)) config.add(Config.Option("debug", True, validator=lambda x: True if True or False else False))
config.add(Config.Option("binary_path", "Binaries", validator=os.path.isdir))
config.add(Config.Option("source_path", "Source", validator=os.path.isdir))
try: try:
config = config.read_from_file(CONFIG_PATH) config = config.read_from_file(CONFIG_PATH)
@ -121,19 +124,22 @@ for channel in Handler.Channels.__dict__:
try: try:
Logging.header("fl0w server started on '%s:%d'" % (config.server_address[0], config.server_address[1])) Logging.header("fl0w server started on '%s:%d'" % (config.server_address[0], config.server_address[1]))
last_stop_time = 0 # Trying to obtain last stop time
last_stop = 0
try: try:
last_stop_time = json.loads(open(INFO_PATH, "r").read())["last_stop_time"] last_stop = json.loads(open(INFO_PATH, "r").read())["last_stop"]
except IOError: except IOError:
Logging.warning("Unable to obtain last shutdown time. (You can ignore this message if it's your first time starting fl0w)") Logging.warning("Unable to obtain last shutdown time. (You can ignore this message if it's your first time starting fl0w)")
except (ValueError, KeyError): except (ValueError, KeyError):
Logging.error("%s has been modified an contains invalid information." % CONFIG_PATH) Logging.error("%s has been modified an contains invalid information." % CONFIG_PATH)
Logging.info("Last shutdown time: %d" % last_stop_time) Logging.info("Last shutdown time: %d" % last_stop)
server.run(Handler, server.run(Handler,
{"broadcast" : broadcast, "last_stop_time" : last_stop_time, {"broadcast" : broadcast, "last_stop" : last_stop,
"routes" : {"info" : Info(), "wallaby_control" : WallabyControl(), "set_type" : SetType(), "routes" : {"info" : Info(), "wallaby_control" : WallabyControl(), "set_type" : SetType(),
"w_sync" : SyncServer("Wallaby")}}) "w_sync" : SyncServer(config.binary_path, Handler.Channels.WALLABY)}})
except KeyboardInterrupt: except KeyboardInterrupt:
Logging.header("Gracefully shutting down server.")
server.stop() server.stop()
open(INFO_PATH, "w").write(json.dumps({"last_stop_time" : time()})) # Dumping stop time
Logging.warning("Gracefully shutting down server.") open(INFO_PATH, "w").write(json.dumps({"last_stop" : time()}))
Logging.success("Server shutdown successful.")