X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsonitus%2Fdata%2FPipeline.java;h=1fcec198c9502d2262f365627398cfab9d873b31;hb=1cb3955aa53eee7acfbfd9228d60cfb6ddf747f3;hp=5bbcd636091231c4b64c74b528eaeacde258f21d;hpb=633a841142f978235ed9f745b6ba16c278963e62;p=sonitus.git diff --git a/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java b/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java index 5bbcd63..1fcec19 100644 --- a/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java +++ b/src/main/java/net/pterodactylus/sonitus/data/Pipeline.java @@ -72,19 +72,6 @@ public class Pipeline implements Iterable { private Pipeline(Filter source, Multimap filters) { this.source = Preconditions.checkNotNull(source, "source must not be null"); this.filters = ArrayListMultimap.create(Preconditions.checkNotNull(filters, "filters must not be null")); - for (Filter filter : Lists.reverse(filters())) { - logger.finest(String.format("Adding Listener to %s.", filter.name())); - filter.addMetadataListener(new MetadataListener() { - - @Override - public void metadataUpdated(Filter filter, Metadata metadata) { - for (Filter sinks : filters(filter)) { - logger.fine(String.format("Updating Metadata from %s to %s as %s.", filter.name(), sinks.name(), metadata)); - sinks.metadataUpdated(metadata); - } - } - }); - } } // @@ -151,14 +138,16 @@ public class Pipeline implements Iterable { } List filters = Lists.newArrayList(); filters.add(source); + Metadata currentMetadata = Metadata.UNKNOWN; /* collect all source->sink pairs. */ while (!filters.isEmpty()) { Filter filter = filters.remove(0); + logger.info(String.format("Opening %s with %s...", filter.name(), currentMetadata)); + filter.open(currentMetadata); + currentMetadata = filter.metadata(); Collection sinks = this.filters.get(filter); connections.add(new Connection(filter, sinks)); for (Filter sink : sinks) { - logger.info(String.format("Opening %s with %s...", sink.name(), source.metadata())); - sink.open(filter.metadata()); filters.add(sink); } } @@ -308,7 +297,10 @@ public class Pipeline implements Iterable { * * @author David ‘Bombe’ Roden */ - public class Connection implements Runnable { + public static class Connection implements Runnable { + + /** The logger. */ + private static final Logger logger = Logger.getLogger(Connection.class.getName()); /** The source. */ private final Filter source; @@ -328,6 +320,9 @@ public class Pipeline implements Iterable { /** The number of copied bytes. */ private long counter; + /** The exception that was encountered, if any. */ + private Optional ioException = Optional.absent(); + /** * Creates a new connection. * @@ -351,6 +346,24 @@ public class Pipeline implements Iterable { // /** + * Returns the source of this connection. + * + * @return The source of this connection + */ + public Filter source() { + return source; + } + + /** + * Returns the sinks of this connection. + * + * @return The sinks of this connection + */ + public Collection sinks() { + return sinks; + } + + /** * Returns the time this connection was started. * * @return The time this connection was started (in milliseconds since Jan 1, @@ -370,6 +383,17 @@ public class Pipeline implements Iterable { return counter; } + /** + * Returns the I/O exception that was encountered while processing this + * connection. + * + * @return The I/O exception that occured, or {@link Optional#absent()} if no + * exception occured + */ + public Optional ioException() { + return ioException; + } + // // ACTIONS // @@ -388,14 +412,10 @@ public class Pipeline implements Iterable { startTime = System.currentTimeMillis(); while (!stopped.get()) { try { - final byte[] buffer; - try { - logger.finest(String.format("Getting %d bytes from %s...", 4096, source.name())); - buffer = source.get(4096); - logger.finest(String.format("Got %d bytes from %s.", buffer.length, source.name())); - } catch (IOException ioe1) { - throw new IOException(String.format("I/O error while reading from %s.", source.name()), ioe1); - } + final DataPacket dataPacket; + logger.finest(String.format("Getting %d bytes from %s...", 4096, source.name())); + dataPacket = source.get(4096); + logger.finest(String.format("Got %d bytes from %s.", dataPacket.buffer().length, source.name())); List> futures = executorService.invokeAll(FluentIterable.from(sinks).transform(new Function>() { @Override @@ -404,13 +424,9 @@ public class Pipeline implements Iterable { @Override public Void call() throws Exception { - try { - logger.finest(String.format("Sending %d bytes to %s.", buffer.length, sink.name())); - sink.process(buffer); - logger.finest(String.format("Sent %d bytes to %s.", buffer.length, sink.name())); - } catch (IOException ioe1) { - throw new IOException(String.format("I/O error while writing to %s", sink.name()), ioe1); - } + logger.finest(String.format("Sending %d bytes to %s.", dataPacket.buffer().length, sink.name())); + sink.process(dataPacket); + logger.finest(String.format("Sent %d bytes to %s.", dataPacket.buffer().length, sink.name())); return null; } }; @@ -420,10 +436,9 @@ public class Pipeline implements Iterable { for (Future future : futures) { future.get(); } - counter += buffer.length; + counter += dataPacket.buffer().length; } catch (IOException e) { - /* TODO */ - e.printStackTrace(); + ioException = Optional.of(e); break; } catch (InterruptedException e) { /* TODO */