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.clients;
21
22 import java.io.*;
23 import java.util.*;
24 import java.util.logging.*;
25
26 import net.jetrix.*;
27 import net.jetrix.config.*;
28 import net.jetrix.messages.*;
29 import net.jetrix.messages.channel.*;
30 import net.jetrix.protocols.*;
31
32 /***
33 * Client for the query protocol on port 31457.
34 *
35 * @author Emmanuel Bourg
36 * @version $Revision: 857 $, $Date: 2010-05-04 19:55:19 +0200 (mar., 04 mai 2010) $
37 */
38 public class QueryClient extends TetrinetClient
39 {
40 private Message firstMessage;
41
42 public void run()
43 {
44 if (log.isLoggable(Level.FINE))
45 {
46 log.fine("Client started " + this);
47 }
48
49 connectionTime = new Date();
50
51 Server server = Server.getInstance();
52 if (server != null)
53 {
54 serverConfig = server.getConfig();
55 }
56
57 try
58 {
59 process(firstMessage);
60
61 while (!disconnected && serverConfig.isRunning())
62 {
63 process(receive());
64 }
65 }
66 catch (Exception e)
67 {
68
69 }
70 finally
71 {
72 try { in.close(); } catch (IOException e) { e.printStackTrace(); }
73 try { out.close(); } catch (IOException e) { e.printStackTrace(); }
74 try { socket.close(); } catch (IOException e) { e.printStackTrace(); }
75 }
76
77 if (log.isLoggable(Level.FINE))
78 {
79 log.fine("Client disconnected (" + getInetAddress().getHostAddress() + ")");
80 }
81 }
82
83 private void process(Message m)
84 {
85 if (m != null && m instanceof CommandMessage)
86 {
87 CommandMessage command = (CommandMessage) m;
88 PlineMessage response = new PlineMessage();
89
90 if ("listuser".equals(command.getCommand()))
91 {
92
93 StringBuilder message = new StringBuilder();
94 for (Client client : ClientRepository.getInstance().getClients())
95 {
96 User user = client.getUser();
97 message.append("\"");
98 message.append(user.getName());
99 message.append("\" \"");
100 message.append(user.getTeam() == null ? "" : user.getTeam());
101 message.append("\" \"");
102 message.append(client.getAgent() + " " + client.getVersion());
103 message.append("\" ");
104 message.append(client.getChannel().getClientSlot(client));
105 message.append(" ");
106 message.append(user.isPlaying() ? "1" : "0");
107 message.append(" ");
108 message.append(user.getAccessLevel());
109 message.append(" \"");
110 message.append(client.getChannel().getConfig().getName());
111 message.append("\"");
112 message.append(QueryProtocol.EOL);
113 }
114
115 response.setText(message.toString());
116 }
117 else if ("listchan".equals(command.getCommand()))
118 {
119
120 StringBuilder message = new StringBuilder();
121 for (Channel channel : ChannelManager.getInstance().channels())
122 {
123 ChannelConfig config = channel.getConfig();
124
125 if (config.isVisible())
126 {
127 message.append("\"");
128 message.append(config.getName());
129 message.append("\" \"");
130 message.append(config.getDescription());
131 message.append("\" ");
132 message.append(channel.getPlayerCount());
133 message.append(" ");
134 message.append(config.getMaxPlayers());
135 message.append(" 0 ");
136 message.append(channel.getGameState().getValue());
137 message.append(QueryProtocol.EOL);
138 }
139 }
140
141 response.setText(message.toString());
142 }
143 else if ("playerquery".equals(command.getCommand()))
144 {
145 response.setText("Number of players logged in: " + ClientRepository.getInstance().getClientCount());
146 }
147 else if ("version".equals(command.getCommand()))
148 {
149 response.setText("Jetrix/" + ServerConfig.VERSION + QueryProtocol.EOL);
150 }
151
152 send(response);
153 }
154 else
155 {
156 NoConnectingMessage noconnecting = new NoConnectingMessage();
157 noconnecting.setText("Wrong command");
158 send(noconnecting);
159 disconnected = true;
160 }
161 }
162
163 /***
164 * Set the first command issued by this client.
165 */
166 public void setFirstMessage(Message firstMessage)
167 {
168 this.firstMessage = firstMessage;
169 }
170
171 public void send(Message m)
172 {
173 String rawMessage = m.getRawMessage(getProtocol(), null);
174
175 try
176 {
177 out.write(rawMessage.getBytes(getEncoding()));
178 out.write(QueryProtocol.EOL);
179 out.flush();
180
181 if (log.isLoggable(Level.FINEST))
182 {
183 log.finest("> " + rawMessage);
184 }
185 }
186 catch (Exception e)
187 {
188 if (log.isLoggable(Level.FINE))
189 {
190 log.fine(e.getMessage());
191 }
192 }
193 }
194
195 public boolean supportsAutoJoin()
196 {
197 return false;
198 }
199
200 protected boolean isAsynchronous()
201 {
202 return false;
203 }
204
205 }