KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > client > ServerConnection


1 /*- ServerConneciton.java -----------------------------------------+
2  | |
3  | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
4  | countjoe@users.sourceforge.net |
5  | http://www.42llamas.com/LlamaChat/ |
6  | |
7  | This program is free software; you can redistribute it and/or |
8  | modify it under the terms of the GNU General Public License |
9  | as published by the Free Software Foundation; either version 2 |
10  | of the License, or (at your option) any later version |
11  | |
12  | This program is distributed in the hope that it will be useful, |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15  | GNU General Public License for more details. |
16  | |
17  | A copy of the GNU General Public License may be found in the |
18  | installation directory named "GNUGPL.txt" |
19  | |
20  +-----------------------------------------------------------------+
21  */

22
23 package client;
24
25 import javax.net.ssl.*;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.BufferedOutputStream JavaDoc;
28 import java.io.ObjectOutputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.lang.ClassNotFoundException JavaDoc;
32 import javax.swing.JOptionPane JavaDoc;
33
34 import common.*;
35 import common.sd.*;
36
37 /* -------------------- JavaDoc Information ----------------------*/
38 /**
39  * A threaded connection class to maintain the conneciton to the server
40  * @author Joseph Monti <a HREF="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
41  * @version 0.8
42  */

43 public class ServerConnection implements Runnable JavaDoc, SocketConnection {
44
45     private LlamaChat client;
46     private SSLSocket socket;
47     private ObjectOutputStream JavaDoc out;
48     private ObjectInputStream JavaDoc in;
49     public boolean connected;
50     public boolean finalized;
51     public String JavaDoc channel;
52     private MessageQueue queue;
53     
54     ServerConnection(LlamaChat c) {
55         connected = false;
56         finalized = false;
57         client = c;
58         channel = "Lobby";
59         queue = new MessageQueue(this);
60
61         try {
62             // initialize socket
63
SSLSocketFactory sslFact =
64                     (SSLSocketFactory)SSLSocketFactory.getDefault();
65             socket = (SSLSocket)sslFact.createSocket(c.site, c.PORT);
66             // Checking the supported Cipher suites.
67
String JavaDoc [] enabledCipher = socket.getSupportedCipherSuites ();
68             // Enabled the Cipher suites.
69
socket.setEnabledCipherSuites (enabledCipher);
70
71             // make in/out stream connection
72
out = new ObjectOutputStream JavaDoc(socket.getOutputStream());
73             in = new ObjectInputStream JavaDoc(socket.getInputStream());
74             connected = true;
75             c.setConnected(true);
76             new Thread JavaDoc(this).start();
77         } catch (IOException JavaDoc e) {
78             c.error("Server not available");
79             c.setConnected(false);
80             connected = false;
81         }
82     }
83     
84     /**
85      * Method to read an object from the server
86      * @return returns the SocketData object recieved
87      */

88     private SocketData readObject() {
89         try {
90             return (SocketData) in.readObject();
91         } catch (Throwable JavaDoc t) {
92             return null;
93         }
94     }
95
96     /**
97      * Method to write an object to the server
98      * @param sd A SocketData object to be sent
99      */

100     public void writeObject(Object JavaDoc obj) {
101         queue.enqueue(obj);
102     }
103
104     /**
105      * Method to write an object to the server
106      * @param sd A SocketData object to be sent
107      */

108     public void _writeObject(Object JavaDoc obj) {
109         if (connected) {
110             try {
111                 out.writeObject(obj);
112                 out.flush();
113             } catch (IOException JavaDoc e) {
114                 close();
115             }
116         }
117     }
118
119     /**
120      * closes the connection to the server
121      */

122     public void close() {
123         try {
124             connected = false;
125             client.setConnected(false);
126             if (connected) socket.close();
127         } catch (IOException JavaDoc e) { }
128         client.close();
129     }
130     
131 // -- SocketConnection Interface methods -- \\
132

133     /**
134      * receives server configurations
135      * @param type the type of data being sent
136      * @param obj the object being sent
137      */

138     public void serverCap(char type, Object JavaDoc obj) {
139         try {
140             switch (type) {
141             case SD_ServerCap.T_CREATE:
142                 char tmp = ((Character JavaDoc) obj).charValue();
143                 switch (tmp) {
144                 case 'y':
145                     client.butCreate.setEnabled(true);
146                     client.chanAdmin = false;
147                     break;
148                 case 'a':
149                     client.chanAdmin = true;
150                     break;
151                 default:
152                     client.chanAdmin = false;
153                     break;
154                 }
155                 break;
156             default:
157                 break;
158             }
159         } catch (ClassCastException JavaDoc e) { }
160     }
161
162     /**
163      * adds a user to the server
164      * @param username the name of the user to be added
165      */

166     public void userAdd(String JavaDoc username) {
167         if (username == null) {
168             client.showUserStatus = true;
169         } else {
170             client.userAdd(username);
171         }
172     }
173     /**
174      * adds an admin to the server. If the name of the user
175      * is yours, set interal admin status
176      * @param username the name of the user made an admin
177      */

178     public void adminAdd(String JavaDoc username) {
179         if (username.equals(client.username)) {
180             client.setAdmin();
181         } if (!client.admins.contains(username)) {
182             client.admins.add(username);
183             client.serverMessage(username + " is now an admin");
184             client.updateList();
185         }
186     }
187     /**
188      * removes a user from the server
189      * @param username the name of the user to be removed
190      */

191     public void userDel(String JavaDoc username) {
192         client.userDel(username);
193     }
194     /**
195      * renames a user, if the name of the user is yours,
196      * rename yourself first.
197      * @param on the old name of the user
198      * @param nn the new name of the user
199      */

200     public void rename(String JavaDoc on, String JavaDoc nn) {
201         if (on.equals(client.username)) {
202             client.username = nn;
203         }
204         client.rename(on, nn);
205     }
206     /**
207      * recieves kick notification
208      * @param un should be null, so not used
209      */

210     public void kick(String JavaDoc un) {
211        client.username = null;
212     }
213     
214     /**
215      * handles a change in channels
216      * @param nc true if a new channel, false when you have
217                     succesfully changed channels so clear all user
218                     lists and add yourself; should be recieving
219                     a list of any connected users in this channel
220                     shortly
221      * @param n the name of the channel
222      * @param p should be null, not used here
223      */

224     public void channel(boolean nc, String JavaDoc n, String JavaDoc p) {
225         if (nc) {
226             client.newChannel(n, (p == null ? false : true));
227         } else {
228             if (n == null) {
229                 client.cboChannels.setSelectedItem(channel);
230                 client.showUserStatus = true;
231             } else {
232                 client.users.clear();
233                 client.afks.clear();
234                 client.ignores.clear();
235                 client.admins.clear();
236                 if (client.admin) {
237                     client.admins.add(client.username);
238                 }
239                 client.updateList();
240                 client.serverMessage("You have joined " + n);
241                 client.userAdd(client.username);
242                 client.cboChannels.setSelectedItem(n);
243                 channel = n;
244             }
245         }
246     }
247     /**
248      * recieves a chat message
249      * @param username the user sending the chat
250      * @param message the message
251      */

252     public void chat(String JavaDoc username, String JavaDoc message) {
253         client.recieveChat(username, message);
254     }
255     /**
256      * recieves a private message
257      * @param username the user sending the message
258      * @param message the message
259      */

260     public void private_msg(String JavaDoc username, String JavaDoc message) {
261         client.recievePrivate(username, message);
262     }
263     /**
264      * recieves a whisper
265      * @param username the user sending the whisper
266      * @param message the message being sent
267      */

268     public void whisper(String JavaDoc username, String JavaDoc message) {
269         client.recieveWhisper(username, message);
270     }
271     /**
272      * receives notification in change in logging status
273      * @param start true when starting logging, false otherwise
274      */

275     public void chatLog(boolean start) {
276         if (start) {
277             client.serverMessage("now logging in " + channel);
278         } else {
279             client.serverMessage("no longer logging in " + channel);
280         }
281     }
282     /**
283      * receives an error from the server
284      * @param s the error
285      */

286     public void error(String JavaDoc s) {
287         client.error(s);
288     }
289
290 // -- End SocketConnection Interface methods -- \\
291

292     /**
293      * Method to implement runnable first reports its username
294      * to the server in the form of an SD_UserAdd and listens
295      * for incoming objects
296      */

297     public void run() {
298         writeObject(new SD_UserAdd(client.username));
299         SocketData sd;
300         while (connected && (sd = readObject()) != null) {
301             sd.performAction(this);
302         }
303         close();
304     }
305     
306 }
Popular Tags