2017-12-17 16:15:44 +01:00
|
|
|
import importlib
|
2017-11-09 05:04:48 +01:00
|
|
|
import logging
|
2017-12-13 04:14:46 +01:00
|
|
|
import sys
|
2017-12-20 20:25:08 +01:00
|
|
|
import threading
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
from threading import Thread
|
|
|
|
|
2017-12-18 03:09:38 +01:00
|
|
|
from platypush.bus import Bus
|
2017-12-18 01:10:51 +01:00
|
|
|
from platypush.config import Config
|
2017-12-20 20:25:08 +01:00
|
|
|
from platypush.utils import get_message_class_by_type
|
2017-12-17 16:15:44 +01:00
|
|
|
from platypush.message import Message
|
2017-12-20 20:25:08 +01:00
|
|
|
from platypush.message.event import Event, StopEvent
|
2017-12-17 16:15:44 +01:00
|
|
|
from platypush.message.request import Request
|
|
|
|
from platypush.message.response import Response
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
class Backend(Thread):
|
2017-12-17 16:15:44 +01:00
|
|
|
""" Parent class for backends """
|
|
|
|
|
|
|
|
def __init__(self, bus=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Params:
|
|
|
|
bus -- Reference to the Platypush bus where the requests and the
|
2017-12-18 03:09:38 +01:00
|
|
|
responses will be posted [Bus]
|
2017-12-17 16:15:44 +01:00
|
|
|
kwargs -- key-value configuration for this backend [Dict]
|
|
|
|
"""
|
|
|
|
|
|
|
|
# If no bus is specified, create an internal queue where
|
|
|
|
# the received messages will be pushed
|
2017-12-18 03:09:38 +01:00
|
|
|
self.bus = bus if bus else Bus()
|
2017-12-18 01:10:51 +01:00
|
|
|
self.device_id = Config.get('device_id')
|
2017-12-20 20:25:08 +01:00
|
|
|
self.thread_id = None
|
|
|
|
self._stop = False
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
Thread.__init__(self)
|
2017-12-18 01:10:51 +01:00
|
|
|
logging.basicConfig(stream=sys.stdout, level=Config.get('logging')
|
2017-12-17 16:15:44 +01:00
|
|
|
if 'logging' not in kwargs
|
|
|
|
else getattr(logging, kwargs['logging']))
|
2017-11-09 05:04:48 +01:00
|
|
|
|
2017-12-11 16:48:28 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
def on_message(self, msg):
|
2017-12-17 16:15:44 +01:00
|
|
|
"""
|
|
|
|
Callback when a message is received on the backend.
|
|
|
|
It parses and posts the message on the main bus.
|
|
|
|
It should be called by the derived classes whenever
|
|
|
|
a new message should be processed.
|
|
|
|
|
|
|
|
Params:
|
|
|
|
msg -- The message. It can be either a key-value
|
|
|
|
dictionary, a platypush.message.Message
|
|
|
|
object, or a string/byte UTF-8 encoded string
|
|
|
|
"""
|
|
|
|
|
|
|
|
msg = Message.parse(msg)
|
|
|
|
if 'type' not in msg:
|
2017-12-20 20:25:08 +01:00
|
|
|
logging.warning('Ignoring message with no type: {}'.format(msg))
|
2017-12-17 16:15:44 +01:00
|
|
|
return
|
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
msgtype = get_message_class_by_type(msg['type'])
|
2017-12-17 16:15:44 +01:00
|
|
|
msg = msgtype.build(msg)
|
2017-12-13 23:55:38 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
if not getattr(msg, 'target') or msg.target != self.device_id:
|
2017-11-09 05:04:48 +01:00
|
|
|
return # Not for me
|
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
logging.info('Message received on the {} backend: {}'.format(
|
|
|
|
self.__class__.__name__, msg))
|
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
msg.backend = self # Augment message
|
2017-12-20 20:25:08 +01:00
|
|
|
|
|
|
|
if isinstance(msg, StopEvent) and msg.targets_me():
|
|
|
|
logging.info('Received STOP event on the {} backend: {}'.format(
|
|
|
|
self.__class__.__name__, msg))
|
|
|
|
self._stop = True
|
|
|
|
else:
|
|
|
|
self.bus.post(msg)
|
2017-12-16 04:56:43 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
def send_request(self, request):
|
|
|
|
"""
|
|
|
|
Send a request message on the backend
|
|
|
|
Params:
|
|
|
|
request -- The request, either a dict, a string/bytes UTF-8 JSON,
|
|
|
|
or a platypush.message.request.Request object
|
|
|
|
"""
|
|
|
|
|
|
|
|
request = Request.build(request)
|
|
|
|
assert isinstance(request, Request)
|
|
|
|
|
|
|
|
request.origin = self.device_id
|
2017-12-20 20:25:08 +01:00
|
|
|
self.send_message(request)
|
2017-12-17 16:15:44 +01:00
|
|
|
|
|
|
|
def send_response(self, response):
|
|
|
|
"""
|
|
|
|
Send a response message on the backend
|
|
|
|
Params:
|
|
|
|
response -- The response, either a dict, a string/bytes UTF-8 JSON,
|
|
|
|
or a platypush.message.response.Response object
|
|
|
|
"""
|
|
|
|
|
|
|
|
response = Response.build(response)
|
|
|
|
assert isinstance(response, Response)
|
2017-11-09 05:04:48 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
response.origin = self.device_id
|
2017-12-20 20:25:08 +01:00
|
|
|
self.send_message(response)
|
|
|
|
|
2017-11-09 05:04:48 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
def send_message(self, msg):
|
2017-12-17 16:15:44 +01:00
|
|
|
"""
|
|
|
|
Sends a platypush.message.Message to a node.
|
|
|
|
To be implemented in the derived classes.
|
2017-12-20 20:25:08 +01:00
|
|
|
Always call send_request or send_response instead of send_message directly
|
2017-12-16 04:56:43 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
Param:
|
|
|
|
msg -- The message
|
|
|
|
"""
|
2017-12-16 04:56:43 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
raise NotImplementedError("send_message should be implemented in a derived class")
|
2017-11-29 02:42:36 +01:00
|
|
|
|
2017-11-09 05:04:48 +01:00
|
|
|
def run(self):
|
2017-12-17 16:15:44 +01:00
|
|
|
""" Starts the backend thread. To be implemented in the derived classes """
|
2017-12-20 20:25:08 +01:00
|
|
|
self.thread_id = threading.get_ident()
|
2017-11-09 05:04:48 +01:00
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
def on_stop(self):
|
|
|
|
""" Callback invoked when the process stops """
|
2017-12-18 22:40:56 +01:00
|
|
|
pass
|
|
|
|
|
2017-12-20 20:25:08 +01:00
|
|
|
def stop(self):
|
|
|
|
""" Stops the backend thread by sending a STOP event on its bus """
|
|
|
|
evt = StopEvent(target=self.device_id, origin=self.device_id,
|
|
|
|
thread_id=self.thread_id)
|
|
|
|
|
|
|
|
self.send_message(evt)
|
|
|
|
self.on_stop()
|
|
|
|
|
|
|
|
def should_stop(self):
|
|
|
|
return self._stop
|
|
|
|
|
2017-12-18 22:40:56 +01:00
|
|
|
|
2017-11-09 05:04:48 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|