Migration to Highway, incorporate piping, hostname changing

The port of the Wallaby client has kicked off and with it come lots of broken features.
WallabyControl will have to be remade to incorporate piping and routes for sensor readouts as well as wi-fi network selection have to be added.

As a quick piping demo the Hostname changer route was implemented which already works in the Sublime Text client.
This commit is contained in:
Philip Trauner 2016-12-02 23:20:40 +01:00
parent 4d6dcf7d84
commit e2bfb79cdf

View file

@ -1,21 +1,17 @@
from ESock import ESock from Highway import Route, Pipe, Client
from Sync import SyncClient
from Utils import is_socket_related_error
from Utils import capture_trace
from Utils import is_wallaby
import Routing
import Logging import Logging
import Config import Config
from Utils import is_wallaby, set_hostname, get_hostname
import socket import socket
import time import time
import os import os
import sys import sys
import platform
import subprocess import subprocess
import _thread import _thread
CHANNEL = "w" CHANNEL = 2
IS_WALLABY = is_wallaby() IS_WALLABY = is_wallaby()
PATH = "/home/root/Documents/KISS/bin/" if IS_WALLABY else (sys.argv[1] if len(sys.argv) > 1 else None) PATH = "/home/root/Documents/KISS/bin/" if IS_WALLABY else (sys.argv[1] if len(sys.argv) > 1 else None)
@ -26,7 +22,8 @@ if not PATH:
if not IS_WALLABY: if not IS_WALLABY:
Logging.warning("Binaries that were created for Wallaby Controllers will not run on a simulated Wallaby.") Logging.warning("Binaries that were created for Wallaby Controllers will not run on a simulated Wallaby.")
class WallabyControl(Routing.ClientRoute):
class WallabyControl(Route):
def __init__(self, output_unbuffer): def __init__(self, output_unbuffer):
self.output_unbuffer = output_unbuffer self.output_unbuffer = output_unbuffer
self.actions_with_params = {"run" : self.run_program} self.actions_with_params = {"run" : self.run_program}
@ -80,69 +77,47 @@ class WallabyControl(Routing.ClientRoute):
handler.sock.close() handler.sock.close()
def get_wallaby_hostname():
return open("/etc/hostname", "r").read()
class GetInfo(Routing.ClientRoute): class Subscribe(Route):
def run(self, data, handler): def start(self, handler):
if data == "": handler.send({"name" : get_hostname(), "channel" : CHANNEL}, "subscribe")
handler.sock.send({"type" : CHANNEL,
"name" : platform.node() if not IS_WALLABY else get_wallaby_hostname()}, "get_info")
elif "name" in data:
if IS_WALLABY:
open("/etc/hostname", "w").write(str(data["name"]))
else:
Logging.info("Hostname change: '%s'" % str(data["name"]))
class WallabyClient: class Hostname(Pipe):
def __init__(self, host_port_pair, routes, debug=False): def run(self, data, peer, handler):
self.sock = ESock(socket.create_connection(host_port_pair), debug=debug) if type(data) is dict:
self.connected = True if "set" in data:
self.debug = debug set_hostname(str(data["set"]))
self.sync = SyncClient(self.sock, PATH, "w_sync", debug=True)
routes.update({"w_sync" : self.sync})
self.routes = routes
def start(self):
self.sync.start()
while 1 and self.connected:
data = self.sock.recv()
try:
if data[1] in self.routes:
self.routes[data[1]].run(data[0], self)
except Exception as e:
if not is_socket_related_error(e):
capture_trace()
break
class Handler(Client):
def setup(self, routes, debug=False):
super().setup(routes, piping=True, debug=debug)
def stop(self):
self.sock.close()
CONFIG_PATH = "wallaby.cfg" CONFIG_PATH = "wallaby.cfg"
config = Config.Config() config = Config.Config()
config.add(Config.Option("server_address", ("127.0.0.1", 3077))) config.add(Config.Option("server_address", "ws://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", False, validator=lambda x: True if True or False else False))
config.add(Config.Option("output_unbuffer", "stdbuf")) config.add(Config.Option("output_unbuffer", "stdbuf"))
config.add(Config.Option("compression_level", 0, validator=lambda x: x >= 0 and x <= 9))
try: try:
config = config.read_from_file(CONFIG_PATH) config = config.read_from_file(CONFIG_PATH)
except FileNotFoundError: except FileNotFoundError:
config.write_to_file(CONFIG_PATH) config.write_to_file(CONFIG_PATH)
Logging.info("Config file created. Please modify to reflect your setup.")
exit(1)
config = config.read_from_file(CONFIG_PATH) config = config.read_from_file(CONFIG_PATH)
wallaby_client = WallabyClient(config.server_address,
{"wallaby_control" : WallabyControl(config.output_unbuffer), "get_info" : GetInfo()},
debug=config.debug)
try: try:
wallaby_client.start() ws = Handler(config.server_address)
# setup has to be called before the connection is established
ws.setup({"subscribe" : Subscribe(), "hostname" : Hostname()},
debug=config.debug)
ws.connect()
ws.run_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
wallaby_client.stop() ws.close()