use of com.ircclouds.irc.api.listeners.VariousMessageListenerAdapter 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;
}
}
};
}
Aggregations