comparison sat/bridge/bridge_constructor/constructors/pb/pb_frontend_template.py @ 3489:d71a163c0861

bridge (pb): connection can be set to used either UNIX socket or TCP socket: all settings are to be put in `[DEFAULT]` section - `bridge_pb_connection_type` can be set to `unix_socket` or `socket` - for `socket`, `bridge_pb_host` and `bridge_pb_port` can be specified
author Goffi <goffi@goffi.org>
date Sun, 21 Mar 2021 22:00:55 +0100
parents be6d91572633
children 3c7a64d6f49f
comparison
equal deleted inserted replaced
3488:c80a0f864b5d 3489:d71a163c0861
15 15
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import asyncio 19 import asyncio
20 from logging import getLogger
20 from functools import partial 21 from functools import partial
22 from pathlib import Path
21 from twisted.spread import pb 23 from twisted.spread import pb
22 from twisted.internet import reactor, defer 24 from twisted.internet import reactor, defer
23 from twisted.internet.error import ConnectionRefusedError 25 from twisted.internet.error import ConnectionRefusedError, ConnectError
24 from logging import getLogger
25 from sat.core import exceptions 26 from sat.core import exceptions
27 from sat.tools import config
26 from sat_frontends.bridge.bridge_frontend import BridgeException 28 from sat_frontends.bridge.bridge_frontend import BridgeException
27 29
28 log = getLogger(__name__) 30 log = getLogger(__name__)
29 31
30 32
128 d.addErrback(self._initBridgeEb) 130 d.addErrback(self._initBridgeEb)
129 return d 131 return d
130 132
131 def getRootObjectEb(self, failure_): 133 def getRootObjectEb(self, failure_):
132 """Call errback with appropriate bridge error""" 134 """Call errback with appropriate bridge error"""
133 if failure_.check(ConnectionRefusedError): 135 if failure_.check(ConnectionRefusedError, ConnectError):
134 raise exceptions.BridgeExceptionNoService 136 raise exceptions.BridgeExceptionNoService
135 else: 137 else:
136 raise failure_ 138 raise failure_
137 139
138 def bridgeConnect(self, callback, errback): 140 def bridgeConnect(self, callback, errback):
139 factory = pb.PBClientFactory() 141 factory = pb.PBClientFactory()
140 reactor.connectTCP("localhost", 8789, factory) 142 conf = config.parseMainConf()
143 conn_type = config.getConfig(
144 conf,
145 "",
146 "bridge_pb_connection_type",
147 "unix_socket"
148 )
149 if conn_type == "unix_socket":
150 local_dir = Path(config.getConfig(conf, "", "local_dir")).resolve()
151 socket_path = local_dir / "bridge_pb"
152 reactor.connectUNIX(str(socket_path), factory)
153 elif conn_type == "socket":
154 host = int(config.getConfig(
155 conf,
156 "",
157 "bridge_pb_host",
158 "localhost"
159 ))
160 port = int(config.getConfig(
161 conf,
162 "",
163 "bridge_pb_port",
164 8789
165 ))
166 reactor.connectTCP(host, port, factory)
167 else:
168 raise ValueError(f"Unknown pb connection type: {conn_type!r}")
141 d = factory.getRootObject() 169 d = factory.getRootObject()
142 d.addCallback(self._set_root) 170 d.addCallback(self._set_root)
143 if callback is not None: 171 if callback is not None:
144 d.addCallback(lambda __: callback()) 172 d.addCallback(lambda __: callback())
145 d.addErrback(self.getRootObjectEb) 173 d.addErrback(self.getRootObjectEb)