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.server;
016:
017: import java.net.Socket;
018: import java.nio.channels.SocketChannel;
019:
020: /**
021: * Encapsulates client socket and its configuration details. Used by
022: * {@link QuickServer} and {@link ClientHandler} classes.
023: * @author Akshathkumar Shetty
024: */
025: public class TheClient {
026: private String timeoutMsg;
027: private String maxAuthTryMsg;
028: private int maxAuthTry;
029: private Socket socket;
030: private Authenticator authenticator;
031: private ClientAuthenticationHandler clientAuthenticationHandler; //v1.4.6
032: private ClientEventHandler eventHandler;//v1.4.6
033: private ClientExtendedEventHandler extendedEventHandler;//v1.4.6
034: private ClientCommandHandler commandHandler;
035: private ClientObjectHandler objectHandler; //v1.2
036: private ClientBinaryHandler binaryHandler; //v1.4
037: private QuickServer quickServer;
038: private ClientData clientData;
039: //--v1.3.2
040: private boolean trusted = false;
041: private boolean communicationLogging = true;
042: //--v1.4.5
043: private int socketTimeout;
044: private String maxConnectionMsg;
045: private ClientEvent event = ClientEvent.RUN_BLOCKING;
046: private SocketChannel socketChannel;
047: private ClientWriteHandler writeHandler;
048:
049: /**
050: * Sets the QuickServer object associated with this Client
051: * @see #getServer
052: */
053: public void setServer(QuickServer server) {
054: this .quickServer = server;
055: }
056:
057: /**
058: * Gets the QuickServer object associated with this Client
059: * @see #getServer
060: */
061: public QuickServer getServer() {
062: return quickServer;
063: }
064:
065: /** Sets client socket associated. */
066: public void setSocket(Socket socket) {
067: this .socket = socket;
068: }
069:
070: /** Returns client socket associated. */
071: public Socket getSocket() {
072: return socket;
073: }
074:
075: /**
076: * Sets the Authenticator class that handles the
077: * authentication of a client.
078: * @param authenticator object that implements {@link Authenticator}.
079: * @see #getAuthenticator
080: * @since 1.3
081: * @deprecated As of 1.4.6 use {@link #setClientAuthenticationHandler}
082: */
083: public void setAuthenticator(Authenticator authenticator) {
084: this .authenticator = authenticator;
085: }
086:
087: /**
088: * Returns the Authenticator object that handles the
089: * authentication of a client.
090: * @see #setAuthenticator
091: * @since 1.3
092: * @deprecated As of 1.4.6 use {@link #getClientAuthenticationHandler}
093: */
094: public Authenticator getAuthenticator() {
095: return authenticator;
096: }
097:
098: /**
099: * Sets the ClientAuthenticationHandler class that handles the
100: * authentication of a client.
101: * @param clientAuthenticationHandler object that implements {@link ClientAuthenticationHandler}.
102: * @see #getClientAuthenticationHandler
103: * @since 1.4.6
104: */
105: public void setClientAuthenticationHandler(
106: ClientAuthenticationHandler clientAuthenticationHandler) {
107: this .clientAuthenticationHandler = clientAuthenticationHandler;
108: }
109:
110: /**
111: * Returns the ClientAuthenticationHandler object that handles the
112: * authentication of a client.
113: * @see #setClientAuthenticationHandler
114: * @since 1.4.6
115: */
116: public ClientAuthenticationHandler getClientAuthenticationHandler() {
117: return clientAuthenticationHandler;
118: }
119:
120: /**
121: * Sets the ClientData object that carries client data.
122: * @param data object of the class that
123: * extends {@link ClientData}.
124: * @see #getClientData
125: */
126: public void setClientData(ClientData data) {
127: this .clientData = data;
128: }
129:
130: /**
131: * Returns the ClientData object that carries client data.
132: * @return object of the class that implements {@link ClientData}.
133: * @see #setClientData
134: */
135: public ClientData getClientData() {
136: return clientData;
137: }
138:
139: /**
140: * Sets maximum allowed login attempts.
141: * @since 1.2
142: */
143: public void setMaxAuthTry(int authTry) {
144: maxAuthTry = authTry;
145: }
146:
147: /**
148: * Returns maximum allowed login attempts.
149: * @since 1.2
150: */
151: public int getMaxAuthTry() {
152: return maxAuthTry;
153: }
154:
155: /** Sets message to be displayed when max login attempt reaches.*/
156: public void setMaxAuthTryMsg(String msg) {
157: maxAuthTryMsg = msg;
158: }
159:
160: /**
161: * Returns message to be displayed to the client when maximum
162: * allowed login attempts reaches.
163: */
164: public String getMaxAuthTryMsg() {
165: return maxAuthTryMsg;
166: }
167:
168: /** Sets timeout message. */
169: public void setTimeoutMsg(String msg) {
170: timeoutMsg = msg;
171: }
172:
173: /** Returns timeout message. */
174: public String getTimeoutMsg() {
175: return timeoutMsg;
176: }
177:
178: /**
179: * Sets the ClientEventHandler objects class that gets notified of
180: * client events.
181: * @param handler object that
182: * implements {@link ClientEventHandler}
183: * @see #getClientEventHandler
184: * @since 1.4.6
185: */
186: public void setClientEventHandler(ClientEventHandler handler) {
187: this .eventHandler = handler;
188: }
189:
190: /**
191: * Returns the ClientEventHandler object that gets notified of
192: * client events.
193: * @see #setClientEventHandler
194: * @since 1.4.6
195: */
196: public ClientEventHandler getClientEventHandler() {
197: return eventHandler;
198: }
199:
200: /**
201: * Sets the ClientExtendedEventHandler objects class that gets notified of
202: * extended client events.
203: * @param handler object that
204: * implements {@link ClientExtendedEventHandler}
205: * @see #getClientExtendedEventHandler
206: * @since 1.4.6
207: */
208: public void setClientExtendedEventHandler(
209: ClientExtendedEventHandler handler) {
210: this .extendedEventHandler = handler;
211: }
212:
213: /**
214: * Returns the ClientExtendedEventHandler object that gets notified of
215: * client events.
216: * @see #setClientExtendedEventHandler
217: * @since 1.4.6
218: */
219: public ClientExtendedEventHandler getClientExtendedEventHandler() {
220: return extendedEventHandler;
221: }
222:
223: /**
224: * Sets the ClientCommandHandler objects that interacts with
225: * client sockets.
226: * @param handler object that
227: * implements {@link ClientCommandHandler}
228: * @see #getClientCommandHandler
229: */
230: public void setClientCommandHandler(ClientCommandHandler handler) {
231: this .commandHandler = handler;
232: }
233:
234: /**
235: * Returns the ClientCommandHandler object that interacts with
236: * client sockets.
237: * @see #setClientCommandHandler
238: */
239: public ClientCommandHandler getClientCommandHandler() {
240: return commandHandler;
241: }
242:
243: /**
244: * Sets the ClientObjectHandler object that interacts with
245: * client sockets.
246: * @param handler object that
247: * implements {@link ClientObjectHandler}
248: * @see #getClientObjectHandler
249: * @since 1.2
250: */
251: public void setClientObjectHandler(ClientObjectHandler handler) {
252: this .objectHandler = handler;
253: }
254:
255: /**
256: * Returns the ClientObjectHandler object that interacts with
257: * client sockets.
258: * @see #setClientObjectHandler
259: * @since 1.2
260: */
261: public ClientObjectHandler getClientObjectHandler() {
262: return objectHandler;
263: }
264:
265: /**
266: * Returns flag to skip timeout setting and authentication of this client.
267: * @since 1.3.2
268: */
269: public boolean getTrusted() {
270: return trusted;
271: }
272:
273: /**
274: * Sets flag to skip timeout setting and authentication of this client.
275: * @since 1.3.2
276: */
277: public void setTrusted(boolean flag) {
278: trusted = flag;
279: }
280:
281: /**
282: * Sets the communication logging flag.
283: * @see #getCommunicationLogging
284: * @since 1.3.2
285: */
286: public void setCommunicationLogging(boolean communicationLogging) {
287: this .communicationLogging = communicationLogging;
288: }
289:
290: /**
291: * Returns the communication logging flag.
292: * @see #setCommunicationLogging
293: * @since 1.3.2
294: */
295: public boolean getCommunicationLogging() {
296: return communicationLogging;
297: }
298:
299: /**
300: * Sets the ClientBinaryHandler object that interacts with
301: * client sockets.
302: * @param handler object that
303: * implements {@link ClientBinaryHandler}
304: * @see #getClientBinaryHandler
305: * @since 1.4
306: */
307: public void setClientBinaryHandler(ClientBinaryHandler handler) {
308: this .binaryHandler = handler;
309: }
310:
311: /**
312: * Returns the ClientBinaryHandler object that interacts with
313: * client sockets.
314: * @see #setClientBinaryHandler
315: * @since 1.4
316: */
317: public ClientBinaryHandler getClientBinaryHandler() {
318: return binaryHandler;
319: }
320:
321: /**
322: * Sets the client socket's timeout.
323: * @param time client socket timeout in milliseconds.
324: * @see #getTimeout
325: * @since 1.4.5
326: */
327: public void setTimeout(int time) {
328: socketTimeout = time;
329: }
330:
331: /**
332: * Returns the Client socket timeout in milliseconds.
333: * @see #setTimeout
334: * @since 1.4.5
335: */
336: public int getTimeout() {
337: return socketTimeout;
338: }
339:
340: /**
341: * Sets ClientEvent.
342: * @since 1.4.5
343: */
344: public void setClientEvent(ClientEvent event) {
345: this .event = event;
346: }
347:
348: /**
349: * Returns ClientEvent.
350: * @since 1.4.5
351: */
352: public ClientEvent getClientEvent() {
353: return event;
354: }
355:
356: /**
357: * Sets message to be displayed when maximum connection reaches.
358: * @since 1.4.5
359: */
360: public void setMaxConnectionMsg(String msg) {
361: maxConnectionMsg = msg;
362: }
363:
364: /**
365: * Returns message to be displayed to the client when maximum
366: * connection reaches.
367: * @since 1.4.5
368: */
369: public String getMaxConnectionMsg() {
370: return maxConnectionMsg;
371: }
372:
373: /**
374: * Sets client socket channel associated, if any.
375: * @since 1.4.5
376: */
377: public void setSocketChannel(SocketChannel socketChannel) {
378: this .socketChannel = socketChannel;
379: }
380:
381: /**
382: * Returns client socket channel associated, if any.
383: * @since 1.4.5
384: */
385: public SocketChannel getSocketChannel() {
386: return socketChannel;
387: }
388:
389: /**
390: * Sets the ClientWriteHandler object that interacts with
391: * client sockets.
392: * @param handler object that
393: * implements {@link ClientWriteHandler}
394: * @see #getClientWriteHandler
395: * @since 1.4.5
396: */
397: public void setClientWriteHandler(ClientWriteHandler handler) {
398: this .writeHandler = handler;
399: }
400:
401: /**
402: * Returns the ClientWriteHandler object that interacts with
403: * client sockets.
404: * @see #setClientWriteHandler
405: * @since 1.4.5
406: */
407: public ClientWriteHandler getClientWriteHandler() {
408: return writeHandler;
409: }
410:
411: /**
412: * Returns client info.
413: * @since 1.4.5
414: */
415: public String toString() {
416: StringBuffer sb = new StringBuffer();
417: sb.append("{TheClient ");
418: if (socket != null)
419: sb.append(socket);
420: else
421: sb.append("no socket");
422: sb.append(", Event: " + event);
423: sb.append('}');
424: return sb.toString();
425: }
426: }
|