public void update(int id, Inet4Address address, int port) { System.out.println("update: " + id + " - " + address.getHostAddress() + ":" + port); readLock.lock(); ClientInfo client = clients.get(id); if (client == null) { readLock.unlock(); return; } // Note, due to the update atomically changing data for this client, we do not need a writelock // we do however need to notify the matcher of the change client.update(address, port); notifyMatcher(); readLock.unlock(); }
private boolean isHost(ClientInfo clientOne, ClientInfo clientTwo) { DeviceInfo one = clientOne.getDeviceInfo(); DeviceInfo two = clientTwo.getDeviceInfo(); if (one.getNumProcessors() != two.getNumProcessors()) return one.getNumProcessors() > two.getNumProcessors(); if (one.getMaxMemory() != two.getMaxMemory()) return one.getMaxMemory() > two.getMaxMemory(); if (one.getApiLevel() != two.getApiLevel()) return one.getApiLevel() > two.getApiLevel(); if (one.getGameEngineSpeed() != two.getGameEngineSpeed()) return one.getGameEngineSpeed() > two.getGameEngineSpeed(); if (one.getMeshQuality() != two.getMeshQuality()) return one.getMeshQuality() > two.getMeshQuality(); if (one.getTextureQuality() != two.getTextureQuality()) return one.getTextureQuality() > two.getTextureQuality(); return true; }
public int prepareGame(ClientInfo clientInfo) { System.out.println("prepareGame: " + clientInfo); int id = currentID.incrementAndGet(); clientInfo.resetPing(); writeLock.lock(); clients.put(id, clientInfo); notifyMatcher(); writeLock.unlock(); return id; }
private void matchClients() { final TIntArrayList ids = new TIntArrayList(clients.size()); clients.forEachEntry( new TIntObjectProcedure<ClientInfo>() { @Override public boolean execute(int id, ClientInfo client) { if (client.readyForGame()) ids.add(id); return true; } }); if (ids.size() < 2) return; ids.sort(); for (int i = 0; i < ids.size(); i += 2) { int idOne = ids.get(i); int idTwo = ids.get(i + 1); ClientInfo clientOne = clients.remove(idOne); ClientInfo clientTwo = clients.remove(idTwo); if (clientOne.getAddress().equals(clientTwo.getAddress()) && clientOne.getPort() == clientTwo.getPort()) { if (clientOne.getLastPingTime() > clientTwo.getLastPingTime()) { clients.put(idOne, clientOne); } else { clients.put(idTwo, clientTwo); } continue; } boolean isHost = isHost(clientOne, clientTwo); NetworkGameInfo networkGameInfo; if (isHost) { networkGameInfo = new NetworkGameInfo( idOne, clientOne, clientTwo, clientOne.getRequestedBoard(), clientOne.getRequestedSpeed()); } else { networkGameInfo = new NetworkGameInfo( idTwo, clientTwo, clientOne, clientTwo.getRequestedBoard(), clientTwo.getRequestedSpeed()); } games.put(idOne, networkGameInfo); games.put(idTwo, networkGameInfo); System.out.println("Made game for " + idOne + " and " + idTwo + " " + networkGameInfo); } }