[jboss-cvs] JBoss Messaging SVN: r3487 - in trunk: src/main/org/jboss/messaging/core/remoting/integration and 1 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 12 10:41:40 EST 2007
Author: jmesnil
Date: 2007-12-12 10:41:40 -0500 (Wed, 12 Dec 2007)
New Revision: 3487
Modified:
trunk/src/main/org/jboss/messaging/core/remoting/codec/AbstractPacketCodec.java
trunk/src/main/org/jboss/messaging/core/remoting/integration/MinaInspector.java
trunk/tests/src/org/jboss/messaging/core/remoting/wireformat/test/unit/PacketTypeTest.java
Log:
* reduced size of packet sent on the wire by using a single NULL byte to represent targetID and callbackID if they are not set instead of a literal String "ID_NOT_SET"
Modified: trunk/src/main/org/jboss/messaging/core/remoting/codec/AbstractPacketCodec.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/codec/AbstractPacketCodec.java 2007-12-12 15:00:23 UTC (rev 3486)
+++ trunk/src/main/org/jboss/messaging/core/remoting/codec/AbstractPacketCodec.java 2007-12-12 15:41:40 UTC (rev 3487)
@@ -11,6 +11,7 @@
import static org.jboss.messaging.core.remoting.codec.DecoderStatus.NEED_DATA;
import static org.jboss.messaging.core.remoting.codec.DecoderStatus.NOT_OK;
import static org.jboss.messaging.core.remoting.codec.DecoderStatus.OK;
+import static org.jboss.messaging.core.remoting.wireformat.AbstractPacket.NO_ID_SET;
import static org.jboss.messaging.core.remoting.wireformat.AbstractPacket.NO_VERSION_SET;
import java.io.ByteArrayInputStream;
@@ -39,12 +40,15 @@
public static final int LONG_LENGTH = 8;
- private static final Logger log = Logger.getLogger(AbstractPacketCodec.class);
+ private static final Logger log = Logger
+ .getLogger(AbstractPacketCodec.class);
// Attributes ----------------------------------------------------
private PacketType type;
+ // Constructors --------------------------------------------------
+
protected AbstractPacketCodec(PacketType type)
{
assert type != null;
@@ -54,22 +58,31 @@
// Public --------------------------------------------------------
- public void encode(P packet, RemotingBuffer buf)
- throws Exception
+ public void encode(P packet, RemotingBuffer buf) throws Exception
{
assert packet != null;
assert buf != null;
-
+
byte version = packet.getVersion();
if (version == NO_VERSION_SET)
{
throw new IllegalStateException("packet must be versioned: " + packet);
}
-
+
long correlationID = packet.getCorrelationID();
+ // to optimize the size of the packets, if the targetID
+ // or the callbackID are not set, they are encoded as null
+ // Strings and will be correctly reset in decode(RemotingBuffer) method
String targetID = packet.getTargetID();
+ if (NO_ID_SET.equals(targetID))
+ {
+ targetID = null;
+ }
String callbackID = packet.getCallbackID();
-
+ if (NO_ID_SET.equals(callbackID))
+ {
+ callbackID = null;
+ }
int headerLength = LONG_LENGTH + sizeof(targetID) + sizeof(callbackID);
buf.put(packet.getType().byteValue());
@@ -82,6 +95,18 @@
encodeBody(packet, buf);
}
+ public static int sizeof(String nullableString)
+ {
+ if (nullableString == null)
+ {
+ return 1; // NULL_STRING byte
+ } else
+ {
+ return nullableString.getBytes().length + 2;// NOT_NULL_STRING +
+ // NULL_BYTE
+ }
+ }
+
// MessageDecoder implementation ---------------------------------
public DecoderStatus decodable(RemotingBuffer buffer)
@@ -164,33 +189,27 @@
return null;
}
packet.setVersion(version);
+ if (targetID == null)
+ targetID = NO_ID_SET;
packet.setTargetID(targetID);
packet.setCorrelationID(correlationID);
+ if (callbackID == null)
+ callbackID = NO_ID_SET;
packet.setCallbackID(callbackID);
-
+
return packet;
}
-
+
+ // Protected -----------------------------------------------------
+
protected abstract void encodeBody(P packet, RemotingBuffer buf)
- throws Exception;
+ throws Exception;
- protected abstract P decodeBody(RemotingBuffer buffer)
- throws Exception;
+ protected abstract P decodeBody(RemotingBuffer buffer) throws Exception;
- public static int sizeof(String nullableString)
+ protected static byte[] encode(JBossDestination destination)
+ throws Exception
{
- if (nullableString == null)
- {
- return 1; // NULL_STRING byte
- } else
- {
- return nullableString.getBytes().length + 2;// NOT_NULL_STRING +
- // NULL_BYTE
- }
- }
-
- protected static byte[] encode(JBossDestination destination) throws Exception
- {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
writeDestination(new DataOutputStream(baos), destination);
baos.flush();
@@ -202,7 +221,7 @@
ByteArrayInputStream bais = new ByteArrayInputStream(b);
return readDestination(new DataInputStream(bais));
}
-
+
protected static byte[] encode(Message message) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -218,7 +237,7 @@
msg.read(new DataInputStream(bais));
return msg;
}
-
+
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
Modified: trunk/src/main/org/jboss/messaging/core/remoting/integration/MinaInspector.java
===================================================================
--- trunk/src/main/org/jboss/messaging/core/remoting/integration/MinaInspector.java 2007-12-12 15:00:23 UTC (rev 3486)
+++ trunk/src/main/org/jboss/messaging/core/remoting/integration/MinaInspector.java 2007-12-12 15:41:40 UTC (rev 3487)
@@ -7,6 +7,7 @@
package org.jboss.messaging.core.remoting.integration;
import static org.apache.mina.filter.reqres.ResponseType.WHOLE;
+import static org.jboss.messaging.core.remoting.wireformat.AbstractPacket.NO_CORRELATION_ID;
import org.apache.mina.filter.reqres.ResponseInspector;
import org.apache.mina.filter.reqres.ResponseType;
@@ -38,13 +39,10 @@
return null;
}
AbstractPacket packet = (AbstractPacket) message;
- if (packet.isRequest())
- {
+ if (packet.getCorrelationID() != NO_CORRELATION_ID)
return packet.getCorrelationID();
- } else
- {
+ else
return null;
- }
}
public ResponseType getResponseType(Object message)
Modified: trunk/tests/src/org/jboss/messaging/core/remoting/wireformat/test/unit/PacketTypeTest.java
===================================================================
--- trunk/tests/src/org/jboss/messaging/core/remoting/wireformat/test/unit/PacketTypeTest.java 2007-12-12 15:00:23 UTC (rev 3486)
+++ trunk/tests/src/org/jboss/messaging/core/remoting/wireformat/test/unit/PacketTypeTest.java 2007-12-12 15:41:40 UTC (rev 3487)
@@ -8,6 +8,7 @@
import static org.jboss.messaging.core.remoting.codec.AbstractPacketCodec.LONG_LENGTH;
import static org.jboss.messaging.core.remoting.codec.AbstractPacketCodec.sizeof;
+import static org.jboss.messaging.core.remoting.wireformat.AbstractPacket.NO_ID_SET;
import static org.jboss.messaging.core.remoting.wireformat.PacketType.MSG_ACKDELIVERIES;
import static org.jboss.messaging.core.remoting.wireformat.PacketType.MSG_ADDTEMPORARYDESTINATION;
import static org.jboss.messaging.core.remoting.wireformat.PacketType.MSG_BROWSER_RESET;
@@ -239,12 +240,27 @@
assertEquals(buffer.get(), packet.getType().byteValue());
assertEquals(buffer.get(), packet.getVersion());
- int headerLength = LONG_LENGTH + sizeof(packet.getTargetID())
- + sizeof(packet.getCallbackID());
+ String targetID = packet.getTargetID();
+ if (NO_ID_SET.equals(packet.getTargetID()))
+ targetID = null;
+ String callbackID = packet.getCallbackID();
+ if (NO_ID_SET.equals(packet.getCallbackID()))
+ callbackID = null;
+
+ int headerLength = LONG_LENGTH + sizeof(targetID)
+ + sizeof(callbackID);
assertEquals(buffer.getInt(), headerLength);
assertEquals(buffer.getLong(), packet.getCorrelationID());
- assertEquals(buffer.getNullableString(), packet.getTargetID());
- assertEquals(buffer.getNullableString(), packet.getCallbackID());
+
+ String bufferTargetID = buffer.getNullableString();
+ if (bufferTargetID == null)
+ bufferTargetID = NO_ID_SET;
+ String bufferCallbackID = buffer.getNullableString();
+ if (bufferCallbackID == null)
+ bufferCallbackID = NO_ID_SET;
+
+ assertEquals(bufferTargetID, packet.getTargetID());
+ assertEquals(bufferCallbackID, packet.getCallbackID());
}
private static void assertEqualsAcks(List<Ack> expected, List<Ack> actual)
More information about the jboss-cvs-commits
mailing list