diff options
| author | Roy <roy.mail.net@gmail.com> | 2018-01-03 01:57:58 +0200 |
|---|---|---|
| committer | Roy <roy.mail.net@gmail.com> | 2018-01-03 01:57:58 +0200 |
| commit | 63b2a192ba12f8239668b6818db4ad02db68dbbc (patch) | |
| tree | c9c1de011b38fbff94ce87ac2771667b9a3e773d /Software/Android_Studio/Tango.PMR/src | |
| parent | f917366c405a4308b4b60e30eb1bd0dcf880532c (diff) | |
| download | Tango-63b2a192ba12f8239668b6818db4ad02db68dbbc.tar.gz Tango-63b2a192ba12f8239668b6818db4ad02db68dbbc.zip | |
Implemented abstraction of message encoding/decoding by adding another layer of "Encoders" to Transporters.
Diffstat (limited to 'Software/Android_Studio/Tango.PMR/src')
3 files changed, 67 insertions, 30 deletions
diff --git a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/ITangoMessage.java b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/ITangoMessage.java index 83153d154..f64411a62 100644 --- a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/ITangoMessage.java +++ b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/ITangoMessage.java @@ -1,9 +1,31 @@ package com.twine.tango.pmr; +import com.twine.tango.pmr.common.MessageContainerOuterClass.MessageContainer; +import com.twine.tango.pmr.common.MessageTypeOuterClass.MessageType; + /** * Created by Roy on 1/3/2018. */ - public interface ITangoMessage { + /** + * Gets the container that will encapsulate the actual PMR message. + * + * @return the container + */ + MessageContainer.Builder getContainer(); + + /** + * Gets or sets the PMR message type. + * + * @return the type + */ + MessageType getType(); + + /** + * Serializes the Tango message to byte array. + * + * @return the byte [ ] + */ + byte[] toBytes(); } diff --git a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/MessageFactory.java b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/MessageFactory.java index 413824e8d..689d2c25a 100644 --- a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/MessageFactory.java +++ b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/MessageFactory.java @@ -121,6 +121,7 @@ public class MessageFactory * * @param <T> the type parameter * @param typeName the type name + * @param message the message * @param token the token * @return the tango message */ @@ -135,9 +136,8 @@ public class MessageFactory /** * Creates a new tango message with the specified message token. * - * @param <T> the type parameter - * @param typeName the type name - * @param token the token + * @param container the container + * @param message the message * @return the tango message */ public static TangoMessage createTangoMessage(MessageContainer container, GeneratedMessageV3 message) @@ -188,6 +188,24 @@ public class MessageFactory } } + + /** + * Parses the specified byte array to an unknown Tango message type. + * + * @param data the data + * @return the tango message + * @throws InvalidProtocolBufferException the invalid protocol buffer exception + * @throws NoSuchMethodException the no such method exception + * @throws IllegalAccessException the illegal access exception + * @throws InvocationTargetException the invocation target exception + */ + public static TangoMessage parseTangoMessageAgnostic(byte[] data) throws InvalidProtocolBufferException, NoSuchMethodException, IllegalAccessException, InvocationTargetException + { + MessageContainer container = parseContainer(data); + GeneratedMessageV3 message = parseMessageFromContainerAgnostic(container); + return MessageFactory.createTangoMessage(container, message); + } + /** * Parses the internal PMR message from the message container. * @@ -212,7 +230,7 @@ public class MessageFactory * Parses the internal PMR message from the message container and returns the basic protobuf message interface. * * @param container the container - * @return GeneratedMessageV3 + * @return GeneratedMessageV3 generated message v 3 * @throws NoSuchMethodException the no such method exception * @throws InvocationTargetException the invocation target exception * @throws IllegalAccessException the illegal access exception diff --git a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/TangoMessage.java b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/TangoMessage.java index 8403f2cab..eaf6de233 100644 --- a/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/TangoMessage.java +++ b/Software/Android_Studio/Tango.PMR/src/main/java/com/twine/tango/pmr/TangoMessage.java @@ -1,6 +1,7 @@ package com.twine.tango.pmr; import com.twine.tango.pmr.common.MessageContainerOuterClass.MessageContainer; +import com.twine.tango.pmr.common.MessageContainerOuterClass.MessageContainer.Builder; import com.twine.tango.pmr.common.MessageTypeOuterClass.MessageType; import java.lang.reflect.ParameterizedType; @@ -12,8 +13,9 @@ import java.util.UUID; * * @param <T> the type parameter */ -public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { - +public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> implements ITangoMessage +{ + private T message; private MessageType type; private MessageContainer.Builder container; @@ -24,10 +26,11 @@ public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { * @param message The message. * @param type The message type. */ - public TangoMessage(T message, MessageType type) { + public TangoMessage(T message, MessageType type) + { this.message = message; this.type = type; - + this.container = MessageContainer.newBuilder() .setToken(UUID.randomUUID().toString()) .setType(type); @@ -38,7 +41,8 @@ public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { * * @return The inner message. */ - public T getMessage() { + public T getMessage() + { return message; } @@ -47,16 +51,14 @@ public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { * * @param message The inner message. */ - public void setMessage(T message) { + public void setMessage(T message) + { this.message = message; } - /** - * Gets the inner message type. - * - * @return The inner message type. - */ - public MessageType getType() { + @Override + public MessageType getType() + { return type; } @@ -65,16 +67,14 @@ public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { * * @param type Inner message type. */ - public void setType(MessageType type) { + public void setType(MessageType type) + { this.type = type; } - /** - * Gets the message container. - * - * @return the message container. - */ - public MessageContainer.Builder getContainer() { + @Override + public Builder getContainer() + { return container; } @@ -83,15 +83,12 @@ public class TangoMessage<T extends com.google.protobuf.GeneratedMessageV3> { * * @param container the container */ - public void setContainer(MessageContainer.Builder container) { + public void setContainer(MessageContainer.Builder container) + { this.container = container; } - /** - * Serializes the container and message to byte array. - * - * @return Container and message byte array. - */ + @Override public byte[] toBytes() { container.setData(message.toByteString()); |
