Revision: 226 http://skycastle.svn.sourceforge.net/skycastle/?rev=226&view=rev Author: zzorn Date: 2007-10-06 23:02:46 -0700 (Sat, 06 Oct 2007) Log Message: ----------- Some correctd typos (genera -> general) Modified Paths: -------------- trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleClientSessionListener.java trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleServerListener.java Modified: trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleClientSessionListener.java =================================================================== --- trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleClientSessionListener.java 2007-10-07 06:01:03 UTC (rev 225) +++ trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleClientSessionListener.java 2007-10-07 06:02:46 UTC (rev 226) @@ -5,20 +5,19 @@ import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.math.BigInteger; -import java.util.HashMap; import java.util.Map; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; /** - * Initializes a client session and listens to messages (commands) and session + * Initializes a client session and listens to messages (commands) and session * events from the client. - * + * <p/> * TODO: - * - * See SkycastleServerListener. Like SkycastleServerListener, this is a - * general hub for dispatching network communication and should not + * <p/> + * See SkycastleServerListener. Like SkycastleServerListener, this is a + * general hub for dispatching network communication and should not * specifically implement chat service features. */ public class SkycastleClientSessionListener @@ -47,15 +46,17 @@ private static final Logger logger = Logger.getLogger( SkycastleClientSessionListener.class.getName() ); - /** The message encoding. */ + /** + * The message encoding. + */ public static final String MESSAGE_CHARSET = "UTF-8"; - + /** * The server listener. * This is used for querying connected users etc. */ protected ManagedReference serverListener; - + //====================================================================== // Public Methods @@ -68,22 +69,22 @@ * * @param session the session this listener is associated with */ - public SkycastleClientSessionListener( ClientSession session, - SkycastleServerListener initServerListener ) + public SkycastleClientSessionListener( ClientSession session, + SkycastleServerListener initServerListener ) { this.session = session; final DataManager dataManager = AppContext.getDataManager(); - serverListener = dataManager.createReference(initServerListener); + serverListener = dataManager.createReference( initServerListener ); // Add client to general chat directly final ChannelManager channelManager = AppContext.getChannelManager(); - final Channel generalChat = channelManager.getChannel( SkycastleServerListener.GENERA_CHAT ); + final Channel generalChat = channelManager.getChannel( SkycastleServerListener.GENERAL_CHAT ); generalChat.join( session, null ); /* Let the connected users know that the user has joined the chat by sending a JOIN message. */ - generalChat.send(encodeString("JOIN " - + encodeHexString(session.getSessionId().getBytes()) + " " - + session.getName())); + generalChat.send( encodeString( "JOIN " + + encodeHexString( session.getSessionId().getBytes() ) + " " + + session.getName() ) ); /* Let the user know who else is there. */ sendUserList(); } @@ -111,15 +112,15 @@ public void disconnected( boolean graceful ) { final ChannelManager channelManager = AppContext.getChannelManager(); - final Channel generalChat = channelManager.getChannel( SkycastleServerListener.GENERA_CHAT ); - final SkycastleServerListener sl = - serverListener.get(SkycastleServerListener.class); + final Channel generalChat = channelManager.getChannel( SkycastleServerListener.GENERAL_CHAT ); + final SkycastleServerListener sl = + serverListener.get( SkycastleServerListener.class ); /* Let the connected users know that the user has left the chat by sending a QUIT message. */ - generalChat.send(encodeString("QUIT " - + encodeHexString(session.getSessionId().getBytes()) + " " - + session.getName())); - sl.removeUser(session.getSessionId()); + generalChat.send( encodeString( "QUIT " + + encodeHexString( session.getSessionId().getBytes() ) + " " + + session.getName() ) ); + sl.removeUser( session.getSessionId() ); // DEBUG: final String grace = graceful ? "graceful" : "forced"; logger.log( Level.INFO, @@ -134,73 +135,83 @@ protected void sendUserList() { final SkycastleServerListener sl = - serverListener.get(SkycastleServerListener.class); - Set<Map.Entry<ClientSessionId, String> > entries = - sl.getConnectedUsers(); - for (Map.Entry<ClientSessionId, String> e : entries) - if (!e.getKey().equals(session.getSessionId())) - session.send(encodeString("JOIN " - + encodeHexString(e.getKey().getBytes()) + " " - + e.getValue())); + serverListener.get( SkycastleServerListener.class ); + Set<Map.Entry<ClientSessionId, String>> entries = + sl.getConnectedUsers(); + for ( Map.Entry<ClientSessionId, String> e : entries ) + { + if ( !e.getKey().equals( session.getSessionId() ) ) + { + session.send( encodeString( "JOIN " + + encodeHexString( e.getKey().getBytes() ) + " " + + e.getValue() ) ); + } + } } - + /** * Encodes a {@code String} into an array of bytes. * * @param s the string to encode + * * @return the byte array which encodes the given string */ - protected static byte[] encodeString(String s) + protected static byte[] encodeString( String s ) { try { - return s.getBytes(MESSAGE_CHARSET); - } catch (UnsupportedEncodingException e) + return s.getBytes( MESSAGE_CHARSET ); + } + catch ( UnsupportedEncodingException e ) { - throw new Error("Required character set " + MESSAGE_CHARSET - + " not found", e); + throw new Error( "Required character set " + MESSAGE_CHARSET + + " not found", e ); } } - + /** * Decodes an array of bytes into a {@code String}. * * @param bytes the bytes to decode + * * @return the decoded string */ - protected static String decodeString(byte[] bytes) + protected static String decodeString( byte[] bytes ) { try { - return new String(bytes, MESSAGE_CHARSET); - } catch (UnsupportedEncodingException e) + return new String( bytes, MESSAGE_CHARSET ); + } + catch ( UnsupportedEncodingException e ) { - throw new Error("Required character set " + MESSAGE_CHARSET - + " not found", e); + throw new Error( "Required character set " + MESSAGE_CHARSET + + " not found", e ); } } - + /** * Encode an array of bytes into a hexadecimal string. * * @param bytes The bytes to be encoded. + * * @return The encoded string. */ - protected static String encodeHexString(byte[] bytes) + protected static String encodeHexString( byte[] bytes ) { - BigInteger bi = new BigInteger(bytes); - return bi.toString(16); + BigInteger bi = new BigInteger( bytes ); + return bi.toString( 16 ); } - + /** * Decode a hexadecimal string into an array of bytes. * - * @param bytes The string to be decoded. + * @param source The string to be decoded. + * * @return The decoded string. */ - protected static byte[] decodeHexString(String source) + protected static byte[] decodeHexString( String source ) { - BigInteger bi = new BigInteger(source, 16); + BigInteger bi = new BigInteger( source, 16 ); return bi.toByteArray(); } } Modified: trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleServerListener.java =================================================================== --- trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleServerListener.java 2007-10-07 06:01:03 UTC (rev 225) +++ trunk/skycastle/modules/server/src/main/java/org/skycastle/server/SkycastleServerListener.java 2007-10-07 06:02:46 UTC (rev 226) @@ -3,9 +3,9 @@ import com.sun.sgs.app.*; import java.io.Serializable; -import java.util.Properties; import java.util.HashMap; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -13,28 +13,28 @@ /** * The Skycastle Server, running inside Darkstar. - * Recieves events when the server is started the first time, and when a + * Recieves events when the server is started the first time, and when a * client logs in. - * + * <p/> * This also implements some features of a chat server right now, which means * maintaining the list of users for the general chat. - * + * <p/> * TODO: - * - * 1) Channels are probably not the right way to do implement a chat, since - * they are essentially a method for peer-to-peer communication, not message - * dispatching. It is not possible to enforce any state since clients are - * free to post arbitrary messages on the channel which are then received by - * all other clients. There is no possibility for the server to filter out - * bogus messages. - * - * 2) Chat functionality should be handled by a dedicated service, not the - * server listener, which is the point where new connections to the game in - * general are accepted. Chat is (if at all) just one of the services - * offered by the Skycastle server. - * - * 3) Implement a proper chat protocol, or even better, use idioms that will - * remain valid as development on other game features progresses. + * <p/> + * 1) Channels are probably not the right way to do implement a chat, since + * they are essentially a method for peer-to-peer communication, not message + * dispatching. It is not possible to enforce any state since clients are + * free to post arbitrary messages on the channel which are then received by + * all other clients. There is no possibility for the server to filter out + * bogus messages. + * <p/> + * 2) Chat functionality should be handled by a dedicated service, not the + * server listener, which is the point where new connections to the game in + * general are accepted. Chat is (if at all) just one of the services + * offered by the Skycastle server. + * <p/> + * 3) Implement a proper chat protocol, or even better, use idioms that will + * remain valid as development on other game features progresses. */ public class SkycastleServerListener implements Serializable, AppListener @@ -44,9 +44,9 @@ // Public Constants /** - * The name of the general chat channel: '{@value #GENERA_CHAT}' + * The name of the general chat channel: '{@value #GENERAL_CHAT}' */ - public static final String GENERA_CHAT = "General"; + public static final String GENERAL_CHAT = "General"; //====================================================================== // Private Constants @@ -62,10 +62,12 @@ private static final Logger logger = Logger.getLogger( SkycastleServerListener.class.getName() ); - /** Map that associates a session ID with a nickname. */ - protected final Map<ClientSessionId, String> nicknamesById = - new HashMap<ClientSessionId, String>(); - + /** + * Map that associates a session ID with a nickname. + */ + protected final Map<ClientSessionId, String> nicknamesById = + new HashMap<ClientSessionId, String>(); + //====================================================================== // Public Methods @@ -82,7 +84,7 @@ { final ChannelManager channelManager = AppContext.getChannelManager(); - channelManager.createChannel( GENERA_CHAT, null, Delivery.RELIABLE ); + channelManager.createChannel( GENERAL_CHAT, null, Delivery.RELIABLE ); } @@ -95,29 +97,29 @@ { // DEBUG: logger.log( Level.INFO, "User {0} has logged in", session.getName() ); - nicknamesById.put(session.getSessionId(), session.getName()); - + nicknamesById.put( session.getSessionId(), session.getName() ); + return new SkycastleClientSessionListener( session, this ); } - + /** * Get a set of nicknames of currently connected users keyed by session ID. * * @return Set of nicknames. */ - public Set<Map.Entry<ClientSessionId, String> > getConnectedUsers() + public Set<Map.Entry<ClientSessionId, String>> getConnectedUsers() { return nicknamesById.entrySet(); } - + /** * Remove the user with the specified session ID from the nickname list. * * @param sessionId session ID of the user to be removed */ - public void removeUser(ClientSessionId sessionId) + public void removeUser( ClientSessionId sessionId ) { - nicknamesById.remove(sessionId); + nicknamesById.remove( sessionId ); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.