Mercurial > libervia-backend
annotate sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py @ 3580:8dd5e1bac9c3
merge changes from main branch
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 18 Jun 2021 18:19:23 +0200 |
parents | d71a163c0861 |
children | 3c7a64d6f49f |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
3243
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
3 # SàT communication bridge |
3479 | 4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # (at your option) any later version. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # 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
|
12 # 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
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # 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
|
17 # 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
|
18 |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
19 import asyncio |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
20 from logging import getLogger |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
21 from functools import partial |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
22 from pathlib import Path |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
23 from twisted.spread import pb |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
24 from twisted.internet import reactor, defer |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
25 from twisted.internet.error import ConnectionRefusedError, ConnectError |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
26 from sat.core import exceptions |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
27 from sat.tools import config |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
28 from sat_frontends.bridge.bridge_frontend import BridgeException |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
29 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 log = getLogger(__name__) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 class SignalsHandler(pb.Referenceable): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 def __getattr__(self, name): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 if name.startswith("remote_"): |
3028 | 36 log.debug("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
|
37 return lambda *args, **kwargs: None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 raise AttributeError(name) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 def register_signal(self, name, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 log.debug("registering signal {name}".format(name=name)) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 method_name = "remote_" + name |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 try: |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
46 self.__getattribute__(method_name) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 except AttributeError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 raise exceptions.InternalError( |
3028 | 51 "{name} signal handler has been registered twice".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 name=method_name |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 ) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 setattr(self, method_name, handler) |
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 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 class Bridge(object): |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
59 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def __init__(self): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 self.signals_handler = SignalsHandler() |
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 def __getattr__(self, name): |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
64 return partial(self.call, name) |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
3063
84bb63e1e4c4
bridge (pb): added missing _generic_errback
Goffi <goffi@goffi.org>
parents:
3039
diff
changeset
|
66 def _generic_errback(self, err): |
84bb63e1e4c4
bridge (pb): added missing _generic_errback
Goffi <goffi@goffi.org>
parents:
3039
diff
changeset
|
67 log.error(f"bridge error: {err}") |
84bb63e1e4c4
bridge (pb): added missing _generic_errback
Goffi <goffi@goffi.org>
parents:
3039
diff
changeset
|
68 |
3243
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
69 def _errback(self, failure_, ori_errback): |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
70 """Convert Failure to BridgeException""" |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
71 ori_errback( |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
72 BridgeException( |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
73 name=failure_.type.decode('utf-8'), |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
74 message=str(failure_.value) |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
75 ) |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
76 ) |
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
77 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 def remoteCallback(self, result, callback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 """call callback with argument or None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 if result is not None not argument is used, |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 else result is used as argument |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 @param result: remote call result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 @param callback(callable): method to call on result |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 if result is None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 callback() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 else: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 callback(result) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
91 def call(self, name, *args, **kwargs): |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 """call a remote method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 @param name(str): name of the bridge method |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 @param args(list): arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 may contain callback and errback as last 2 items |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 @param kwargs(dict): keyword arguments |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 may contain callback and errback |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 callback = errback = None |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 if kwargs: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 callback = kwargs.pop("callback") |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 errback = kwargs.pop("errback") |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 except KeyError: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 pass |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 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
|
111 errback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 callback = args.pop() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 d = self.root.callRemote(name, *args, **kwargs) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 if callback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 d.addCallback(self.remoteCallback, callback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 if errback is not None: |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 d.addErrback(errback) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
119 def _initBridgeEb(self, failure_): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
120 log.error("Can't init bridge: {msg}".format(msg=failure_)) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
121 return failure_ |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 def _set_root(self, root): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 """set remote root object |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 bridge will then be initialised |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 """ |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 self.root = root |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 d = root.callRemote("initBridge", self.signals_handler) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 d.addErrback(self._initBridgeEb) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 return d |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
133 def getRootObjectEb(self, failure_): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
134 """Call errback with appropriate bridge error""" |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
135 if failure_.check(ConnectionRefusedError, ConnectError): |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
136 raise exceptions.BridgeExceptionNoService |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
137 else: |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
138 raise failure_ |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 def bridgeConnect(self, callback, errback): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 factory = pb.PBClientFactory() |
3489
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
142 conf = config.parseMainConf() |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
143 conn_type = config.getConfig( |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
144 conf, |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
145 "", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
146 "bridge_pb_connection_type", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
147 "unix_socket" |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
148 ) |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
149 if conn_type == "unix_socket": |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve() |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
151 socket_path = local_dir / "bridge_pb" |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
152 reactor.connectUNIX(str(socket_path), factory) |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
153 elif conn_type == "socket": |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
154 host = int(config.getConfig( |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
155 conf, |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
156 "", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
157 "bridge_pb_host", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
158 "localhost" |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
159 )) |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
160 port = int(config.getConfig( |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
161 conf, |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
162 "", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
163 "bridge_pb_port", |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
164 8789 |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
165 )) |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
166 reactor.connectTCP(host, port, factory) |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
167 else: |
d71a163c0861
bridge (pb): connection can be set to used either UNIX socket or TCP socket:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
168 raise ValueError(f"Unknown pb connection type: {conn_type!r}") |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 d = factory.getRootObject() |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 d.addCallback(self._set_root) |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
171 if callback is not None: |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
172 d.addCallback(lambda __: callback()) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
173 d.addErrback(self.getRootObjectEb) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
174 if errback is not None: |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
175 d.addErrback(lambda failure_: errback(failure_.value)) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
176 return d |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 def register_signal(self, functionName, handler, iface="core"): |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 self.signals_handler.register_signal(functionName, handler, iface) |
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 |
2092
5e79ba00c1e2
bridbe(pb): Perspective Broker bridge, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 ##METHODS_PART## |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
183 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
184 class AIOSignalsHandler(SignalsHandler): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
185 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
186 def register_signal(self, name, handler, iface="core"): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
187 async_handler = lambda *args, **kwargs: defer.Deferred.fromFuture( |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
188 asyncio.ensure_future(handler(*args, **kwargs))) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
189 return super().register_signal(name, async_handler, iface) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
190 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
191 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
192 class AIOBridge(Bridge): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
193 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
194 def __init__(self): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
195 self.signals_handler = AIOSignalsHandler() |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
196 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
197 def _errback(self, failure_): |
3243
f2e30aa031e9
bridge (pb): fixed conversions of Failure to BridgeException in non-AIO bridge
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
198 """Convert Failure to BridgeException""" |
3039
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
199 raise BridgeException( |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
200 name=failure_.type.decode('utf-8'), |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
201 message=str(failure_.value) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
202 ) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
203 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
204 def call(self, name, *args, **kwargs): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
205 d = self.root.callRemote(name, *args, *kwargs) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
206 d.addErrback(self._errback) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
207 return d.asFuture(asyncio.get_event_loop()) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
208 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
209 async def bridgeConnect(self): |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
210 d = super().bridgeConnect(callback=None, errback=None) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
211 return await d.asFuture(asyncio.get_event_loop()) |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
212 |
a1bc34f90fa5
bridge (pb): implemented an asyncio compatible bridge:
Goffi <goffi@goffi.org>
parents:
3028
diff
changeset
|
213 ##ASYNC_METHODS_PART## |