|
| 1 | +import logging |
| 2 | +import sys |
| 3 | +import threading |
| 4 | +import time |
| 5 | +from time import monotonic |
| 6 | + |
| 7 | +from . import exception |
| 8 | +from . import utils |
| 9 | +from ..pb.util import ProtoMessageUtil |
| 10 | + |
| 11 | + |
| 12 | +class Publisher(object): |
| 13 | + """ |
| 14 | + Simply a registry of listeners. |
| 15 | + """ |
| 16 | + |
| 17 | + def set_listener(self, name, listener): |
| 18 | + """ |
| 19 | + Set a named listener to use with this connection. See :py:class:`stomp.listener.ConnectionListener` |
| 20 | +
|
| 21 | + :param str name: the name of the listener |
| 22 | + :param ConnectionListener listener: the listener object |
| 23 | + """ |
| 24 | + pass |
| 25 | + |
| 26 | + def remove_listener(self, name): |
| 27 | + """ |
| 28 | + Remove a listener. |
| 29 | +
|
| 30 | + :param str name: the name of the listener to remove |
| 31 | + """ |
| 32 | + pass |
| 33 | + |
| 34 | + def get_listener(self, name): |
| 35 | + """ |
| 36 | + Return the named listener. |
| 37 | +
|
| 38 | + :param str name: the listener to return |
| 39 | +
|
| 40 | + :rtype: ConnectionListener |
| 41 | + """ |
| 42 | + return None |
| 43 | + |
| 44 | + |
| 45 | +class ConnectionListener(object): |
| 46 | + """ |
| 47 | + This class should be used as a base class for objects registered |
| 48 | + using Connection.set_listener(). |
| 49 | + """ |
| 50 | + |
| 51 | + def on_connecting(self, host_and_port): |
| 52 | + pass |
| 53 | + |
| 54 | + def on_connected(self, frame): |
| 55 | + pass |
| 56 | + |
| 57 | + def on_disconnecting(self): |
| 58 | + pass |
| 59 | + |
| 60 | + def on_disconnected(self): |
| 61 | + pass |
| 62 | + |
| 63 | + def on_message(self, frame): |
| 64 | + pass |
| 65 | + |
| 66 | + def on_error(self, frame): |
| 67 | + pass |
| 68 | + |
| 69 | + def on_send(self, frame): |
| 70 | + pass |
| 71 | + |
| 72 | + def on_heartbeat(self, frame): |
| 73 | + pass |
| 74 | + |
| 75 | + def on_receiver_loop_completed(self, frame): |
| 76 | + pass |
| 77 | + |
| 78 | + |
| 79 | +class HeartbeatListener(ConnectionListener): |
| 80 | + """ |
| 81 | + Listener used to handle heartbeating. |
| 82 | + """ |
| 83 | + def __init__(self, transport, heartbeats, heart_beat_receive_scale=1.5): |
| 84 | + self.running = False |
| 85 | + self.transport = transport |
| 86 | + self.heartbeats = heartbeats |
| 87 | + self.received_heartbeat = None |
| 88 | + self.heartbeat_thread = None |
| 89 | + self.next_outbound_heartbeat = None |
| 90 | + self.heart_beat_receive_scale = heart_beat_receive_scale |
| 91 | + self.heartbeat_terminate_event = threading.Event() |
| 92 | + self.disconnecting = False |
| 93 | + |
| 94 | + def on_connected(self, frame): |
| 95 | + """ |
| 96 | + Once the connection is established, and 'heart-beat' is found in the headers, we calculate the real |
| 97 | + heartbeat numbers (based on what the server sent and what was specified by the client) - if the heartbeats |
| 98 | + are not 0, we start up the heartbeat loop accordingly. |
| 99 | +
|
| 100 | + :param Frame frame: the frame |
| 101 | + """ |
| 102 | + self.disconnecting = False |
| 103 | + heartbeat = ProtoMessageUtil.extract_heart_beat(frame) |
| 104 | + if heartbeat: |
| 105 | + self.heartbeats = heartbeat |
| 106 | + logging.debug("heartbeats calculated %s", str(self.heartbeats)) |
| 107 | + if self.heartbeats != (0, 0): |
| 108 | + self.send_sleep = self.heartbeats[0] / 1000 |
| 109 | + |
| 110 | + # by default, receive gets an additional grace of 50% |
| 111 | + # set a different heart-beat-receive-scale when creating the connection to override that |
| 112 | + self.receive_sleep = (self.heartbeats[1] / 1000) * self.heart_beat_receive_scale |
| 113 | + |
| 114 | + logging.debug("set receive_sleep to %s, send_sleep to %s", self.receive_sleep, self.send_sleep) |
| 115 | + |
| 116 | + # Give grace of receiving the first heartbeat |
| 117 | + self.received_heartbeat = monotonic() + self.receive_sleep |
| 118 | + |
| 119 | + self.running = True |
| 120 | + if self.heartbeat_thread is None: |
| 121 | + self.heartbeat_thread = utils.default_create_thread( |
| 122 | + self.__heartbeat_loop) |
| 123 | + self.heartbeat_thread.name = "Heartbeat%s" % \ |
| 124 | + getattr(self.heartbeat_thread, "name", "Thread") |
| 125 | + |
| 126 | + def on_disconnected(self): |
| 127 | + self.running = False |
| 128 | + self.heartbeat_terminate_event.set() |
| 129 | + |
| 130 | + def on_disconnecting(self): |
| 131 | + self.disconnecting = True |
| 132 | + |
| 133 | + def on_message(self, frame): |
| 134 | + """ |
| 135 | + Reset the last received time whenever a message is received. |
| 136 | +
|
| 137 | + :param Frame frame: the frame |
| 138 | + """ |
| 139 | + self.__update_heartbeat() |
| 140 | + |
| 141 | + def on_error(self, *_): |
| 142 | + """ |
| 143 | + Reset the last received time whenever an error is received. |
| 144 | + """ |
| 145 | + self.__update_heartbeat() |
| 146 | + |
| 147 | + def on_heartbeat(self, frame): |
| 148 | + """ |
| 149 | + Reset the last received time whenever a heartbeat message is received. |
| 150 | + """ |
| 151 | + self.__update_heartbeat() |
| 152 | + |
| 153 | + def on_send(self, frame): |
| 154 | + """ |
| 155 | + Add the heartbeat header to the frame when connecting, and bump |
| 156 | + next outbound heartbeat timestamp. |
| 157 | +
|
| 158 | + :param Frame frame: the Frame object |
| 159 | + """ |
| 160 | + if self.next_outbound_heartbeat is not None: |
| 161 | + self.next_outbound_heartbeat = monotonic() + self.send_sleep |
| 162 | + |
| 163 | + def __update_heartbeat(self): |
| 164 | + if self.received_heartbeat is None: |
| 165 | + return |
| 166 | + now = monotonic() |
| 167 | + if now > self.received_heartbeat: |
| 168 | + self.received_heartbeat = now |
| 169 | + |
| 170 | + def __heartbeat_loop(self): |
| 171 | + """ |
| 172 | + Main loop for sending (and monitoring received) heartbeats. |
| 173 | + """ |
| 174 | + logging.debug("starting heartbeat loop") |
| 175 | + now = monotonic() |
| 176 | + |
| 177 | + # Setup the initial due time for the outbound heartbeat |
| 178 | + if self.send_sleep != 0: |
| 179 | + self.next_outbound_heartbeat = now + self.send_sleep |
| 180 | + logging.debug("calculated next outbound heartbeat as %s", self.next_outbound_heartbeat) |
| 181 | + |
| 182 | + while self.running: |
| 183 | + now = monotonic() |
| 184 | + |
| 185 | + next_events = [] |
| 186 | + if self.next_outbound_heartbeat is not None: |
| 187 | + next_events.append(self.next_outbound_heartbeat - now) |
| 188 | + if self.receive_sleep != 0: |
| 189 | + t = self.received_heartbeat + self.receive_sleep - now |
| 190 | + if t > 0: |
| 191 | + next_events.append(t) |
| 192 | + sleep_time = min(next_events) if next_events else 0 |
| 193 | + if sleep_time > 0: |
| 194 | + terminate = self.heartbeat_terminate_event.wait(sleep_time) |
| 195 | + if terminate: |
| 196 | + break |
| 197 | + |
| 198 | + now = monotonic() |
| 199 | + |
| 200 | + if not self.transport.is_connected() or self.disconnecting: |
| 201 | + time.sleep(self.send_sleep) |
| 202 | + continue |
| 203 | + |
| 204 | + if self.send_sleep != 0 and now > self.next_outbound_heartbeat: |
| 205 | + logging.debug("sending a heartbeat message at %s", now) |
| 206 | + try: |
| 207 | + msg = ProtoMessageUtil.build_heart_beat_message() |
| 208 | + self.transport.transmit(msg) |
| 209 | + except exception.NotConnectedException: |
| 210 | + logging.debug("lost connection, unable to send heartbeat") |
| 211 | + except Exception: |
| 212 | + _, e, _ = sys.exc_info() |
| 213 | + logging.debug("unable to send heartbeat, due to: %s", e) |
| 214 | + |
| 215 | + if self.receive_sleep != 0: |
| 216 | + diff_receive = now - self.received_heartbeat |
| 217 | + |
| 218 | + if diff_receive > self.receive_sleep: |
| 219 | + # heartbeat timeout |
| 220 | + logging.warning("heartbeat timeout: diff_receive=%s, time=%s, lastrec=%s", |
| 221 | + diff_receive, now, self.received_heartbeat) |
| 222 | + self.transport.set_connected(False) |
| 223 | + self.transport.disconnect_socket() |
| 224 | + self.transport.stop() |
| 225 | + for listener in self.transport.listeners.values(): |
| 226 | + listener.on_heartbeat_timeout() |
| 227 | + self.heartbeat_thread = None |
| 228 | + self.heartbeat_terminate_event.clear() |
| 229 | + if self.heartbeats != (0, 0): |
| 230 | + # don't bother logging this if heartbeats weren't setup to start with |
| 231 | + logging.debug("heartbeat loop ended") |
0 commit comments