def test_try_auth(self): g = governor.Governor(ticker=False) new_ship = g.gamestate.create_ship() id_map = {'abc': new_ship.uuid} msg = "AUTH: abc" msg2 = "AUTH: xyz" (ship_ref, auth_id) = clientparser.try_auth(msg, id_map, g) self.assertEquals(new_ship, ship_ref) self.assertEquals(auth_id, 'abc') self.assertIsNone(clientparser.try_auth(None, id_map, g)) (ship_ref, auth_id) = clientparser.try_auth(msg2, id_map, g) self.assertIsNotNone(ship_ref) self.assertEquals(auth_id, 'xyz') self.assertIn('xyz', id_map)
def sockethandler(client_socket, game_governor, identity_map, debug): """a method run as a greenlet thread to handle connection and processing of data from connected clients. Responsible for accepting the connection, authenticating the client, and passing any relevant client requests to the "actor" object attached. During setup, hands over the socket connection so that the actor object can send updates at its desired frequency back to the client. :param client_socket: opened and 'accepted' socket :param game_governor: instance of the single game governor :param identity_map: dictionary to map identity strings to ship UUIDs :param debug: print debugging detail """ identity_string = None ship_ref = None while True: # read up to 1024 chars on the socket c = client_socket.recv(1024) if debug: peername = client_socket.getpeername() print "%s:%s >>>%s<<<" % (peername[0], peername[1], c) if not c: break if identity_string is None: (ship_ref, id_string) = clientparser.try_auth(c, identity_map, game_governor) if ship_ref: ship_ref.socket = client_socket client_socket.sendall("CONNECTED TO %s" % ship_ref.uuid) if id_string: identity_string = id_string if debug: print "Authenticated %s to %s" % (id_string, ship_ref.uuid) else: client_socket.close() if debug: print "Authentication failed" else: if debug: print "%s %s:%s >>>%s<<<" % (identity_string, peername[0], peername[1], c) # TODO(heck): add in additional client command parsing here clientparser.process_message(c, ship_ref, game_governor)