Search in sources :

Example 1 with IIRCState

use of com.ircclouds.irc.api.state.IIRCState in project druid by druid-io.

the class IrcFirehoseFactory method connect.

@Override
public Firehose connect(final IrcInputRowParser firehoseParser) throws IOException {
    final IRCApi irc = new IRCApiImpl(false);
    final LinkedBlockingQueue<Pair<DateTime, ChannelPrivMsg>> queue = new LinkedBlockingQueue<Pair<DateTime, ChannelPrivMsg>>();
    irc.addListener(new VariousMessageListenerAdapter() {

        @Override
        public void onChannelMessage(ChannelPrivMsg aMsg) {
            try {
                queue.put(Pair.of(DateTime.now(), aMsg));
            } catch (InterruptedException e) {
                throw new RuntimeException("interrupted adding message to queue", e);
            }
        }
    });
    log.info("connecting to irc server [%s]", host);
    irc.connect(new IServerParameters() {

        @Override
        public String getNickname() {
            return nick;
        }

        @Override
        public List<String> getAlternativeNicknames() {
            return Lists.newArrayList(nick + UUID.randomUUID(), nick + UUID.randomUUID(), nick + UUID.randomUUID());
        }

        @Override
        public String getIdent() {
            return "druid";
        }

        @Override
        public String getRealname() {
            return nick;
        }

        @Override
        public IRCServer getServer() {
            return new IRCServer(host, false);
        }
    }, new Callback<IIRCState>() {

        @Override
        public void onSuccess(IIRCState aObject) {
            log.info("irc connection to server [%s] established", host);
            for (String chan : channels) {
                log.info("Joining channel %s", chan);
                irc.joinChannel(chan);
            }
        }

        @Override
        public void onFailure(Exception e) {
            log.error(e, "Unable to connect to irc server [%s]", host);
            throw new RuntimeException("Unable to connect to server", e);
        }
    });
    closed = false;
    return new Firehose() {

        InputRow nextRow = null;

        @Override
        public boolean hasMore() {
            try {
                while (true) {
                    Pair<DateTime, ChannelPrivMsg> nextMsg = queue.poll(100, TimeUnit.MILLISECONDS);
                    if (closed) {
                        return false;
                    }
                    if (nextMsg == null) {
                        continue;
                    }
                    try {
                        nextRow = firehoseParser.parse(nextMsg);
                        if (nextRow != null) {
                            return true;
                        }
                    } catch (IllegalArgumentException iae) {
                        log.debug("ignoring invalid message in channel [%s]", nextMsg.rhs.getChannelName());
                    }
                }
            } catch (InterruptedException e) {
                Thread.interrupted();
                throw new RuntimeException("interrupted retrieving elements from queue", e);
            }
        }

        @Override
        public InputRow nextRow() {
            return nextRow;
        }

        @Override
        public Runnable commit() {
            return new Runnable() {

                @Override
                public void run() {
                // nothing to see here
                }
            };
        }

        @Override
        public void close() throws IOException {
            try {
                log.info("disconnecting from irc server [%s]", host);
                irc.disconnect("");
            } finally {
                closed = true;
            }
        }
    };
}
Also used : VariousMessageListenerAdapter(com.ircclouds.irc.api.listeners.VariousMessageListenerAdapter) Firehose(io.druid.data.input.Firehose) IRCApiImpl(com.ircclouds.irc.api.IRCApiImpl) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DateTime(org.joda.time.DateTime) ChannelPrivMsg(com.ircclouds.irc.api.domain.messages.ChannelPrivMsg) IOException(java.io.IOException) IRCServer(com.ircclouds.irc.api.domain.IRCServer) IRCApi(com.ircclouds.irc.api.IRCApi) IIRCState(com.ircclouds.irc.api.state.IIRCState) InputRow(io.druid.data.input.InputRow) IServerParameters(com.ircclouds.irc.api.IServerParameters) List(java.util.List) Pair(io.druid.java.util.common.Pair)

Aggregations

IRCApi (com.ircclouds.irc.api.IRCApi)1 IRCApiImpl (com.ircclouds.irc.api.IRCApiImpl)1 IServerParameters (com.ircclouds.irc.api.IServerParameters)1 IRCServer (com.ircclouds.irc.api.domain.IRCServer)1 ChannelPrivMsg (com.ircclouds.irc.api.domain.messages.ChannelPrivMsg)1 VariousMessageListenerAdapter (com.ircclouds.irc.api.listeners.VariousMessageListenerAdapter)1 IIRCState (com.ircclouds.irc.api.state.IIRCState)1 Firehose (io.druid.data.input.Firehose)1 InputRow (io.druid.data.input.InputRow)1 Pair (io.druid.java.util.common.Pair)1 IOException (java.io.IOException)1 List (java.util.List)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 DateTime (org.joda.time.DateTime)1