Esempio n. 1
0
        /// <summary>
        /// Stringify a message.
        /// </summary>
        public static string ToString(Message msg)
        {
            StringBuilder sb = new StringBuilder();
            string        kind = "Message", code = "Code";

            if (msg.IsRequest)
            {
                kind = "Request";
                code = "Method";
            }
            else if (msg.IsResponse)
            {
                kind = "Response";
                code = "Status";
            }

            sb.AppendFormat("==[ COAP {0} ]============================================\n", kind)
            .AppendFormat("ID     : {0}\n", msg.ID)
            .AppendFormat("Type   : {0}\n", msg.Type)
            .AppendFormat("Token  : {0}\n", msg.TokenString)
            .AppendFormat("{1}: {0}\n", Code.ToString(msg.Code), code.PadRight(7));

            if (msg.Source != null)
            {
                sb.AppendFormat("Source : {0}\n", msg.Source);
            }

            if (msg.Destination != null)
            {
                sb.AppendFormat("Dest   : {0}\n", msg.Destination);
            }

            sb.AppendFormat("Options: {0}\n", OptionsToString(msg))
            .AppendFormat("Payload: {0} Bytes\n", msg.PayloadSize);

            if (msg.PayloadSize > 0)
            {
                sb.AppendLine("---------------------------------------------------------------");
                if (MediaType.IsPrintable(msg.ContentType))
                {
                    sb.AppendLine(msg.PayloadString);
                }
                else if (MediaType.IsCbor(msg.ContentType))
                {
                    sb.AppendLine(CBORObject.DecodeFromBytes(msg.Payload).ToString());
                }
                else
                {
                    string x = Hex.ToHexString(msg.Payload);
                    for (int i = 0; i < x.Length; i += 64)
                    {
                        int chunk = (i + 64 < x.Length) ? 64 : (x.Length - i);
                        sb.AppendLine(x.Substring(i, chunk));
                    }
                }
            }


            return(sb.ToString());
        }