KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > chatserver > Server


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.chatserver;
31
32 /**
33  * A representation of the state of the chat engine.
34  * This object is the main entry-point for the servlet
35  * to update and query the state of the chat room.
36  */

37 public class Server {
38
39     private UserManager userManager;
40     private MessageStore messageStore;
41     
42     /**
43      * Creates a new <code>Server</code>.
44      */

45     public Server() {
46         super();
47         userManager = new UserManager();
48         messageStore = new MessageStore();
49     }
50     
51     /**
52      * Adds a user.
53      *
54      * @param userName the user name
55      * @param remoteHost the remote host requesting the operation
56      * @return an authentication token if the user was successfully added or
57      * null if it was not (e.g., due to the name already being in use)
58      */

59     public String JavaDoc addUser(String JavaDoc userName, String JavaDoc remoteHost) {
60         String JavaDoc token = userManager.add(userName);
61         if (token != null) {
62             messageStore.post(null, "User \"" + userName + "\" has joined the chat.");
63             Log.log(Log.ACTION_AUTH, remoteHost, userName, null);
64         }
65         return token;
66     }
67     
68     /**
69      * Retrieves all messages with ids greater than the specified id.
70      *
71      * @param lastRetrievedId the id of the last message retrieved
72      * @return an array containing messages posted after the message identified
73      */

74     public Message[] getMessages(long lastRetrievedId) {
75         return messageStore.getMessages(lastRetrievedId);
76     }
77     
78     /**
79      * Returns an array of recently posted messages.
80      * This method is queried by clients that have just joined the chat room
81      * to retrieve context on the chat.
82      *
83      * @return the recently posted messages
84      */

85     public Message[] getRecentMessages() {
86         return messageStore.getRecentMessages();
87     }
88     
89     /**
90      * Removes a user from the chat room.
91      *
92      * @param userName the name of the user
93      * @param authToken the authentication token provided to the
94      * user when s/he was authenticated
95      * @param remoteHost the remote host requesting the operation
96      * @return true if the user was successfully removed
97      */

98     public boolean removeUser(String JavaDoc userName, String JavaDoc authToken, String JavaDoc remoteHost) {
99         if (userManager.authenticate(userName, authToken)) {
100             userManager.remove(userName);
101             messageStore.post(null, "User \"" + userName + "\" has exited the chat.");
102             Log.log(Log.ACTION_EXIT, remoteHost, userName, null);
103             return true;
104         } else {
105             return false;
106         }
107     }
108     
109     /**
110      * Posts a message.
111      *
112      * @param userName the name of the posting user
113      * @param authToken the authentication token provided to the posting
114      * user when s/he was authenticated
115      * @param remoteHost the remote host requesting the operation
116      * @param messageContent the content of the message to post
117      * @return true if the message was successfully posted
118      */

119     public boolean postMessage(String JavaDoc userName, String JavaDoc authToken, String JavaDoc remoteHost, String JavaDoc messageContent) {
120         if (userManager.authenticate(userName, authToken)) {
121             messageStore.post(userName, messageContent);
122             Log.log(Log.ACTION_POST, remoteHost, userName, messageContent);
123             return true;
124         } else {
125             return false;
126         }
127     }
128 }
129
Popular Tags