forked from nortti/tea_cah
Have logger as its own thread
This commit is contained in:
parent
abb579bc88
commit
164cede500
1 changed files with 61 additions and 31 deletions
92
ircbot.py
92
ircbot.py
|
@ -11,6 +11,46 @@ import line_handling
|
||||||
|
|
||||||
Server = namedtuple('Server', ['host', 'port', 'nick', 'realname', 'channels'])
|
Server = namedtuple('Server', ['host', 'port', 'nick', 'realname', 'channels'])
|
||||||
|
|
||||||
|
class LoggerThread(threading.Thread):
|
||||||
|
def __init__(self, logging_channel, dead_notify_channel):
|
||||||
|
self.logging_channel = logging_channel
|
||||||
|
self.dead_notify_channel = dead_notify_channel
|
||||||
|
|
||||||
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
while True:
|
||||||
|
message_type, *message_data = self.logging_channel.recv()
|
||||||
|
|
||||||
|
# Lines that were sent between server and client
|
||||||
|
if message_type == logmessage_types.sent:
|
||||||
|
assert len(message_data) == 1
|
||||||
|
print('>' + message_data[0])
|
||||||
|
|
||||||
|
elif message_type == logmessage_types.received:
|
||||||
|
assert len(message_data) == 1
|
||||||
|
print('<' + message_data[0])
|
||||||
|
|
||||||
|
# Messages that are from internal components
|
||||||
|
elif message_type == logmessage_types.internal:
|
||||||
|
if message_data[0] == internal_submessage_types.quit:
|
||||||
|
assert len(message_data) == 1
|
||||||
|
print('--- Quit')
|
||||||
|
|
||||||
|
# TODO: don't quit, restart
|
||||||
|
self.dead_notify_channel.send((controlmessage_types.quit,))
|
||||||
|
break
|
||||||
|
|
||||||
|
elif message_data[0] == internal_submessage_types.error:
|
||||||
|
assert len(message_data) == 2
|
||||||
|
print('--- Error', message_data[1])
|
||||||
|
|
||||||
|
else:
|
||||||
|
print('--- ???', message_data)
|
||||||
|
|
||||||
|
else:
|
||||||
|
print('???', message_type, message_data)
|
||||||
|
|
||||||
# API(serverthread_object)
|
# API(serverthread_object)
|
||||||
# Create a new API object corresponding to given ServerThread object
|
# Create a new API object corresponding to given ServerThread object
|
||||||
class API:
|
class API:
|
||||||
|
@ -162,46 +202,36 @@ class ServerThread(threading.Thread):
|
||||||
# Tell controller we're quiting
|
# Tell controller we're quiting
|
||||||
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.quit))
|
self.logging_channel.send((logmessage_types.internal, internal_submessage_types.quit))
|
||||||
|
|
||||||
# spawn_serverthread(server) → control_channel, logging_channel
|
# spawn_serverthread(server, logging_channel) → control_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 channel for controlling it
|
||||||
def spawn_serverthread(server):
|
def spawn_serverthread(server, logging_channel):
|
||||||
thread_control_socket, spawner_control_socket = socket.socketpair()
|
thread_control_socket, spawner_control_socket = socket.socketpair()
|
||||||
control_channel = channel.Channel()
|
control_channel = channel.Channel()
|
||||||
logging_channel = channel.Channel()
|
|
||||||
ServerThread(server, control_channel, logging_channel).start()
|
ServerThread(server, control_channel, logging_channel).start()
|
||||||
return (control_channel, logging_channel)
|
return control_channel
|
||||||
|
|
||||||
|
# spawn_loggerthread() → logging_channel, dead_notify_channel
|
||||||
|
# Spawn logger thread and returns the channel it logs and the channel it uses to notify about quiting
|
||||||
|
def spawn_loggerthread():
|
||||||
|
logging_channel = channel.Channel()
|
||||||
|
dead_notify_channel = channel.Channel()
|
||||||
|
LoggerThread(logging_channel, dead_notify_channel).start()
|
||||||
|
return logging_channel, dead_notify_channel
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
server = Server(host = 'irc.freenode.net', port = 6667, nick = 'HynneFlip', realname = 'HynneFlip IRC bot', channels = ['##ingsoc'])
|
server = Server(host = 'irc.freenode.net', port = 6667, nick = 'HynneFlip', realname = 'HynneFlip IRC bot', channels = ['##ingsoc'])
|
||||||
control_channel, logging_channel = spawn_serverthread(server)
|
|
||||||
|
logging_channel, dead_notify_channel = spawn_loggerthread()
|
||||||
|
control_channel = spawn_serverthread(server, logging_channel)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
cmd = input(': ')
|
message = dead_notify_channel.recv(blocking = False)
|
||||||
if cmd == '':
|
if message is not None:
|
||||||
while True:
|
if message[0] == controlmessage_types.quit:
|
||||||
data = logging_channel.recv(blocking = False)
|
break
|
||||||
if data == None:
|
|
||||||
break
|
|
||||||
message_type, *message_data = data
|
|
||||||
if message_type == logmessage_types.sent:
|
|
||||||
assert len(message_data) == 1
|
|
||||||
print('>' + message_data[0])
|
|
||||||
elif message_type == logmessage_types.received:
|
|
||||||
assert len(message_data) == 1
|
|
||||||
print('<' + message_data[0])
|
|
||||||
elif message_type == logmessage_types.internal:
|
|
||||||
if message_data[0] == internal_submessage_types.quit:
|
|
||||||
assert len(message_data) == 1
|
|
||||||
print('--- Quit')
|
|
||||||
elif message_data[0] == internal_submessage_types.error:
|
|
||||||
assert len(message_data) == 2
|
|
||||||
print('--- Error', message_data[1])
|
|
||||||
else:
|
|
||||||
print('--- ???', message_data)
|
|
||||||
else:
|
|
||||||
print('???', message_type, message_data)
|
|
||||||
|
|
||||||
elif cmd == 'q':
|
cmd = input('')
|
||||||
|
if cmd == 'q':
|
||||||
control_channel.send((controlmessage_types.quit,))
|
control_channel.send((controlmessage_types.quit,))
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue