/** * @(#)gnsd.java 1.0.1 98/12/20 *

* Copyright (C) 1998 David E. Wexler *

* 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. See * license.txt for a full copy of the GNU GPL. *

* 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. *

* To Contact the author send e-mail to vagabond@netdragon.com or send * snailmail to: 511 Bobcat Ct, Punta Gorda, FL 3398. */ import java.util.*; import logger; import User; import gnsd; /** * Group - Stores group data and provides fast, easy data exchange between * all members. *

* A vector of users is kept for each group and users can join or leave * any group that matches their client/gameID. * * @author David Wexler (vagabond@netdragons.com) * @version 1.0.1 * @since gnsd.1.0.1 */ public class Group { /** * The Users that are in the group. This allows the server to send them * data when require, to remove them from the group, and to keep an * accurate user count for this channel. */ private Vector users; /** * An instance of the primary server. This is used to perform server * level function, such as the removal of groups from the server. */ private gnsd server; /** * The groups ID. */ private int id; /** * The client/game ID that can connect to this group. */ private int gameID; /** * The title or topic of the group. */ private String title; /** * Information about the state or other group wide information. */ private String mode; /** * Determins if data should be sent back to the client it came from. */ private boolean toAll = false; public boolean useName = true; /** * Creats and instance of a new group with all the required information. * The group starts out empty, and no information is sent to the client * about a new group being formed. * * @param id the group id * @param gID the Client/Game ID that can use this group * @param t the group title/topic. * @param m the group mode * @param s Instance of the server. * @since gnsd.1.0.1 */ public Group (int id, int gID, String t, String m, gnsd s) { users = new Vector(); this.id = id; gameID = gID; title = t; mode = m; server = s; } /** * Checks to see if the passed ID is equal to the channel ID. This is used * by the server when checking if a channel with a given ID exists. * * @param cID the group ID to check * @return true if cID == id, false otherwise. * @see id * @since gnsd.1.0.1 */ public boolean checkID(int cID) { return (cID == id); } /** * Checks to see if the passed ID is equal to the channel gameID. This is * used when the server lists channels. If the game ID's don't match, * the channel is not listed. * * @param gID the game ID to check * @return true if gID == gameID, false otherwise. * @see gameID * @since gnsd.1.0.1 */ public boolean checkGameID(int gID) { return (gameID == gID); } /** * Sets the broadcast type. This allows the client to either request data * it sends back, or not. * * @param b do we want data back? * @see toAll * @since gnsd.1.0.1 */ public void setBroadcastType(boolean b) { toAll = b; } /** * Adds a user to the group. This also sends the join command back to all * clients that are in the group. * * @param u the user to add * @since gnsd.1.0.1 */ public void addUser(User u) { users.addElement(u); sendAll("@|join|" + u.getUserName() + "`" + u.getID() + "`" + u.getMode(), u.getID()); } /** * Removes a user from the group. This also sends the part command back to * all the clients taht are in the group. * * @param u the user to remove. * @since gnsd.1.0.1 */ public void removeUser(User u) { sendAll("@|part|" + u.getUserName(), -1); users.removeElement(u); if (users.size() == 0) server.remove(this); } /** * Gets the group ID. * * @return the group ID. * @see id * @since gnsd.1.0.1 */ public int getID() { return id; } /** * Gets the group's title/topic. * * @return the group's title. * @see title * @since gnsd.1.0.1 */ public String getTitle() { return title; } /** * Gets the group's mode. * * @return the group's mode. * @see mode * @since gnsd.1.0.1 */ public String getMode() { return mode; } /** * Displays the name of all the users that are in the group. They are * seperated by a "," so the client can easily pick them appart. * * @return a string that list all the users. * @since gnsd.1.0.1 */ public String getUserList() { String userList = ""; Enumeration e = users.elements(); while (e.hasMoreElements ()) { User u = (User) e.nextElement(); userList = userList + "," + u.getUserName() + "`" + u.getID() + "`" + u.getMode(); } if (userList.length() > 0) return new String(userList.toCharArray(), 1, userList.length()-1); else return "null"; } /** * Gets that data that is sent to the client for the list. Data is * "id`title`mode`userCount". * * @see id * @see title * @see mode * @since gnsd.1.0.1 */ public String getListData() { return "" + id + "`" + title + "`" + mode + "`" + users.size(); } /** * Transmits data to all members of the group. If toAll is set to fall * the person who sends the data does not get a copy back. * * @param data the message to send * @param uID the user who sent it. * @see toAll * @since gnsd.1.0.1 */ public synchronized void sendAll(String data, int uID) { synchronized (users) { Enumeration e = users.elements(); while (e.hasMoreElements ()) { User u = (User) e.nextElement(); if (toAll || u.getID() != uID) u.send(data); } } } /** * Sends a private message to only one user of the group. No Data is * ever sent back to the originating client. * * @param who the person who is going to get the private message. * @param data the private message * @since gnsd.1.0.1 */ public synchronized void sendPriv(String who, String data) { synchronized (users) { Enumeration e = users.elements(); while (e.hasMoreElements ()) { User u = (User) e.nextElement(); if (u.getUserName().compareTo(who) == 0) u.send("!|" + u.getUserName() + "|" + data); } } } }