| 1 | /* |
| 2 | * Copyright (c) 2008, https://code.google.com/p/pyronet/ |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: |
| 6 | * |
| 7 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. |
| 9 | * Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. |
| 10 | * |
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 12 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 13 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
| 14 | * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 15 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 16 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 17 | */ |
| 18 | |
| 19 | package jawnae.pyronet.traffic; |
| 20 | |
| 21 | import java.nio.ByteBuffer; |
| 22 | import java.util.LinkedList; |
| 23 | |
| 24 | import jawnae.pyronet.PyroClient; |
| 25 | import jawnae.pyronet.PyroSelector; |
| 26 | import jawnae.pyronet.events.PyroClientAdapter; |
| 27 | |
| 28 | public class PyroByteSinkFeeder extends PyroClientAdapter { |
| 29 | private final PyroSelector selector; |
| 30 | |
| 31 | private final ByteStream inbound; |
| 32 | |
| 33 | private final LinkedList<ByteSink> sinks; |
| 34 | |
| 35 | public PyroByteSinkFeeder(PyroClient client) { |
| 36 | this(client.selector()); |
| 37 | } |
| 38 | |
| 39 | public PyroByteSinkFeeder(PyroSelector selector) { |
| 40 | this(selector, 8 * 1024); |
| 41 | } |
| 42 | |
| 43 | public PyroByteSinkFeeder(PyroSelector selector, int bufferSize) { |
| 44 | this.selector = selector; |
| 45 | this.inbound = new ByteStream(); |
| 46 | this.sinks = new LinkedList<ByteSink>(); |
| 47 | } |
| 48 | |
| 49 | // |
| 50 | |
| 51 | @Override |
| 52 | public void receivedData(PyroClient client, ByteBuffer data) { |
| 53 | this.feed(data); |
| 54 | } |
| 55 | |
| 56 | // |
| 57 | |
| 58 | public ByteBuffer shutdown() { |
| 59 | int bytes = this.inbound.getByteCount(); |
| 60 | ByteBuffer tmp = this.selector.malloc(bytes); |
| 61 | this.inbound.get(tmp); |
| 62 | this.inbound.discard(bytes); |
| 63 | tmp.flip(); |
| 64 | return tmp; |
| 65 | } |
| 66 | |
| 67 | public void addByteSink(ByteSink sink) { |
| 68 | this.selector.checkThread(); |
| 69 | |
| 70 | this.register(sink); |
| 71 | } |
| 72 | |
| 73 | public void feed(ByteBuffer data) { |
| 74 | ByteBuffer copy = this.selector.copy(data); |
| 75 | |
| 76 | this.inbound.append(copy); |
| 77 | |
| 78 | this.fill(); |
| 79 | } |
| 80 | |
| 81 | final void register(ByteSink sink) { |
| 82 | this.sinks.addLast(sink); |
| 83 | |
| 84 | this.fill(); |
| 85 | } |
| 86 | |
| 87 | private final void fill() { |
| 88 | if (this.sinks.isEmpty()) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | ByteSink currentSink = this.sinks.removeFirst(); |
| 93 | |
| 94 | while (currentSink != null && inbound.hasData()) { |
| 95 | switch (currentSink.feed(inbound.read())) { |
| 96 | case ByteSink.FEED_ACCEPTED: |
| 97 | continue; // continue to next feed |
| 98 | |
| 99 | case ByteSink.FEED_ACCEPTED_LAST: |
| 100 | break; // break out switch, not while |
| 101 | |
| 102 | case ByteSink.FEED_REJECTED: |
| 103 | break; // break out switch, not while |
| 104 | } |
| 105 | |
| 106 | if (this.sinks.isEmpty()) { |
| 107 | currentSink = null; |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | currentSink = this.sinks.removeFirst(); |
| 112 | } |
| 113 | |
| 114 | if (currentSink != null) { |
| 115 | this.sinks.addFirst(currentSink); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | } |
| 120 | |