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.protocols;
021
022 import java.util.*;
023
024 import net.jetrix.*;
025 import net.jetrix.messages.*;
026 import net.jetrix.messages.channel.*;
027
028 /**
029 * Query Protocol. See http://jetrix.sourceforge.net/dev-guide.php#section2-4
030 * for more information.
031 *
032 * @author Emmanuel Bourg
033 * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
034 */
035 public class QueryProtocol extends AbstractProtocol
036 {
037 /** Line terminator */
038 public static final char EOL = 0x0A;
039
040 /** Response terminator */
041 public static final String OK = "+OK";
042
043 public String getName()
044 {
045 return "query";
046 }
047
048 public Message getMessage(String message)
049 {
050 CommandMessage m = null;
051 if (isQueryCommand(message))
052 {
053 m = new CommandMessage();
054 m.setCommand(message);
055 }
056 return m;
057 }
058
059 public String translate(Message m, Locale locale)
060 {
061 if (m instanceof PlineMessage) { return translate((PlineMessage) m, locale); }
062 else if (m instanceof NoConnectingMessage) { return translate((NoConnectingMessage) m, locale); }
063 else
064 {
065 return null;
066 }
067 }
068
069 public String translate(PlineMessage m, Locale locale)
070 {
071 StringBuilder message = new StringBuilder();
072 message.append(m.getText());
073
074 // add the response terminator except for playerquery
075 if (!m.getText().startsWith("Number"))
076 {
077 message.append(OK);
078 }
079
080 return message.toString();
081 }
082
083 public String translate(NoConnectingMessage m, Locale locale)
084 {
085 StringBuilder message = new StringBuilder();
086 message.append("noconnecting ");
087 message.append(m.getText());
088 return message.toString();
089 }
090
091 public String applyStyle(String text)
092 {
093 throw new UnsupportedOperationException();
094 }
095
096 public char getEOL()
097 {
098 return 0xFF;
099 }
100
101 /**
102 * Test if the specified string is a valid query request.
103 */
104 public static boolean isQueryCommand(String command)
105 {
106 return ("listuser".equals(command)
107 || "playerquery".equals(command)
108 || "listchan".equals(command)
109 || "version".equals(command));
110 }
111
112 }