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.net.*;
24 import java.nio.charset.Charset;
25 import java.util.*;
26 import java.util.logging.Logger;
27
28 import net.jetrix.*;
29 import net.jetrix.protocols.*;
30 import net.jetrix.config.*;
31
32 /***
33 * Command line console.
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 ConsoleClient implements Client
39 {
40 private Console console = System.console();
41 private ServerConfig conf;
42 private Protocol protocol;
43 private User user;
44 private Channel channel;
45 private Logger log = Logger.getLogger("net.jetrix");
46 private boolean closed = false;
47
48 public ConsoleClient()
49 {
50 conf = Server.getInstance().getConfig();
51 protocol = ProtocolManager.getInstance().getProtocol(ConsoleProtocol.class);
52 user = new User();
53 user.setName("Admin");
54 user.setAccessLevel(100);
55 user.setLocale(conf.getLocale());
56 user.setSpectator();
57 }
58
59 public Protocol getProtocol()
60 {
61 return this.protocol;
62 }
63
64 public void run()
65 {
66 if (console == null)
67 {
68 log.info("Console interface unavailable");
69 return;
70 }
71
72 while (conf.isRunning() && !closed)
73 {
74 try
75 {
76 Message message = receive();
77
78 if (message != null)
79 {
80 Server.getInstance().send(message);
81 }
82 }
83 catch (Exception e)
84 {
85 e.printStackTrace();
86 }
87 }
88
89 if (closed)
90 {
91 log.info("Standard input closed, shutting down the console...");
92 }
93 }
94
95 public void send(Message message)
96 {
97 String msg = protocol.translate(message, user.getLocale());
98 if (msg != null)
99 {
100 console.writer().println(msg);
101 }
102 }
103
104 public Message receive() throws IOException
105 {
106 String line = console.readLine();
107 if (line == null)
108 {
109 closed = true;
110 }
111
112 Message message = protocol.getMessage(line);
113 if (message != null)
114 {
115 message.setSource(this);
116 }
117
118 return message;
119 }
120
121 public InetAddress getInetAddress()
122 {
123 return conf.getHost();
124 }
125
126 public void setChannel(Channel channel)
127 {
128 this.channel = channel;
129 }
130
131 public Channel getChannel()
132 {
133 return channel;
134 }
135
136 public boolean supportsMultipleChannels()
137 {
138 return false;
139 }
140
141 public boolean supportsAutoJoin()
142 {
143 return true;
144 }
145
146 public User getUser()
147 {
148 return user;
149 }
150
151 public String getAgent()
152 {
153 return "Console";
154 }
155
156 public String getVersion()
157 {
158 return "1.0";
159 }
160
161 public Date getConnectionTime()
162 {
163 return null;
164 }
165
166 public long getIdleTime()
167 {
168 return 0;
169 }
170
171 public String getEncoding()
172 {
173 return Charset.defaultCharset().name();
174 }
175
176 public void disconnect()
177 {
178 try
179 {
180 System.in.close();
181 }
182 catch (IOException e)
183 {
184 log.warning("Unable to close the standard input : " + e.getMessage());
185 }
186 }
187
188 }