~vcs-imports/skycastle/trunk

« back to all changes in this revision

Viewing changes to modules/server/src/main/java/org/skycastle/server/hardcoded/SkycastleServerListener.java

  • Committer: zzorn
  • Date: 2008-11-10 14:55:40 UTC
  • Revision ID: vcs-imports@canonical.com-20081110145540-l1hvmkbhnd0612ws
Fixed compile: removed packages of unused code, updated to use new Darkstar API, moved external library versions to the master pom.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.skycastle.server.hardcoded;
2
 
 
3
 
import com.sun.sgs.app.*;
4
 
 
5
 
import java.io.Serializable;
6
 
import java.util.HashMap;
7
 
import java.util.Map;
8
 
import java.util.Properties;
9
 
import java.util.Set;
10
 
import java.util.logging.Level;
11
 
import java.util.logging.Logger;
12
 
 
13
 
 
14
 
/**
15
 
 * The Skycastle Server, running inside Darkstar. Recieves events when the server is started the first time,
16
 
 * and when a client logs in.
17
 
 * <p/>
18
 
 * This also implements some features of a chat server right now, which means maintaining the list of users
19
 
 * for the general chat.
20
 
 * <p/>
21
 
 * TODO:
22
 
 * <p/>
23
 
 * 1) Channels are probably not the right way to do implement a chat, since they are essentially a method for
24
 
 * peer-to-peer communication, not message dispatching. It is not possible to enforce any state since clients
25
 
 * are free to post arbitrary messages on the channel which are then received by all other clients. There is
26
 
 * no possibility for the server to filter out bogus messages.
27
 
 * <p/>
28
 
 * 2) Chat functionality should be handled by a dedicated service, not the server listener, which is the point
29
 
 * where new connections to the game in general are accepted. Chat is (if at all) just one of the services
30
 
 * offered by the Skycastle server.
31
 
 * <p/>
32
 
 * 3) Implement a proper chat protocol, or even better, use idioms that will remain valid as development on
33
 
 * other game features progresses.
34
 
 */
35
 
public class SkycastleServerListener
36
 
        implements Serializable, AppListener
37
 
{
38
 
 
39
 
    //======================================================================
40
 
    // Public Constants
41
 
 
42
 
    /**
43
 
     * The name of the general chat channel: '{@value #GENERAL_CHAT}'
44
 
     */
45
 
    public static final String GENERAL_CHAT = "General";
46
 
 
47
 
    //======================================================================
48
 
    // Private Constants
49
 
 
50
 
    /**
51
 
     * The version of the serialized form of this class.
52
 
     */
53
 
    private static final long serialVersionUID = 1L;
54
 
 
55
 
    /**
56
 
     * The {@link Logger} for this class.
57
 
     */
58
 
    private static final Logger logger =
59
 
            Logger.getLogger( SkycastleServerListener.class.getName() );
60
 
 
61
 
    /**
62
 
     * Map that associates a session ID with a nickname.
63
 
     */
64
 
    protected final Map<ClientSessionId, String> nicknamesById =
65
 
            new HashMap<ClientSessionId, String>();
66
 
 
67
 
    //======================================================================
68
 
    // Public Methods
69
 
 
70
 
    //----------------------------------------------------------------------
71
 
    // AppListener Implementation
72
 
 
73
 
    /**
74
 
     * {@inheritDoc}
75
 
     * <p/>
76
 
     * Creates the general chat channel.  Channels persist across server restarts, so they only need to be
77
 
     * created here in {@code initialize}.
78
 
     */
79
 
    public void initialize( Properties props )
80
 
    {
81
 
        final ChannelManager channelManager = AppContext.getChannelManager();
82
 
 
83
 
        channelManager.createChannel( GENERAL_CHAT, null, Delivery.RELIABLE );
84
 
    }
85
 
 
86
 
 
87
 
    /**
88
 
     * {@inheritDoc}
89
 
     * <p/>
90
 
     * Returns a {@link SkycastleClientSessionListener} for the logged-in session.
91
 
     */
92
 
    public SkycastleClientSessionListener loggedIn( ClientSession session )
93
 
    {
94
 
        // DEBUG:
95
 
        logger.log( Level.INFO, "User {0} has logged in", session.getName() );
96
 
        nicknamesById.put( session.getSessionId(), session.getName() );
97
 
 
98
 
 
99
 
        return new SkycastleClientSessionListener( session, this );
100
 
    }
101
 
 
102
 
    /**
103
 
     * Get a set of nicknames of currently connected users keyed by session ID.
104
 
     *
105
 
     * @return Set of nicknames.
106
 
     */
107
 
    public Set<Map.Entry<ClientSessionId, String>> getConnectedUsers()
108
 
    {
109
 
        return nicknamesById.entrySet();
110
 
    }
111
 
 
112
 
    /**
113
 
     * Remove the user with the specified session ID from the nickname list.
114
 
     *
115
 
     * @param sessionId session ID of the user to be removed
116
 
     */
117
 
    public void removeUser( ClientSessionId sessionId )
118
 
    {
119
 
        nicknamesById.remove( sessionId );
120
 
    }
121
 
 
122
 
}
123
 
 
124