Move connection handling to server.py.
This commit is contained in:
parent
0663fc1470
commit
bb5b90691e
2 changed files with 64 additions and 60 deletions
63
main.py
63
main.py
|
@ -52,6 +52,9 @@ from matrix.commands import (
|
||||||
|
|
||||||
from matrix.server import (
|
from matrix.server import (
|
||||||
MatrixServer,
|
MatrixServer,
|
||||||
|
create_default_server,
|
||||||
|
matrix_server_reconnect,
|
||||||
|
matrix_timer_cb,
|
||||||
matrix_config_server_read_cb,
|
matrix_config_server_read_cb,
|
||||||
matrix_config_server_write_cb,
|
matrix_config_server_write_cb,
|
||||||
matrix_config_server_change_cb,
|
matrix_config_server_change_cb,
|
||||||
|
@ -231,7 +234,7 @@ def connect_cb(data, status, gnutls_rc, sock, error, ip_address):
|
||||||
if not server.access_token:
|
if not server.access_token:
|
||||||
matrix_login(server)
|
matrix_login(server)
|
||||||
else:
|
else:
|
||||||
reconnect(server)
|
matrix_server_reconnect(server)
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
elif status_value == W.WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND:
|
elif status_value == W.WEECHAT_HOOK_CONNECT_ADDRESS_NOT_FOUND:
|
||||||
|
@ -275,27 +278,10 @@ 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)
|
||||||
)
|
)
|
||||||
|
|
||||||
reconnect(server)
|
matrix_server_reconnect(server)
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
def reconnect(server):
|
|
||||||
# type: (MatrixServer) -> None
|
|
||||||
server.connecting = True
|
|
||||||
timeout = server.reconnect_count * 5 * 1000
|
|
||||||
|
|
||||||
if timeout > 0:
|
|
||||||
server_buffer_prnt(
|
|
||||||
server,
|
|
||||||
"Reconnecting in {timeout} seconds.".format(
|
|
||||||
timeout=timeout / 1000))
|
|
||||||
W.hook_timer(timeout, 0, 1, "reconnect_cb", server.name)
|
|
||||||
else:
|
|
||||||
connect(server)
|
|
||||||
|
|
||||||
server.reconnect_count += 1
|
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def reconnect_cb(server_name, remaining):
|
def reconnect_cb(server_name, remaining):
|
||||||
server = SERVERS[server_name]
|
server = SERVERS[server_name]
|
||||||
|
@ -352,51 +338,12 @@ def room_close_cb(data, buffer):
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
|
||||||
def matrix_timer_cb(server_name, remaining_calls):
|
|
||||||
server = SERVERS[server_name]
|
|
||||||
|
|
||||||
if not server.connected:
|
|
||||||
if not server.connecting:
|
|
||||||
server_buffer_prnt(server, "Reconnecting timeout blaaaa")
|
|
||||||
reconnect(server)
|
|
||||||
return W.WEECHAT_RC_OK
|
|
||||||
|
|
||||||
while server.send_queue:
|
|
||||||
message = server.send_queue.popleft()
|
|
||||||
prnt_debug(DebugType.MESSAGING, server,
|
|
||||||
("Timer hook found message of type {t} in queue. Sending "
|
|
||||||
"out.".format(t=message.type)))
|
|
||||||
|
|
||||||
if not send(server, message):
|
|
||||||
# We got an error while sending the last message return the message
|
|
||||||
# to the queue and exit the loop
|
|
||||||
server.send_queue.appendleft(message)
|
|
||||||
break
|
|
||||||
|
|
||||||
for message in server.message_queue:
|
|
||||||
server_buffer_prnt(
|
|
||||||
server,
|
|
||||||
"Handling message: {message}".format(message=message))
|
|
||||||
|
|
||||||
return W.WEECHAT_RC_OK
|
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def matrix_unload_cb():
|
def matrix_unload_cb():
|
||||||
matrix_config_free(CONFIG)
|
matrix_config_free(CONFIG)
|
||||||
return W.WEECHAT_RC_OK
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
def create_default_server(config_file):
|
|
||||||
server = MatrixServer('matrix.org', W, config_file)
|
|
||||||
SERVERS[server.name] = server
|
|
||||||
|
|
||||||
W.config_option_set(server.options["address"], "matrix.org", 1)
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def autoconnect(servers):
|
def autoconnect(servers):
|
||||||
for server in servers.values():
|
for server in servers.values():
|
||||||
if server.autoconnect:
|
if server.autoconnect:
|
||||||
|
|
|
@ -22,10 +22,11 @@ import ssl
|
||||||
from collections import deque
|
from collections import deque
|
||||||
from http_parser.pyparser import HttpParser
|
from http_parser.pyparser import HttpParser
|
||||||
|
|
||||||
from matrix.plugin_options import Option
|
from matrix.plugin_options import Option, DebugType
|
||||||
from matrix.utils import key_from_value
|
from matrix.utils import key_from_value, prnt_debug, server_buffer_prnt
|
||||||
from matrix.utf import utf8_decode
|
from matrix.utf import utf8_decode
|
||||||
from matrix.globals import W, SERVERS
|
from matrix.globals import W, SERVERS
|
||||||
|
from matrix.socket import send, connect
|
||||||
|
|
||||||
|
|
||||||
class MatrixServer:
|
class MatrixServer:
|
||||||
|
@ -214,3 +215,59 @@ def matrix_config_server_change_cb(server_name, option):
|
||||||
server.update_option(option, option_name)
|
server.update_option(option, option_name)
|
||||||
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
@utf8_decode
|
||||||
|
def matrix_timer_cb(server_name, remaining_calls):
|
||||||
|
server = SERVERS[server_name]
|
||||||
|
|
||||||
|
if not server.connected:
|
||||||
|
if not server.connecting:
|
||||||
|
server_buffer_prnt(server, "Reconnecting timeout blaaaa")
|
||||||
|
matrix_server_reconnect(server)
|
||||||
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
while server.send_queue:
|
||||||
|
message = server.send_queue.popleft()
|
||||||
|
prnt_debug(DebugType.MESSAGING, server,
|
||||||
|
("Timer hook found message of type {t} in queue. Sending "
|
||||||
|
"out.".format(t=message.type)))
|
||||||
|
|
||||||
|
if not send(server, message):
|
||||||
|
# We got an error while sending the last message return the message
|
||||||
|
# to the queue and exit the loop
|
||||||
|
server.send_queue.appendleft(message)
|
||||||
|
break
|
||||||
|
|
||||||
|
for message in server.message_queue:
|
||||||
|
server_buffer_prnt(
|
||||||
|
server,
|
||||||
|
"Handling message: {message}".format(message=message))
|
||||||
|
|
||||||
|
return W.WEECHAT_RC_OK
|
||||||
|
|
||||||
|
|
||||||
|
def create_default_server(config_file):
|
||||||
|
server = MatrixServer('matrix.org', W, config_file)
|
||||||
|
SERVERS[server.name] = server
|
||||||
|
|
||||||
|
W.config_option_set(server.options["address"], "matrix.org", 1)
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def matrix_server_reconnect(server):
|
||||||
|
# type: (MatrixServer) -> None
|
||||||
|
server.connecting = True
|
||||||
|
timeout = server.reconnect_count * 5 * 1000
|
||||||
|
|
||||||
|
if timeout > 0:
|
||||||
|
server_buffer_prnt(
|
||||||
|
server,
|
||||||
|
"Reconnecting in {timeout} seconds.".format(
|
||||||
|
timeout=timeout / 1000))
|
||||||
|
W.hook_timer(timeout, 0, 1, "reconnect_cb", server.name)
|
||||||
|
else:
|
||||||
|
connect(server)
|
||||||
|
|
||||||
|
server.reconnect_count += 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue