KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > qsadmin > gui > QSAdminMain


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.qsadmin.gui;
16
17 import java.net.*;
18 import java.io.*;
19 import java.util.LinkedList JavaDoc;
20
21 import org.quickserver.util.*;
22 import org.quickserver.swing.JFrameUtilities;
23
24 import java.util.logging.*;
25
26 /**
27  * Main Class of QSAdminGUI
28  * QuickServer Admin GUI - QSAdminGUI
29  * @author Akshathkumar Shetty
30  */

31 public class QSAdminMain {
32     private static Logger logger = Logger.getLogger(QSAdminMain.class.getName());
33     private final static String JavaDoc NEW_LINE = "\r\n";
34
35     private Socket socket;
36     private InputStream in;
37     private OutputStream out;
38     private BufferedReader br;
39     private BufferedWriter bw;
40
41     private boolean connected = false;
42     //v1.3.2
43
private boolean loggedIn = false;
44     private boolean appendToConsole = true;
45
46     private QSAdminGUI gui;
47
48     private LinkedList JavaDoc receivedMsg;
49
50     public static String JavaDoc VERSION_OF_SERVER = "1.4.6"; //QuickServer.getVersion();
51

52     public QSAdminMain() {
53         
54     }
55
56     public boolean doLogin(String JavaDoc ipAddress, int port,
57             String JavaDoc username, String JavaDoc password) throws IOException {
58         connected = false;
59         loggedIn = false;
60         String JavaDoc backupVersionOfServer = VERSION_OF_SERVER;
61         VERSION_OF_SERVER = null;
62         logger.fine("Logging in to " + ipAddress + ":"+port);
63         try {
64             socket = new Socket(ipAddress, port);
65             connected = true;
66             in = socket.getInputStream();
67             out = socket.getOutputStream();
68             br = new BufferedReader(new InputStreamReader(in));
69             bw = new BufferedWriter(new OutputStreamWriter(out));
70             getGUI().setStatus("Connected");
71             startSocketListener();
72
73             String JavaDoc res = null;
74             res = sendCommunicationSilent(null, false, true);
75             if(res.startsWith("+OK")==false)
76                 throw new IOException(res.substring(4));
77             res = sendCommunicationSilent(null, false, true);
78             if(res.startsWith("+OK")==false)
79                 throw new IOException(res.substring(4));
80             res = sendCommunicationSilent(null, false, true);
81             if(res.startsWith("+OK")==false)
82                 throw new IOException(res.substring(4));
83             //gui.setResponse(res);
84

85             //try to login
86
res = sendCommunicationSilent(null, false, true);
87             res = sendCommunicationSilent(username, false, true);
88             if(res.startsWith("+OK")) { //+OK Password required
89
getGUI().setStatus("Authorising..");
90             } else if(res.startsWith("-ERR")) {
91                 getGUI().setStatus("Error: "+res.substring(4));
92                 throw new IOException("Bad QSAdmin Username! Server reply: "+res);
93             } else {
94                 getGUI().setStatus("Protocol Error: "+res);
95                 throw new IOException("Bad QSAdmin Username! Server reply: "+res);
96             }
97
98             //password
99
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
100             for(int i=0;i<password.length();i++)
101                 buffer.append('*');
102             getGUI().appendToConsole(buffer.toString());
103             res = sendCommunicationSilent(password, false, false);
104             
105             if(res.startsWith("+OK")) {
106                 getGUI().setStatus("Authorised");
107                 loggedIn = true;
108             } else {
109                 getGUI().setStatus("Error : "+res.substring(4));
110                 throw new IOException(res.substring(4));
111             }
112             getGUI().setConsoleSend(true);
113             getGUI().updateConnectionStatus(true);
114
115             getGUI().appendToConsole("Checking version at host..");
116             VERSION_OF_SERVER = sendCommunicationSilent("version", false, true);
117             if(VERSION_OF_SERVER!=null && VERSION_OF_SERVER.startsWith("+OK "))
118                 VERSION_OF_SERVER = VERSION_OF_SERVER.substring(4);
119             return true;
120         } catch(UnknownHostException e) {
121             if(socket!=null) socket.close();
122             logger.warning("Error "+e);
123             connected = false;
124             loggedIn = false;
125             socket = null;
126             in = null;
127             out = null;
128             br = null;
129             bw = null;
130             gui.setResponse("-ERR Unknown Host : "+e.getMessage());
131             gui.setConsoleSend(false);
132             VERSION_OF_SERVER = backupVersionOfServer;
133             return false;
134         } catch(IOException e) {
135             if(socket!=null) socket.close();
136             logger.warning("Error "+e);
137             connected = false;
138             socket = null;
139             in = null;
140             out = null;
141             br = null;
142             bw = null;
143             gui.setResponse("-ERR "+e.getMessage());
144             gui.setConsoleSend(false);
145             VERSION_OF_SERVER = backupVersionOfServer;
146             return false;
147         }
148     }
149
150     public void doLogout() throws IOException {
151         if(socket==null)
152             throw new IllegalStateException JavaDoc("Not connected");
153         String JavaDoc res = sendCommunicationSilent("quit", false, true);
154         if(res.startsWith("+OK"))
155             gui.setStatus("Disconnecting");
156         else {
157             gui.setStatus("Error : "+res.substring(4));
158         }
159         clean();
160     }
161
162     private void clean() {
163         if(socket!=null) {
164             try {
165                 socket.close();
166             } catch(Exception JavaDoc e) {
167                 logger.warning("Error : "+e);
168             }
169             socket = null;
170         }
171         in = null;
172         out = null;
173         br = null;
174         bw = null;
175         connected = false;
176         loggedIn = false;
177         gui.setConsoleSend(false);
178         gui.setStatus("Disconnected");
179         gui.updateConnectionStatus(false);
180         setAppendToConsole(true);
181     }
182
183     public void sendCommand(String JavaDoc command, boolean echo) {
184         logger.fine("Got command : "+command);
185         if(isConnected()==false) {
186             gui.setResponse("-ERR Not connected yet.");
187             return;
188         }
189         if(command!=null && command.equals("")==false) {
190             if(socket==null)
191                 throw new IllegalStateException JavaDoc("Not connected");
192             if(echo==true)
193                 gui.appendToConsole(command);
194             command += NEW_LINE;
195             try {
196                 bw.write(command, 0, command.length());
197                 bw.flush();
198             } catch(Exception JavaDoc e) {
199                 gui.setResponse("-ERR "+e.getMessage());
200             }
201         }
202     }
203
204     public String JavaDoc readResponse(boolean multiLineResponse) {
205         StringBuffer JavaDoc command = new StringBuffer JavaDoc();
206         try {
207             if(multiLineResponse==true) {
208                 String JavaDoc res = getReceivedMsg();
209                 //check if is single line
210
if(res!=null && res.equals("+OK info follows")==false)
211                     return res;
212
213                 if(res!=null && res.equals("+OK info follows")==true) {
214                     command.append("+OK ");
215                     res = getReceivedMsg();
216                 }
217                 while(res!=null && res.equals(".")==false) {
218                     logger.fine(res);
219                     command.append(res + NEW_LINE);
220                     res = getReceivedMsg();
221                 }
222             } else {
223                 command.append(getReceivedMsg());
224             }
225         } catch(Exception JavaDoc e) {
226             command.append("-ERR "+e.getMessage());
227         }
228         return command.toString();
229     }
230
231     public synchronized String JavaDoc sendCommunication(String JavaDoc command,
232             boolean multiLineResponse, boolean echo) {
233         logger.fine("Got command : "+command);
234         if(isConnected()==false) {
235             gui.setResponse("-ERR Not connected yet.");
236             return "-ERR Not connected yet";
237         }
238         if(command!=null && command.equals("")==false) {
239             if(socket==null)
240                 throw new IllegalStateException JavaDoc("Not connected");
241             if(echo==true)
242                 gui.appendToConsole(command);
243             command += NEW_LINE;
244             emptyReceivedMsg();
245             try {
246                 bw.write(command, 0, command.length());
247                 bw.flush();
248             } catch(Exception JavaDoc e) {
249                 gui.setResponse("-ERR "+e.getMessage());
250                 return null;
251             }
252         }
253         command = readResponse(multiLineResponse);
254         gui.setResponse(command);
255         return command;
256     }
257
258
259     public synchronized String JavaDoc sendCommunicationSilent(String JavaDoc command,
260             boolean multiLineResponse, boolean echo) throws IOException {
261         logger.fine("Got command : "+command);
262         if(isConnected()==false)
263             return "-ERR Not connected yet";
264         if(socket==null)
265             throw new IllegalStateException JavaDoc("Not connected");
266         if(command!=null && command.equals("")==false) {
267             if(echo==true)
268                 gui.appendToConsole(command);
269             command += NEW_LINE;
270             emptyReceivedMsg();
271             bw.write(command, 0, command.length());
272             bw.flush();
273         }
274         return readResponse(multiLineResponse);
275     }
276
277     public synchronized String JavaDoc sendCommunicationNoEcho(String JavaDoc command,
278             boolean multiLineResponse) throws IOException {
279         try {
280             setAppendToConsole(false);
281             logger.fine("Got command : "+command);
282             if(isConnected()==false)
283                 return "-ERR Not connected yet";
284             if(socket==null)
285                 throw new IllegalStateException JavaDoc("Not connected");
286             if(command!=null && command.equals("")==false) {
287                 command += NEW_LINE;
288                 emptyReceivedMsg();
289                 bw.write(command, 0, command.length());
290                 bw.flush();
291             }
292             command = readResponse(multiLineResponse);
293         } catch(IllegalStateException JavaDoc e) {
294             throw e;
295         } catch(Exception JavaDoc e) {
296             throw new IOException("Exception Got : "+ e);
297         } finally {
298             setAppendToConsole(true);
299         }
300         return command;
301     }
302
303     public String JavaDoc toString() {
304         if(socket==null) {
305             return "Not connected";
306         }
307         StringBuffer JavaDoc info = new StringBuffer JavaDoc("Connected to ");
308         info.append(socket.getInetAddress().getHostName());
309         return info.toString();
310     }
311
312     public boolean isConnected(){
313         return connected;
314     }
315
316     public boolean isLoggedIn(){
317         return loggedIn;
318     }
319
320     public void setGUI(QSAdminGUI gui) {
321         this.gui = gui;
322     }
323     public QSAdminGUI getGUI(){
324         return gui;
325     }
326
327     public void startSocketListener() {
328         receivedMsg = new LinkedList JavaDoc();
329         Thread JavaDoc t = new Thread JavaDoc() {
330             public void run() {
331                 String JavaDoc rec = null;
332                 logger.info("Started");
333                 while(true) {
334                     try {
335                         rec = br.readLine();
336                     } catch(IOException e) {
337                         logger.warning("Error : "+e);
338                         if(isConnected()==true) clean();
339                         break;
340                     }
341                     if(rec==null) {
342                         if(isConnected()==true) clean();
343                         break;
344                     }
345                     receivedMsg.add(rec);
346                     if(getAppendToConsole()==true)
347                         gui.appendToConsole(rec);
348                 }
349                 logger.info("Finished");
350             }
351         };
352         t.setPriority(Thread.NORM_PRIORITY);
353         t.start();
354     }
355
356     public String JavaDoc getReceivedMsg() {
357         while(receivedMsg.size()==0 && isConnected()==true) {
358             try {
359                 Thread.currentThread().sleep(50);
360             } catch(InterruptedException JavaDoc e) {
361                 logger.warning("Error : "+e);
362             }
363         }
364         if(receivedMsg.size()!=0)
365             return (String JavaDoc)receivedMsg.removeFirst();
366         else
367             return null;
368     }
369
370     public void emptyReceivedMsg() {
371         receivedMsg.clear();
372     }
373
374     /**
375      * Returns the numerical version of the server connected to.
376      */

377     public float getServerVersionNo() {
378         String JavaDoc ver = VERSION_OF_SERVER;
379         if(ver==null) {
380             gui.setResponse("-ERR Not connected yet");
381             return 0;
382         }
383
384         float version = 0;
385         int i = ver.indexOf(" "); //check if beta
386
if(i == -1)
387             i = ver.length();
388         ver = ver.substring(0, i);
389
390         i = ver.indexOf("."); //check for sub version
391
if(i!=-1) {
392             int j = ver.indexOf(".", i);
393             if(j!=-1) {
394                 ver = ver.substring(0, i)+"."+
395                     MyString.replaceAll(ver.substring(i+1), ".", "");
396             }
397         }
398
399         try {
400             version = Float.parseFloat(ver);
401         } catch(NumberFormatException JavaDoc e) {
402             logger.warning("Error : "+e);
403             gui.setResponse("-ERR Corrupt QuickServer running @ host :"+e.getMessage());
404         }
405         return version;
406     }
407
408     public String JavaDoc getIpAddress() {
409         if(socket==null) return null;
410         return socket.getInetAddress().getHostName();
411     }
412
413
414     public boolean getAppendToConsole() {
415         return appendToConsole;
416     }
417     public void setAppendToConsole(boolean appendToConsole) {
418         this.appendToConsole = appendToConsole;
419     }
420 }
421
Popular Tags