001    /**
002     * Jetrix TetriNET Server
003     * Copyright (C) 2001-2004  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.services;
021    
022    import net.jetrix.Client;
023    import net.jetrix.ClientRepository;
024    import net.jetrix.Message;
025    import net.jetrix.messages.NoopMessage;
026    
027    import java.util.logging.*;
028    
029    /**
030     * A service to remove players not properly disconnected from the server due to
031     * a network issue.
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 GhostbusterService extends ScheduledService
037    {
038        private Logger log = Logger.getLogger("net.jetrix");
039    
040        public String getName()
041        {
042            return "Ghostbuster - ghost clients killer";
043        }
044    
045        protected void init()
046        {
047            setDelay(10000);
048            setPeriod(10000);
049        }
050    
051        public void run()
052        {
053            // get the list of clients connected
054            for (Client client : ClientRepository.getInstance().getClients())
055            {
056                if (client.getIdleTime() > 5000)
057                {
058                    log.finest("checking connection for " + client.getUser().getName());
059    
060                    try
061                    {
062                        Message noop = new NoopMessage();
063                        noop.setDestination(client);
064                        client.send(noop);
065                    }
066                    catch (Exception e)
067                    {
068                        log.info(e.getMessage());
069                    }
070                }
071            }
072        }
073    }