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.clients;
021
022 import java.io.*;
023 import java.net.*;
024 import java.nio.charset.Charset;
025 import java.util.*;
026 import java.util.logging.Logger;
027
028 import net.jetrix.*;
029 import net.jetrix.protocols.*;
030 import net.jetrix.config.*;
031
032 /**
033 * Command line console.
034 *
035 * @author Emmanuel Bourg
036 * @version $Revision: 857 $, $Date: 2010-05-04 19:55:19 +0200 (mar., 04 mai 2010) $
037 */
038 public class ConsoleClient implements Client
039 {
040 private Console console = System.console();
041 private ServerConfig conf;
042 private Protocol protocol;
043 private User user;
044 private Channel channel;
045 private Logger log = Logger.getLogger("net.jetrix");
046 private boolean closed = false;
047
048 public ConsoleClient()
049 {
050 conf = Server.getInstance().getConfig();
051 protocol = ProtocolManager.getInstance().getProtocol(ConsoleProtocol.class);
052 user = new User();
053 user.setName("Admin");
054 user.setAccessLevel(100);
055 user.setLocale(conf.getLocale());
056 user.setSpectator();
057 }
058
059 public Protocol getProtocol()
060 {
061 return this.protocol;
062 }
063
064 public void run()
065 {
066 if (console == null)
067 {
068 log.info("Console interface unavailable");
069 return;
070 }
071
072 while (conf.isRunning() && !closed)
073 {
074 try
075 {
076 Message message = receive();
077
078 if (message != null)
079 {
080 Server.getInstance().send(message);
081 }
082 }
083 catch (Exception e)
084 {
085 e.printStackTrace();
086 }
087 }
088
089 if (closed)
090 {
091 log.info("Standard input closed, shutting down the console...");
092 }
093 }
094
095 public void send(Message message)
096 {
097 String msg = protocol.translate(message, user.getLocale());
098 if (msg != null)
099 {
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 }