001: /*- ServerConneciton.java -----------------------------------------+
002: | |
003: | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
004: | countjoe@users.sourceforge.net |
005: | http://www.42llamas.com/LlamaChat/ |
006: | |
007: | This program is free software; you can redistribute it and/or |
008: | modify it under the terms of the GNU General Public License |
009: | as published by the Free Software Foundation; either version 2 |
010: | of the License, or (at your option) any later version |
011: | |
012: | This program is distributed in the hope that it will be useful, |
013: | but WITHOUT ANY WARRANTY; without even the implied warranty of |
014: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
015: | GNU General Public License for more details. |
016: | |
017: | A copy of the GNU General Public License may be found in the |
018: | installation directory named "GNUGPL.txt" |
019: | |
020: +-----------------------------------------------------------------+
021: */
022:
023: package client;
024:
025: import javax.net.ssl.*;
026: import java.io.BufferedInputStream;
027: import java.io.BufferedOutputStream;
028: import java.io.ObjectOutputStream;
029: import java.io.ObjectInputStream;
030: import java.io.IOException;
031: import java.lang.ClassNotFoundException;
032: import javax.swing.JOptionPane;
033:
034: import common.*;
035: import common.sd.*;
036:
037: /* -------------------- JavaDoc Information ----------------------*/
038: /**
039: * A threaded connection class to maintain the conneciton to the server
040: * @author Joseph Monti <a href="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
041: * @version 0.8
042: */
043: public class ServerConnection implements Runnable, SocketConnection {
044:
045: private LlamaChat client;
046: private SSLSocket socket;
047: private ObjectOutputStream out;
048: private ObjectInputStream in;
049: public boolean connected;
050: public boolean finalized;
051: public String channel;
052: private MessageQueue queue;
053:
054: ServerConnection(LlamaChat c) {
055: connected = false;
056: finalized = false;
057: client = c;
058: channel = "Lobby";
059: queue = new MessageQueue(this );
060:
061: try {
062: // initialize socket
063: SSLSocketFactory sslFact = (SSLSocketFactory) SSLSocketFactory
064: .getDefault();
065: socket = (SSLSocket) sslFact.createSocket(c.site, c.PORT);
066: // Checking the supported Cipher suites.
067: String[] enabledCipher = socket.getSupportedCipherSuites();
068: // Enabled the Cipher suites.
069: socket.setEnabledCipherSuites(enabledCipher);
070:
071: // make in/out stream connection
072: out = new ObjectOutputStream(socket.getOutputStream());
073: in = new ObjectInputStream(socket.getInputStream());
074: connected = true;
075: c.setConnected(true);
076: new Thread(this ).start();
077: } catch (IOException e) {
078: c.error("Server not available");
079: c.setConnected(false);
080: connected = false;
081: }
082: }
083:
084: /**
085: * Method to read an object from the server
086: * @return returns the SocketData object recieved
087: */
088: private SocketData readObject() {
089: try {
090: return (SocketData) in.readObject();
091: } catch (Throwable t) {
092: return null;
093: }
094: }
095:
096: /**
097: * Method to write an object to the server
098: * @param sd A SocketData object to be sent
099: */
100: public void writeObject(Object 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 obj) {
109: if (connected) {
110: try {
111: out.writeObject(obj);
112: out.flush();
113: } catch (IOException 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)
127: socket.close();
128: } catch (IOException e) {
129: }
130: client.close();
131: }
132:
133: // -- SocketConnection Interface methods -- \\
134:
135: /**
136: * receives server configurations
137: * @param type the type of data being sent
138: * @param obj the object being sent
139: */
140: public void serverCap(char type, Object obj) {
141: try {
142: switch (type) {
143: case SD_ServerCap.T_CREATE:
144: char tmp = ((Character) obj).charValue();
145: switch (tmp) {
146: case 'y':
147: client.butCreate.setEnabled(true);
148: client.chanAdmin = false;
149: break;
150: case 'a':
151: client.chanAdmin = true;
152: break;
153: default:
154: client.chanAdmin = false;
155: break;
156: }
157: break;
158: default:
159: break;
160: }
161: } catch (ClassCastException e) {
162: }
163: }
164:
165: /**
166: * adds a user to the server
167: * @param username the name of the user to be added
168: */
169: public void userAdd(String username) {
170: if (username == null) {
171: client.showUserStatus = true;
172: } else {
173: client.userAdd(username);
174: }
175: }
176:
177: /**
178: * adds an admin to the server. If the name of the user
179: * is yours, set interal admin status
180: * @param username the name of the user made an admin
181: */
182: public void adminAdd(String username) {
183: if (username.equals(client.username)) {
184: client.setAdmin();
185: }
186: if (!client.admins.contains(username)) {
187: client.admins.add(username);
188: client.serverMessage(username + " is now an admin");
189: client.updateList();
190: }
191: }
192:
193: /**
194: * removes a user from the server
195: * @param username the name of the user to be removed
196: */
197: public void userDel(String username) {
198: client.userDel(username);
199: }
200:
201: /**
202: * renames a user, if the name of the user is yours,
203: * rename yourself first.
204: * @param on the old name of the user
205: * @param nn the new name of the user
206: */
207: public void rename(String on, String nn) {
208: if (on.equals(client.username)) {
209: client.username = nn;
210: }
211: client.rename(on, nn);
212: }
213:
214: /**
215: * recieves kick notification
216: * @param un should be null, so not used
217: */
218: public void kick(String un) {
219: client.username = null;
220: }
221:
222: /**
223: * handles a change in channels
224: * @param nc true if a new channel, false when you have
225: succesfully changed channels so clear all user
226: lists and add yourself; should be recieving
227: a list of any connected users in this channel
228: shortly
229: * @param n the name of the channel
230: * @param p should be null, not used here
231: */
232: public void channel(boolean nc, String n, String p) {
233: if (nc) {
234: client.newChannel(n, (p == null ? false : true));
235: } else {
236: if (n == null) {
237: client.cboChannels.setSelectedItem(channel);
238: client.showUserStatus = true;
239: } else {
240: client.users.clear();
241: client.afks.clear();
242: client.ignores.clear();
243: client.admins.clear();
244: if (client.admin) {
245: client.admins.add(client.username);
246: }
247: client.updateList();
248: client.serverMessage("You have joined " + n);
249: client.userAdd(client.username);
250: client.cboChannels.setSelectedItem(n);
251: channel = n;
252: }
253: }
254: }
255:
256: /**
257: * recieves a chat message
258: * @param username the user sending the chat
259: * @param message the message
260: */
261: public void chat(String username, String message) {
262: client.recieveChat(username, message);
263: }
264:
265: /**
266: * recieves a private message
267: * @param username the user sending the message
268: * @param message the message
269: */
270: public void private_msg(String username, String message) {
271: client.recievePrivate(username, message);
272: }
273:
274: /**
275: * recieves a whisper
276: * @param username the user sending the whisper
277: * @param message the message being sent
278: */
279: public void whisper(String username, String message) {
280: client.recieveWhisper(username, message);
281: }
282:
283: /**
284: * receives notification in change in logging status
285: * @param start true when starting logging, false otherwise
286: */
287: public void chatLog(boolean start) {
288: if (start) {
289: client.serverMessage("now logging in " + channel);
290: } else {
291: client.serverMessage("no longer logging in " + channel);
292: }
293: }
294:
295: /**
296: * receives an error from the server
297: * @param s the error
298: */
299: public void error(String s) {
300: client.error(s);
301: }
302:
303: // -- End SocketConnection Interface methods -- \\
304:
305: /**
306: * Method to implement runnable first reports its username
307: * to the server in the form of an SD_UserAdd and listens
308: * for incoming objects
309: */
310: public void run() {
311: writeObject(new SD_UserAdd(client.username));
312: SocketData sd;
313: while (connected && (sd = readObject()) != null) {
314: sd.performAction(this);
315: }
316: close();
317: }
318:
319: }
|