Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move sending raw lines to a background thread #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions pircbot/src/main/java/org/yaaic/protocol/OutputThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ General Public License (GPL) and the www.jibble.org Commercial License.

package org.yaaic.protocol;

import java.io.*;
import java.io.BufferedWriter;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
* A Thread which is responsible for sending messages to the IRC server.
Expand All @@ -28,14 +30,13 @@ General Public License (GPL) and the www.jibble.org Commercial License.
* @version 1.4.6 (Build time: Wed Apr 11 19:20:59 2007)
*/
public class OutputThread extends Thread {



/**
* Constructs an OutputThread for the underlying PircBot. All messages
* sent to the IRC server are sent by this OutputThread to avoid hammering
* the server. Messages are sent immediately if possible. If there are
* multiple messages queued, then there is a delay imposed.
*
*
* @param bot The underlying PircBot instance.
* @param outQueue The Queue from which we will obtain our messages.
*/
Expand All @@ -44,34 +45,41 @@ public class OutputThread extends Thread {
_outQueue = outQueue;
this.setName(this.getClass() + "-Thread");
}


/**
* A static method to write a line to a BufferedOutputStream and then pass
* the line to the log method of the supplied PircBot instance.
*
*
* @param bot The underlying PircBot instance.
* @param out The BufferedOutputStream to write to.
* @param line The line to be written. "\r\n" is appended to the end.
* @param encoding The charset to use when encoing this string into a
* byte array.
*/
static void sendRawLine(IRCClient bot, BufferedWriter bwriter, String line) {
if (line.length() > bot.getMaxLineLength() - 2) {
line = line.substring(0, bot.getMaxLineLength() - 2);
}
synchronized(bwriter) {
try {
bwriter.write(line + "\r\n");
bwriter.flush();
}
catch (Exception e) {
// Silent response - just lose the line.
static void sendRawLine(final IRCClient bot, final BufferedWriter bwriter, final String line) {
RAW_LINE_SENDER_THREAD.execute(new Runnable() {
@Override public void run() {
synchronized (bwriter) {
String lineToSend;
if (line.length() > bot.getMaxLineLength() - 2) {
lineToSend = line.substring(0, bot.getMaxLineLength() - 2);
} else {
lineToSend = line;
}

try {
bwriter.write(lineToSend + "\r\n");
bwriter.flush();
} catch (Exception e) {
// Silent response - just lose the line.
}
}
}
}
});
}


/**
* This method starts the Thread consuming from the outgoing message
* Queue and sending lines to the server.
Expand All @@ -82,7 +90,7 @@ public void run() {
while (running) {
// Small delay to prevent spamming of the channel
Thread.sleep(_bot.getMessageDelay());

String line = (String) _outQueue.next();
if (line != null) {
_bot.sendRawLine(line);
Expand All @@ -96,8 +104,9 @@ public void run() {
// Just let the method return naturally...
}
}


private static final Executor RAW_LINE_SENDER_THREAD = Executors.newSingleThreadExecutor();
private IRCClient _bot = null;
private Queue _outQueue = null;

}