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 chatserver;
016:
017: import org.quickserver.net.server.*;
018:
019: import org.quickserver.util.pool.PoolableObject;
020: import org.apache.commons.pool.BasePoolableObjectFactory;
021: import org.apache.commons.pool.PoolableObjectFactory;
022:
023: import java.util.*;
024: import java.io.*;
025: import java.nio.*;
026: import java.util.logging.*;
027:
028: /**
029: *
030: * @author Akshathkumar Shetty
031: */
032: public class ChatData implements ClientData, ClientIdentifiable,
033: PoolableObject {
034: private static final Logger logger = Logger
035: .getLogger(ChatData.class.getName());
036:
037: private static Set usernameList = new HashSet();
038:
039: private String username = null;
040: private String room = null;
041: private String info = null;
042:
043: //for auth
044: private String lastAsked = null;
045: private byte password[] = null;
046:
047: public void setLastAsked(String lastAsked) {
048: this .lastAsked = lastAsked;
049: }
050:
051: public String getLastAsked() {
052: return lastAsked;
053: }
054:
055: public void setPassword(byte[] password) {
056: this .password = password;
057: }
058:
059: public byte[] getPassword() {
060: return password;
061: }
062:
063: public boolean registerUsername(String username) {
064: return usernameList.add(username);
065: }
066:
067: public void deregisterUsername(String username) {
068: usernameList.remove(username);
069: }
070:
071: public void setUsername(String username) {
072: this .username = username;
073: }
074:
075: public String getUsername() {
076: return username;
077: }
078:
079: public void setRoom(String room) {
080: this .room = room;
081: }
082:
083: public String getRoom() {
084: return room;
085: }
086:
087: public String getClientId() {
088: return username;
089: }
090:
091: public String getClientKey() {
092: if (room == null)
093: return username;
094: else
095: return username + "@" + room;
096: }
097:
098: public void setClientInfo(String info) {
099: this .info = info;
100: }
101:
102: public String getClientInfo() {
103: return getClientKey() + " - " + info;
104: }
105:
106: public String toString() {
107: return getClientInfo();
108: }
109:
110: //-- PoolableObject
111: public void clean() {
112: usernameList.remove(username);
113: username = null;
114: room = null;
115: info = null;
116: lastAsked = null;
117: }
118:
119: public boolean isPoolable() {
120: return true;
121: }
122:
123: public PoolableObjectFactory getPoolableObjectFactory() {
124: return new BasePoolableObjectFactory() {
125: public Object makeObject() {
126: return new ChatData();
127: }
128:
129: public void passivateObject(Object obj) {
130: ChatData ed = (ChatData) obj;
131: ed.clean();
132: }
133:
134: public void destroyObject(Object obj) {
135: if (obj == null)
136: return;
137: passivateObject(obj);
138: obj = null;
139: }
140:
141: public boolean validateObject(Object obj) {
142: if (obj == null)
143: return false;
144: else
145: return true;
146: }
147: };
148: }
149: }
|