Skip to content

Commit

Permalink
Restore original ToString functionality and add debuggable format
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Mar 12, 2018
1 parent 0da5483 commit 5a0b28b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/CoAPNet.Udp/CoapUdpEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,17 @@ public async Task SendAsync(CoapPacket packet, CancellationToken token)

/// <inheritdoc />
public override string ToString()
=> ToString(CoapEndpointStringFormat.Simple);

/// <inheritdoc />
public string ToString(CoapEndpointStringFormat format)
{
return $"[ udp://{_endpoint.Address}:{_endpoint.Port} {(IsMulticast ? "(M) " : "")}{(IsSecure ? "(S) " : "")}]";
if (format == CoapEndpointStringFormat.Simple)
return $"{_endpoint.Address}:{_endpoint.Port}";
if (format == CoapEndpointStringFormat.Debuggable)
return $"[ udp://{_endpoint.Address}:{_endpoint.Port} {(IsMulticast ? "(M) " : "")}{(IsSecure ? "(S) " : "")}]";

throw new ArgumentException(nameof(format));
}

/// <inheritdoc />
Expand Down
59 changes: 58 additions & 1 deletion src/CoAPNet/ICoapEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,54 @@

namespace CoAPNet
{
/// <summary/>
[ExcludeFromCodeCoverage]
public class CoapEndpointException : Exception
{
/// <summary/>
public CoapEndpointException() : base() { }

/// <summary/>
public CoapEndpointException(string message) : base(message) { }

/// <summary/>
public CoapEndpointException(string message, Exception innerException) : base(message, innerException) { }
}

/// <summary>
/// Contains the local <see cref="ICoapEndpoint"/> and the remote <see cref="ICoapEndpoint"/> that are part of the associate message request or reponse.
/// </summary>
public interface ICoapConnectionInformation
{
/// <summary>
/// The Local <see cref="ICoapEndpoint"/> that represents the current connection.
/// </summary>
ICoapEndpoint LocalEndpoint { get; }

/// <summary>
/// The Remote <see cref="ICoapEndpoint"/> that represents the 3rd party.
/// </summary>
ICoapEndpoint RemoteEndpoint { get; }
}

/// <summary>
/// Used with <see cref="ICoapEndpoint.ToString(CoapEndpointStringFormat)"/> to get a string representation of a <see cref="ICoapEndpoint"/>.
/// </summary>
public enum CoapEndpointStringFormat
{
/// <summary>
/// Return a simple string format represeantion of <see cref="ICoapEndpoint"/> (usually in the form of &lt;address&gt;:&lt;port&gt;)
/// </summary>
Simple,
/// <summary>
/// Used to get string representation of a <see cref="ICoapEndpoint"/> for debugging purposes.
/// </summary>
Debuggable,
}

/// <summary>
/// CoAP usses a <see cref="ICoapEndpoint"/> as a addressing mechanism for other CoAP clients and servers on a transport.
/// </summary>
public interface ICoapEndpoint : IDisposable
{
/// <summary>
Expand Down Expand Up @@ -71,6 +102,13 @@ public interface ICoapEndpoint : IDisposable
/// </summary>
/// <returns></returns>
Task<CoapPacket> ReceiveAsync(CancellationToken tokens);

/// <summary>
/// Returns a string representation of the <see cref="ICoapEndpoint"/>.
/// </summary>
/// <param name="format"></param>
/// <returns></returns>
string ToString(CoapEndpointStringFormat format);
}

/// <summary>
Expand All @@ -79,17 +117,26 @@ public interface ICoapEndpoint : IDisposable
[ExcludeFromCodeCoverage]
public class CoapEndpoint : ICoapEndpoint
{
/// <inheritdoc />
public void Dispose()
{ }

/// <inheritdoc />
public bool IsSecure { get; internal set; }

/// <inheritdoc />
public bool IsMulticast { get; internal set; }

/// <inheritdoc />
public Uri BaseUri { get; internal set; }

/// <inheritdoc />
public Task SendAsync(CoapPacket packet, CancellationToken token)
{
throw new InvalidOperationException($"{nameof(CoapEndpoint)} can not be used to send and receive");
}

/// <inheritdoc />
public Task<CoapPacket> ReceiveAsync(CancellationToken token)
{
throw new InvalidOperationException($"{nameof(CoapEndpoint)} can not be used to send and receive");
Expand Down Expand Up @@ -117,10 +164,20 @@ public override int GetHashCode()
return 1404189491;
}


/// <inheritdoc />
public override string ToString()
=> ToString(CoapEndpointStringFormat.Simple);

/// <inheritdoc />
public string ToString(CoapEndpointStringFormat format)
{
return $"[ {BaseUri.Host}{(BaseUri.IsDefaultPort ? "" : ":" + BaseUri.Port)} {(IsMulticast ? "(M) " : "")}{(IsSecure ? "(S) " : "")}]";
if(format == CoapEndpointStringFormat.Simple)
return $"{BaseUri.Host}{(BaseUri.IsDefaultPort ? "" : ":" + BaseUri.Port)}";
if (format == CoapEndpointStringFormat.Debuggable)
return $"[ {BaseUri.Host}{(BaseUri.IsDefaultPort ? "" : ":" + BaseUri.Port)} {(IsMulticast ? "(M) " : "")}{(IsSecure ? "(S) " : "")}]";

throw new ArgumentException(nameof(format));
}
}
}
5 changes: 5 additions & 0 deletions tests/CoAPNet.Tests/Mocks/MockEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ public virtual async Task<CoapPacket> MockReceiveAsync(CancellationToken token)
Debug.WriteLine($"MockEndpoint: Read packet {{{string.Join(", ", packet.Payload)}}}");
return packet;
}

public string ToString(CoapEndpointStringFormat format)
{
return $"[ {nameof(MockEndpoint)} ]";
}
}
}
5 changes: 5 additions & 0 deletions tests/CoAPNet.Tests/Mocks/NonDisposableEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ public virtual async Task<CoapPacket> ReceiveAsync(CancellationToken token)
return await tcs.Task;

}

public string ToString(CoapEndpointStringFormat format)
{
return $"[ {nameof(NonDisposableEndpoint)} ]";
}
}
}

0 comments on commit 5a0b28b

Please sign in to comment.