001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package echo2example.chatserver;
031:
032: /**
033: * A representation of the state of the chat engine.
034: * This object is the main entry-point for the servlet
035: * to update and query the state of the chat room.
036: */
037: public class Server {
038:
039: private UserManager userManager;
040: private MessageStore messageStore;
041:
042: /**
043: * Creates a new <code>Server</code>.
044: */
045: public Server() {
046: super ();
047: userManager = new UserManager();
048: messageStore = new MessageStore();
049: }
050:
051: /**
052: * Adds a user.
053: *
054: * @param userName the user name
055: * @param remoteHost the remote host requesting the operation
056: * @return an authentication token if the user was successfully added or
057: * null if it was not (e.g., due to the name already being in use)
058: */
059: public String addUser(String userName, String remoteHost) {
060: String token = userManager.add(userName);
061: if (token != null) {
062: messageStore.post(null, "User \"" + userName
063: + "\" has joined the chat.");
064: Log.log(Log.ACTION_AUTH, remoteHost, userName, null);
065: }
066: return token;
067: }
068:
069: /**
070: * Retrieves all messages with ids greater than the specified id.
071: *
072: * @param lastRetrievedId the id of the last message retrieved
073: * @return an array containing messages posted after the message identified
074: */
075: public Message[] getMessages(long lastRetrievedId) {
076: return messageStore.getMessages(lastRetrievedId);
077: }
078:
079: /**
080: * Returns an array of recently posted messages.
081: * This method is queried by clients that have just joined the chat room
082: * to retrieve context on the chat.
083: *
084: * @return the recently posted messages
085: */
086: public Message[] getRecentMessages() {
087: return messageStore.getRecentMessages();
088: }
089:
090: /**
091: * Removes a user from the chat room.
092: *
093: * @param userName the name of the user
094: * @param authToken the authentication token provided to the
095: * user when s/he was authenticated
096: * @param remoteHost the remote host requesting the operation
097: * @return true if the user was successfully removed
098: */
099: public boolean removeUser(String userName, String authToken,
100: String remoteHost) {
101: if (userManager.authenticate(userName, authToken)) {
102: userManager.remove(userName);
103: messageStore.post(null, "User \"" + userName
104: + "\" has exited the chat.");
105: Log.log(Log.ACTION_EXIT, remoteHost, userName, null);
106: return true;
107: } else {
108: return false;
109: }
110: }
111:
112: /**
113: * Posts a message.
114: *
115: * @param userName the name of the posting user
116: * @param authToken the authentication token provided to the posting
117: * user when s/he was authenticated
118: * @param remoteHost the remote host requesting the operation
119: * @param messageContent the content of the message to post
120: * @return true if the message was successfully posted
121: */
122: public boolean postMessage(String userName, String authToken,
123: String remoteHost, String messageContent) {
124: if (userManager.authenticate(userName, authToken)) {
125: messageStore.post(userName, messageContent);
126: Log.log(Log.ACTION_POST, remoteHost, userName,
127: messageContent);
128: return true;
129: } else {
130: return false;
131: }
132: }
133: }
|