Use enums for message types in channels
This commit is contained in:
parent
044fc0d4bd
commit
2e1bc6a6b1
2 changed files with 39 additions and 17 deletions
10
constants.py
Normal file
10
constants.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import enum
|
||||||
|
|
||||||
|
class logmessage_types(enum.Enum):
|
||||||
|
sent, received, internal = range(3)
|
||||||
|
|
||||||
|
class internal_submessage_types(enum.Enum):
|
||||||
|
quit, error = range(2)
|
||||||
|
|
||||||
|
class controlmessage_types(enum.Enum):
|
||||||
|
quit, send_line = range(2)
|
46
ircbot.py
46
ircbot.py
|
@ -5,6 +5,7 @@ import threading
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
import channel
|
import channel
|
||||||
|
from constants import logmessage_types, internal_submessage_types, controlmessage_types
|
||||||
|
|
||||||
Server = namedtuple('Server', ['host', 'port'])
|
Server = namedtuple('Server', ['host', 'port'])
|
||||||
|
|
||||||
|
@ -27,13 +28,11 @@ class ServerThread(threading.Thread):
|
||||||
with self.server_socket_write_lock:
|
with self.server_socket_write_lock:
|
||||||
self.server_socket.sendall(line + b'\r\n')
|
self.server_socket.sendall(line + b'\r\n')
|
||||||
|
|
||||||
# FIXME: use a real data structure
|
self.logging_channel.send((logmessage_types.sent, line.decode(encoding = 'utf-8', errors = 'replace')))
|
||||||
self.logging_channel.send('>' + line.decode(encoding = 'utf-8', errors = 'replace'))
|
|
||||||
|
|
||||||
def handle_line(self, line):
|
def handle_line(self, line):
|
||||||
# TODO: implement line handling
|
# TODO: implement line handling
|
||||||
# FIXME: use a real data structure
|
self.logging_channel.send((logmessage_types.received, line.decode(encoding = 'utf-8', errors = 'replace')))
|
||||||
self.logging_channel.send('<' + line.decode(encoding = 'utf-8', errors = 'replace'))
|
|
||||||
|
|
||||||
def mainloop(self):
|
def mainloop(self):
|
||||||
# Register both the server socket and the control channel to or polling object
|
# Register both the server socket and the control channel to or polling object
|
||||||
|
@ -65,19 +64,18 @@ class ServerThread(threading.Thread):
|
||||||
|
|
||||||
# Control
|
# Control
|
||||||
elif fd == self.control_channel.fileno():
|
elif fd == self.control_channel.fileno():
|
||||||
command = self.control_channel.recv()
|
command_type, *arguments = self.control_channel.recv()
|
||||||
|
if command_type == controlmessage_types.quit:
|
||||||
# FIXME: use a real data structure
|
|
||||||
if command == 'q':
|
|
||||||
quitting = True
|
quitting = True
|
||||||
|
|
||||||
elif len(command) > 0 and command[0] == '/':
|
elif command_type == controlmessage_types.send_line:
|
||||||
irc_command, space, arguments = command[1:].encode('utf-8').partition(b' ')
|
assert len(arguments) == 1
|
||||||
|
irc_command, space, arguments = arguments[0].encode('utf-8').partition(b' ')
|
||||||
line = irc_command.upper() + space + arguments
|
line = irc_command.upper() + space + arguments
|
||||||
self.send_line_raw(line)
|
self.send_line_raw(line)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.logging_channel.send('?')
|
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.error))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
assert False #unreachable
|
assert False #unreachable
|
||||||
|
@ -89,7 +87,8 @@ class ServerThread(threading.Thread):
|
||||||
self.server_socket = socket.create_connection(address)
|
self.server_socket = socket.create_connection(address)
|
||||||
except ConnectionRefusedError:
|
except ConnectionRefusedError:
|
||||||
# Tell controller we failed
|
# Tell controller we failed
|
||||||
self.logging_channel.send('f')
|
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.error))
|
||||||
|
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.quit))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Run initialization
|
# Run initialization
|
||||||
|
@ -105,7 +104,7 @@ class ServerThread(threading.Thread):
|
||||||
self.server_socket.close()
|
self.server_socket.close()
|
||||||
|
|
||||||
# Tell controller we're quiting
|
# Tell controller we're quiting
|
||||||
self.logging_channel.send('q')
|
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.quit))
|
||||||
|
|
||||||
# spawn_serverthread(server) → control_channel, logging_channel
|
# spawn_serverthread(server) → control_channel, logging_channel
|
||||||
# Creates a ServerThread for given server and returns the channels for controlling and monitoring it
|
# Creates a ServerThread for given server and returns the channels for controlling and monitoring it
|
||||||
|
@ -126,11 +125,24 @@ if __name__ == '__main__':
|
||||||
data = logging_channel.recv(blocking = False)
|
data = logging_channel.recv(blocking = False)
|
||||||
if data == None:
|
if data == None:
|
||||||
break
|
break
|
||||||
print(data)
|
message_type, message_data = data
|
||||||
|
if message_type == logmessage_types.sent:
|
||||||
|
print('>' + message_data)
|
||||||
|
elif message_type == logmessage_types.received:
|
||||||
|
print('<' + message_data)
|
||||||
|
elif message_type == logmessage_types.internal:
|
||||||
|
if message_data == internal_submessage_types.quit:
|
||||||
|
print('--- Quit')
|
||||||
|
elif message_data == internal_submessage_types.error:
|
||||||
|
print('--- Error')
|
||||||
|
else:
|
||||||
|
print('--- ???', message_data)
|
||||||
|
else:
|
||||||
|
print('???', message_type, message_data)
|
||||||
|
|
||||||
elif cmd == 'q':
|
elif cmd == 'q':
|
||||||
control_channel.send('q')
|
control_channel.send((controlmessage_types.quit,))
|
||||||
break
|
break
|
||||||
|
|
||||||
else:
|
elif len(cmd) > 0 and cmd[0] == '/':
|
||||||
control_channel.send(cmd)
|
control_channel.send((controlmessage_types.send_line, cmd[1:]))
|
||||||
|
|
Loading…
Reference in a new issue