001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.stream.provider.irc;
031
032import java.io.IOException;
033
034import org.openimaj.util.concurrent.ArrayBlockingDroppingQueue;
035import org.openimaj.util.concurrent.BlockingDroppingQueue;
036import org.openimaj.util.data.Context;
037
038/**
039 * Basic streaming dataset from IRC messages. Messages are provided as
040 * {@link Context} objects. The contexts contain keys for channel, sender,
041 * login, hostname and message. All values are {@link String}s.
042 * 
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 * 
045 */
046public class BasicIRCStreamDataset extends AbstractIRCStreamDataset<Context> {
047        /**
048         * The IRC channel name key
049         */
050        public String CHANNEL_KEY = "channel";
051        /**
052         * The sender key
053         */
054        public String SENDER_KEY = "sender";
055        /**
056         * The login key
057         */
058        public String LOGIN_KEY = "login";
059        /**
060         * The host name key
061         */
062        public String HOSTNAME_KEY = "hostname";
063        /**
064         * The message text key
065         */
066        public String MESSAGE_KEY = "message";
067
068        /**
069         * Connect to the given host and channel. Internally a
070         * {@link ArrayBlockingDroppingQueue} with size of 1 is created to
071         * buffer/drop messages.
072         * 
073         * @param hostname
074         *            the host
075         * @param channel
076         *            the channel
077         * @throws IOException
078         */
079        public BasicIRCStreamDataset(String hostname, String channel)
080                        throws IOException
081        {
082                this(new ArrayBlockingDroppingQueue<Context>(1), hostname, channel);
083        }
084
085        /**
086         * Construct with the given buffer, connecting to the given host and
087         * channel.
088         * 
089         * @param buffer
090         *            the buffer
091         * @param hostname
092         *            the host
093         * @param channel
094         *            the channel
095         * @throws IOException
096         */
097        public BasicIRCStreamDataset(BlockingDroppingQueue<Context> buffer, String hostname, String channel)
098                        throws IOException
099        {
100                super(buffer, hostname, channel);
101        }
102
103        @Override
104        public Context construct(String channel, String sender, String login, String hostname, String message) {
105                final Context ctx = new Context();
106
107                ctx.put(CHANNEL_KEY, channel);
108                ctx.put(SENDER_KEY, sender);
109                ctx.put(LOGIN_KEY, login);
110                ctx.put(HOSTNAME_KEY, hostname);
111                ctx.put(MESSAGE_KEY, message);
112
113                return ctx;
114        }
115}