Skip to content

Commit

Permalink
move MessageType back inside QueuedMessage since the client doesn't n…
Browse files Browse the repository at this point in the history
…eed 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
  • Loading branch information
ajanata committed Jan 13, 2012
1 parent a7cc7c8 commit 8da78eb
Show file tree
Hide file tree
Showing 27 changed files with 222 additions and 58 deletions.
11 changes: 11 additions & 0 deletions .externalToolBuilders/Update Handlers [Builder].launch
Original file line number Diff line number Diff line change
@@ -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>
11 changes: 11 additions & 0 deletions .externalToolBuilders/Update JS Constants [Builder].launch
Original file line number Diff line number Diff line change
@@ -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>
28 changes: 28 additions & 0 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
6 changes: 5 additions & 1 deletion .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions WebContent/index.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion WebContent/js/cah.ajax.handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

Expand Down
2 changes: 1 addition & 1 deletion WebContent/js/cah.ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions WebContent/js/cah.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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();
}
22 changes: 22 additions & 0 deletions WebContent/js/cah.constants.js
Original file line number Diff line number Diff line change
@@ -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";

10 changes: 5 additions & 5 deletions WebContent/js/cah.longpoll.handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 + ").");
};
Expand Down
34 changes: 20 additions & 14 deletions src/net/socialgamer/cah/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

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;
}

@Override
public String toString() {
return 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);
public enum AjaxOperation {
CHAT("chat"), FIRST_LOAD("firstload"), LOG_OUT("logout"), NAMES("names"), REGISTER("register");

private final int weight;
private final String op;

MessageType(final int weight) {
this.weight = weight;
AjaxOperation(final String op) {
this.op = op;
}

public int getWeight() {
return weight;
@Override
public String toString() {
return op;
}
}
}
1 change: 1 addition & 0 deletions src/net/socialgamer/cah/UpdateHandlerList.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -cp build\classes net.socialgamer.cah.UpdateHandlerList
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.socialgamer.cah.handlers;
package net.socialgamer.cah;

import java.io.File;
import java.io.PrintWriter;
Expand Down
1 change: 1 addition & 0 deletions src/net/socialgamer/cah/UpdateJsConstants.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -cp build\classes net.socialgamer.cah.UpdateJsConstants
76 changes: 76 additions & 0 deletions src/net/socialgamer/cah/UpdateJsConstants.java
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion src/net/socialgamer/cah/data/ConnectedUsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Map;

import net.socialgamer.cah.Constants.DisconnectReason;
import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.data.QueuedMessage.MessageType;


/**
Expand Down
2 changes: 1 addition & 1 deletion src/net/socialgamer/cah/data/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.Iterator;
import java.util.List;

import net.socialgamer.cah.Constants.MessageType;
import net.socialgamer.cah.data.QueuedMessage.MessageType;


public class Game {
Expand Down
22 changes: 20 additions & 2 deletions src/net/socialgamer/cah/data/QueuedMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Map;

import net.socialgamer.cah.Constants.MessageType;


public class QueuedMessage implements Comparable<QueuedMessage> {

Expand Down Expand Up @@ -31,4 +29,24 @@ public Map<String, Object> getData() {
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;
}
}
}
5 changes: 3 additions & 2 deletions src/net/socialgamer/cah/handlers/ChatHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

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;


public class ChatHandler extends Handler {

public static final String OP = "chat";
public static final String OP = AjaxOperation.CHAT.toString();

private final ConnectedUsers users;

Expand Down
Loading

0 comments on commit 8da78eb

Please sign in to comment.