001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2016, 2018  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.BufferedReader;
022import java.io.IOException;
023import java.util.Map;
024import org.jgrapes.core.Event;
025import org.jgrapes.core.EventPipeline;
026import org.jgrapes.io.IOSubchannel;
027import org.jgrapes.io.events.IOError;
028import org.jgrapes.io.events.Input;
029import org.jgrapes.io.events.Output;
030
031/**
032 * Forwards the lines from a {@link BufferedReader} as a sequence of 
033 * {@link Output} (or optionally {@link Input}) events.
034 */
035public class BufferedReaderPipeline implements Runnable {
036
037    private BufferedReader reader;
038    private IOSubchannel channel;
039    private EventPipeline eventPipeline;
040    private boolean sendClosed = true;
041    private Map<Object, Object> eventAssociations;
042    private boolean sendInputEvents;
043
044    /**
045     * Creates a new pipeline that sends the lines from the given reader
046     * as events on the given channel, using the given event pipeline.
047     * 
048     * @param in the reader to read from
049     * @param channel the channel to send to
050     * @param eventPipeline the event pipeline used for firing events
051     */
052    @SuppressWarnings("PMD.ShortVariable")
053    public BufferedReaderPipeline(BufferedReader in, IOSubchannel channel,
054            EventPipeline eventPipeline) {
055        this.reader = in;
056        this.channel = channel;
057        this.eventPipeline = eventPipeline;
058    }
059
060    /**
061     * Creates a new pipeline that sends the lines from the given reader
062     * as events on the given channel, using the channel's response pipeline.
063     * 
064     * @param in the reader to read from
065     * @param channel the channel to send to
066     */
067    @SuppressWarnings("PMD.ShortVariable")
068    public BufferedReaderPipeline(BufferedReader in, IOSubchannel channel) {
069        this(in, channel, channel.responsePipeline());
070    }
071
072    /**
073     * Causes the data to be fired as {@link Input} events rather
074     * than the usual {@link Output} events. 
075     * 
076     * @return the stream for easy chaining
077     */
078    public BufferedReaderPipeline sendInputEvents() {
079        sendInputEvents = true;
080        return this;
081    }
082
083    /**
084     * Suppresses the sending of a closed event when the stream is closed. 
085     * 
086     * @return the stream for easy chaining
087     */
088    public BufferedReaderPipeline suppressClosed() {
089        sendClosed = false;
090        return this;
091    }
092
093    /**
094     * Configure associations that are applied to the generated
095     * Output events, see {@link Event#setAssociated}.
096     * 
097     * @param associations the associations to apply
098     * @return the pipeline for easy chaining
099     */
100    public BufferedReaderPipeline
101            setEventAssociations(Map<Object, Object> associations) {
102        eventAssociations = associations;
103        return this;
104    }
105
106    @Override
107    public void run() {
108        try (var out = new CharBufferWriter(channel, eventPipeline)) {
109            if (sendInputEvents) {
110                out.sendInputEvents();
111            }
112            if (!sendClosed) {
113                out.suppressClose();
114            }
115            out.setEventAssociations(eventAssociations);
116            while (true) {
117                String line = reader.readLine();
118                if (line == null) {
119                    break;
120                }
121                out.write(line);
122                out.flush();
123            }
124        } catch (IOException e) {
125            eventPipeline.fire(associate(new IOError(null, e)), channel);
126        }
127    }
128
129    private Event<?> associate(Event<?> event) {
130        if (eventAssociations != null) {
131            for (var entry : eventAssociations.entrySet()) {
132                event.setAssociated(entry.getKey(), entry.getValue());
133            }
134        }
135        return event;
136    }
137
138}