All pastes #958938 Raw Edit

Mine

public text v1 · immutable
#958938 ·published 2008-03-27 00:35 UTC
rendered paste body
package MainFeatures;

import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import java.util.PriorityQueue;

public class TCommandSender extends Thread
{
    private LinkedList<CSender>                senders       = null;
    private PriorityQueue<CPrioritizedCommand> commands      = null;
    private int                                commandNumber = 0;
    private boolean                            done          = false;

    TCommandSender()
    {
        this.commands = new PriorityQueue<CPrioritizedCommand>();
        this.senders = new LinkedList<CSender>();
    }

    public synchronized void addCommand(final String command)
    {
        this.commands.add(new CPrioritizedCommand(command + "\r\n", ECommandPriority.Normal, this.commandNumber++));
    }

    public synchronized void addCommand(final String command, final ECommandPriority priority)
    {
        this.commands.add(new CPrioritizedCommand(command + "\r\n", priority, this.commandNumber++));
    }

    public synchronized void addSender(final PrintWriter sender)
    {
        this.senders.add(new CSender(sender));
    }

    public void done()
    {
        this.done = true;
    }

    @Override
    public void run()
    {
        CSender tempSender = null;
        while (!this.done)
            try
            {
                sleep(50);
                final ListIterator<CSender> senderiter = this.senders.listIterator();
                if (!this.commands.isEmpty())
                {
                    final CPrioritizedCommand thisCommand = this.commands.remove();
                    boolean messageSent = false;
                    while (senderiter.hasNext())
                    {
                        tempSender = senderiter.next();
                        if (tempSender.canHandle(thisCommand.getCommand()))
                        {
                            tempSender.execute(thisCommand.getCommand());
                            sleep(200);
                            messageSent = true;
                            break;
                        }
                    }
                    if (messageSent == false)
                        this.commands.add(thisCommand);
                }
            }
            catch (final NoSuchElementException e)
            {
                e.printStackTrace();
            }
            catch (final InterruptedException e)
            {
                e.printStackTrace();
            }
    }
}