[11f7a07] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2014 Virtual Cable S.L. |
---|
| 4 | # All rights reserved. |
---|
| 5 | # |
---|
| 6 | # Redistribution and use in source and binary forms, with or without modification, |
---|
| 7 | # are permitted provided that the following conditions are met: |
---|
| 8 | # |
---|
| 9 | # * Redistributions of source code must retain the above copyright notice, |
---|
| 10 | # this list of conditions and the following disclaimer. |
---|
| 11 | # * Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | # this list of conditions and the following disclaimer in the documentation |
---|
| 13 | # and/or other materials provided with the distribution. |
---|
| 14 | # * Neither the name of Virtual Cable S.L. nor the names of its contributors |
---|
| 15 | # may be used to endorse or promote products derived from this software |
---|
| 16 | # without specific prior written permission. |
---|
| 17 | # |
---|
| 18 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 19 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 20 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
---|
| 21 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
---|
| 22 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 23 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
---|
| 24 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
---|
| 25 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
| 26 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
| 27 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
[9525724] | 28 | """ |
---|
[11f7a07] | 29 | @author: Adolfo Gómez, dkmaster at dkmon dot com |
---|
[9525724] | 30 | """ |
---|
[11f7a07] | 31 | # pylint: disable=unused-wildcard-import,wildcard-import |
---|
[53e7d45] | 32 | |
---|
[11f7a07] | 33 | |
---|
| 34 | class ClientWorker(object): |
---|
[9525724] | 35 | """ |
---|
[11f7a07] | 36 | A ServerWorker is a server module that "works" for service |
---|
[9525724] | 37 | Most method are invoked inside their own thread, except onActivation & onDeactivation. |
---|
[11f7a07] | 38 | This two methods are invoked inside main service thread, take that into account when creating them |
---|
[9525724] | 39 | |
---|
[11f7a07] | 40 | * You must provide a module name (override name on your class), so we can identify the module by a "valid" name. |
---|
| 41 | A valid name is like a valid python variable (do not use spaces, etc...) |
---|
| 42 | * The name of the module is used as REST message destination id: |
---|
| 43 | https://sampleserver:8888/[name]/.... |
---|
| 44 | Remember that module names and REST path are case sensitive!!! |
---|
[9525724] | 45 | |
---|
| 46 | """ |
---|
[11f7a07] | 47 | name = None |
---|
| 48 | service = None |
---|
| 49 | |
---|
| 50 | def __init__(self, service): |
---|
| 51 | self.service = service |
---|
| 52 | |
---|
| 53 | def activate(self): |
---|
[9525724] | 54 | """ |
---|
[11f7a07] | 55 | Convenient method to wrap onActivation, so we can include easyly custom common logic for activation in a future |
---|
[9525724] | 56 | """ |
---|
[11f7a07] | 57 | self.onActivation() |
---|
| 58 | |
---|
| 59 | def deactivate(self): |
---|
[9525724] | 60 | """ |
---|
[11f7a07] | 61 | Convenient method to wrap onActivation, so we can include easyly custom common logic for deactivation in a future |
---|
[9525724] | 62 | """ |
---|
[11f7a07] | 63 | self.onDeactivation() |
---|
| 64 | |
---|
| 65 | def processMessage(self, message, params): |
---|
| 66 | ''' |
---|
| 67 | This method can be overriden to provide your own message proccessor, or better you can |
---|
| 68 | implement a method that is called "process_" + message and this default processMessage will invoke it |
---|
| 69 | * Example: |
---|
| 70 | We got a message from OGAgent "Mazinger", with json params |
---|
| 71 | module.processMessage("mazinger", jsonParams) |
---|
| 72 | |
---|
| 73 | This method will process "mazinguer", and look for a "self" method that is called "process_mazinger", and invoke it this way: |
---|
| 74 | return self.process_mazinger(jsonParams) |
---|
| 75 | |
---|
| 76 | The methods must return data that can be serialized to json (i.e. Ojects are not serializable to json, basic type are) |
---|
| 77 | ''' |
---|
| 78 | try: |
---|
| 79 | operation = getattr(self, 'process_' + message) |
---|
| 80 | except Exception: |
---|
| 81 | raise Exception('Message processor for "{}" not found'.format(message)) |
---|
| 82 | |
---|
| 83 | return operation(params) |
---|
| 84 | |
---|
| 85 | def onActivation(self): |
---|
[9525724] | 86 | """ |
---|
[11f7a07] | 87 | Invoked by Service for activation. |
---|
| 88 | This MUST be overridden by modules! |
---|
| 89 | This method is invoked inside main thread, so if it "hangs", complete service will hang |
---|
| 90 | This should be no problem, but be advised about this |
---|
[9525724] | 91 | """ |
---|
[11f7a07] | 92 | pass |
---|
| 93 | |
---|
| 94 | def onDeactivation(self): |
---|
[9525724] | 95 | """ |
---|
[11f7a07] | 96 | Invoked by Service before unloading service |
---|
| 97 | This MUST be overridden by modules! |
---|
| 98 | This method is invoked inside main thread, so if it "hangs", complete service will hang |
---|
| 99 | This should be no problem, but be advised about this |
---|
[9525724] | 100 | """ |
---|
[11f7a07] | 101 | pass |
---|
| 102 | |
---|
| 103 | # ************************************* |
---|
| 104 | # * Helper, convenient helper methods * |
---|
| 105 | # ************************************* |
---|
| 106 | def sendServerMessage(self, message, data): |
---|
[9525724] | 107 | """ |
---|
[11f7a07] | 108 | Sends a message to connected ipc clients |
---|
| 109 | By convenience, it uses the "current" moduel name as destination module name also. |
---|
| 110 | If you need to send a message to a different module, you can use self.service.sendClientMessage(module, message, data) instead |
---|
| 111 | og this helmer |
---|
[9525724] | 112 | """ |
---|
[11f7a07] | 113 | self.service.ipc.sendMessage(self.name, message, data) |
---|