aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.PMR/TangoMessage.cs
blob: 4b9f1cc5be3322bddb1c69e01e3101228105ea51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.PMR.Common;
using Google.Protobuf;
using System.IO;

namespace Tango.PMR
{
    /// <summary>
    /// Represents composition of message <see cref="MessageContainer"/> and a PMR message. aka Tango message.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class TangoMessage<T> : ITangoMessage where T : IMessage<T>
    {
        /// <summary>
        /// Gets the container that will encapsulate the actual PMR message.
        /// </summary>
        public MessageContainer Container { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        public T Message { get; set; }

        /// <summary>
        /// Gets or sets the PMR message type.
        /// </summary>
        public MessageType Type { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TangoMessage{T}"/> class.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="type">The type.</param>
        public TangoMessage(T message, MessageType type)
        {
            Message = message;
            Type = type;

            Container = new MessageContainer();
            Container.Token = Guid.NewGuid().ToString();
            Container.Type = Type;
        }

        /// <summary>
        /// Serializes the Tango message to byte array.
        /// </summary>
        /// <returns></returns>
        public byte[] ToBytes()
        {
            using (MemoryStream ms = new MemoryStream())
            {
                Message.WriteTo(ms);
                ms.Position = 0;
                Container.Data = ByteString.FromStream(ms);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                Container.WriteTo(ms);
                return ms.ToArray();
            }
        }

        /// <summary>
        /// Serializes the Tango message to byte array representing a serialized JSON object of this Tango message.
        /// </summary>
        /// <returns></returns>
        public byte[] ToJsonBytes()
        {
            Container.Data = Message.ToByteString();
            return Encoding.UTF8.GetBytes(Container.ToString());
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="T"/> to <see cref="TangoMessage{T}"/>.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator TangoMessage<T>(T message)
        {
            var tangoMessage = MessageFactory.CreateTangoMessage<T>();
            tangoMessage.Message = message;
            return tangoMessage;
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="TangoMessage{T}"/> to <see cref="T"/>.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator T(TangoMessage<T> instance)
        {
            return instance.Message;
        }

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.Byte[]"/> to <see cref="TangoMessage{T}"/>.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator TangoMessage<T>(byte[] data)
        {
            return MessageFactory.ParseTangoMessage<T>(data);
        }

        /// <summary>
        /// Returns a <see cref="System.String" /> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String" /> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return String.Format(@"Message Type: {0}
Token: {1}
Error: {2}
Message: {3}", Container.Type, Container.Token, Container.Error, Message.ToString());
        }
    }
}