/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.communication.server;
import gecco.communication.messages.*;
import gecco.server.core.Server;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 *  A class that keep track of all the parts needed for a connection to a client
 *
 * @author     Niklas Hallenfur
 * @created    June 27, 2001
 */


public class ClientConnectionHandler {
    /**
     *  Description of the Field
     */
    private ClientConnection connection;
    /**
     *  Description of the Field
     */
    private RoleConnection roleconnection;
    /**
     *  Description of the Field
     */
    private DataOutputExchangeHandler DEHOutput;
    /**
     *  Description of the Field
     */
    private DataInputExchangeHandler DEHInput;
    /**
     *  Description of the Field
     */
    private Thread outputThread;
    /**
     *  Description of the Field
     */
    private Thread inputThread;


    /**
     *  Constructor for the ClientConnectionHandler object
     *
     * @param  connection      Description of Parameter
     * @param  server          Description of Parameter
     * @param  roleconnection  Description of Parameter
     */
    public ClientConnectionHandler(ClientConnection connection,
            Server server,
            RoleConnection roleconnection) {

        this.connection = connection;
        this.roleconnection = roleconnection;

        DEHOutput = new DataOutputExchangeHandler(new MessageQueue(), connection, server, this);
        outputThread = new Thread(DEHOutput);

        DEHInput = new DataInputExchangeHandler(connection, server, this, DEHOutput);
        inputThread = new Thread(DEHInput);

    }


    /**
     *  Gets the ClientConnection attribute of the ClientConnectionHandler
     *  object
     *
     * @return    The ClientConnection value
     */
    public ClientConnection getClientConnection() {
        return connection;
    }


    /**
     *  Description of the Method
     */
    public void startThreads() {
        inputThread.start();
        outputThread.start();
    }


    /**
     *  called by RoleConnection, to send a message to the client represented by
     *  this ClientConnectionHandler
     *
     * @param  m  Description of Parameter
     */

    public void sendMessage(ServerMessage m) {
        DEHOutput.addMessageToQueue(m);
    }


    /**
     *  Description of the Method
     *
     * @param  role  Description of Parameter
     * @param  id    Description of Parameter
     */
    public void connectionLost(String role, int id) {
        roleconnection.remove(this);
        DEHInput.kill();
        DEHOutput.kill();
    }
}
