public class MessageType extends DataType
EMessageObject
sub-class. The serialized
binary format is:
EMessageObject
class name (serialized as a
String
).
EMessage
and
EField
classes to 31 fields.
EFieldInfo.fields()
. Serialization is done based
on the field class.
EMessageObject
class or its sub-class. This allows a
EMessage
and EField
to use an abstract class
extending EField
as a field and initializing that
field with a concrete sub-class. This allows the message/field
to support new information without changing the message/field
class.
Note: as of eBus 4.4.0, messages are
automatically compiled when first used for serialization
or de-serialization. This increases the time it takes to
send or receive a message the first time. When a message
is frequently transmitted, it is recommended that the
message class is "compiled" by calling
DataType.findType(java.lang.Class)
for that
message class. So when a message is sent or received the
first time, the message compilation is already done.
Modifier and Type | Class and Description |
---|---|
static class |
MessageType.MessageField
Stores a field index, field name, class field, eBus data
type.
|
Modifier and Type | Field and Description |
---|---|
protected java.lang.String |
_subject
EMessage has a public subject field but
this field is not serialized because it is
include in the message key identifier. |
protected static java.util.List<java.lang.Class<? extends EReplyMessage>> |
NO_REPLY
The default empty reply class array used for non-request
message types.
|
_builtinFlag, _class, _classes, _compiledClasses, _defaultValue, ADDRESS_TYPE, ARRAY_SUFFIX, CHARSET, CLASS_TYPE, EMPTY_STRING, FIELD_MASK_SIZE, INDENT, INDENT1, KEY_TYPE, MAX_ARRAY_SIZE, MAX_FIELDS, MAX_FILE_NAME_LENGTH, MAX_STRING_LENGTH, STRING_TYPE
Modifier | Constructor and Description |
---|---|
protected |
MessageType(java.lang.Class<?> clazz,
java.util.List<MessageType.MessageField> fields,
java.util.List<java.lang.Class<? extends EReplyMessage>> replies,
java.lang.invoke.MethodHandle ctor)
Creates a new message type instance for the given class,
message fields, and constructor method handle.
|
Modifier and Type | Method and Description |
---|---|
protected void |
createDeserializer(java.lang.String fieldName,
java.lang.String indent,
java.util.Formatter output)
Generates the code to de-serialize the named field
from a
ByteBuffer . |
protected void |
createSerializer(java.lang.String fieldName,
java.lang.String indent,
java.util.Formatter output)
Adds the Java code used to serialize the named field to
a
ByteBuffer . |
java.lang.Object |
deserialize(java.nio.ByteBuffer buffer)
Returns a message object de-serialized from the given
buffer.
|
protected java.lang.Object |
deserializeFields(int fieldMask,
java.nio.ByteBuffer buffer)
Returns an
EMessageObject de-serialized from the
given buffer. |
java.util.List<MessageType.MessageField> |
fields()
Returns the message object public fields.
|
boolean |
isMessage()
Returns
true if this message type is for an
EMessage and false if for an
EField . |
boolean |
isValidReply(java.lang.Class<?> clazz)
Returns
true if clazz is listed in the
allowed reply message class list and false if not. |
java.lang.Object |
newInstance(java.lang.Object[] args)
|
int |
numberFields()
Returns the number of fields in this
EMessage
type. |
java.util.List<java.lang.Class<? extends EReplyMessage>> |
replyTypes()
Returns the reply classes associated with a request
message class.
|
void |
serialize(java.lang.Object o,
java.nio.ByteBuffer buffer)
Serializes the message fields to the given the buffer,
setting the field mask.
|
protected int |
serializeFields(java.lang.Object o,
java.nio.ByteBuffer buffer)
Writes the message object fields to the given buffer and
returns the resulting field mask.
|
void |
subject(java.lang.String subject)
Sets the message subject to the given value.
|
java.util.List<java.lang.Object> |
values(EMessageObject message)
Returns the message object's field values as an
Object list. |
compareTo, dataClass, dataClassName, defaultValue, equals, fields, findType, hashCode, isArray, isBuiltin, isEnum, isInstance, isPrimitive, replyClasses, toString
protected java.lang.String _subject
EMessage
has a public subject
field but
this field is not serialized because it is
include in the message key identifier. But it must be
included in
EMessage.EMessage(java.lang.String, long)
constructor. This problem is resolved setting the
subject via subject(String)
before de-serializing
the message.protected static final java.util.List<java.lang.Class<? extends EReplyMessage>> NO_REPLY
protected MessageType(java.lang.Class<?> clazz, java.util.List<MessageType.MessageField> fields, java.util.List<java.lang.Class<? extends EReplyMessage>> replies, java.lang.invoke.MethodHandle ctor)
clazz
- the message class instance.fields
- the message fields.replies
- reply classes associated with a request
message class. Will be an empty array for non-request
message types.ctor
- the message class de-serialization
constructor.public void serialize(java.lang.Object o, java.nio.ByteBuffer buffer) throws java.nio.BufferOverflowException
Note: as of eBus 4.4.0, messages are
automatically compiled when first used for serialization
or de-serialization. This increases the time it takes to
send or receive a message the first time. When a message
is frequently transmitted, it is recommended that the
message class is "compiled" by calling
DataType.findType(java.lang.Class)
for that
message class. So when a message is sent or received the
first time, the message compilation is already done.
public java.lang.Object deserialize(java.nio.ByteBuffer buffer) throws java.lang.IllegalStateException, java.nio.BufferUnderflowException, UnknownMessageException, InvalidMessageException
Note: as of eBus 4.4.0, messages are
automatically compiled when first used for serialization
or de-serialization. This increases the time it takes to
send or receive a message the first time. When a message
is frequently transmitted, it is recommended that the
message class is "compiled" by calling
DataType.findType(java.lang.Class)
for that
message class. So when a message is sent or received the
first time, the message compilation is already done.
deserialize
in class DataType
buffer
- read in the message from this buffer.java.lang.IllegalStateException
- if subject(String)
was not called prior to
de-serializing.java.nio.BufferUnderflowException
- if buffer
contains an incomplete message.UnknownMessageException
- if the serialized message is unknown to this eBus.InvalidMessageException
- if the message is incorrectly serialized.protected void createSerializer(java.lang.String fieldName, java.lang.String indent, java.util.Formatter output)
ByteBuffer
.createSerializer
in class DataType
fieldName
- the message field name.indent
- indent the code by this amount.output
- write the code to this formatter.protected void createDeserializer(java.lang.String fieldName, java.lang.String indent, java.util.Formatter output)
ByteBuffer
.createDeserializer
in class DataType
fieldName
- store the de-serialized field in this
name.indent
- indent the code by this amount.output
- append the code to this formatter.public boolean isValidReply(java.lang.Class<?> clazz)
true
if clazz
is listed in the
allowed reply message class list and false
if not.
In short, returns true
if clazz
message
may be sent in reply to this request message type.clazz
- the reply message class.true
if clazz
message
may be sent in reply to this request message type.public java.util.List<java.lang.Class<? extends EReplyMessage>> replyTypes()
public boolean isMessage()
true
if this message type is for an
EMessage
and false
if for an
EField
.true
if the underlying type is an
EMessage
.public java.util.List<java.lang.Object> values(EMessageObject message) throws java.lang.Throwable
Object
list. The returned list values are in
the same order as fields()
.message
- extract the field values from this message
object.java.lang.Throwable
- if any sort of error occurs when extracting the values
from message
.public int numberFields()
EMessage
type.public java.util.List<MessageType.MessageField> fields()
public void subject(java.lang.String subject) throws java.lang.IllegalArgumentException
subject
- de-serialized message subject.java.lang.IllegalArgumentException
- if subject
is null
or empty.public java.lang.Object newInstance(java.lang.Object[] args) throws java.lang.IllegalArgumentException, InvalidMessageException
EMessage
or
EField
instance
instantiated from the given arguments. The arguments are
expected to contain valid values and be in the correct
order as specified by the EFieldInfo
annotation.args
- constructor arguments.java.lang.IllegalArgumentException
- if args
is either null
or does not contain
the correct number of arguments.InvalidMessageException
- if the instantiation failed.protected int serializeFields(java.lang.Object o, java.nio.ByteBuffer buffer) throws java.nio.BufferOverflowException
As of eBus v. 4.4.0, all EMessageObject
classes
are automatically "compiled" - meaning that an
MessageType
-subclass is generated for any given
EMessageObject
subclass. The MessageType
subclass overrides this serializeFields
method,
meaning this method implementation is never called.
Therefore, this method implementation has an empty body
and always returns zero.
o
- write this EMessageObject
's fields to
buffer
. Note: o
is not null
.buffer
- serialize fields to this buffer.java.nio.BufferOverflowException
- if buffer
does not contain sufficient space to
store the message.protected java.lang.Object deserializeFields(int fieldMask, java.nio.ByteBuffer buffer) throws java.nio.BufferUnderflowException, UnknownMessageException, InvalidMessageException
EMessageObject
de-serialized from the
given buffer.
As of eBus v. 4.4.0, all EMessageObject
classes
are automatically "compiled" - meaning that an
MessageType
-subclass is generated for any given
EMessageObject
subclass. The MessageType
subclass overrides this deserializeFields
method,
meaning this method implementation is never called.
Therefore, this method implementation has an empty body
and always returns null
.
fieldMask
- the message field mask specifying which
fields are serialized and which are null
.buffer
- read in the message from this buffer.java.nio.BufferUnderflowException
- if buffer
contains an incomplete message.UnknownMessageException
- if the serialized message is unknown to this eBus.InvalidMessageException
- if the message is incorrectly serialized.