|
| 1 | +""" |
| 2 | +Composite Handler allows multiple standard handlers to be combined into a single handler. |
| 3 | +
|
| 4 | +And ordered dictionary is used to make the component handlers accessible by name. |
| 5 | +The ordered dictionary also controls the order in which the handlers are called. |
| 6 | +""" |
| 7 | + |
| 8 | +from collections import OrderedDict |
| 9 | +from typing import Optional |
| 10 | + |
| 11 | +from p4p import Value |
| 12 | +from p4p.server import ServerOperation |
| 13 | +from p4p.server.thread import Handler, SharedPV |
| 14 | +from typing_extensions import deprecated |
| 15 | + |
| 16 | + |
| 17 | +class HandlerException(Exception): |
| 18 | + """Exception raised for errors in the handler operations.""" |
| 19 | + |
| 20 | + |
| 21 | +class AbortHandlerException(HandlerException): |
| 22 | + """Exception raised to abort the current operation in the handler.""" |
| 23 | + |
| 24 | + def __init__(self, message: str = "Operation aborted"): |
| 25 | + super().__init__(message) |
| 26 | + self.message = message |
| 27 | + |
| 28 | + |
| 29 | +class CompositeHandler(Handler): |
| 30 | + """Composite Handler for combining multiple component handlers into a single handler.""" |
| 31 | + |
| 32 | + def __init__(self, handlers: Optional[OrderedDict[str, Handler]] = None): |
| 33 | + """Initialize the CompositeHandler with an optional list of handlers.""" |
| 34 | + super().__init__() |
| 35 | + self.handlers = handlers |
| 36 | + |
| 37 | + def __getitem__(self, name: str) -> Handler | None: |
| 38 | + if self.handlers: |
| 39 | + return self.handlers[name] |
| 40 | + |
| 41 | + return None |
| 42 | + |
| 43 | + def open(self, value: Value): |
| 44 | + """Open all handlers in the composite handler.""" |
| 45 | + if self.handlers: |
| 46 | + for handler in self.handlers.values(): |
| 47 | + handler.open(value) |
| 48 | + |
| 49 | + def put(self, pv: SharedPV, op: ServerOperation): |
| 50 | + errmsg = None |
| 51 | + |
| 52 | + if self.handlers: |
| 53 | + for handler in self.handlers.values(): |
| 54 | + try: |
| 55 | + handler.put(pv, op) |
| 56 | + except AbortHandlerException as e: |
| 57 | + errmsg = e.message |
| 58 | + break |
| 59 | + |
| 60 | + op.done(error=errmsg) |
| 61 | + |
| 62 | + def post(self, pv: SharedPV, value: Value): |
| 63 | + if self.handlers: |
| 64 | + for handler in self.handlers.values(): |
| 65 | + handler.post(pv, value) |
| 66 | + |
| 67 | + def rpc(self, pv: SharedPV, op: ServerOperation): |
| 68 | + errmsg = None |
| 69 | + |
| 70 | + if self.handlers: |
| 71 | + for handler in self.handlers.values(): |
| 72 | + try: |
| 73 | + handler.rpc(pv, op) |
| 74 | + except AbortHandlerException as e: |
| 75 | + errmsg = e.message |
| 76 | + break |
| 77 | + |
| 78 | + op.done(error=errmsg) |
| 79 | + |
| 80 | + def on_first_connect(self, pv: SharedPV): |
| 81 | + """Called when the first client connects to the PV.""" |
| 82 | + if self.handlers: |
| 83 | + for handler in self.handlers.values(): |
| 84 | + handler.onFirstConnect(pv) |
| 85 | + |
| 86 | + @deprecated("onFirstConnect is deprecated, use on_first_connect instead") |
| 87 | + def onFirstConnect(self, pv: Value): |
| 88 | + self.on_first_connect(pv) |
| 89 | + |
| 90 | + def on_last_connect(self, pv: SharedPV): |
| 91 | + """Called when the last client channel is closed.""" |
| 92 | + if self.handlers: |
| 93 | + for handler in self.handlers.values(): |
| 94 | + handler.onFirstConnect(pv) |
| 95 | + |
| 96 | + @deprecated("onLastDisconnect is deprecated, use on_last_connect instead") |
| 97 | + def onLastDisconnect(self, pv: Value): |
| 98 | + self.on_last_connect(pv) |
| 99 | + |
| 100 | + def close(self, pv: SharedPV): |
| 101 | + if self.handlers: |
| 102 | + for handler in self.handlers.values(): |
| 103 | + handler.close(pv) |
0 commit comments