001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016, 2023  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.io.util;
020
021import java.io.ByteArrayInputStream;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.ByteBuffer;
026import java.nio.channels.SeekableByteChannel;
027import java.util.Map;
028import org.jgrapes.core.Event;
029import org.jgrapes.core.EventPipeline;
030import org.jgrapes.io.IOSubchannel;
031import org.jgrapes.io.events.Closed;
032import org.jgrapes.io.events.IOError;
033import org.jgrapes.io.events.IOEvent;
034import org.jgrapes.io.events.Input;
035import org.jgrapes.io.events.Output;
036
037/**
038 * Forwards the content of an input stream as a sequence of 
039 * {@link Output} (or optionally {@link Input}) events.
040 * 
041 * The default settings and the constructor 
042 * {@link #InputStreamPipeline(InputStream, IOSubchannel)} reflect
043 * the usage of this class for generating a response (e.g. provide
044 * the content of a file in response to a request from a client).
045 * Using the class with a "downstream" event pipeline, generating
046 * {@link Input} events is used when an input stream generates events
047 * that should be processed as requests by the application.
048 */
049public class InputStreamPipeline implements Runnable {
050
051    private InputStream inStream;
052    private IOSubchannel channel;
053    private EventPipeline eventPipeline;
054    private boolean sendClosed = true;
055    private Map<Object, Object> eventAssociations;
056    private boolean sendInputEvents;
057
058    /**
059     * Creates a new pipeline that sends the data from the given input stream
060     * as events on the given channel, using the given event pipeline.
061     * 
062     * @param in the input stream to read from
063     * @param channel the channel to send to
064     * @param eventPipeline
065     *            the event pipeline used for firing events
066     */
067    @SuppressWarnings("PMD.ShortVariable")
068    public InputStreamPipeline(InputStream in, IOSubchannel channel,
069            EventPipeline eventPipeline) {
070        this.inStream = in;
071        this.channel = channel;
072        this.eventPipeline = eventPipeline;
073    }
074
075    /**
076     * Creates a new pipeline that sends the data from the given input stream
077     * as events on the given channel, using the channel's response pipeline.
078     * 
079     * @param in the input stream to read from
080     * @param channel the channel to send to
081     */
082    @SuppressWarnings("PMD.ShortVariable")
083    public InputStreamPipeline(InputStream in, IOSubchannel channel) {
084        this(in, channel, channel.responsePipeline());
085    }
086
087    /**
088     * Causes the data to be fired as {@link Input} events rather
089     * than the usual {@link Output} events. 
090     * 
091     * @return the stream for easy chaining
092     */
093    public InputStreamPipeline sendInputEvents() {
094        sendInputEvents = true;
095        return this;
096    }
097
098    /**
099     * Suppresses the sending of a closed event when the stream is closed. 
100     * 
101     * @return the stream for easy chaining
102     */
103    public InputStreamPipeline suppressClosed() {
104        sendClosed = false;
105        return this;
106    }
107
108    /**
109     * Configure associations that are applied to the generated
110     * Output events, see {@link Event#setAssociated}.
111     * 
112     * @param associations the associations to apply
113     * @return the pipeline for easy chaining
114     */
115    public InputStreamPipeline
116            setEventAssociations(Map<Object, Object> associations) {
117        eventAssociations = associations;
118        return this;
119    }
120
121    @Override
122    @SuppressWarnings("PMD.CloseResource")
123    public void run() {
124        try {
125            if (inStream instanceof FileInputStream fip) {
126                seekableTransfer(fip.getChannel());
127            } else {
128                defaultTransfer();
129            }
130            if (sendClosed) {
131                eventPipeline.fire(associate(new Closed<>()), channel);
132            }
133        } catch (InterruptedException e) { // NOPMD
134            // Just stop
135        } catch (IOException e) {
136            eventPipeline.fire(associate(new IOError(null, e)), channel);
137        }
138    }
139
140    private void defaultTransfer() throws InterruptedException, IOException {
141        // If available() returns remaining, we can optimize.
142        // Regrettably, there is no marker interface for this, but
143        // the assumption should be true for ByteArrayInputStream.
144        boolean availableIsRemaining = inStream instanceof ByteArrayInputStream;
145        while (true) {
146            ManagedBuffer<ByteBuffer> buffer = null;
147            try {
148                buffer = channel.byteBufferPool().acquire();
149                var backing = buffer.backing;
150                int recvd = inStream.read(backing.array(),
151                    backing.position(), backing.remaining());
152                if (recvd > 0) {
153                    boolean eof
154                        = availableIsRemaining && inStream.available() == 0;
155                    backing.position(backing.position() + recvd);
156                    eventPipeline.fire(associate(ioEvent(buffer, eof)),
157                        channel);
158                    if (eof) {
159                        break;
160                    }
161                    continue;
162                }
163                if (recvd == -1) {
164                    eventPipeline.fire(associate(ioEvent(buffer, true)),
165                        channel);
166                    break;
167                }
168                // Reading 0 bytes shouldn't happen.
169                buffer.unlockBuffer();
170            } catch (IOException e) {
171                buffer.unlockBuffer();
172                throw e;
173            }
174        }
175    }
176
177    /**
178     * A seekable channel allows us to avoid generating an event with
179     * no data and eof set, because we can check after reading if there
180     * is remaining data.
181     *
182     * @param input the input
183     * @throws InterruptedException the interrupted exception
184     * @throws IOException Signals that an I/O exception has occurred.
185     */
186    private void seekableTransfer(SeekableByteChannel input)
187            throws InterruptedException, IOException {
188        while (true) {
189            ManagedBuffer<ByteBuffer> buffer = null;
190            try {
191                buffer = channel.byteBufferPool().acquire();
192                int recvd = input.read(buffer.backing);
193                if (recvd > 0) {
194                    boolean eof = input.position() == input.size();
195                    eventPipeline.fire(associate(ioEvent(buffer, eof)),
196                        channel);
197                    if (eof) {
198                        break;
199                    }
200                    continue;
201                }
202                if (recvd == -1) {
203                    eventPipeline.fire(associate(ioEvent(buffer, true)),
204                        channel);
205                    break;
206                }
207                // Reading 0 bytes shouldn't happen.
208                buffer.unlockBuffer();
209            } catch (IOException e) {
210                buffer.unlockBuffer();
211                throw e;
212            }
213        }
214    }
215
216    private IOEvent<ByteBuffer> ioEvent(ManagedBuffer<ByteBuffer> buffer,
217            boolean eor) {
218        if (sendInputEvents) {
219            return Input.fromSink(buffer, eor);
220        }
221        return Output.fromSink(buffer, eor);
222    }
223
224    private Event<?> associate(Event<?> event) {
225        if (eventAssociations != null) {
226            for (var entry : eventAssociations.entrySet()) {
227                event.setAssociated(entry.getKey(), entry.getValue());
228            }
229        }
230        return event;
231    }
232
233}