private void reportLocation(String newClient, LocalClient me) { // Suspend user input and random generator me.pause(); // Wait 100ms for all packets to be queued try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } // Clear queue Mazewar.actionQueue.clear(); Mazewar.ackTracker.clear(); // Get my location DirectedPoint myDp = new DirectedPoint(me.getPoint(), me.getOrientation()); // Prepare packet to report my location MazewarPacket outgoing = new MazewarPacket(); outgoing.type = MazewarPacket.REPORT_LOCATION; outgoing.owner = Mazewar.myName; outgoing.directedPoint = myDp; outgoing.score = me.getScore(); synchronized (Mazewar.connectedOuts) { logger.info("Reporting my location to: " + newClient); // Report my location to the new guy try { Mazewar.connectedOuts.get(newClient).writeObject(outgoing); } catch (IOException e) { e.printStackTrace(); } } }
public boolean sendEvent(LocalClient client, ClientEvent clientevent) { MazewarPacket payload = new MazewarPacket(); payload.clientName = client.getName(); if (clientevent.equals(ClientEvent.moveForward)) { payload.eventType = MazewarPacket.ACTION_MOVE_UP; } else if (clientevent.equals(ClientEvent.moveBackward)) { payload.eventType = MazewarPacket.ACTION_MOVE_DOWN; } else if (clientevent.equals(ClientEvent.turnLeft)) { payload.eventType = MazewarPacket.ACTION_TURN_LEFT; } else if (clientevent.equals(ClientEvent.turnRight)) { payload.eventType = MazewarPacket.ACTION_TURN_RIGHT; } else if (clientevent.equals(ClientEvent.fire)) { payload.eventType = MazewarPacket.ACTION_FIRE_PROJECTILE; } else if (clientevent.equals(ClientEvent.register)) { payload.eventType = MazewarPacket.REGISTER; } else if (clientevent.equals(ClientEvent.quit)) { payload.eventType = MazewarPacket.QUIT; } else { return false; } try { outStream.writeObject(payload); } catch (IOException e) { /* TODO how do we handle this? */ e.printStackTrace(); return false; } return true; }
/** * Override the abstract {@link Client}'s registerMaze method so that we know when to start * control thread. * * @param maze The {@link Maze} that we are begin registered with. */ public synchronized void registerMaze(Maze maze) { assert (maze != null); super.registerMaze(maze); // Get the control thread going. active = true; thread.start(); }
/** * Override the abstract {@link Client}'s unregisterMaze method so we know when to stop the * control thread. */ public synchronized void unregisterMaze() { // Signal the control thread to stop active = false; // Wait half a second for the thread to complete. try { thread.join(500); } catch (Exception e) { // Shouldn't happen } super.unregisterMaze(); }