1 /*** 2 * Jetrix TetriNET Server 3 * Copyright (C) 2001-2003 Emmanuel Bourg 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 2 8 * of the License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 */ 19 20 package net.jetrix; 21 22 import java.net.InetAddress; 23 import java.util.Collection; 24 import java.util.Iterator; 25 import java.util.Map; 26 import java.util.List; 27 import java.util.ArrayList; 28 import java.util.concurrent.ConcurrentSkipListMap; 29 30 /*** 31 * Repository of clients connected to the server. 32 * 33 * @author Emmanuel Bourg 34 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $ 35 */ 36 public class ClientRepository 37 { 38 private static ClientRepository instance = new ClientRepository(); 39 private Map<String, Client> clients = new ConcurrentSkipListMap<String, Client>(); 40 41 private ClientRepository() 42 { 43 } 44 45 public static ClientRepository getInstance() 46 { 47 return instance; 48 } 49 50 /*** 51 * Add a client into the repository. 52 * 53 * @param client the client to add 54 */ 55 public void addClient(Client client) 56 { 57 if (client != null) 58 { 59 clients.put(client.getUser().getName().toLowerCase(), client); 60 } 61 } 62 63 /*** 64 * Remove a client from the repository. 65 * 66 * @param client the client to remove 67 */ 68 public void removeClient(Client client) 69 { 70 if (client != null) 71 { 72 clients.remove(client.getUser().getName().toLowerCase()); 73 } 74 } 75 76 /*** 77 * Return an iterator of players online in alphabetical order. 78 */ 79 public Iterator<Client> getPlayers() 80 { 81 List<Client> player = new ArrayList<Client>(); 82 83 for (Client client : clients.values()) 84 { 85 if (client.getUser().isPlayer()) 86 { 87 player.add(client); 88 } 89 } 90 91 return player.iterator(); 92 } 93 94 /*** 95 * Return the number of players connected to this server. 96 */ 97 public int getPlayerCount() 98 { 99 int count = 0; 100 for (Client client : clients.values()) 101 { 102 if (client.getUser().isPlayer()) 103 { 104 count++; 105 } 106 } 107 108 return count; 109 } 110 111 /*** 112 * Return an iterator of spectators online in alphabetical order. 113 */ 114 public Iterator<Client> getSpectators() 115 { 116 List<Client> spectators = new ArrayList<Client>(); 117 118 for (Client client : clients.values()) 119 { 120 if (client.getUser().isSpectator()) 121 { 122 spectators.add(client); 123 } 124 } 125 126 return spectators.iterator(); 127 } 128 129 /*** 130 * Return the number of spectators connected to this server. 131 */ 132 public int getSpectatorCount() 133 { 134 int count = 0; 135 for (Client client : clients.values()) 136 { 137 if (client.getUser().isSpectator()) 138 { 139 count++; 140 } 141 } 142 143 return count; 144 } 145 146 /*** 147 * Return an iterator of operators online in alphabetical order. 148 */ 149 public Iterator<Client> getOperators() 150 { 151 List<Client> operators = new ArrayList<Client>(); 152 153 for (Client client : clients.values()) 154 { 155 if (client.getUser().getAccessLevel() > 0) 156 { 157 operators.add(client); 158 } 159 } 160 161 return operators.iterator(); 162 } 163 164 /*** 165 * Return the client of the player or spectator with 166 * the specified nickname. 167 * 168 * @param nickname nickname of the client to return 169 */ 170 public Client getClient(String nickname) 171 { 172 return (nickname == null) ? null : clients.get(nickname.toLowerCase()); 173 } 174 175 /*** 176 * Return an iterator of all clients online in alphabetical order. 177 */ 178 public Collection<Client> getClients() 179 { 180 return clients.values(); 181 } 182 183 /*** 184 * Return the number of clients connected to this server. 185 */ 186 public int getClientCount() 187 { 188 return clients.size(); 189 } 190 191 /*** 192 * Return the number of clients connected from the specified 193 * internet address. 194 * 195 * @param address 196 */ 197 public int getHostCount(InetAddress address) 198 { 199 int count = 0; 200 201 for (Client client : clients.values()) 202 { 203 if (address.equals(client.getInetAddress())) 204 { 205 count++; 206 } 207 } 208 209 return count; 210 } 211 212 public void clear() 213 { 214 clients.clear(); 215 } 216 217 }