Move the connection functions to the server class.

This commit is contained in:
poljar (Damir Jelić) 2018-02-02 10:29:59 +01:00
parent c63551d37e
commit f9df032667
4 changed files with 111 additions and 122 deletions

14
main.py
View file

@ -52,11 +52,7 @@ from matrix.commands import (
from matrix.server import ( from matrix.server import (
MatrixServer, MatrixServer,
create_default_server, create_default_server,
matrix_server_connect,
send_cb, send_cb,
matrix_server_disconnect,
matrix_server_reconnect,
matrix_server_reconnect_schedule,
matrix_timer_cb, matrix_timer_cb,
matrix_config_server_read_cb, matrix_config_server_read_cb,
matrix_config_server_write_cb, matrix_config_server_write_cb,
@ -221,7 +217,7 @@ def try_ssl_handshake(server):
("{prefix}matrix: disconnecting from server...").format( ("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network"))) prefix=W.prefix("network")))
matrix_server_disconnect(server) server.disconnect()
return False return False
@ -251,7 +247,7 @@ def receive_cb(server_name, file_descriptor):
("{prefix}matrix: disconnecting from server...").format( ("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network"))) prefix=W.prefix("network")))
matrix_server_disconnect(server) server.disconnect()
# Queue the failed message for resending # Queue the failed message for resending
if server.receive_queue: if server.receive_queue:
@ -275,7 +271,7 @@ def receive_cb(server_name, file_descriptor):
message = server.receive_queue.popleft() message = server.receive_queue.popleft()
server.send_queue.appendleft(message) server.send_queue.appendleft(message)
matrix_server_disconnect(server) server.disconnect()
break break
received = len(data) # type: int 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) 'Unexpected error: {status}'.format(status=status_value)
) )
matrix_server_reconnect_schedule(server) server.schedule_reconnect()
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK
@ -446,7 +442,7 @@ def matrix_unload_cb():
def autoconnect(servers): def autoconnect(servers):
for server in servers.values(): for server in servers.values():
if server.autoconnect: if server.autoconnect:
matrix_server_connect(server) server.connect()
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -26,11 +26,7 @@ from matrix.utf import utf8_decode
from matrix.api import MatrixMessage, MessageType from matrix.api import MatrixMessage, MessageType
from matrix.utils import key_from_value, tags_from_line_data from matrix.utils import key_from_value, tags_from_line_data
from matrix.plugin_options import DebugType from matrix.plugin_options import DebugType
from matrix.server import ( from matrix.server import MatrixServer
MatrixServer,
matrix_server_connect,
matrix_server_disconnect,
)
W = matrix.globals.W W = matrix.globals.W
@ -822,7 +818,7 @@ def matrix_command_cb(data, buffer, args):
for server_name in args: for server_name in args:
if check_server_existence(server_name, SERVERS): if check_server_existence(server_name, SERVERS):
server = SERVERS[server_name] server = SERVERS[server_name]
matrix_server_connect(server) server.connect()
def disconnect_server(args): def disconnect_server(args):
for server_name in args: for server_name in args:
@ -832,7 +828,7 @@ def matrix_command_cb(data, buffer, args):
# W.unhook(server.timer_hook) # W.unhook(server.timer_hook)
# server.timer_hook = None # server.timer_hook = None
server.access_token = "" server.access_token = ""
matrix_server_disconnect(server, reconnect=False) server.disconnect(reconnect=False)
split_args = list(filter(bool, args.split(' '))) split_args = list(filter(bool, args.split(' ')))

View file

@ -38,7 +38,7 @@ from matrix.api import (
from matrix.utils import server_buffer_prnt, tags_from_line_data, prnt_debug from matrix.utils import server_buffer_prnt, tags_from_line_data, prnt_debug
from matrix.plugin_options import RedactType, DebugType from matrix.plugin_options import RedactType, DebugType
from matrix.server import matrix_server_disconnect
def strip_matrix_server(string): def strip_matrix_server(string):
# type: (str) -> str # type: (str) -> str
@ -809,7 +809,7 @@ def handle_http_response(server, message):
W.unhook(server.timer_hook) W.unhook(server.timer_hook)
server.timer_hook = None server.timer_hook = None
matrix_server_disconnect(server) server.disconnect()
elif message.type == MessageType.STATE: elif message.type == MessageType.STATE:
response = decode_json(server, message.response.body) response = decode_json(server, message.response.body)
reason = ("." if not response or not response["error"] else reason = ("." if not response or not response["error"] else

View file

@ -224,7 +224,7 @@ class MatrixServer:
("{prefix}matrix: disconnecting from server...").format( ("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network"))) prefix=W.prefix("network")))
matrix_server_disconnect(self) self.disconnect()
return False return False
if sent == 0: if sent == 0:
@ -238,7 +238,7 @@ class MatrixServer:
self, self,
("{prefix}matrix: disconnecting from server...").format( ("{prefix}matrix: disconnecting from server...").format(
prefix=W.prefix("network"))) prefix=W.prefix("network")))
matrix_server_disconnect(self) self.disconnect()
return False return False
total_sent = total_sent + sent total_sent = total_sent + sent
@ -276,6 +276,104 @@ class MatrixServer:
return True 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 @utf8_decode
def matrix_config_server_read_cb( def matrix_config_server_read_cb(
@ -341,7 +439,7 @@ def matrix_timer_cb(server_name, remaining_calls):
if ((not server.connected) and if ((not server.connected) and
server.reconnect_time and server.reconnect_time and
current_time >= (server.reconnect_time + server.reconnect_delay)): current_time >= (server.reconnect_time + server.reconnect_delay)):
matrix_server_reconnect(server) server.reconnect()
if not server.connected: if not server.connected:
return W.WEECHAT_RC_OK return W.WEECHAT_RC_OK
@ -370,107 +468,6 @@ def create_default_server(config_file):
return True 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 @utf8_decode
def send_cb(server_name, file_descriptor): def send_cb(server_name, file_descriptor):
# type: (str, int) -> int # type: (str, int) -> int