/** * Returns a {@link ListenableFuture} for an open connection corresponding to the given id. * * <p>N.B. calling this method 'consumes' the connection and as such calling it twice with the * same id will not work, the second future returned will never complete. Similarly calling it * with an id that does not correspond to a worker trying to connect will also fail. */ public ListenableFuture<OpenedSocket> getConnection(UUID id) { checkState(isRunning(), "You can only get connections from a running service: %s", this); return getConnectionImpl(id, Source.REQUEST); }
/** * Returns a {@link ListenableFuture} for an open connection corresponding to the given id. * * <p>N.B. calling this method 'consumes' the connection and as such calling it twice with the * same id will not work, the second future returned will never complete. Similarly calling it * with an id that does not correspond to a worker trying to connect will also fail. */ public ListenableFuture<OpenedSocket> getConnection(UUID id) { checkState(isRunning(), "You can only get connections from a running service: %s", this); return getConnectionImpl(id, Source.REQUEST); }
@Override protected void run() throws Exception { while (isRunning()) { Socket socket; try { socket = serverSocket.accept(); } catch (SocketException e) { // we were closed return; } OpenedSocket openedSocket = OpenedSocket.fromSocket(socket); UUID id = gson.fromJson(openedSocket.reader().readLine(), StartupAnnounceMessage.class) .trialId(); // N.B. you should not call set with the lock held, to prevent same thread executors from // running with the lock. getConnectionImpl(id, Source.ACCEPT).set(openedSocket); } }
@Override protected void run() throws Exception { while (isRunning()) { Socket socket; try { socket = serverSocket.accept(); } catch (SocketException e) { // we were closed return; } OpenedSocket openedSocket = OpenedSocket.fromSocket(socket); UUID id = ((StartupAnnounceMessage) openedSocket.reader().read()).trialId(); // N.B. you should not call set with the lock held, to prevent same thread executors from // running with the lock. getConnectionImpl(id, Source.ACCEPT).set(openedSocket); } }