Mercurial > libervia-backend
annotate sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py @ 2717:e3f6de6ce320
plugin XEP-0059: added serialise method
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 10 Dec 2018 20:34:45 +0100 |
parents | 56f94936df1e |
children | 378188abe941 |
rev | line source |
---|---|
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
2 # -*- coding: utf-8 -*- |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT communication bridge |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
21 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 log = getLogger(__name__) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core import exceptions |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.spread import pb |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.internet import reactor |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 class SignalsHandler(pb.Referenceable): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 def __getattr__(self, name): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 if name.startswith("remote_"): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
31 log.debug(u"calling an unregistered signal: {name}".format(name=name[7:])) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 return lambda *args, **kwargs: None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 raise AttributeError(name) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 def register_signal(self, name, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 log.debug("registering signal {name}".format(name=name)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 method_name = "remote_" + name |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 try: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 self.__getattribute__(self, method_name) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 except AttributeError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 raise exceptions.InternalError( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
46 u"{name} signal handler has been registered twice".format( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
47 name=method_name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
48 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 ) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 setattr(self, method_name, handler) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 class Bridge(object): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 def __init__(self): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 self.signals_handler = SignalsHandler() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def __getattr__(self, name): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 return lambda *args, **kwargs: self.call(name, args, kwargs) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def remoteCallback(self, result, callback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """call callback with argument or None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 if result is not None not argument is used, |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 else result is used as argument |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 @param result: remote call result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 @param callback(callable): method to call on result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if result is None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 callback() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 callback(result) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def call(self, name, args, kwargs): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 """call a remote method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @param name(str): name of the bridge method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @param args(list): arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 may contain callback and errback as last 2 items |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 @param kwargs(dict): keyword arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 may contain callback and errback |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 callback = errback = None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 if kwargs: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
85 callback = kwargs.pop("callback") |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 errback = kwargs.pop("errback") |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 elif len(args) >= 2 and callable(args[-1]) and callable(args[-2]): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 errback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 callback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 d = self.root.callRemote(name, *args, **kwargs) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 if callback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 d.addCallback(self.remoteCallback, callback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 if errback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 d.addErrback(errback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 def _initBridgeEb(self, failure): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 log.error(u"Can't init bridge: {msg}".format(msg=failure)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 def _set_root(self, root): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 """set remote root object |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 bridge will then be initialised |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 self.root = root |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 d = root.callRemote("initBridge", self.signals_handler) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 d.addErrback(self._initBridgeEb) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 return d |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 def _generic_errback(self, failure): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 log.error(u"bridge failure: {}".format(failure)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def bridgeConnect(self, callback, errback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 factory = pb.PBClientFactory() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 reactor.connectTCP("localhost", 8789, factory) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 d = factory.getRootObject() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 d.addCallback(self._set_root) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 d.addCallback(lambda dummy: callback()) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 d.addErrback(errback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 def register_signal(self, functionName, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.signals_handler.register_signal(functionName, handler, iface) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 ##METHODS_PART## |