From f9df03266731d89cd04fee87ebba14ba0f8c805d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Fri, 2 Feb 2018 10:29:59 +0100 Subject: [PATCH] Move the connection functions to the server class. --- main.py | 14 ++-- matrix/commands.py | 10 +-- matrix/messages.py | 4 +- matrix/server.py | 205 ++++++++++++++++++++++----------------------- 4 files changed, 111 insertions(+), 122 deletions(-) diff --git a/main.py b/main.py index eb556f2..f0918a1 100644 --- a/main.py +++ b/main.py @@ -52,11 +52,7 @@ from matrix.commands import ( from matrix.server import ( MatrixServer, create_default_server, - matrix_server_connect, send_cb, - matrix_server_disconnect, - matrix_server_reconnect, - matrix_server_reconnect_schedule, matrix_timer_cb, matrix_config_server_read_cb, matrix_config_server_write_cb, @@ -221,7 +217,7 @@ def try_ssl_handshake(server): ("{prefix}matrix: disconnecting from server...").format( prefix=W.prefix("network"))) - matrix_server_disconnect(server) + server.disconnect() return False @@ -251,7 +247,7 @@ def receive_cb(server_name, file_descriptor): ("{prefix}matrix: disconnecting from server...").format( prefix=W.prefix("network"))) - matrix_server_disconnect(server) + server.disconnect() # Queue the failed message for resending if server.receive_queue: @@ -275,7 +271,7 @@ def receive_cb(server_name, file_descriptor): message = server.receive_queue.popleft() server.send_queue.appendleft(message) - matrix_server_disconnect(server) + server.disconnect() break received = len(data) # type: int @@ -385,7 +381,7 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address): 'Unexpected error: {status}'.format(status=status_value) ) - matrix_server_reconnect_schedule(server) + server.schedule_reconnect() return W.WEECHAT_RC_OK @@ -446,7 +442,7 @@ def matrix_unload_cb(): def autoconnect(servers): for server in servers.values(): if server.autoconnect: - matrix_server_connect(server) + server.connect() if __name__ == "__main__": diff --git a/matrix/commands.py b/matrix/commands.py index e889388..c1f0ef5 100644 --- a/matrix/commands.py +++ b/matrix/commands.py @@ -26,11 +26,7 @@ from matrix.utf import utf8_decode from matrix.api import MatrixMessage, MessageType from matrix.utils import key_from_value, tags_from_line_data from matrix.plugin_options import DebugType -from matrix.server import ( - MatrixServer, - matrix_server_connect, - matrix_server_disconnect, -) +from matrix.server import MatrixServer W = matrix.globals.W @@ -822,7 +818,7 @@ def matrix_command_cb(data, buffer, args): for server_name in args: if check_server_existence(server_name, SERVERS): server = SERVERS[server_name] - matrix_server_connect(server) + server.connect() def disconnect_server(args): for server_name in args: @@ -832,7 +828,7 @@ def matrix_command_cb(data, buffer, args): # W.unhook(server.timer_hook) # server.timer_hook = None server.access_token = "" - matrix_server_disconnect(server, reconnect=False) + server.disconnect(reconnect=False) split_args = list(filter(bool, args.split(' '))) diff --git a/matrix/messages.py b/matrix/messages.py index be0c3ab..e5db99c 100644 --- a/matrix/messages.py +++ b/matrix/messages.py @@ -38,7 +38,7 @@ from matrix.api import ( from matrix.utils import server_buffer_prnt, tags_from_line_data, prnt_debug from matrix.plugin_options import RedactType, DebugType -from matrix.server import matrix_server_disconnect + def strip_matrix_server(string): # type: (str) -> str @@ -809,7 +809,7 @@ def handle_http_response(server, message): W.unhook(server.timer_hook) server.timer_hook = None - matrix_server_disconnect(server) + server.disconnect() elif message.type == MessageType.STATE: response = decode_json(server, message.response.body) reason = ("." if not response or not response["error"] else diff --git a/matrix/server.py b/matrix/server.py index d6f16e7..7659a09 100644 --- a/matrix/server.py +++ b/matrix/server.py @@ -224,7 +224,7 @@ class MatrixServer: ("{prefix}matrix: disconnecting from server...").format( prefix=W.prefix("network"))) - matrix_server_disconnect(self) + self.disconnect() return False if sent == 0: @@ -238,7 +238,7 @@ class MatrixServer: self, ("{prefix}matrix: disconnecting from server...").format( prefix=W.prefix("network"))) - matrix_server_disconnect(self) + self.disconnect() return False total_sent = total_sent + sent @@ -276,6 +276,104 @@ class MatrixServer: return True + def reconnect(self): + message = ("{prefix}matrix: reconnecting to server...").format( + prefix=W.prefix("network")) + + server_buffer_prnt(self, message) + + self.reconnect_time = None + + if not self.connect(): + self.schedule_reconnect() + + def schedule_reconnect(self): + # type: (MatrixServer) -> None + self.connecting = True + self.reconnect_time = time.time() + + if self.reconnect_delay: + self.reconnect_delay = self.reconnect_delay * 2 + else: + self.reconnect_delay = 10 + + message = ("{prefix}matrix: reconnecting to server in {t} " + "seconds").format( + prefix=W.prefix("network"), + t=self.reconnect_delay) + + server_buffer_prnt(self, message) + + def disconnect(self, reconnect=True): + # type: (MatrixServer) -> None + if self.fd_hook: + W.unhook(self.fd_hook) + + if self.socket: + close_socket(self.socket) + + self.fd_hook = None + self.socket = None + self.connected = False + self.access_token = "" + self.receive_queue.clear() + + self.reconnect_delay = 0 + self.reconnect_time = None + + if self.server_buffer: + message = ("{prefix}matrix: disconnected from server").format( + prefix=W.prefix("network")) + server_buffer_prnt(self, message) + + if reconnect: + self.schedule_reconnect() + + def connect(self): + # type: (MatrixServer) -> int + if not self.address or not self.port: + message = "{prefix}Server address or port not set".format( + prefix=W.prefix("error")) + W.prnt("", message) + return False + + if not self.user or not self.password: + message = "{prefix}User or password not set".format( + prefix=W.prefix("error")) + W.prnt("", message) + return False + + if self.connected: + return True + + if not self.server_buffer: + create_server_buffer(self) + + if not self.timer_hook: + self.timer_hook = W.hook_timer( + 1 * 1000, + 0, + 0, + "matrix_timer_cb", + self.name + ) + + ssl_message = " (SSL)" if self.ssl_context.check_hostname else "" + + message = ("{prefix}matrix: Connecting to " + "{server}:{port}{ssl}...").format( + prefix=W.prefix("network"), + server=self.address, + port=self.port, + ssl=ssl_message) + + W.prnt(self.server_buffer, message) + + W.hook_connect("", self.address, self.port, 1, 0, "", + "connect_cb", self.name) + + return True + @utf8_decode def matrix_config_server_read_cb( @@ -341,7 +439,7 @@ def matrix_timer_cb(server_name, remaining_calls): if ((not server.connected) and server.reconnect_time and current_time >= (server.reconnect_time + server.reconnect_delay)): - matrix_server_reconnect(server) + server.reconnect() if not server.connected: return W.WEECHAT_RC_OK @@ -370,107 +468,6 @@ def create_default_server(config_file): return True -def matrix_server_reconnect(server): - message = ("{prefix}matrix: reconnecting to server...").format( - prefix=W.prefix("network")) - - server_buffer_prnt(server, message) - - server.reconnect_time = None - - if not matrix_server_connect(server): - matrix_server_reconnect_schedule(server) - - -def matrix_server_reconnect_schedule(server): - # type: (MatrixServer) -> None - server.connecting = True - server.reconnect_time = time.time() - - if server.reconnect_delay: - server.reconnect_delay = server.reconnect_delay * 2 - else: - server.reconnect_delay = 10 - - message = ("{prefix}matrix: reconnecting to server in {t} " - "seconds").format( - prefix=W.prefix("network"), - t=server.reconnect_delay) - - server_buffer_prnt(server, message) - - -def matrix_server_disconnect(server, reconnect=True): - # type: (MatrixServer) -> None - if server.fd_hook: - W.unhook(server.fd_hook) - - if server.socket: - close_socket(server.socket) - - server.fd_hook = None - server.socket = None - server.connected = False - server.access_token = "" - server.receive_queue.clear() - - server.reconnect_delay = 0 - server.reconnect_time = None - - if server.server_buffer: - message = ("{prefix}matrix: disconnected from server").format( - prefix=W.prefix("network")) - server_buffer_prnt(server, message) - - if reconnect: - matrix_server_reconnect_schedule(server) - - -def matrix_server_connect(server): - # type: (MatrixServer) -> int - if not server.address or not server.port: - message = "{prefix}Server address or port not set".format( - prefix=W.prefix("error")) - W.prnt("", message) - return False - - if not server.user or not server.password: - message = "{prefix}User or password not set".format( - prefix=W.prefix("error")) - W.prnt("", message) - return False - - if server.connected: - return True - - if not server.server_buffer: - create_server_buffer(server) - - if not server.timer_hook: - server.timer_hook = W.hook_timer( - 1 * 1000, - 0, - 0, - "matrix_timer_cb", - server.name - ) - - ssl_message = " (SSL)" if server.ssl_context.check_hostname else "" - - message = "{prefix}matrix: Connecting to {server}:{port}{ssl}...".format( - prefix=W.prefix("network"), - server=server.address, - port=server.port, - ssl=ssl_message) - - W.prnt(server.server_buffer, message) - - W.hook_connect("", server.address, server.port, 1, 0, "", - "connect_cb", server.name) - - return True - - @utf8_decode def send_cb(server_name, file_descriptor): # type: (str, int) -> int