proxysteerabledevice.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. """Classes for running a steerable ZMQ proxy"""
  2. # Copyright (C) PyZMQ Developers
  3. # Distributed under the terms of the Modified BSD License.
  4. import zmq
  5. from zmq.devices.proxydevice import ProcessProxy, Proxy, ThreadProxy
  6. class ProxySteerableBase:
  7. """Base class for overriding methods."""
  8. def __init__(self, in_type, out_type, mon_type=zmq.PUB, ctrl_type=None):
  9. super().__init__(in_type=in_type, out_type=out_type, mon_type=mon_type)
  10. self.ctrl_type = ctrl_type
  11. self._ctrl_binds = []
  12. self._ctrl_connects = []
  13. self._ctrl_sockopts = []
  14. def bind_ctrl(self, addr):
  15. """Enqueue ZMQ address for binding on ctrl_socket.
  16. See zmq.Socket.bind for details.
  17. """
  18. self._ctrl_binds.append(addr)
  19. def bind_ctrl_to_random_port(self, addr, *args, **kwargs):
  20. """Enqueue a random port on the given interface for binding on
  21. ctrl_socket.
  22. See zmq.Socket.bind_to_random_port for details.
  23. """
  24. port = self._reserve_random_port(addr, *args, **kwargs)
  25. self.bind_ctrl(f'{addr}:{port}')
  26. return port
  27. def connect_ctrl(self, addr):
  28. """Enqueue ZMQ address for connecting on ctrl_socket.
  29. See zmq.Socket.connect for details.
  30. """
  31. self._ctrl_connects.append(addr)
  32. def setsockopt_ctrl(self, opt, value):
  33. """Enqueue setsockopt(opt, value) for ctrl_socket
  34. See zmq.Socket.setsockopt for details.
  35. """
  36. self._ctrl_sockopts.append((opt, value))
  37. def _setup_sockets(self):
  38. ins, outs, mons = super()._setup_sockets()
  39. ctx = self._context
  40. ctrls = ctx.socket(self.ctrl_type)
  41. self._sockets.append(ctrls)
  42. for opt, value in self._ctrl_sockopts:
  43. ctrls.setsockopt(opt, value)
  44. for iface in self._ctrl_binds:
  45. ctrls.bind(iface)
  46. for iface in self._ctrl_connects:
  47. ctrls.connect(iface)
  48. return ins, outs, mons, ctrls
  49. def run_device(self):
  50. ins, outs, mons, ctrls = self._setup_sockets()
  51. zmq.proxy_steerable(ins, outs, mons, ctrls)
  52. class ProxySteerable(ProxySteerableBase, Proxy):
  53. """Class for running a steerable proxy in the background.
  54. See zmq.devices.Proxy for most of the spec. If the control socket is not
  55. NULL, the proxy supports control flow, provided by the socket.
  56. If PAUSE is received on this socket, the proxy suspends its activities. If
  57. RESUME is received, it goes on. If TERMINATE is received, it terminates
  58. smoothly. If the control socket is NULL, the proxy behave exactly as if
  59. zmq.devices.Proxy had been used.
  60. This subclass adds a <method>_ctrl version of each <method>_{in|out}
  61. method, for configuring the control socket.
  62. .. versionadded:: libzmq-4.1
  63. .. versionadded:: 18.0
  64. """
  65. class ThreadProxySteerable(ProxySteerableBase, ThreadProxy):
  66. """ProxySteerable in a Thread. See ProxySteerable for details."""
  67. class ProcessProxySteerable(ProxySteerableBase, ProcessProxy):
  68. """ProxySteerable in a Process. See ProxySteerable for details."""
  69. __all__ = [
  70. 'ProxySteerable',
  71. 'ThreadProxySteerable',
  72. 'ProcessProxySteerable',
  73. ]