Added WallabyControl, SetType, channel integration, rewrote info to provide some actual information, renamed main handler, removed VarSync and added error_report support. A ton of stuff basically.

This commit is contained in:
PhilipTrauner 2016-01-31 20:44:24 +01:00
parent f2468ffeb6
commit 22b6002cd2

View file

@ -1,46 +1,96 @@
import Logging import Logging
import Routing import Routing
import VarSync
from .AsyncServer import Server from .AsyncServer import Server
from .Broadcast import Broadcast from .Broadcast import Broadcast
from time import strftime
class Info(Routing.ServerRoute): class Info(Routing.ServerRoute):
def setup(self, **kwargs): def setup(self):
self.message = kwargs.pop("message") self.start_time = strftime("%H:%M:%S")
self.counter = 0
def run(self, data, handler): def run(self, data, handler):
self.counter += 1 handler.sock.send("Start time: %s\nConnected ST clients: %d\nConnected Wallabies: %d\nAvaliable routes: %s" % (
handler.sock.send("Currently connected: %d (Command has been called %d times.) (%s)" % self.start_time, len(handler.broadcast.channels[Handler.Channels.SUBLIME]),
(len(handler.broadcast.socks), self.counter, self.message), "info") len(handler.broadcast.channels[Handler.Channels.WALLABY]), ", ".join([route for route in list(handler.routes.keys())])), "info")
class SublimeHandler(Server.Handler): class WallabyControl(Routing.ServerRoute):
def setup(self): def run(self, data, handler):
Logging.info("Handler for '%s:%d' initalised." % (self.info[0], self.info[1])) if data == "list":
self.routes = Routing.create_routes(self.kwargs.pop("routes")) wallabies = []
self.broadcast = self.kwargs.pop("broadcast") for wallaby in handler.broadcast.channels[Handler.Channels.WALLABY]:
self.broadcast.add(self.sock) wallabies.append("%s:%d" % (wallaby.address, wallaby.port))
handler.sock.send(wallabies, "wallaby_control")
elif type(data) is dict:
for wallaby in handler.broadcast.channels[Handler.Channels.WALLABY]:
address_pair = "%s:%d" % (wallaby.address, wallaby.port)
if address_pair in data.keys():
if data[address_pair] in ("stop", "restart", "disconnect"):
wallaby.send(data[address_pair], "wallaby_control")
elif type(data[address_pair]) is dict:
if "run" in data[address_pair]:
Logging.warning("Remote binary execution not yet implemented. (file_sync and compile required)")
return
handler.sock.send("Wallaby not connected anymore.", "error_report")
class SetType(Routing.ServerRoute):
def run(self, data, handler):
if data == "st":
handler.channel = Handler.Channels.SUBLIME
handler.broadcast.add(handler.sock, handler.channel)
elif data == "w":
handler.channel = Handler.Channels.WALLABY
handler.broadcast.add(handler.sock, handler.channel)
else:
handler.sock.close()
return
Logging.info("'%s:%d' has identified as a %s client." % (handler.info[0], handler.info[1],
"Sublime Text" if handler.channel == Handler.Channels.SUBLIME else
"Wallaby" if handler.channel == Handler.Channels.WALLABY else
"Unknown (will not subscribe to broadcast)"))
class Handler(Server.Handler):
class Channels:
SUBLIME = 1
WALLABY = 2
def setup(self, routes, broadcast):
Logging.info("Handler for '%s:%d' initalised." % (self.sock.address, self.sock.port))
self.routes = Routing.create_routes(routes)
self.broadcast = broadcast
self.channel = None
def handle(self, data, route): def handle(self, data, route):
if route in self.routes: if route in self.routes:
self.routes[route].run(data, self) self.routes[route].run(data, self)
else:
self.sock.send("Invalid route '%s'" % route, "error_report")
def finish(self): def finish(self):
self.broadcast.remove(self.sock) if self.channel != None:
Logging.info("'%s:%d' disconnected." % (self.info[0], self.info[1])) self.broadcast.remove(self.sock, self.channel)
Logging.info("'%s:%d' disconnected." % (self.sock.address, self.sock.port))
server = Server(("127.0.0.1", 3077), debug=True) server = Server(("127.0.0.1", 3077), debug=True)
varsync = VarSync.Server()
varsync.links = ["link1", "link2"] broadcast = Broadcast()
print(varsync.attrs) # Populating broadcast channels with all channels defined in Handler.Channels
for channel in Handler.Channels.__dict__:
if not channel.startswith("_"):
broadcast.add_channel(Handler.Channels.__dict__[channel])
try: try:
Logging.header("fl0w server started.") Logging.header("fl0w server started.")
server.run(SublimeHandler, {"routes" : {"info" : (Info, {"message" : "Test"}), "varsync" : varsync}, "broadcast" : Broadcast()}) server.run(Handler,
{"broadcast" : broadcast,
"routes" : {"info" : Info(), "wallaby_control" : WallabyControl(), "set_type" : SetType()}})
except KeyboardInterrupt: except KeyboardInterrupt:
server.stop() server.stop()
Logging.warning("Gracefully shutting down server.") Logging.warning("Gracefully shutting down server.")