diff options
Diffstat (limited to 'module/remote/wsbackend')
-rw-r--r-- | module/remote/wsbackend/AbstractHandler.py | 79 | ||||
-rw-r--r-- | module/remote/wsbackend/ApiHandler.py | 50 | ||||
-rw-r--r-- | module/remote/wsbackend/EventHandler.py | 32 |
3 files changed, 107 insertions, 54 deletions
diff --git a/module/remote/wsbackend/AbstractHandler.py b/module/remote/wsbackend/AbstractHandler.py new file mode 100644 index 000000000..291dbf100 --- /dev/null +++ b/module/remote/wsbackend/AbstractHandler.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +############################################################################### +# Copyright(c) 2008-2012 pyLoad Team +# http://www.pyload.org +# +# This file is part of pyLoad. +# pyLoad is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# Subjected to the terms and conditions in LICENSE +# +# @author: RaNaN +############################################################################### + +from mod_pywebsocket.msgutil import send_message +from mod_pywebsocket.util import get_class_logger +from module.remote.json_converter import loads, dumps + + +class AbstractHandler: + """ + Abstract Handler providing common methods shared across WebSocket handlers + """ + + def __init__(self, api): + self.log = get_class_logger() + self.api = api + + def do_extra_handshake(self, req): + self.log.debug("WS Connected: %s" % req) + self.on_open(req) + + def on_open(self, req): + pass + + def passive_closing_handshake(self, req): + self.log.debug("WS Closed: %s" % req) + self.on_close(req) + + def on_close(self, req): + pass + + def transfer_data(self, req): + raise NotImplemented + + def handle_call(self, msg, req): + """ Parses the msg for an argument call. If func is null an response was already sent. + + :param msg: + :param req: + :return: func, args, kwargs + """ + try: + o = loads(msg) + except ValueError, e: #invalid json object + self.log.debug("Invalid Request: %s" % e) + return None, None, None + + if type(o) != list and len(o) > 2: + self.log.debug("Invalid Api call: %s" % o) + self.send_result(req, 500, "Invalid Api call") + return None, None, None + if len(o) == 1: # arguments omitted + o.append([]) + + func, args = o + if type(args) == list: + kwargs = {} + else: + args, kwargs = [], args + + return func, args, kwargs + + def send_result(self, req, code, result): + return send_message(req, dumps([code, result]))
\ No newline at end of file diff --git a/module/remote/wsbackend/ApiHandler.py b/module/remote/wsbackend/ApiHandler.py index 478ea6de0..57d9ecd5b 100644 --- a/module/remote/wsbackend/ApiHandler.py +++ b/module/remote/wsbackend/ApiHandler.py @@ -16,11 +16,11 @@ # @author: RaNaN ############################################################################### -from mod_pywebsocket.util import get_class_logger -from mod_pywebsocket.msgutil import receive_message, send_message -from module.remote.json_converter import loads, dumps +from mod_pywebsocket.msgutil import receive_message -class ApiHandler: +from AbstractHandler import AbstractHandler + +class ApiHandler(AbstractHandler): """Provides access to the API. Send your request as json encoded string in the following manner: @@ -34,44 +34,22 @@ class ApiHandler: Non json request will be ignored. """ - def __init__(self, api): - self.log = get_class_logger() - self.api = api - - def do_extra_handshake(self, req): - self.log.debug("WS Connected: %s" % req) - def transfer_data(self, req): - while True: try: line = receive_message(req) - self.handle_message(line, req) except TypeError, e: # connection closed - print e + self.log.debug("WS Error: %s" % e) + self.passive_closing_handshake(req) return - def passive_closing_handshake(self, req): - self.log.debug("WS Closed: %s" % req) + self.handle_message(line, req) def handle_message(self, msg, req): - try: - o = loads(msg) - except ValueError, e: #invalid json object - self.log.debug("Invalid Request: %s" % e) - return - - if type(o) != list and len(o) > 2: - self.log.debug("Invalid Api call: %s" % o) - return self.send_result(req, 500, "Invalid Api call") - if len(o) == 1: # arguments omitted - o.append([]) - - func, args = o - if type(args) == list: - kwargs = {} - else: - args, kwargs = [], args + + func, args, kwargs = self.handle_call(msg, req) + if not func: + return # Result was already sent if func == 'login': user = self.api.checkAuth(*args, **kwargs) @@ -103,8 +81,4 @@ class ApiHandler: # None is invalid json type if result is None: result = True - return self.send_result(req, 200, result) - - - def send_result(self, req, code, result): - return send_message(req, dumps([code, result]))
\ No newline at end of file + return self.send_result(req, 200, result)
\ No newline at end of file diff --git a/module/remote/wsbackend/EventHandler.py b/module/remote/wsbackend/EventHandler.py index deab25a6c..2550ff2eb 100644 --- a/module/remote/wsbackend/EventHandler.py +++ b/module/remote/wsbackend/EventHandler.py @@ -16,26 +16,26 @@ # @author: RaNaN ############################################################################### -from mod_pywebsocket.msgutil import receive_message, send_message +from threading import Lock -class EventHandler: - def __init__(self, api): - self.api = api +from module.utils import lock +from AbstractHandler import AbstractHandler - def do_extra_handshake(self, req): - pass +class EventHandler(AbstractHandler): - def transfer_data(self, req): + def __init__(self, api): + AbstractHandler.__init__(self, api) + self.clients = [] + self.lock = Lock() - while True: - try: - line = receive_message(req) - except TypeError: # connection closed - return + @lock + def on_open(self, req): + self.clients.append(req) - print "Got", line - send_message(req, "You send: %s" % line) + @lock + def on_close(self, req): + self.clients.remove(req) - def passive_closing_handshake(self, req): - print "Closed", req
\ No newline at end of file + def handle_message(self, line, req): + pass
\ No newline at end of file |