001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2003  Emmanuel Bourg
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License
007     * as published by the Free Software Foundation; either version 2
008     * of the License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
018     */
019    
020    package net.jetrix;
021    
022    import java.net.InetAddress;
023    import java.util.Collection;
024    import java.util.Iterator;
025    import java.util.Map;
026    import java.util.List;
027    import java.util.ArrayList;
028    import java.util.concurrent.ConcurrentSkipListMap;
029    
030    /**
031     * Repository of clients connected to the server.
032     *
033     * @author Emmanuel Bourg
034     * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
035     */
036    public class ClientRepository
037    {
038        private static ClientRepository instance = new ClientRepository();
039        private Map<String, Client> clients = new ConcurrentSkipListMap<String, Client>();
040    
041        private ClientRepository()
042        {
043        }
044    
045        public static ClientRepository getInstance()
046        {
047            return instance;
048        }
049    
050        /**
051         * Add a client into the repository.
052         *
053         * @param client the client to add
054         */
055        public void addClient(Client client)
056        {
057            if (client != null)
058            {
059                clients.put(client.getUser().getName().toLowerCase(), client);
060            }
061        }
062    
063        /**
064         * Remove a client from the repository.
065         *
066         * @param client the client to remove
067         */
068        public void removeClient(Client client)
069        {
070            if (client != null)
071            {
072                clients.remove(client.getUser().getName().toLowerCase());
073            }
074        }
075    
076        /**
077         * Return an iterator of players online in alphabetical order.
078         */
079        public Iterator<Client> getPlayers()
080        {
081            List<Client> player = new ArrayList<Client>();
082    
083            for (Client client : clients.values())
084            {
085                if (client.getUser().isPlayer())
086                {
087                    player.add(client);
088                }
089            }
090    
091            return player.iterator();
092        }
093    
094        /**
095         * Return the number of players connected to this server.
096         */
097        public int getPlayerCount()
098        {
099            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    }