summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/InputHandler.java
blob: a3830afa9421b0df2176b0c4237a2f841cd02304 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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<String> 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<String>();
        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 --------------------------------------------
}