Migrated to subscription based peers, added support for program listing

Still some bugs to be fixed related to controller actions.
This commit is contained in:
Philip Trauner 2016-12-18 23:24:25 +01:00
parent 8995fb8e6f
commit 597de36476

View file

@ -2,6 +2,7 @@ from sys import path
import os
import _thread
from time import strftime
from functools import partial
fl0w_path = os.path.dirname(os.path.realpath(__file__))
shared_path = os.path.dirname(os.path.realpath(__file__)) + "/Shared/"
@ -36,7 +37,7 @@ def plugin_unloaded():
class Fl0wClient(Client):
def setup(self, routes, fl0w, debug=False):
super().setup(routes, piping=True, debug=debug)
super().setup(routes, debug=debug)
self.fl0w = fl0w
@ -44,6 +45,7 @@ class Fl0wClient(Client):
self.fl0w.connected = True
if self.fl0w.debug:
Logging.info("Connection ready!")
self.send({"subscribe" : [2]}, "peers")
def closed(self, code, reason):
@ -52,6 +54,10 @@ class Fl0wClient(Client):
Logging.info("Connection closed: %s (%s)" % (reason, code))
def peer_unavaliable(self, peer):
sublime.error_message("The specifed controller is not connected anymore.")
class Info(Route):
def run(self, data, handler):
info = ""
@ -62,25 +68,36 @@ class Fl0wClient(Client):
class Peers(Route):
def start(self, handler):
self.selected_action_menu = None
def run(self, data, handler):
controller_menu = Menu()
handler.fl0w.controller_menu.clear()
for id_ in data:
action_menu = Menu()
print(id(action_menu))
action_menu.id_ = id_
action_menu += Entry("Programs",
"Lists all executable programs on the controller.",
action=partial(lambda handler, id_: handler.pipe(None, "list_programs", id_),
handler, id_))
action_menu += Entry("Set Name",
"Sets the hostname of the selected controller",
action=lambda: Input("New Hostname:",
initial_text=data[id_]["name"], on_done=self.set_hostname,
kwargs={"id_" : id_, "handler" : handler}).invoke(handler.fl0w.window))
action=lambda: Input("New Hostname:", initial_text=data[id_]["name"],
on_done=lambda hostname: handler.pipe({"set" : hostname},
"hostname", id_)).invoke(handler.fl0w.window))
action_menu += Entry("Processes",
"Lists processes currently running on controller.",
action=lambda handler: handler.pipe(None, "processes", id_),
kwargs={"handler" : handler})
controller_menu += Entry(data[id_]["name"], id_, sub_menu=action_menu)
controller_menu.invoke(handler.fl0w.window, back=handler.fl0w.main_menu)
action=partial(lambda handler: handler.pipe(None, "processes", id_),
handler, id_))
action_menu.back = handler.fl0w.controller_menu
handler.fl0w.controller_menu += Entry(data[id_]["name"], id_, sub_menu=action_menu,
action=self.set_selected_action_menu,
kwargs={"selected_action_menu" : action_menu})
def set_hostname(self, hostname, id_, handler):
handler.pipe({"set" : hostname}, "hostname", id_)
def set_selected_action_menu(self, selected_action_menu):
self.selected_action_menu = selected_action_menu
class Processes(Pipe):
@ -93,6 +110,20 @@ class Fl0wClient(Client):
view.set_read_only(True)
class ListPrograms(Pipe):
def run(self, data, peer, handler):
program_menu = Menu(subtitles=False)
for program in data:
program_menu += Entry(program,
action=lambda handler: handler.pipe(program,
"run_program",
handler.routes["peers"].selected_action_menu.id_),
kwargs={"handler" : handler})
program_menu.invoke(handler.fl0w.window,
back=handler.routes["peers"].selected_action_menu)
class Fl0w:
def __init__(self, window, debug=False):
self.connected = False
@ -127,8 +158,9 @@ class Fl0w:
self.main_menu = Menu()
self.controller_menu = Menu()
self.main_menu += Entry("Controllers", "All connected controllers.",
action=lambda: self.ws.send({"channel" : 2}, "peers"))
sub_menu=self.controller_menu)
self.main_menu += Entry("Settings", "General purpose settings",
sub_menu=self.settings)
self.main_menu += Entry("Disconnect", "Disconnect from server",
@ -175,10 +207,13 @@ class Fl0w:
try:
self.ws = Fl0wClient('ws://%s' % connect_details)
self.ws.setup({"info" : Fl0wClient.Info(), "peers" : Fl0wClient.Peers(),
"processes" : Fl0wClient.Processes()},
"processes" : Fl0wClient.Processes(),
"list_programs" : Fl0wClient.ListPrograms()},
self, debug=True)
self.ws.connect()
sublime.set_timeout_async(self.ws.run_forever, 0)
self.window.active_view().set_status(FL0W_STATUS,
"Connection opened '%s'" % self.folder)
except OSError as e:
sublime.error_message("Error during connection creation:\n %s" % str(e))