0

我正在使用 IRCLib for Java 创建 IRC 机器人。它使用图书馆提供的 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);
            }
        });

    }
}
4

1 回答 1

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;
    }
}
于 2012-12-16T16:04:21.973 回答