From 66104b45b6f09076dd1f0db621d0d6987ff4f03e Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Thu, 13 Jun 2013 21:25:43 +0200 Subject: beinc_server - pull completed and tested --- beinc_server.json | 2 +- beinc_server.py | 72 ++++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/beinc_server.json b/beinc_server.json index 1743239..8fa042c 100644 --- a/beinc_server.json +++ b/beinc_server.json @@ -10,7 +10,7 @@ { "name": "test", "password": "changeme", - "osd_system": "none", + "osd_system": "pynotify", "osd_timeout": 5000, "queue_size": 4 } diff --git a/beinc_server.py b/beinc_server.py index cad8d61..da47e22 100755 --- a/beinc_server.py +++ b/beinc_server.py @@ -22,19 +22,22 @@ BEINC_OSD_TYPE_PYNOTIFY = 1 class BEINCInstance(object): """ + Represents a single server-instance """ def __init__(self, instance_dict): """ + instance_dict: the config-dictionary node that represents this instance """ - self.__instance_dict = instance_dict + + self.__message_queue = list() + self.__osd_type = BEINC_OSD_TYPE_NONE + self.__osd_notification = None + try: self.__name = instance_dict['name'] - self.__osd_type = BEINC_OSD_TYPE_NONE self.__password = instance_dict['password'] self.__queue_size = int(instance_dict['queue_size']) - self.__message_queue = list() - self.__osd_notification = None except Exception as e: sys.stderr.write( @@ -42,7 +45,6 @@ class BEINCInstance(object): e)) sys.exit(1) - if instance_dict['osd_system'].lower() == 'pynotify': if not pynotify: sys.stderr.write( @@ -77,7 +79,7 @@ class BEINCInstance(object): @property def queueable(self): """ - name-property for the server instance + True if this instance has a queueing capability """ return bool(self.__queue_size) @@ -101,7 +103,7 @@ class BEINCInstance(object): if self.__osd_type == BEINC_OSD_TYPE_PYNOTIFY: self.__send_pynotify_messaage(title, message) else: - self.____send_message_to_queue(title, message) + self.__send_message_to_queue(title, message) def get_queue(self): @@ -138,7 +140,9 @@ def beinc_instance_login(method): """ decorator for checking login credentials """ + from functools import wraps + @wraps(method) def tmp_func(self, *args, **kwargs): if not args: @@ -147,18 +151,24 @@ def beinc_instance_login(method): print('args: {0}'.format(args)) print('kwargs: {0}'.format(kwargs)) print(cherrypy.request.config) + print('args[0]: {0} ({1})'.format(args[0], str(type(args[0])))) + print('instances: {0}'.format(str(self.__instances))) try: - instance = self.__instances(args[0]) + instance = self.__instances[args[0]] except Exception as e: + sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) raise cherrypy.HTTPError('403 Forbidden', 'Wrong instance or password') + print('First') + if not instance.password_match(kwargs.get('password')): raise cherrypy.HTTPError('403 Forbidden', 'Wrong instance or password') - method(self, *args, **kwargs) + print('second') + return method(self, *args, **kwargs) return tmp_func @@ -175,6 +185,7 @@ class WebNotifyServer(object): try: for instance in self.__config['server']['instances']: self.__instances[instance['name']] = BEINCInstance(instance) + print('Instance "{0}" added'.format(instance['name'])) except Exception as e: sys.stderr.write('Unable to initialize queues: {0}\n'.format(e)) @@ -198,11 +209,42 @@ class WebNotifyServer(object): @cherrypy.expose - @beinc_instance_login def push(self, *args, **kwargs): + print('push called') - instance = self.__instances(args[0]) - return 'OK' + if not args: + raise cherrypy.HTTPError(400) + + # print('args: {0}'.format(args)) + # print('kwargs: {0}'.format(unicode(kwargs))) + # print(cherrypy.request.config) + # print('args[0]: {0} ({1})'.format(args[0], str(type(args[0])))) + # print('instances: {0}'.format(str(self.__instances))) + + try: + instance = self.__instances[args[0]] + except Exception as e: + sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) + raise cherrypy.HTTPError('403 Forbidden', + 'Wrong instance or password') + + if not instance.password_match(kwargs.get('password')): + sys.stderr.write('Wrong instance or password: {0}\n'.format(e)) + raise cherrypy.HTTPError('403 Forbidden', + 'Wrong instance or password') + +# instance = self.__instances[args[0]] + title = kwargs.get('title', '') + message = kwargs.get('message', '') + try: + instance.send_message(title, message) + return 'OK' + except Exception as e: + sys.stderr.write( + 'Unable to handle message in {0}: ({1})\n'.format( + instance.name, + e)) + raise cherrypy.HTTPError(500, 'Unable to send message') @cherrypy.expose @@ -267,9 +309,12 @@ def main(): 'server.socket_port': args.port, 'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'), 'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'], - 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], + 'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'], + 'tools.encode.on': True, + 'tools.encode.encoding': 'utf-8' }) + global pynotify try: import pynotify if not pynotify.init("BEINC Notify"): @@ -287,7 +332,6 @@ def main(): sys.stderr.write("WebServer error: {0}".format(e)) sys.exit(1) - sys.exit(0) -- cgit v1.3