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 store for posted <code>Message</code>s.
034: */
035: public class MessageStore {
036:
037: private static final int MAX_MESSAGES = 1000;
038: private static final int RECENT_MESSAGES = 15;
039:
040: private long[] messageIds = new long[MAX_MESSAGES];
041: private Message[] messages = new Message[MAX_MESSAGES];
042: private int firstMessageIndex = 0;
043: private int messageCount = 0;
044:
045: /**
046: * Creates a new <code>MessageStore</code>.
047: */
048: public MessageStore() {
049: super ();
050: }
051:
052: /**
053: * Returns the message at the specified index.
054: *
055: * @param index the index
056: * @return the message
057: */
058: private Message getMessage(int index) {
059: return messages[(firstMessageIndex + index) % MAX_MESSAGES];
060: }
061:
062: /**
063: * Retrieves all messages with ids greater than the specified id.
064: *
065: * @param lastRetrievedId the id of the last message retrieved
066: * @return an array containing messages posted after the message identified
067: */
068: public Message[] getMessages(long lastRetrievedId) {
069: int startIndex = 0;
070: while (startIndex < messageCount) {
071: Message message = getMessage(startIndex);
072: if (message.getId() > lastRetrievedId) {
073: break;
074: }
075: ++startIndex;
076: }
077: Message[] matchingMessages = new Message[messageCount
078: - startIndex];
079: for (int i = startIndex; i < messageCount; ++i) {
080: matchingMessages[i - startIndex] = getMessage(i);
081: }
082: return matchingMessages;
083: }
084:
085: /**
086: * Returns an array of recently posted messages.
087: * This method is queried by clients that have just joined the chat room
088: * to retrieve context on the chat.
089: *
090: * @return the recently posted messages
091: */
092: public Message[] getRecentMessages() {
093: Message[] matchingMessages = new Message[messageCount < RECENT_MESSAGES ? messageCount
094: : RECENT_MESSAGES];
095: for (int i = 0; i < matchingMessages.length; ++i) {
096: matchingMessages[i] = getMessage(messageCount
097: - matchingMessages.length + i);
098: }
099: return matchingMessages;
100: }
101:
102: /**
103: * Posts a message.
104: *
105: * @param userName the name of the user
106: * @param messageContent the message content to post
107: */
108: public synchronized void post(String userName, String messageContent) {
109: int messageIndex = (firstMessageIndex + messageCount)
110: % MAX_MESSAGES;
111:
112: Message message = new Message(userName, messageContent);
113: messages[messageIndex] = message;
114: messageIds[messageIndex] = message.getId();
115:
116: if (messageCount < MAX_MESSAGES) {
117: ++messageCount;
118: } else {
119: firstMessageIndex = (firstMessageIndex + 1) % MAX_MESSAGES;
120: }
121: }
122: }
|