001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.qsadmin.gui;
016:
017: import java.net.*;
018: import java.io.*;
019: import java.util.LinkedList;
020:
021: import org.quickserver.util.*;
022: import org.quickserver.swing.JFrameUtilities;
023:
024: import java.util.logging.*;
025:
026: /**
027: * Main Class of QSAdminGUI
028: * QuickServer Admin GUI - QSAdminGUI
029: * @author Akshathkumar Shetty
030: */
031: public class QSAdminMain {
032: private static Logger logger = Logger.getLogger(QSAdminMain.class
033: .getName());
034: private final static String NEW_LINE = "\r\n";
035:
036: private Socket socket;
037: private InputStream in;
038: private OutputStream out;
039: private BufferedReader br;
040: private BufferedWriter bw;
041:
042: private boolean connected = false;
043: //v1.3.2
044: private boolean loggedIn = false;
045: private boolean appendToConsole = true;
046:
047: private QSAdminGUI gui;
048:
049: private LinkedList receivedMsg;
050:
051: public static String VERSION_OF_SERVER = "1.4.6"; //QuickServer.getVersion();
052:
053: public QSAdminMain() {
054:
055: }
056:
057: public boolean doLogin(String ipAddress, int port, String username,
058: String password) throws IOException {
059: connected = false;
060: loggedIn = false;
061: String backupVersionOfServer = VERSION_OF_SERVER;
062: VERSION_OF_SERVER = null;
063: logger.fine("Logging in to " + ipAddress + ":" + port);
064: try {
065: socket = new Socket(ipAddress, port);
066: connected = true;
067: in = socket.getInputStream();
068: out = socket.getOutputStream();
069: br = new BufferedReader(new InputStreamReader(in));
070: bw = new BufferedWriter(new OutputStreamWriter(out));
071: getGUI().setStatus("Connected");
072: startSocketListener();
073:
074: String res = null;
075: res = sendCommunicationSilent(null, false, true);
076: if (res.startsWith("+OK") == false)
077: throw new IOException(res.substring(4));
078: res = sendCommunicationSilent(null, false, true);
079: if (res.startsWith("+OK") == false)
080: throw new IOException(res.substring(4));
081: res = sendCommunicationSilent(null, false, true);
082: if (res.startsWith("+OK") == false)
083: throw new IOException(res.substring(4));
084: //gui.setResponse(res);
085:
086: //try to login
087: res = sendCommunicationSilent(null, false, true);
088: res = sendCommunicationSilent(username, false, true);
089: if (res.startsWith("+OK")) { //+OK Password required
090: getGUI().setStatus("Authorising..");
091: } else if (res.startsWith("-ERR")) {
092: getGUI().setStatus("Error: " + res.substring(4));
093: throw new IOException(
094: "Bad QSAdmin Username! Server reply: " + res);
095: } else {
096: getGUI().setStatus("Protocol Error: " + res);
097: throw new IOException(
098: "Bad QSAdmin Username! Server reply: " + res);
099: }
100:
101: //password
102: StringBuffer buffer = new StringBuffer();
103: for (int i = 0; i < password.length(); i++)
104: buffer.append('*');
105: getGUI().appendToConsole(buffer.toString());
106: res = sendCommunicationSilent(password, false, false);
107:
108: if (res.startsWith("+OK")) {
109: getGUI().setStatus("Authorised");
110: loggedIn = true;
111: } else {
112: getGUI().setStatus("Error : " + res.substring(4));
113: throw new IOException(res.substring(4));
114: }
115: getGUI().setConsoleSend(true);
116: getGUI().updateConnectionStatus(true);
117:
118: getGUI().appendToConsole("Checking version at host..");
119: VERSION_OF_SERVER = sendCommunicationSilent("version",
120: false, true);
121: if (VERSION_OF_SERVER != null
122: && VERSION_OF_SERVER.startsWith("+OK "))
123: VERSION_OF_SERVER = VERSION_OF_SERVER.substring(4);
124: return true;
125: } catch (UnknownHostException e) {
126: if (socket != null)
127: socket.close();
128: logger.warning("Error " + e);
129: connected = false;
130: loggedIn = false;
131: socket = null;
132: in = null;
133: out = null;
134: br = null;
135: bw = null;
136: gui.setResponse("-ERR Unknown Host : " + e.getMessage());
137: gui.setConsoleSend(false);
138: VERSION_OF_SERVER = backupVersionOfServer;
139: return false;
140: } catch (IOException e) {
141: if (socket != null)
142: socket.close();
143: logger.warning("Error " + e);
144: connected = false;
145: socket = null;
146: in = null;
147: out = null;
148: br = null;
149: bw = null;
150: gui.setResponse("-ERR " + e.getMessage());
151: gui.setConsoleSend(false);
152: VERSION_OF_SERVER = backupVersionOfServer;
153: return false;
154: }
155: }
156:
157: public void doLogout() throws IOException {
158: if (socket == null)
159: throw new IllegalStateException("Not connected");
160: String res = sendCommunicationSilent("quit", false, true);
161: if (res.startsWith("+OK"))
162: gui.setStatus("Disconnecting");
163: else {
164: gui.setStatus("Error : " + res.substring(4));
165: }
166: clean();
167: }
168:
169: private void clean() {
170: if (socket != null) {
171: try {
172: socket.close();
173: } catch (Exception e) {
174: logger.warning("Error : " + e);
175: }
176: socket = null;
177: }
178: in = null;
179: out = null;
180: br = null;
181: bw = null;
182: connected = false;
183: loggedIn = false;
184: gui.setConsoleSend(false);
185: gui.setStatus("Disconnected");
186: gui.updateConnectionStatus(false);
187: setAppendToConsole(true);
188: }
189:
190: public void sendCommand(String command, boolean echo) {
191: logger.fine("Got command : " + command);
192: if (isConnected() == false) {
193: gui.setResponse("-ERR Not connected yet.");
194: return;
195: }
196: if (command != null && command.equals("") == false) {
197: if (socket == null)
198: throw new IllegalStateException("Not connected");
199: if (echo == true)
200: gui.appendToConsole(command);
201: command += NEW_LINE;
202: try {
203: bw.write(command, 0, command.length());
204: bw.flush();
205: } catch (Exception e) {
206: gui.setResponse("-ERR " + e.getMessage());
207: }
208: }
209: }
210:
211: public String readResponse(boolean multiLineResponse) {
212: StringBuffer command = new StringBuffer();
213: try {
214: if (multiLineResponse == true) {
215: String res = getReceivedMsg();
216: //check if is single line
217: if (res != null
218: && res.equals("+OK info follows") == false)
219: return res;
220:
221: if (res != null
222: && res.equals("+OK info follows") == true) {
223: command.append("+OK ");
224: res = getReceivedMsg();
225: }
226: while (res != null && res.equals(".") == false) {
227: logger.fine(res);
228: command.append(res + NEW_LINE);
229: res = getReceivedMsg();
230: }
231: } else {
232: command.append(getReceivedMsg());
233: }
234: } catch (Exception e) {
235: command.append("-ERR " + e.getMessage());
236: }
237: return command.toString();
238: }
239:
240: public synchronized String sendCommunication(String command,
241: boolean multiLineResponse, boolean echo) {
242: logger.fine("Got command : " + command);
243: if (isConnected() == false) {
244: gui.setResponse("-ERR Not connected yet.");
245: return "-ERR Not connected yet";
246: }
247: if (command != null && command.equals("") == false) {
248: if (socket == null)
249: throw new IllegalStateException("Not connected");
250: if (echo == true)
251: gui.appendToConsole(command);
252: command += NEW_LINE;
253: emptyReceivedMsg();
254: try {
255: bw.write(command, 0, command.length());
256: bw.flush();
257: } catch (Exception e) {
258: gui.setResponse("-ERR " + e.getMessage());
259: return null;
260: }
261: }
262: command = readResponse(multiLineResponse);
263: gui.setResponse(command);
264: return command;
265: }
266:
267: public synchronized String sendCommunicationSilent(String command,
268: boolean multiLineResponse, boolean echo) throws IOException {
269: logger.fine("Got command : " + command);
270: if (isConnected() == false)
271: return "-ERR Not connected yet";
272: if (socket == null)
273: throw new IllegalStateException("Not connected");
274: if (command != null && command.equals("") == false) {
275: if (echo == true)
276: gui.appendToConsole(command);
277: command += NEW_LINE;
278: emptyReceivedMsg();
279: bw.write(command, 0, command.length());
280: bw.flush();
281: }
282: return readResponse(multiLineResponse);
283: }
284:
285: public synchronized String sendCommunicationNoEcho(String command,
286: boolean multiLineResponse) throws IOException {
287: try {
288: setAppendToConsole(false);
289: logger.fine("Got command : " + command);
290: if (isConnected() == false)
291: return "-ERR Not connected yet";
292: if (socket == null)
293: throw new IllegalStateException("Not connected");
294: if (command != null && command.equals("") == false) {
295: command += NEW_LINE;
296: emptyReceivedMsg();
297: bw.write(command, 0, command.length());
298: bw.flush();
299: }
300: command = readResponse(multiLineResponse);
301: } catch (IllegalStateException e) {
302: throw e;
303: } catch (Exception e) {
304: throw new IOException("Exception Got : " + e);
305: } finally {
306: setAppendToConsole(true);
307: }
308: return command;
309: }
310:
311: public String toString() {
312: if (socket == null) {
313: return "Not connected";
314: }
315: StringBuffer info = new StringBuffer("Connected to ");
316: info.append(socket.getInetAddress().getHostName());
317: return info.toString();
318: }
319:
320: public boolean isConnected() {
321: return connected;
322: }
323:
324: public boolean isLoggedIn() {
325: return loggedIn;
326: }
327:
328: public void setGUI(QSAdminGUI gui) {
329: this .gui = gui;
330: }
331:
332: public QSAdminGUI getGUI() {
333: return gui;
334: }
335:
336: public void startSocketListener() {
337: receivedMsg = new LinkedList();
338: Thread t = new Thread() {
339: public void run() {
340: String rec = null;
341: logger.info("Started");
342: while (true) {
343: try {
344: rec = br.readLine();
345: } catch (IOException e) {
346: logger.warning("Error : " + e);
347: if (isConnected() == true)
348: clean();
349: break;
350: }
351: if (rec == null) {
352: if (isConnected() == true)
353: clean();
354: break;
355: }
356: receivedMsg.add(rec);
357: if (getAppendToConsole() == true)
358: gui.appendToConsole(rec);
359: }
360: logger.info("Finished");
361: }
362: };
363: t.setPriority(Thread.NORM_PRIORITY);
364: t.start();
365: }
366:
367: public String getReceivedMsg() {
368: while (receivedMsg.size() == 0 && isConnected() == true) {
369: try {
370: Thread.currentThread().sleep(50);
371: } catch (InterruptedException e) {
372: logger.warning("Error : " + e);
373: }
374: }
375: if (receivedMsg.size() != 0)
376: return (String) receivedMsg.removeFirst();
377: else
378: return null;
379: }
380:
381: public void emptyReceivedMsg() {
382: receivedMsg.clear();
383: }
384:
385: /**
386: * Returns the numerical version of the server connected to.
387: */
388: public float getServerVersionNo() {
389: String ver = VERSION_OF_SERVER;
390: if (ver == null) {
391: gui.setResponse("-ERR Not connected yet");
392: return 0;
393: }
394:
395: float version = 0;
396: int i = ver.indexOf(" "); //check if beta
397: if (i == -1)
398: i = ver.length();
399: ver = ver.substring(0, i);
400:
401: i = ver.indexOf("."); //check for sub version
402: if (i != -1) {
403: int j = ver.indexOf(".", i);
404: if (j != -1) {
405: ver = ver.substring(0, i)
406: + "."
407: + MyString.replaceAll(ver.substring(i + 1),
408: ".", "");
409: }
410: }
411:
412: try {
413: version = Float.parseFloat(ver);
414: } catch (NumberFormatException e) {
415: logger.warning("Error : " + e);
416: gui.setResponse("-ERR Corrupt QuickServer running @ host :"
417: + e.getMessage());
418: }
419: return version;
420: }
421:
422: public String getIpAddress() {
423: if (socket == null)
424: return null;
425: return socket.getInetAddress().getHostName();
426: }
427:
428: public boolean getAppendToConsole() {
429: return appendToConsole;
430: }
431:
432: public void setAppendToConsole(boolean appendToConsole) {
433: this.appendToConsole = appendToConsole;
434: }
435: }
|