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.commands;
021    
022    import net.jetrix.*;
023    import net.jetrix.messages.channel.PlayerNumMessage;
024    import net.jetrix.messages.channel.CommandMessage;
025    
026    /**
027     * Display the ping to the server. To compute the ping of tetrinet and
028     * tetrifast clients we send the <tt>playernum<tt> message that triggers
029     * a <tt>team</tt> message as response. We assume the ping is half the time
030     * between the request and the response. Since a command cannot wait for
031     * a client response this command must work with the PingFilter that is
032     * processing all <tt>team</tt> messages. This command sets the client
033     * properties <tt>command.ping=true</tt> and <tt>command.ping.time</tt>
034     * and send the <tt>playernum<tt> message. The filter will then listen for
035     * <tt>team</tt> messages and check if the property <tt>command.ping</tt>
036     * is true. If so it will display the ping of the player.
037     *
038     * @author Emmanuel Bourg
039     * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
040     */
041    public class PingCommand extends AbstractCommand
042    {
043        public String getAlias()
044        {
045            return "ping";
046        }
047    
048        public void execute(CommandMessage m)
049        {
050            Client client = (Client) m.getSource();
051            User user = client.getUser();
052    
053            // @todo check if the client use the tetrinet protocol
054    
055            // check if a previous /ping request has still to be completed
056            if ("true".equals(user.getProperty("command.ping"))) return;
057    
058            PlayerNumMessage response = new PlayerNumMessage();
059            response.setSlot(client.getChannel().getClientSlot(client));
060    
061            // set the client properties to be used by the PingFilter
062            user.setProperty("command.ping", "true");
063            user.setProperty("command.ping.time", new Long(System.currentTimeMillis()));
064    
065            client.send(response);
066        }
067    }