From: David ‘Bombe’ Roden Date: Thu, 19 Mar 2009 20:45:25 +0000 (+0100) Subject: Rewrite ExtendedFcpAdapter to handle command execution. X-Git-Tag: v0.1.1~85 X-Git-Url: https://git.pterodactylus.net/?p=jFCPlib.git;a=commitdiff_plain;h=989ae4b49892ef16a846164466636c7ca091fbbe Rewrite ExtendedFcpAdapter to handle command execution. Rewrite existing commands to use ExtendedFcpAdapter. --- diff --git a/src/net/pterodactylus/fcp/highlevel/FcpClient.java b/src/net/pterodactylus/fcp/highlevel/FcpClient.java index 5a3ec09..7694485 100644 --- a/src/net/pterodactylus/fcp/highlevel/FcpClient.java +++ b/src/net/pterodactylus/fcp/highlevel/FcpClient.java @@ -146,29 +146,22 @@ public class FcpClient { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") + public void run() throws IOException { + fcpConnection.connect(); + ClientHello clientHello = new ClientHello(name); + fcpConnection.sendMessage(clientHello); + } + + /** + * {@inheritDoc} + */ + @Override public void receivedNodeHello(FcpConnection fcpConnection, NodeHello nodeHello) { completionLatch.countDown(); } }; - fcpConnection.addFcpListener(fcpListener); - try { - fcpConnection.connect(); - ClientHello clientHello = new ClientHello(name); - fcpConnection.sendMessage(clientHello); - while (true) { - try { - fcpListener.complete(); - break; - } catch (InterruptedException e) { - /* ignore, we’ll loop. */ - } - } - } finally { - fcpConnection.removeFcpListener(fcpListener); - } - if (fcpListener.getFcpException() != null) { - throw fcpListener.getFcpException(); - } + fcpListener.execute(); } /** @@ -202,6 +195,15 @@ public class FcpClient { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") + public void run() throws IOException { + fcpConnection.sendMessage(new ListPeers("list-peers")); + } + + /** + * {@inheritDoc} + */ + @Override public void receivedPeer(FcpConnection fcpConnection, Peer peer) { peers.add(peer); } @@ -214,23 +216,7 @@ public class FcpClient { completionLatch.countDown(); } }; - fcpConnection.addFcpListener(fcpListener); - fcpConnection.sendMessage(new ListPeers("list-peers")); - try { - while (true) { - try { - fcpListener.complete(); - break; - } catch (InterruptedException e) { - /* ignore, we’ll loop. */ - } - } - } finally { - fcpConnection.removeFcpListener(fcpListener); - } - if (fcpListener.getFcpException() != null) { - throw fcpListener.getFcpException(); - } + fcpListener.execute(); return peers; } @@ -305,34 +291,27 @@ public class FcpClient { * @throws FcpException * if an FCP error occurs */ - private void addPeer(AddPeer addPeer) throws IOException, FcpException { + private void addPeer(final AddPeer addPeer) throws IOException, FcpException { ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() { /** * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") + public void run() throws IOException { + fcpConnection.sendMessage(addPeer); + } + + /** + * {@inheritDoc} + */ + @Override public void receivedPeer(FcpConnection fcpConnection, Peer peer) { completionLatch.countDown(); } }; - fcpConnection.addFcpListener(fcpListener); - try { - fcpConnection.sendMessage(addPeer); - while (true) { - try { - fcpListener.complete(); - break; - } catch (InterruptedException ie1) { - /* ignore, we’ll loop. */ - } - } - } finally { - fcpConnection.removeFcpListener(fcpListener); - } - if (fcpListener.getFcpException() != null) { - throw fcpListener.getFcpException(); - } + fcpListener.execute(); } /** @@ -356,26 +335,27 @@ public class FcpClient { * @throws FcpException * if an FCP error occurs */ - public void modifyPeer(Peer peer, Boolean allowLocalAddresses, Boolean disabled, Boolean listenOnly) throws IOException, FcpException { + public void modifyPeer(final Peer peer, final Boolean allowLocalAddresses, final Boolean disabled, final Boolean listenOnly) throws IOException, FcpException { ExtendedFcpAdapter fcpListener = new ExtendedFcpAdapter() { /** * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") + public void run() throws IOException { + fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly)); + } + + /** + * {@inheritDoc} + */ + @Override public void receivedPeer(FcpConnection fcpConnection, Peer peer) { completionLatch.countDown(); } }; - fcpConnection.addFcpListener(fcpListener); - try { - fcpConnection.sendMessage(new ModifyPeer(peer.getIdentity(), allowLocalAddresses, disabled, listenOnly)); - } finally { - fcpConnection.removeFcpListener(fcpListener); - } - if (fcpListener.getFcpException() != null) { - throw fcpListener.getFcpException(); - } + fcpListener.execute(); } /** @@ -384,7 +364,7 @@ public class FcpClient { * * @author David ‘Bombe’ Roden <bombe@freenetproject.org> */ - private static class ExtendedFcpAdapter extends FcpAdapter { + private abstract class ExtendedFcpAdapter extends FcpAdapter { /** The count down latch used to wait for completion. */ protected final CountDownLatch completionLatch = new CountDownLatch(1); @@ -400,24 +380,42 @@ public class FcpClient { } /** - * Returns the FCP exception that occured. If no FCP exception occured, - * null is returned. + * Executes the FCP commands in {@link #run()}, wrapping the execution + * and catching exceptions. * - * @return The FCP exception that occured, or null + * @throws IOException + * if an I/O error occurs + * @throws FcpException + * if an FCP error occurs */ - public FcpException getFcpException() { - return fcpException; + @SuppressWarnings("synthetic-access") + public void execute() throws IOException, FcpException { + fcpConnection.addFcpListener(this); + try { + run(); + while (true) { + try { + completionLatch.await(); + break; + } catch (InterruptedException ie1) { + /* ignore, we’ll loop. */ + } + } + } finally { + fcpConnection.removeFcpListener(this); + } + if (fcpException != null) { + throw fcpException; + } } /** - * Waits for the completion of the command. + * The FCP commands that actually get executed. * - * @throws InterruptedException - * if {@link CountDownLatch#await()} is interrupted + * @throws IOException + * if an I/O error occurs */ - public void complete() throws InterruptedException { - completionLatch.await(); - } + public abstract void run() throws IOException; /** * {@inheritDoc}