move MessageType back inside QueuedMessage since the client doesn't need it

make converter to produce js enums out of java enums
make everything use the constants on both sides for ajax operation code and disconnect reason
move the handler update out of the subpackage that it generates into
make the enum update and handler update run at build
This commit is contained in:
Andy Janata 2012-01-12 20:05:39 -08:00
parent a7cc7c8d36
commit 8da78ebac7
27 changed files with 224 additions and 60 deletions

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${working_set:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;resources&gt;&#13;&#10;&lt;item path=&quot;/cah/WebContent/js/cah.constants.js&quot; type=&quot;1&quot;/&gt;&#13;&#10;&lt;item path=&quot;/cah/src/net/socialgamer/cah/handlers&quot; type=&quot;2&quot;/&gt;&#13;&#10;&lt;/resources&gt;}"/>
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE" value="${working_set:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;resources&gt;&#13;&#10;&lt;item path=&quot;/cah/src/net/socialgamer/cah/handlers&quot; type=&quot;2&quot;/&gt;&#13;&#10;&lt;/resources&gt;}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPE" value="${projects:cah}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/cah/src/net/socialgamer/cah/UpdateHandlerList.cmd}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="incremental,auto,"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
<stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${working_set:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;resources&gt;&#13;&#10;&lt;item path=&quot;/cah/WebContent/js/cah.constants.js&quot; type=&quot;1&quot;/&gt;&#13;&#10;&lt;/resources&gt;}"/>
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_BUILD_SCOPE" value="${working_set:&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#13;&#10;&lt;resources&gt;&#13;&#10;&lt;item path=&quot;/cah/src/net/socialgamer/cah/Constants.java&quot; type=&quot;1&quot;/&gt;&#13;&#10;&lt;/resources&gt;}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LAUNCH_CONFIGURATION_BUILD_SCOPE" value="${projects:cah}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/cah/src/net/socialgamer/cah/UpdateJsConstants.cmd}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="incremental,auto,"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${project_loc}"/>
</launchConfiguration>

View File

@ -5,6 +5,34 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>auto,full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/Update Handlers [Builder].launch</value>
</dictionary>
<dictionary>
<key>incclean</key>
<value>true</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.ui.externaltools.ExternalToolBuilder</name>
<triggers>auto,full,incremental,</triggers>
<arguments>
<dictionary>
<key>LaunchConfigHandle</key>
<value>&lt;project&gt;/.externalToolBuilders/Update JS Constants [Builder].launch</value>
</dictionary>
<dictionary>
<key>incclean</key>
<value>true</value>
</dictionary>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
<arguments>

View File

@ -1,8 +1,12 @@
#Fri Dec 09 20:46:02 PST 2011
#Thu Jan 12 19:45:21 PST 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6

View File

@ -10,6 +10,7 @@
<script type="text/javascript" src="js/cah.js"></script>
<%-- cah must be first, ajax must be before app. app probably has to be last. --%>
<%-- TODO make this be dynamic with looking at the filesystem and using jquery --%>
<script type="text/javascript" src="js/cah.constants.js"></script>
<script type="text/javascript" src="js/cah.log.js"></script>
<script type="text/javascript" src="js/cah.longpoll.js"></script>
<script type="text/javascript" src="js/cah.longpoll.handlers.js"></script>

View File

@ -43,7 +43,7 @@ cah.ajax.ErrorHandlers.firstload = function(data) {
cah.ajax.after_registered = function() {
cah.log.debug("done registering");
// TODO once there are channels, this needs to specify the global channel
cah.Ajax.build("names").run();
cah.Ajax.build(cah.$.AjaxOperation.NAMES).run();
cah.longpoll.longPoll();
};

View File

@ -26,7 +26,7 @@ $(document).ready(function() {
/**
* Singleton instance for ajax utility.
*
* @type {cah.ajax.lib}
* @type {cah.Ajax}
*/
cah.Ajax.instance = new cah.Ajax();
$.ajaxSetup({

View File

@ -6,7 +6,7 @@
$(document).ready(function() {
// see if we already exist on the server so we can resume
cah.Ajax.build("firstload").run();
cah.Ajax.build(cah.$.AjaxOperation.FIRST_LOAD).run();
// TODO see if we have a stored nickname somewhere
$("#nicknameconfirm").click(nicknameconfirm_click);
@ -31,7 +31,7 @@ function nickbox_keyup(e) {
function nicknameconfirm_click(e) {
var nickname = $.trim($("#nickname").val());
cah.Ajax.build("register").withNickname(nickname).run();
cah.Ajax.build(cah.$.AjaxOperation.REGISTER).withNickname(nickname).run();
}
function chat_keyup(e) {
@ -44,12 +44,12 @@ function chat_keyup(e) {
function chatsubmit_click(e) {
var text = $.trim($("#chat").val());
// TODO when I get multiple channels working, this needs to know active and pass it
cah.Ajax.build("chat").withMessage(text).run();
cah.Ajax.build(cah.$.AjaxOperation.CHAT).withMessage(text).run();
cah.log.status("&lt;" + cah.nickname + "&gt; " + text);
$("#chat").val("");
$("#chat").focus();
}
function logout_click(e) {
cah.Ajax.build("logout").run();
cah.Ajax.build(cah.$.AjaxOperation.LOG_OUT).run();
}

View File

@ -0,0 +1,22 @@
// This file is automatically generated. Do not edit.
cah.$ = {};
cah.$.AjaxOperation = function() {
// pass
};
cah.$.AjaxOperation.prototype.dummy = undefined;
cah.$.AjaxOperation.FIRST_LOAD = "firstload";
cah.$.AjaxOperation.LOG_OUT = "logout";
cah.$.AjaxOperation.REGISTER = "register";
cah.$.AjaxOperation.CHAT = "chat";
cah.$.AjaxOperation.NAMES = "names";
cah.$.DisconnectReason = function() {
// pass
};
cah.$.DisconnectReason.prototype.dummy = undefined;
cah.$.DisconnectReason.PING_TIMEOUT = "ping_timeout";
cah.$.DisconnectReason.KICKED = "kicked";
cah.$.DisconnectReason.MANUAL = "manual";

View File

@ -22,15 +22,15 @@ cah.longpoll.EventHandlers.player_leave = function(data) {
var friendly_reason = "Leaving";
// see net.socialgamer.cah.data.User.DisconnectReason
switch (data.reason) {
case "MANUAL":
case cah.$.DisconnectReason.KICKED:
friendly_reason = "Kicked by server";
break;
case cah.$.DisconnectReason.MANUAL:
friendly_reason = "Leaving";
break;
case "PING_TIMEOUT":
case cah.$.DisconnectReason.PING_TIMEOUT:
friendly_reason = "Ping timeout";
break;
case "KICKED":
friendly_reason = "Kicked by server";
break;
}
cah.log.status(data.nickname + " has disconnected (" + friendly_reason + ").");
};

View File

@ -2,26 +2,32 @@ package net.socialgamer.cah;
public class Constants {
public enum DisconnectReason {
MANUAL, PING_TIMEOUT, KICKED
KICKED("kicked"), MANUAL("manual"), PING_TIMEOUT("ping_timeout");
private final String reason;
DisconnectReason(final String reason) {
this.reason = reason;
}
/**
* Types of messages that can be queued. The numerical value is the priority that this message
* should be delivered (lower = more important) compared to other queued messages.
*
* @author ajanata
*/
public enum MessageType {
PLAYER_EVENT(3), GAME_PLAYER_EVENT(4), CHAT(5);
private final int weight;
MessageType(final int weight) {
this.weight = weight;
@Override
public String toString() {
return reason;
}
}
public int getWeight() {
return weight;
public enum AjaxOperation {
CHAT("chat"), FIRST_LOAD("firstload"), LOG_OUT("logout"), NAMES("names"), REGISTER("register");
private final String op;
AjaxOperation(final String op) {
this.op = op;
}
@Override
public String toString() {
return op;
}
}
}

View File

@ -0,0 +1 @@
java -cp build\classes net.socialgamer.cah.UpdateHandlerList

View File

@ -1,4 +1,4 @@
package net.socialgamer.cah.handlers;
package net.socialgamer.cah;
import java.io.File;
import java.io.PrintWriter;

View File

@ -0,0 +1 @@
java -cp build\classes net.socialgamer.cah.UpdateJsConstants

View File

@ -0,0 +1,76 @@
package net.socialgamer.cah;
import java.io.File;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
public class UpdateJsConstants {
private static final String enumHeaderFmt = "cah.$.%s = function() {\r\n // pass\r\n};\r\n";
private static final String enumDummyFmt = "cah.$.%s.prototype.dummy = undefined;\r\n";
private static final String enumValueFmt = "cah.$.%s.%s = \"%s\";\r\n";
/**
* @param args
*/
@SuppressWarnings("rawtypes")
public static void main(final String[] args) throws Exception {
final String dir = "WebContent/js/";
final File outFile = new File(dir + "cah.constants.js");
assert outFile.canWrite();
assert outFile.delete();
assert outFile.createNewFile();
final PrintWriter writer = new PrintWriter(outFile);
writer.println("// This file is automatically generated. Do not edit.");
writer.println();
writer.println("cah.$ = {};");
writer.println();
final Class[] classes = Constants.class.getClasses();
for (final Class c : classes) {
final String cName = c.getName().split("\\$")[1];
System.out.println(cName);
writer.format(enumHeaderFmt, cName);
writer.format(enumDummyFmt, cName);
final Map<String, String> values = getEnumValues(c);
for (final String key : values.keySet()) {
final String value = values.get(key);
writer.format(enumValueFmt, cName, key, value);
}
writer.println();
}
writer.flush();
writer.close();
}
@SuppressWarnings("rawtypes")
private static Map<String, String> getEnumValues(final Class enumClass)
throws IllegalArgumentException, IllegalAccessException {
if (!enumClass.isEnum()) {
throw new IllegalArgumentException(enumClass.getName() + " is not an enum");
}
final Field[] flds = enumClass.getDeclaredFields();
final HashMap<String, String> enumMap = new HashMap<String, String>();
for (final Field f : flds) {
if (f.isEnumConstant()) {
enumMap.put(f.getName(), f.get(null).toString());
}
}
return enumMap;
}
}
////Automatically generated file. Do not edit!
//
//cah.$ = {};
//
//cah.$.DisconnectReason = {};
//cah.$.DisconnectReason.prototype.dummy = undefined;
//cah.$.DisconnectReason.KICKED = "kicked";
//cah.$.DisconnectReason.MANUAL = "manual";
//cah.$.DisconnectReason.PING_TIMEOUT = "ping_timeout";

View File

@ -7,7 +7,7 @@ import java.util.Iterator;
import java.util.Map;
import net.socialgamer.cah.Constants.DisconnectReason;
import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.data.QueuedMessage.MessageType;
/**

View File

@ -5,7 +5,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.data.QueuedMessage.MessageType;
public class Game {

View File

@ -2,8 +2,6 @@ package net.socialgamer.cah.data;
import java.util.Map;
import net.socialgamer.cah.Constants.MessageType;
public class QueuedMessage implements Comparable<QueuedMessage> {
@ -31,4 +29,24 @@ public class QueuedMessage implements Comparable<QueuedMessage> {
public int compareTo(final QueuedMessage qm) {
return this.messageType.getWeight() - qm.messageType.getWeight();
}
/**
* Types of messages that can be queued. The numerical value is the priority that this message
* should be delivered (lower = more important) compared to other queued messages.
*
* @author ajanata
*/
public enum MessageType {
PLAYER_EVENT(3), GAME_PLAYER_EVENT(4), CHAT(5);
private final int weight;
MessageType(final int weight) {
this.weight = weight;
}
public int getWeight() {
return weight;
}
}
}

View File

@ -5,9 +5,10 @@ import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
import net.socialgamer.cah.data.QueuedMessage.MessageType;
import net.socialgamer.cah.data.User;
import com.google.inject.Inject;
@ -15,7 +16,7 @@ import com.google.inject.Inject;
public class ChatHandler extends Handler {
public static final String OP = "chat";
public static final String OP = AjaxOperation.CHAT.toString();
private final ConnectedUsers users;

View File

@ -5,6 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.data.User;
@ -17,7 +18,7 @@ import net.socialgamer.cah.data.User;
*/
public class FirstLoadHandler extends Handler {
public static final String OP = "firstload";
public static final String OP = AjaxOperation.FIRST_LOAD.toString();
@Override
public Map<String, Object> handle(final Map<String, String[]> parameters,

View File

@ -15,6 +15,5 @@ public class Handlers {
LIST.put(LogoutHandler.OP, LogoutHandler.class);
LIST.put(NamesHandler.OP, NamesHandler.class);
LIST.put(RegisterHandler.OP, RegisterHandler.class);
LIST.put(TestHandler.OP, TestHandler.class);
}
}

View File

@ -5,6 +5,7 @@ import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Constants.DisconnectReason;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
@ -15,7 +16,7 @@ import com.google.inject.Inject;
public class LogoutHandler extends Handler {
public final static String OP = "logout";
public final static String OP = AjaxOperation.LOG_OUT.toString();
private final ConnectedUsers users;

View File

@ -8,6 +8,7 @@ import java.util.Map;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
import net.socialgamer.cah.data.User;
@ -17,7 +18,7 @@ import com.google.inject.Inject;
public class NamesHandler extends Handler {
public static final String OP = "names";
public static final String OP = AjaxOperation.NAMES.toString();
private final ConnectedUsers users;

View File

@ -6,6 +6,7 @@ import java.util.regex.Pattern;
import javax.servlet.http.HttpSession;
import net.socialgamer.cah.Constants.AjaxOperation;
import net.socialgamer.cah.Server;
import net.socialgamer.cah.data.ConnectedUsers;
import net.socialgamer.cah.data.User;
@ -15,7 +16,7 @@ import com.google.inject.Inject;
public class RegisterHandler extends Handler {
public static final String OP = "register";
public static final String OP = AjaxOperation.REGISTER.toString();
private static final Pattern validName = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]{2,29}");

View File

@ -1,18 +0,0 @@
package net.socialgamer.cah.handlers;
import java.util.Map;
import javax.servlet.http.HttpSession;
public class TestHandler extends Handler {
public static final String OP = "test";
@Override
public Map<String, Object> handle(final Map<String, String[]> parameters,
final HttpSession session) {
// TODO Auto-generated method stub
return null;
}
}

View File

@ -1 +0,0 @@
java -cp build\classes net.socialgamer.cah.handlers.UpdateHandlerList

View File

@ -13,7 +13,7 @@ import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.HashMap;
import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.data.QueuedMessage.MessageType;
import org.junit.Before;
import org.junit.Test;