aboutsummaryrefslogtreecommitdiff
path: root/service.py
diff options
context:
space:
mode:
authorJonathan Weth <git@jonathanweth.de>2020-07-20 16:55:07 +0200
committerJonathan Weth <git@jonathanweth.de>2020-07-20 16:55:07 +0200
commit79f419d79c50ddca09209404d0cbc2d3b4f33429 (patch)
tree8c307b61b22a246b93257b9aea56991c4e57bbd4 /service.py
parentd7dd6a69cdd55439e19a5d6f1bcc2f241e608710 (diff)
downloadRWA.Support.SessionService-79f419d79c50ddca09209404d0cbc2d3b4f33429.tar.gz
RWA.Support.SessionService-79f419d79c50ddca09209404d0cbc2d3b4f33429.tar.bz2
RWA.Support.SessionService-79f419d79c50ddca09209404d0cbc2d3b4f33429.zip
Implement basic session daemon
Diffstat (limited to 'service.py')
-rw-r--r--service.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/service.py b/service.py
new file mode 100644
index 0000000..6125a75
--- /dev/null
+++ b/service.py
@@ -0,0 +1,81 @@
+import json
+import time
+from threading import Thread
+
+import dbus
+import dbus.service
+
+from session import Session
+
+
+class RWAService(dbus.service.Object):
+ def __init__(self):
+ self.bus = dbus.SessionBus()
+ name = dbus.service.BusName("de.rwa.rwa", bus=self.bus)
+
+ self.update_service_running = False
+ self.sessions = {}
+ super().__init__(name, "/RWA")
+
+ @dbus.service.method("de.rwa.rwa", out_signature="s")
+ def start(self):
+ """Start a new remote session."""
+ # Start session
+ session = Session()
+
+ # Add session to sessions list
+ self.sessions[session.pid] = session
+
+ # Start session update service
+ self._ensure_update_service()
+
+ return json.dumps(session.client_meta)
+
+ @dbus.service.method("de.rwa.rwa", in_signature="i")
+ def stop(self, pid: int):
+ """Stop a remote session."""
+ session = self.sessions[pid]
+ session.stop()
+
+ def _ensure_update_service(self):
+ """Start session update thread if it isn't already running."""
+ if not self.update_service_running:
+ self.update_thread = Thread(target=self._update_sessions)
+ self.update_thread.start()
+
+ def _update_sessions(self):
+ """Go through all running sessions and update their status.
+
+ Things that this function will do:
+ - Check if VNC is still running
+ - Kill websockify if VNC process is dead
+ """
+ while len(self.sessions.values()) > 0:
+ for session in list(self.sessions.values()):
+ print(f"Session #{session.pid}")
+
+ # Check if VNC process is still running
+ running = session.vnc_process_running
+ if running:
+ print("Session is running")
+ else:
+ print("Session is dead.")
+
+ session.stop()
+ del self.sessions[session.pid]
+
+ time.sleep(2)
+
+ self.update_service_running = False
+ # TODO Probably kill daemon here (quit main loop)
+
+
+if __name__ == "__main__":
+ import dbus.mainloop.glib
+ from gi.repository import GLib
+
+ dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
+
+ loop = GLib.MainLoop()
+ object = RWAService()
+ loop.run()