IRCLib Java创建了IRC Bot问题

时间:2012-12-15 17:05:36

标签: java buffer chat bots irc

我正在使用IRCLib for Java来创建一个IRC bot。它使用Moepii作为库中提供的客户端。 Moepii的问题是它没有内置防洪功能,所以我需要实现这一点。目前我的机器人在响应大量命令时触发了Quakesnet超额洪水并获得启动。

有没有人对我如何实施防洪有任何建议?我得到的一个想法是假设一个1024字节的缓冲区,一旦到达就发送一个Ping到服务器并等待响应然后继续。我之前从未与IRC合作过,所以我希望得到一些指示。

当前实施

private class FloodMonitor implements Runnable {

    private final int MAXBYTES = 512;
    private int messageBuffer = 0;
    private boolean suspend = false;
    private boolean stop = false;

    @Override
    public void run() {

        while (!stop) {
            while (!suspend) {
                MessageStructure message = out.peek();
                if (message != null) {
                    messageBuffer += message.msg.getBytes().length;
                    if (messageBuffer < MAXBYTES) {
                        out.poll().sendMessage();
                    } else {
                        suspend();
                        message.bot.send("ping irc.quakenet.org");
                    }
                }
            }
        }
    }

    public void resetBuffer() {
        messageBuffer = 0;
        suspend = false;
    }

    public void stop() {
        stop = true;
    }

    public void suspend() {
        suspend = true;
    }
}

private class MessageStructure {

    public String target;
    public String msg;
    public BotConnection bot;

    MessageStructure(String target, String msg, BotConnection bot) {
        this.target = target;
        this.msg = msg;
        this.bot = bot;
    }

    private void sendMessage() {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                bot.doPrivmsgApproved(target, msg);
            }
        });

    }
}

1 个答案:

答案 0 :(得分:1)

private class FloodMonitor implements Runnable {

    private final int MAXBYTES = 768;
    private int messageBuffer = 0;
    private boolean suspend = false;
    private boolean stop = false;

    @Override
    public void run() {

        while (!stop) {
            while (!suspend) {
                final MessageStructure message = out.peek();
                if (message != null) {
                    messageBuffer += message.msg.getBytes().length;
                    if (messageBuffer < MAXBYTES) {
                        out.poll().sendMessage();
                    } else {
                        suspend();

                        SwingUtilities.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                message.bot.send("ping irc.quakenet.org");
                            }
                        });

                    }
                }
            }
        }
    }

    public void resetBuffer() {
        messageBuffer = 0;
        suspend = false;
    }

    public void stop() {
        stop = true;
    }

    public void suspend() {
        suspend = true;
    }
}