|
From: <tre...@us...> - 2008-03-24 19:53:40
|
Revision: 795
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=795&view=rev
Author: trevorolio
Date: 2008-03-24 12:53:28 -0700 (Mon, 24 Mar 2008)
Log Message:
-----------
Introducing a fast binary message encoder from Matt Kimmel at the Electric Sheep Company.
He also refactored the event code which was long in the tooth and needed a good kick.
Thanks, Matt!
Modified Paths:
--------------
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceClient.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/loadtest/MultiuserTests.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/MeasPerfClient.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Message.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Payload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/PayloadFactory.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStackFactory.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/MessageStackType.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/xml/SpaceEvent.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/plugin/test/MessageStackTest.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/plugin/test/XMLMessageEncoderTest.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/Sim.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/sim/SimMessageHandler.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/MessageProxy.java
Added Paths:
-----------
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticatedPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationFailurePayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationRequestPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationSuccessPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/HeartbeatPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LoggedOutPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LogoutPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/ProxiedSpaceEventPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/SpaceEventPayload.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoEncodable.java
maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoMessageEncoder.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/plugin/test/BinaryMementoMessageEncoderTest.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/test/
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/test/MessageBinaryMementoTest.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/message/test/PayloadBinaryMementoTest.java
maven/trunk/ogoglio-common/src/test/java/com/ogoglio/xml/test/SpaceEventBinaryMementoTest.java
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceClient.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceClient.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/client/SpaceClient.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -15,6 +15,7 @@
import com.ogoglio.client.model.*;
import com.ogoglio.message.*;
+import com.ogoglio.message.payload.*;
import com.ogoglio.message.plugin.MessageStackFactory;
import com.ogoglio.util.ArgumentUtils;
import com.ogoglio.util.Log;
@@ -230,7 +231,7 @@
private void attemptToSendSpaceEventToServer(SpaceEvent event) {
try {
- Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.SpaceEventPayload(event));
+ Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new SpaceEventPayload(event));
messageChannel.sendMessage(message);
} catch (NoSuchDestinationException e) {
e.printStackTrace();
@@ -283,7 +284,7 @@
event.setProperty(SpaceEvent.USERNAME, accountDoc.getUsername());
event.setProperty(SpaceEvent.SOURCE, accountDoc.getUsername());
event.setSplinePath(newPath);
- Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.SpaceEventPayload(event));
+ Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new SpaceEventPayload(event));
try {
messageChannel.sendMessage(message);
} catch (NoSuchDestinationException e) {
@@ -309,7 +310,7 @@
}
private void sendSpaceEvent(SpaceEvent event) {
- Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.SpaceEventPayload(event));
+ Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new SpaceEventPayload(event));
try {
messageChannel.sendMessage(message);
} catch (NoSuchDestinationException e) {
@@ -349,8 +350,8 @@
}
private void handleMessageFromService(Message message) {
- if (message.getPayload() instanceof PayloadFactory.SpaceEventPayload) {
- SpaceEvent event = ((PayloadFactory.SpaceEventPayload) message.getPayload()).getSpaceEvent();
+ if (message.getPayload() instanceof SpaceEventPayload) {
+ SpaceEvent event = ((SpaceEventPayload) message.getPayload()).getSpaceEvent();
if (SpaceEvent.ADD_USER_EVENT.equals(event.getName())) {
BodyConfigurationDocument bodyConfigDoc = event.getBodyConfigurationDocument();
BodyConfiguration bodyConfig = null;
@@ -667,7 +668,7 @@
} else {
Log.warn("Client received (and ignored) event: " + event);
}
- } else if (message.getPayload() instanceof PayloadFactory.HeartbeatPayload) {
+ } else if (message.getPayload() instanceof HeartbeatPayload) {
//ignore it
} else {
Log.error("Client received (and ignored) message: " + message);
@@ -693,12 +694,12 @@
public void handleMessage(Message message, TCPChannel sourceChannel) throws NoSuchDestinationException {
synchronized (this) {
if (waiting) {
- if (message.getPayload() instanceof PayloadFactory.AuthenticationSuccessPayload) {
+ if (message.getPayload() instanceof AuthenticationSuccessPayload) {
authStatus = SUCCESS_STATUS;
return;
- } else if (message.getPayload() instanceof PayloadFactory.AuthenticationFailurePayload) {
+ } else if (message.getPayload() instanceof AuthenticationFailurePayload) {
authStatus = FAILED_STATUS;
- errorMessage = ((PayloadFactory.AuthenticationFailurePayload) message.getPayload()).getMessage();
+ errorMessage = ((AuthenticationFailurePayload) message.getPayload()).getMessage();
return;
}
waitingMessages.add(message);
@@ -721,7 +722,7 @@
void authenticate(String authCookie) throws IOException {
try {
- Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.AuthenticationRequestPayload(authCookie, space.getSpaceID()));
+ Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new AuthenticationRequestPayload(authCookie, space.getSpaceID()));
messageChannel.sendMessage(message);
} catch (NoSuchDestinationException e) {
throw new IOException("Could not send authentication message");
@@ -737,7 +738,7 @@
}
public void heartbeat() throws IOException {
- Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.HeartbeatPayload());
+ Message message = new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new HeartbeatPayload());
try {
messageChannel.sendMessage(message);
} catch (NoSuchDestinationException e) {
@@ -794,7 +795,7 @@
cleanedUp = true;
try {
Log.info("In space client cleanup, about to send logout message:" + accountDoc.getUsername());
- messageChannel.sendMessage(new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new PayloadFactory.LogoutPayload(accountDoc.getUsername())));
+ messageChannel.sendMessage(new Message(messageChannel.getLocalLocator(), messageChannel.getRemoteLocator(), space.getSpaceID(), new LogoutPayload(accountDoc.getUsername())));
Log.info("Success sending logout message");
} catch (Exception e) {
Log.error("Problem sending logout message:" + e.getMessage());
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/loadtest/MultiuserTests.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/loadtest/MultiuserTests.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/loadtest/MultiuserTests.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -46,6 +46,7 @@
}
public static void main(String[] args) {
+ org.apache.log4j.BasicConfigurator.configure();
if (args.length != 4) {
Log.error("usage: ... spaceID serviceURI numRobots duration");
return;
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/MeasPerfClient.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/MeasPerfClient.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/MeasPerfClient.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -1,6 +1,7 @@
package com.ogoglio.message;
import com.ogoglio.message.plugin.MessageStack;
+import com.ogoglio.message.payload.HeartbeatPayload;
import java.util.*;
@@ -68,7 +69,7 @@
try {
Long me = new Long(Thread.currentThread().getId());
while (true) {
- p = new PayloadFactory.HeartbeatPayload();
+ p = new HeartbeatPayload();
long id = r.nextLong();
m = new Message(channel.getLocalLocator(), channel.getRemoteLocator(), id, p);
timeMap.put(new Long(id), new Long(System.currentTimeMillis()));
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Message.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Message.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Message.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -17,10 +17,15 @@
import nanoxml.XMLElement;
import com.ogoglio.message.proto.Locator;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
import com.ogoglio.util.ArgumentUtils;
+import com.ogoglio.util.Log;
-public class Message {
+import java.io.*;
+public class Message implements BinaryMementoEncodable {
+
public static final String MESSAGE = "Message";
private static final String PROXY = "proxy";
@@ -41,6 +46,10 @@
private long spaceID = -1;
+ // Default constructor required for memento pattern
+ public Message() {
+ }
+
public Message(Locator origin, Locator proxy, Locator destination, long spaceID, Payload payload) {
ArgumentUtils.assertNotNull(origin);
this.origin = origin;
@@ -131,4 +140,95 @@
public void setOrigin(Locator origin) {
this.origin = origin;
}
+
+ public String getMementoClassID() {
+ return MESSAGE;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // Before doing anything else, ensure that our payload is encodable. If not, throw
+ // an exception.
+ if (!(payload instanceof BinaryMementoEncodable)) {
+ throw new MessagingException("Attempted to get memento for Message with non-mementoable payload: " + payload);
+ }
+
+ // Create a ByteArrayOutputStream, wrap it in a DataOutputStream and serialize into it.
+ // TODO: Decrease number of buffer allocs/copies?
+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+ DataOutputStream dataOutput = new DataOutputStream(byteStream);
+
+ // Write data
+ try {
+ dataOutput.writeUTF(origin.toString());
+ // If there's a proxy, write a boolean true and the proxy;
+ // otherwise, write a boolean false
+ if (proxy != null) {
+ dataOutput.writeBoolean(true);
+ dataOutput.writeUTF(proxy.toString());
+ } else {
+ dataOutput.writeBoolean(false);
+ }
+ dataOutput.writeUTF(destination.toString());
+ dataOutput.writeLong(spaceID);
+ BinaryMementoEncodable encodablePayload = (BinaryMementoEncodable) payload;
+ byte[] payloadMemento = encodablePayload.getBinaryMemento();
+ dataOutput.writeUTF(encodablePayload.getMementoClassID());
+ dataOutput.writeInt(payloadMemento.length);
+ dataOutput.write(payloadMemento);
+ dataOutput.flush();
+ } catch (IOException e) {
+ Log.error("Encountered IOException while encoding message: " + e);
+ throw new MessagingException("Encountered IOException while encoding message", e);
+ }
+
+ return byteStream.toByteArray();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Create a ByteArrayInputStream, wrap it in a DataInputStream, and deserialize from it.
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(memento);
+ DataInputStream dataInput = new DataInputStream(byteStream);
+
+ // Read data
+ String originString;
+ String proxyString;
+ String destinationString;
+ long incomingSpaceID;
+ String payloadClassID;
+ byte[] payloadMemento;
+ try {
+ originString = dataInput.readUTF();
+ boolean containsProxy = dataInput.readBoolean();
+ if (containsProxy) {
+ proxyString = dataInput.readUTF();
+ } else {
+ proxyString = null;
+ }
+ destinationString = dataInput.readUTF();
+ incomingSpaceID = dataInput.readLong();
+ payloadClassID = dataInput.readUTF();
+ int payloadMementoSize = dataInput.readInt();
+ payloadMemento = new byte[payloadMementoSize];
+ dataInput.read(payloadMemento);
+ } catch (IOException e) {
+ Log.error("Encountered IOException while decoding message: " + e);
+ throw new MessagingException("Encountered IOException while decoding message", e);
+ }
+
+ // Decode payload
+ Payload incomingPayload = PayloadFactory.createPayloadFromMementoID(payloadClassID);
+
+ ((BinaryMementoEncodable) incomingPayload).setBinaryMemento(payloadMemento);
+
+ // Now that everything's in place, populate the object
+ setOrigin(new Locator(originString));
+ if (proxyString != null) {
+ setProxy(new Locator(proxyString));
+ } else {
+ setProxy(null);
+ }
+ setDestination(new Locator(destinationString));
+ setSpaceID(incomingSpaceID);
+ payload = incomingPayload;
+ }
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Payload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Payload.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/Payload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -15,30 +15,34 @@
package com.ogoglio.message;
import nanoxml.XMLElement;
+import com.ogoglio.message.payload.AuthenticatedPayload;
+import com.ogoglio.util.Log;
-public class Payload {
+public abstract class Payload {
public static final String PAYLOAD = "Payload";
- private XMLElement data = null;
-
- public Payload(XMLElement data) {
- if (data == null) {
- throw new IllegalArgumentException("bad data: " + data);
- }
- if (data.getName().equals(PAYLOAD)) {
- throw new IllegalStateException("Cannot nest payloads: " + data);
- }
- this.data = data;
+ public Payload() {
+ // Does nothing for now
}
+
+ /**
+ * Parse the given Payload XML into variables in the implementing class.
+ *
+ * @param data XML data to parse
+ */
+ public abstract void parseData(XMLElement data);
- public XMLElement getData() {
- return data;
- }
+ /**
+ * Create Payload XML data on the fly from variables in the implementing class.
+ *
+ * @return XML data representing data contained in class
+ */
+ public abstract XMLElement getData();
public XMLElement toElement() {
XMLElement element = new XMLElement(PAYLOAD);
- element.addChild(data);
+ element.addChild(getData());
return element;
}
Modified: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/PayloadFactory.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/PayloadFactory.java 2008-03-21 22:44:46 UTC (rev 794)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/PayloadFactory.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -14,7 +14,9 @@
package com.ogoglio.message;
-import com.ogoglio.xml.SpaceEvent;
+import com.ogoglio.message.payload.*;
+import com.ogoglio.message.plugin.MessagingException;
+import com.ogoglio.util.Log;
import nanoxml.XMLElement;
@@ -55,230 +57,40 @@
}
}
- public static class HeartbeatPayload extends Payload {
- public static final String NAME = "HeartbeatPayload";
-
- public HeartbeatPayload() {
- super(new XMLElement(NAME));
+ /**
+ * Instantiate a subclass of Payload based on the supplied payloadClassID (as returned
+ * by BinaryMementoEncodable.getMementoClassID), using the default constructor.
+ *
+ * TODO: Make this a dynamic repository so new class IDs can be added without code change.
+ *
+ * @param payloadClassID Memento class ID of Payload to instantiate
+ * @return An instance of the Payload subclass corresponding to payloadClassID.
+ * @throws com.ogoglio.message.plugin.MessagingException if the payloadClassID is unrecognized.
+ */
+ public static Payload createPayloadFromMementoID(String payloadClassID) throws MessagingException {
+ Payload payload;
+ if (payloadClassID.equals(AuthenticatedPayload.NAME)) {
+ payload = new AuthenticatedPayload();
+ } else if (payloadClassID.equals(AuthenticationFailurePayload.NAME)) {
+ payload = new AuthenticationFailurePayload();
+ } else if (payloadClassID.equals(AuthenticationRequestPayload.NAME)) {
+ payload = new AuthenticationRequestPayload();
+ } else if (payloadClassID.equals(AuthenticationSuccessPayload.NAME)) {
+ payload = new AuthenticationSuccessPayload();
+ } else if (payloadClassID.equals(HeartbeatPayload.NAME)) {
+ payload = new HeartbeatPayload();
+ } else if (payloadClassID.equals(LoggedOutPayload.NAME)) {
+ payload = new LoggedOutPayload();
+ } else if (payloadClassID.equals(LogoutPayload.NAME)) {
+ payload = new LogoutPayload();
+ } else if (payloadClassID.equals(ProxiedSpaceEventPayload.NAME)) {
+ payload = new ProxiedSpaceEventPayload();
+ } else if (payloadClassID.equals(SpaceEventPayload.NAME)) {
+ payload = new SpaceEventPayload();
+ } else {
+ Log.error("Received Message with unknown class ID: " + payloadClassID);
+ throw new MessagingException("Received Message with unknown class ID: " + payloadClassID);
}
-
- public HeartbeatPayload(XMLElement data) {
- super(data);
- }
+ return payload;
}
-
- public static class SpaceEventPayload extends Payload {
- public static final String NAME = "SpaceEventPayload";
-
- public SpaceEventPayload(SpaceEvent spaceEvent) {
- super(create(spaceEvent));
- }
-
- public SpaceEventPayload(XMLElement data) {
- super(data);
- }
-
- public SpaceEvent getSpaceEvent() {
- return new SpaceEvent(getData().getChild(SpaceEvent.SPACE_EVENT));
- }
-
- public static XMLElement create(SpaceEvent spaceEvent) {
- XMLElement data = new XMLElement(NAME);
- data.addChild(spaceEvent.toElement());
- return data;
- }
-
- public void setSpaceEvent(SpaceEvent event) {
- getData().removeChildren(SpaceEvent.SPACE_EVENT);
- getData().addChild(event.toElement());
- }
- }
-
- public static class ProxiedSpaceEventPayload extends Payload {
- public static final String NAME = "ProxiedSpaceEventPayload";
-
- public static final String USERNAME = "username";
-
- public ProxiedSpaceEventPayload(String username, SpaceEvent spaceEvent) {
- super(create(username, spaceEvent));
- }
-
- public ProxiedSpaceEventPayload(XMLElement data) {
- super(data);
- }
-
- public SpaceEvent getSpaceEvent() {
- return new SpaceEvent(getData().getChild(SpaceEvent.SPACE_EVENT));
- }
-
- public static XMLElement create(String username, SpaceEvent spaceEvent) {
- XMLElement data = new XMLElement(NAME);
- data.setAttribute(USERNAME, username);
- data.addChild(spaceEvent.toElement());
- return data;
- }
-
- public String getUsername() {
- return getData().getStringAttribute(USERNAME);
- }
-
- public void setSpaceEvent(SpaceEvent event) {
- getData().removeChildren(SpaceEvent.SPACE_EVENT);
- getData().addChild(event.toElement());
- }
- }
-
- public static class LogoutPayload extends Payload {
- public static final String NAME = "Logout";
-
- public static final String USERNAME = "username";
-
- public LogoutPayload() {
- super(create(null));
- }
-
- public LogoutPayload(String username) {
- super(create(username));
- }
-
- public LogoutPayload(XMLElement data) {
- super(data);
- }
-
- public static XMLElement create(String username) {
- XMLElement data = new XMLElement(NAME);
- if (username != null) {
- data.setAttribute(USERNAME, username);
- }
- return data;
- }
- }
-
- public static class AuthenticationRequestPayload extends Payload {
- public static final String NAME = "AuthenticationRequest";
-
- public static final String LOGIN_COOKIE = "loginCookie";
-
- public static final String SPACE_ID = "spaceID";
-
- public AuthenticationRequestPayload(XMLElement element) {
- super(element);
- }
-
- public AuthenticationRequestPayload(String loginCookie, long spaceID) {
- super(create(loginCookie, spaceID));
- }
-
- public String getLoginCookie() {
- return getData().getStringAttribute(LOGIN_COOKIE);
- }
-
- public long getSpaceID() {
- return getData().getLongAttribute(SPACE_ID);
- }
-
- public static XMLElement create(String loginCookie, long spaceID) {
- XMLElement element = new XMLElement(NAME);
- element.setAttribute(LOGIN_COOKIE, loginCookie);
- element.setAttribute(SPACE_ID, spaceID);
- return element;
- }
- }
-
- public static class AuthenticationSuccessPayload extends Payload {
- public static final String NAME = "AuthenticationSuccess";
-
- public static final String USERNAME = "username";
-
- public AuthenticationSuccessPayload(String username) {
- super(create(username));
- }
-
- public AuthenticationSuccessPayload(XMLElement data) {
- super(data);
- }
-
- public String getUsername() {
- return getData().getStringAttribute(USERNAME);
- }
-
- public static XMLElement create(String username) {
- XMLElement data = new XMLElement(NAME);
- data.setAttribute(USERNAME, username);
- return data;
- }
- }
-
- public static class AuthenticationFailurePayload extends Payload {
- public static final String NAME = "AuthenticationFailure";
- public static final String MESSAGE = "message";
-
- public AuthenticationFailurePayload(String message) {
- super(create(message));
- }
-
- public AuthenticationFailurePayload(XMLElement element) {
- super(element);
- }
-
- private static XMLElement create(String message) {
- XMLElement element = new XMLElement(NAME);
- element.setAttribute(MESSAGE, message);
- return element;
- }
-
- public String getMessage() {
- return getData().getStringAttribute(MESSAGE);
- }
- }
-
- // This is sent to the space simulator when a client is authenticated
- public static class AuthenticatedPayload extends Payload {
- public static final String NAME = "Authenticated";
-
- public static final String USERNAME = "username";
-
- public AuthenticatedPayload(String username) {
- super(create(username));
- }
-
- public AuthenticatedPayload(XMLElement data) {
- super(data);
- }
-
- public String getUsername() {
- return getData().getStringAttribute(USERNAME);
- }
-
- public static XMLElement create(String username) {
- XMLElement data = new XMLElement(NAME);
- data.setAttribute(USERNAME, username);
- return data;
- }
- }
-
- // This is sent to the space simulator when a client logs out
- public static class LoggedOutPayload extends Payload {
- public static final String NAME = "LoggedOut";
-
- public static final String USERNAME = "username";
-
- public LoggedOutPayload(String username) {
- super(create(username));
- }
-
- public LoggedOutPayload(XMLElement data) {
- super(data);
- }
-
- public String getUsername() {
- return getData().getStringAttribute(USERNAME);
- }
-
- public static XMLElement create(String username) {
- XMLElement data = new XMLElement(NAME);
- data.setAttribute(USERNAME, username);
- return data;
- }
- }
}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticatedPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticatedPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticatedPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,77 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+// This is sent to the space simulator when a client is authenticated
+public class AuthenticatedPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "Authenticated";
+
+ public static final String USERNAME = "username";
+
+ protected String username = "";
+
+ public AuthenticatedPayload() {
+ }
+
+ public AuthenticatedPayload(String username) {
+ this.username = username;
+ }
+
+ public AuthenticatedPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public static XMLElement create(String username) {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ return data;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The memento is just the username as bytes
+ return getUsername().getBytes();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Just create a string from the memento
+ setUsername(new String(memento));
+ }
+
+ public void parseData(XMLElement data) {
+ username = data.getStringAttribute(USERNAME);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationFailurePayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationFailurePayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationFailurePayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,69 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+public class AuthenticationFailurePayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "AuthenticationFailure";
+ public static final String MESSAGE = "message";
+
+ protected String message = "";
+
+ public AuthenticationFailurePayload() {
+ }
+
+ public AuthenticationFailurePayload(String message) {
+ this.message = message;
+ }
+
+ public AuthenticationFailurePayload(XMLElement element) {
+ parseData(element);
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public void setMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The memento is just the message as bytes
+ return getMessage().getBytes();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Just create a string from the memento
+ setMessage(new String(memento));
+ }
+
+ public void parseData(XMLElement data) {
+ message = data.getStringAttribute(MESSAGE);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(MESSAGE, message);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationRequestPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationRequestPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationRequestPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,118 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import com.ogoglio.util.Log;
+import nanoxml.XMLElement;
+
+import java.io.*;
+
+/**
+ * @author Matt Kimmel
+*/
+public class AuthenticationRequestPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "AuthenticationRequest";
+
+ public static final String LOGIN_COOKIE = "loginCookie";
+
+ public static final String SPACE_ID = "spaceID";
+
+ protected String loginCookie = "invalid";
+ protected long spaceID = -1;
+
+ public AuthenticationRequestPayload() {
+ }
+
+ public AuthenticationRequestPayload(XMLElement element) {
+ parseData(element);
+ }
+
+ public AuthenticationRequestPayload(String loginCookie, long spaceID) {
+ this.loginCookie = loginCookie;
+ this.spaceID = spaceID;
+ }
+
+ public String getLoginCookie() {
+ return loginCookie;
+ }
+
+ public void setLoginCookie(String loginCookie) {
+ this.loginCookie = loginCookie;
+ }
+
+ public long getSpaceID() {
+ return spaceID;
+ }
+
+ public void setSpaceID(long spaceID) {
+ this.spaceID = spaceID;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // Create a ByteArrayOutputStream and wrap it in a DataOutputStream
+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+ DataOutputStream dataOutput = new DataOutputStream(byteStream);
+
+ // Write the data
+ try {
+ dataOutput.writeLong(getSpaceID());
+ dataOutput.writeUTF(getLoginCookie());
+ dataOutput.flush();
+ } catch (IOException e) {
+ Log.error("IOException while encoding AuthenticationRequestPayload: " + e);
+ throw new MessagingException("IOException while encoding AuthenticationRequestPayload", e);
+ }
+
+ return byteStream.toByteArray();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Create a ByteArrayInputStream and wrap it in a DataInputStream
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(memento);
+ DataInputStream dataInput = new DataInputStream(byteStream);
+
+ // Read the data
+ long spaceID;
+ String loginCookie;
+ try {
+ spaceID = dataInput.readLong();
+ loginCookie = dataInput.readUTF();
+ } catch (IOException e) {
+ Log.error("IOException while decoding AuthenticationRequestPayload: " + e);
+ throw new MessagingException("IOException while decoding AuthenticationRequestPayload", e);
+ }
+
+ setSpaceID(spaceID);
+ setLoginCookie(loginCookie);
+ }
+
+ public void parseData(XMLElement data) {
+ loginCookie = data.getStringAttribute(LOGIN_COOKIE);
+ spaceID = data.getLongAttribute(SPACE_ID);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(LOGIN_COOKIE, loginCookie);
+ data.setAttribute(SPACE_ID, spaceID);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationSuccessPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationSuccessPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/AuthenticationSuccessPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,70 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+public class AuthenticationSuccessPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "AuthenticationSuccess";
+
+ public static final String USERNAME = "username";
+
+ protected String username = "";
+
+ public AuthenticationSuccessPayload() {
+ }
+
+ public AuthenticationSuccessPayload(String username) {
+ this.username = username;
+ }
+
+ public AuthenticationSuccessPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The memento is just the username as bytes
+ return getUsername().getBytes();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Just create a string from the memento
+ setUsername(new String(memento));
+ }
+
+ public void parseData(XMLElement data) {
+ username = data.getStringAttribute(USERNAME);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/HeartbeatPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/HeartbeatPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/HeartbeatPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,52 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+public class HeartbeatPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "HeartbeatPayload";
+
+ public HeartbeatPayload() {
+ }
+
+ public HeartbeatPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // Nothing to encode!
+ return new byte[0];
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Nothing to decode.
+ }
+
+ public void parseData(XMLElement data) {
+ // Does nothing
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LoggedOutPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LoggedOutPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LoggedOutPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,77 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+// This is sent to the space simulator when a client logs out
+public class LoggedOutPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "LoggedOut";
+
+ public static final String USERNAME = "username";
+
+ protected String username = "";
+
+ public LoggedOutPayload() {
+ }
+
+ public LoggedOutPayload(String username) {
+ this.username = username;
+ }
+
+ public LoggedOutPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public static XMLElement create(String username) {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ return data;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The memento is just the username as bytes
+ return getUsername().getBytes();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Just create a string from the memento
+ setUsername(new String(memento));
+ }
+
+ public void parseData(XMLElement data) {
+ username = data.getStringAttribute(USERNAME);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LogoutPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LogoutPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/LogoutPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,84 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import nanoxml.XMLElement;
+
+public class LogoutPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "Logout";
+
+ public static final String USERNAME = "username";
+
+ protected String username = null;
+
+ public LogoutPayload() {
+ }
+
+ public LogoutPayload(String username) {
+ this.username = username;
+ }
+
+ public LogoutPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The memento is a single zero byte if no username;
+ // otherwise it's the username converted to bytes
+ if (getUsername() == null) {
+ byte[] memento = new byte[1];
+ memento[0] = 0;
+ return memento;
+ } else {
+ return getUsername().getBytes();
+ }
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // If the memento starts with a zero, there's no username.
+ // Otherwise decode the bytes into a string.
+ if (memento[0] == 0) {
+ setUsername(null);
+ } else {
+ setUsername(new String(memento));
+ }
+ }
+
+ public void parseData(XMLElement data) {
+ username = data.getStringAttribute(USERNAME, null);
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ if (username != null) {
+ data.setAttribute(USERNAME, username);
+ }
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/ProxiedSpaceEventPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/ProxiedSpaceEventPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/ProxiedSpaceEventPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,115 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import com.ogoglio.xml.SpaceEvent;
+import com.ogoglio.util.Log;
+import nanoxml.XMLElement;
+
+import java.io.*;
+
+public class ProxiedSpaceEventPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "ProxiedSpaceEventPayload";
+
+ public static final String USERNAME = "username";
+
+ protected String username = "";
+ protected SpaceEvent event = null;
+
+ public ProxiedSpaceEventPayload() {
+ }
+
+ public ProxiedSpaceEventPayload(String username, SpaceEvent spaceEvent) {
+ this.username = username;
+ this.event = spaceEvent;
+ }
+
+ public ProxiedSpaceEventPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public SpaceEvent getSpaceEvent() {
+ return event;
+ }
+
+ public String getUsername() {
+ return username;
+ }
+
+ public void setUsername(String username) {
+ this.username = username;
+ }
+
+ public void setSpaceEvent(SpaceEvent event) {
+ this.event = event;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // Create a ByteArrayOutputStream and wrap it in a DataOutputStream
+ ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
+ DataOutputStream dataOutput = new DataOutputStream(byteStream);
+
+ // Write the data
+ try {
+ dataOutput.writeUTF(getUsername());
+ getSpaceEvent().encodeMemento(dataOutput);
+ dataOutput.flush();
+ } catch (IOException e) {
+ Log.error("IOException while encoding ProxiedSpaceEventPayload: " + e);
+ throw new MessagingException("IOException while encoding ProxiedSpaceEventPayload", e);
+ }
+
+ return byteStream.toByteArray();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Create a ByteArrayInputStream and wrap it in a DataInputStream
+ ByteArrayInputStream byteStream = new ByteArrayInputStream(memento);
+ DataInputStream dataInput = new DataInputStream(byteStream);
+
+ // Read the data
+ String username;
+ SpaceEvent event;
+ try {
+ username = dataInput.readUTF();
+ event = new SpaceEvent();
+ event.decodeMemento(dataInput);
+ } catch (IOException e) {
+ Log.error("IOException while decoding ProxiedSpaceEventPayload: " + e);
+ throw new MessagingException("IOException while decoding ProxiedSpaceEventPayload", e);
+ }
+
+ setUsername(username);
+ setSpaceEvent(event);
+ }
+
+ public void parseData(XMLElement data) {
+ username = data.getStringAttribute(USERNAME);
+ event = new SpaceEvent(data.getChild(SpaceEvent.SPACE_EVENT));
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.setAttribute(USERNAME, username);
+ data.addChild(event.toElement());
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/SpaceEventPayload.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/SpaceEventPayload.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/payload/SpaceEventPayload.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,71 @@
+/* Copyright 2007 Transmutable (http://transmutable.com/)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.payload;
+
+import com.ogoglio.message.Payload;
+import com.ogoglio.message.plugin.BinaryMementoEncodable;
+import com.ogoglio.message.plugin.MessagingException;
+import com.ogoglio.xml.SpaceEvent;
+import nanoxml.XMLElement;
+
+public class SpaceEventPayload extends Payload implements BinaryMementoEncodable {
+ public static final String NAME = "SpaceEventPayload";
+
+ protected SpaceEvent event = null;
+
+ public SpaceEventPayload() {
+ }
+
+ public SpaceEventPayload(SpaceEvent spaceEvent) {
+ this.event = spaceEvent;
+ }
+
+ public SpaceEventPayload(XMLElement data) {
+ parseData(data);
+ }
+
+ public SpaceEvent getSpaceEvent() {
+ return event;
+ }
+
+ public void setSpaceEvent(SpaceEvent event) {
+ this.event = event;
+ }
+
+ public String getMementoClassID() {
+ return NAME;
+ }
+
+ public byte[] getBinaryMemento() throws MessagingException {
+ // The only data we have is the SpaceEvent, so just pass back the SpaceEvent's memento
+ return getSpaceEvent().getBinaryMemento();
+ }
+
+ public void setBinaryMemento(byte[] memento) throws MessagingException {
+ // Just create a new SpaceEvent with the memento data
+ SpaceEvent newEvent = new SpaceEvent();
+ newEvent.setBinaryMemento(memento);
+ setSpaceEvent(newEvent);
+ }
+
+ public void parseData(XMLElement data) {
+ event = new SpaceEvent(data.getChild(SpaceEvent.SPACE_EVENT));
+ }
+
+ public XMLElement getData() {
+ XMLElement data = new XMLElement(NAME);
+ data.addChild(event.toElement());
+ return data;
+ }
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoEncodable.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoEncodable.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoEncodable.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,56 @@
+/* Copyright 2008, The Electric Sheep Company, Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.plugin;
+
+/**
+ * This interface should be implemented by any class that wishes to be encoded/decoded by the
+ * BinaryMementoMessageEncoder. In addition to implementing this interface, the class must
+ * have a default constructor.
+ *
+ * As a note, this is the first encoder to use the Memento Pattern. In the future, we may want
+ * to implement a "Grand Memento" encoding system that uses a single Memento interface for multiple
+ * encodings. Or we may just want to use Memento for specific encodings where applicable.
+ *
+ * @author Matt Kimmel
+ */
+public interface BinaryMementoEncodable {
+ /**
+ * Return a string used to uniquely identify the implementing class. This is used when decoding
+ * a memento, to determine which class to instantiate. Note that this string must be unique within
+ * the app.
+ *
+ * @return String uniquely identifying the implementing class.
+ */
+ public String getMementoClassID();
+
+ /**
+ * Get a binary-encoded memento (as a byte array) that reflects the current state of the object.
+ * If the object contains other objects, it is responsible for encoding those objects into the
+ * memento, even for contained objects that implement this interface.
+ *
+ * @return Byte array containing the binary-encoded current state of the object.
+ * @throws MessagingException if an error was encountered while creating the memento.
+ */
+ public byte[] getBinaryMemento() throws MessagingException;
+
+ /**
+ * Set the state of the object to the state encoded in the provided binary-encoded memento.
+ * This includes the states of any contained objects, the states of which should have been
+ * encoded into the memento returned by getBinaryMemento.
+ *
+ * @param memento Binary-encoded memento from which to set the object's state.
+ * @throws MessagingException if an error was encountered while decoding the memento.
+ */
+ public void setBinaryMemento(byte[] memento) throws MessagingException;
+}
Added: maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoMessageEncoder.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoMessageEncoder.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/com/ogoglio/message/plugin/BinaryMementoMessageEncoder.java 2008-03-24 19:53:28 UTC (rev 795)
@@ -0,0 +1,48 @@
+/* Copyright 2008, The Electric Sheep Company, Inc.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. */
+package com.ogoglio.message.plugin;
+
+import com.ogoglio.message.Message;
+import com.ogoglio.util.Log;
+
+/**
+ * This MessageEncoder plug-in class uses the memento pattern, as expressed by the BinaryMementoEncodable
+ * interface, to encode and decode messages in a binary format. This has the trade-off of reducing the
+ * encode/decode time (in particular, the time spent writing and parsing XML) versus potentially being
+ * less friendly to some firewalls.
+ *
+ * @author Matt Kimmel...
[truncated message content] |