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

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 java.io.*;

import java.util.*;

/**
 *  A class that represents the connection from the server to a specific role.
 *  So this class keeps track of all the clients that have joined as that
 *  specific role
 *
 * @author     Niklas Hallenfur
 * @created    June 27, 2001
 */

public class RoleConnection {
    /**
     *  Description of the Field
     */
    private Vector clients;
    /**
     *  Description of the Field
     */
    private String role_name;
    /**
     *  Description of the Field
     */
    private gecco.server.core.Server server;


    /**
     *  Constructor for the RoleConnection object
     *
     * @param  role    Description of Parameter
     * @param  server  Description of Parameter
     */
    public RoleConnection(String role, gecco.server.core.Server server) {
        clients = new Vector();
        role_name = new String(role);
        this.server = server;
    }


    /**
     *  returns the number of clients connected to this role
     *
     * @return    The NumberOfClients value
     */

    public int getNumberOfClients() {
        return clients.size();
    }


    /**
     *  returns the lowest nonnegative integer that is not used as an id by any
     *  client
     *
     * @return    The AvailableId value
     */

    public int getAvailableId() {
        int id = 0;
        boolean[] available = new boolean[1000];
        for (int i = 0; i < 1000; i++) {
            available[i] = true;
        }

        for (int i = 0; i < clients.size(); i++) {
            available[((ClientConnectionHandler) clients.get(i)).getClientConnection().getId()] = false;
        }

        for (int i = 0; i < 1000; i++) {
            if (available[i]) {
                return i;
            }
        }
        return -1;
    }


    /**
     *  adds a new client to this role
     *
     * @param  c  Description of Parameter
     */

    public void add(ClientConnection c) {
        ClientConnectionHandler cH = new ClientConnectionHandler(c,
                server,
                this);
        // IMPORTANT! The ClientConnectionHandler MUST be added
        // to the clients-collection before the communication threads
        // are started!
        clients.add(cH);
        cH.startThreads();
    }


    /**
     *  removes a client from this role
     *
     * @param  c  Description of Parameter
     */

    public void remove(ClientConnectionHandler c) {
        clients.remove(c);
    }


    /**
     *  returns a string with the names of the clients connected to this role.
     *  The name is defined as the role immediately followed by the client's
     *  unique id
     *
     * @return    Description of the Returned Value
     */

    public String whoIsConnected() {
        ClientConnection cc;
        String c = "";
        for (int i = 0; i < clients.size(); i++) {
            cc = ((ClientConnectionHandler) clients.get(i)).getClientConnection();
            c = c + cc.getRole() + Integer.toString(cc.getId()) + " ";
        }
        return c;
    }


    /**
     *  called by ClientAmbassodor to send a message to this role This method
     *  then distributes this message to all clients connected as this role
     *
     * @param  m  Description of Parameter
     */

    public void sendMessage(ServerMessage m) {
        for (int i = 0; i < clients.size(); i++) {
            ((ClientConnectionHandler) clients.get(i)).sendMessage(m);
        }
    }

}
