def flush_messages(self): """Send pending messages to all users.""" for socket, user in self.users.items(): if user.pending: malt.log("Sending messages to {}.".format(user.name)) socket.sendall('\0'.join(user.pending).encode()) user.pending = []
def key_press(key): global program, interface if key == 'R': verts, color = interface.render() log("Refreshing randomized split colors.") log(color, level='DEBUG') program['a_vertices'] = verts program['a_color'] = color
def left_click(screen): global program, interface #world = rocket.screen_to_world(screen) x, y = screen #log(y) y = H - y #log(y) # (malt) BUG: multiple pos args are printed on multiple lines #log((x, y), level='INPUT') log(repr(interface.at((x, y))), level='INPUT')
def select(self): """Select and operate on any ready socket.""" for key, mask in self.selector.select(0.0): socket = key.fileobj if socket == self.socket: # server message, client connecting. self.connect(socket) elif socket in self.users.keys(): # user message self.receive_message(socket) else: malt.log("Unknown selector event? {}".format(socket))
def receive_message(self, socket): """Read a socket for messages. Blocks, so only call if selected.""" user = self.users[socket] # Throws IOError when client disconnects. message = socket.recv(BUF_SIZE).decode() if message == '': # Empty message indicates closed socket. self.disconnect(socket) else: try: head, tail = decode_message(message) except ValueError: return if head == 'name': # TODO: Add a notification message for name change malt.log("Renaming {} to {}.".format(str(user), tail)) user.name = tail elif head == 'message': self.record(user, tail) malt.log("{}: '{}'.".format(user.name, tail)) elif head == 'read': malt.log("Reading messages to {}.".format(user.name)) socket.sendall('\0'.join(user.pending).encode()) user.pending = []