From 860d446d28776ec842fa53e8e08538d4e093d6e9 Mon Sep 17 00:00:00 2001 From: snowleo Date: Wed, 12 Oct 2011 03:14:07 +0200 Subject: EssentialsUpdate WIP --- .../src/f00f/net/irc/martyr/InputHandler.java | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java (limited to 'EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java') diff --git a/EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java b/EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java new file mode 100644 index 000000000..a3830afa9 --- /dev/null +++ b/EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java @@ -0,0 +1,130 @@ +package f00f.net.irc.martyr; + +import java.util.LinkedList; +import java.io.BufferedReader; +import java.io.IOException; + +/** + * A simple class to help manage input from the stream. + */ +public class InputHandler extends Thread +{ + //static Logger log = Logger.getLogger(InputHandler.class); + + private BufferedReader reader; + private IRCConnection connection; + private final LinkedList messages; + + private final Object eventMonitor; + + private static int serialGen = 0; + private int serialNumber = serialGen++; + private boolean doShutdown = false; + + public InputHandler( BufferedReader reader, + IRCConnection connection, + Object eventMonitor ) + { + + super("InputHandler"); + this.reader = reader; + this.connection = connection; + messages = new LinkedList(); + this.eventMonitor = eventMonitor; + + //log.debug("IRCConnection: New"); + } + + /** + * Set the shutdown flag, so that after next read, or on any error, the thread will just exit. + */ + public void signalShutdown() + { + synchronized(this) + { + doShutdown = true; + } + } + + /** + * @return true if there are messages waiting to be processed. + */ + public boolean pendingMessages() + { + synchronized( messages ) + { + return ! messages.isEmpty(); + } + } + + /** + * Gets the message at the top of the message queue and removes it from the + * message queue. + * + * @return Message from top of list. + */ + public String getMessage() + { + synchronized( messages ) + { + return messages.removeFirst(); + } + } + + /** + * Waits for input from the server. When input arrives, it is added to a + * queue and eventMonitor.notifyAll() is called. + */ + public void run() + { + //log.debug("IRCConnection: Running"); + try{ + + String str; + while( true ) + { + synchronized(this) + { + if( doShutdown ) + { + return; + } + } + str = reader.readLine(); + if( str == null ) + { + connection.socketError( new IOException( "Socket disconnected" ) ); + return; + } + synchronized( messages ) + { + messages.addLast( str ); + } + synchronized( eventMonitor ) + { + eventMonitor.notifyAll(); + } + } + } + catch( IOException ioe ) + { + if( doShutdown ) + { + return; + } + connection.socketError( ioe ); + } + finally + { + //log.debug("IRCConnection: Input handler has DIED!"); + } + } + + public String toString() + { + return "InputHandler[" + serialNumber + "]"; + } + + // ----- END InputHandler -------------------------------------------- +} + -- cgit v1.2.3