Skip to content

Commit

Permalink
Add baseclass for each CoapOption type to support easier type matchin…
Browse files Browse the repository at this point in the history
…g Options
  • Loading branch information
NZSmartie committed Mar 14, 2018
1 parent a88e306 commit 81031ae
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 33 deletions.
35 changes: 35 additions & 0 deletions src/CoAPNet/CoapOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,39 @@ public override int GetHashCode()
return hashcode;
}
}

public class CoapStringOption : CoapOption
{
protected internal CoapStringOption(int optionNumber, int minLength = 0, int maxLength = 0, bool isRepeatable = false, object defaultValue = null)
: base(optionNumber, minLength, maxLength, isRepeatable, OptionType.String, defaultValue)
{ }

public string Value { get => ValueString; set => ValueString = value; }
}

public class CoapOpaqueOption : CoapOption
{
protected internal CoapOpaqueOption(int optionNumber, int minLength = 0, int maxLength = 0, bool isRepeatable = false, object defaultValue = null)
: base(optionNumber, minLength, maxLength, isRepeatable, OptionType.Opaque, defaultValue)
{ }

public byte[] Value { get => ValueOpaque; set => ValueOpaque = value; }
}

public class CoapUintOption : CoapOption
{
protected internal CoapUintOption(int optionNumber, int minLength = 0, int maxLength = 0, bool isRepeatable = false, object defaultValue = null)
: base(optionNumber, minLength, maxLength, isRepeatable: isRepeatable, type: OptionType.UInt, defaultValue: defaultValue)
{ }

public uint Value { get => ValueUInt; set => ValueUInt = value; }

}

public class CoapEmptyOption : CoapOption
{
protected internal CoapEmptyOption(int optionNumber, bool isRepeatable = false)
: base(optionNumber, isRepeatable: isRepeatable, type: OptionType.Empty)
{ }
}
}
8 changes: 4 additions & 4 deletions src/CoAPNet/Options/BlockWise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CoAPNet.Options
/// <summary>
/// Base class used with <see cref="Block1"/> and <see cref="Block2"/> as both sublasses share similarities.
/// </summary>
public class BlockBase : CoapOption
public class BlockBase : CoapUintOption
{
internal static readonly List<Tuple<int, int>> InternalSupportedBlockSizes = new List<Tuple<int, int>>
{
Expand Down Expand Up @@ -42,7 +42,7 @@ protected BlockBase(int optionNumber, int blockNumber, int blockSize, bool more)
}

internal BlockBase(int optionNumber)
:base (optionNumber, 0, 3, false, OptionType.UInt)
:base (optionNumber, 0, 3, false)
{ }

private int _blockSize = 1024;
Expand Down Expand Up @@ -227,12 +227,12 @@ public Block2(int blockNumber = 0, int blockSize = 256, bool more = false)
/// <summary>
/// A CoAP Option (as defiend in RFC7959), represents a request for the content's size, or (as a response from the server) the final size of the content.
/// </summary>
public sealed class Size2 : CoapOption
public sealed class Size2 : CoapUintOption
{
/// <summary>
/// Creates a new Size2 CoAP Option.
/// </summary>
public Size2() : base(CoapRegisteredOptionNumber.Size2, 0, 4, false, OptionType.UInt, 0u)
public Size2() : base(CoapRegisteredOptionNumber.Size2, 0, 4, false, 0u)
{ }

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/CoAPNet/Options/Conditional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ namespace CoAPNet.Options
/// parallel on the same resource (i.e., the "lost update" problem).
/// <para>See section 5.10.8.1 of [RFC7252]</para>
/// </summary>
public class IfMatch : CoapOption
public class IfMatch : CoapOpaqueOption
{
public IfMatch() : base(optionNumber: CoapRegisteredOptionNumber.IfMatch, maxLength: 8, isRepeatable: true, type: OptionType.Opaque) { }
public IfMatch() : base(optionNumber: CoapRegisteredOptionNumber.IfMatch, maxLength: 8, isRepeatable: true) { }
}

/// <summary>
Expand All @@ -27,7 +27,7 @@ public IfMatch() : base(optionNumber: CoapRegisteredOptionNumber.IfMatch, maxLen
/// carries no value.
/// <para>See section 5.10.8.2 of [RFC7252]</para>
/// </summary>
public class IfNoneMatch : CoapOption
public class IfNoneMatch : CoapEmptyOption
{
public IfNoneMatch() : base(optionNumber: CoapRegisteredOptionNumber.IfNoneMatch) { }
}
Expand Down
20 changes: 10 additions & 10 deletions src/CoAPNet/Options/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public override string ToString()
/// Content-Format identifier that is defined in the "CoAP Content-Formats" registry (Section 12.3 of [RFC7252]).
/// <para>See section 5.10.3 of [RFC7252]</para>
/// </summary>
public class ContentFormat : CoapOption
public class ContentFormat : CoapUintOption
{
public ContentFormatType MediaType { get => (ContentFormatType)ValueUInt; set => ValueUInt = (uint)value; }

public ContentFormat() : base(optionNumber: CoapRegisteredOptionNumber.ContentFormat, maxLength: 2, type: OptionType.UInt)
public ContentFormat() : base(optionNumber: CoapRegisteredOptionNumber.ContentFormat, maxLength: 2)
{
MediaType = ContentFormatType.TextPlain;
}
Expand All @@ -147,11 +147,11 @@ public ContentFormat(ContentFormatType type) : this()
/// "CoAP Content-Formats" registry (Section 12.3 of [RFC7252]).
/// <para>See section 5.10.4 of [RFC7252]</para>
/// </summary>
public class Accept : CoapOption
public class Accept : CoapUintOption
{
public ContentFormatType MediaType { get => (ContentFormatType)ValueUInt; set => ValueUInt = (uint)value; }

public Accept() : base(optionNumber: CoapRegisteredOptionNumber.Accept, maxLength: 2, type: OptionType.UInt)
public Accept() : base(optionNumber: CoapRegisteredOptionNumber.Accept, maxLength: 2)
{
MediaType = ContentFormatType.TextPlain;
}
Expand All @@ -173,9 +173,9 @@ public Accept(ContentFormatType type) : this()
/// Max-Age SHOULD update the value before each retransmission. (See also Section 5.7.1. of [RFC7252])</para>
/// <para>See section 5.10.5 of [RFC7252]</para>
/// </summary>
public class MaxAge : CoapOption
public class MaxAge : CoapUintOption
{
public MaxAge() : base(optionNumber: CoapRegisteredOptionNumber.MaxAge, maxLength: 4, type: OptionType.UInt, defaultValue: 60u)
public MaxAge() : base(optionNumber: CoapRegisteredOptionNumber.MaxAge, maxLength: 4, defaultValue: 60u)
{
ValueUInt = 0u;
}
Expand All @@ -200,9 +200,9 @@ public MaxAge(uint value) : this()
/// <para>See section 5.10.6 of [RFC7252]</para>
/// </summary>
/// TODO: Implement ETag request/response semantics as descripbed in section 5.10.6.1 and 5.10.6.2 of [RFC7252]
public class ETag : CoapOption
public class ETag : CoapOpaqueOption
{
public ETag() : base(optionNumber: CoapRegisteredOptionNumber.ETag, minLength: 1, maxLength: 8, isRepeatable: true, type: OptionType.Opaque)
public ETag() : base(optionNumber: CoapRegisteredOptionNumber.ETag, minLength: 1, maxLength: 8, isRepeatable: true)
{
ValueOpaque = null;
}
Expand All @@ -222,9 +222,9 @@ public ETag(byte[] value) : this()
/// able and willing to handle.
/// <para>See section 5.10.9 of [RFC7252]</para>
/// </summary>
public class Size1 : CoapOption
public class Size1 : CoapUintOption
{
public Size1() : base(optionNumber: CoapRegisteredOptionNumber.Size1, maxLength: 4, type: OptionType.UInt, defaultValue: 0u)
public Size1() : base(optionNumber: CoapRegisteredOptionNumber.Size1, maxLength: 4, defaultValue: 0u)
{ }

public Size1(uint value) : this()
Expand Down
8 changes: 4 additions & 4 deletions src/CoAPNet/Options/Location.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace CoAPNet.Options
/// <summary>
/// <para>See section 5.10.7 of [RFC7252]</para>
/// </summary>
public class LocationPath : CoapOption
public class LocationPath : CoapStringOption
{
public LocationPath() : base(optionNumber: CoapRegisteredOptionNumber.LocationPath, maxLength: 255, isRepeatable: true, type: OptionType.String) { }
public LocationPath() : base(optionNumber: CoapRegisteredOptionNumber.LocationPath, maxLength: 255, isRepeatable: true) { }
}

/// <summary>
/// <para>See section 5.10.7 of [RFC7252]</para>
/// </summary>
public class LocationQuery : CoapOption
public class LocationQuery : CoapStringOption
{
public LocationQuery() : base(optionNumber: CoapRegisteredOptionNumber.LocationQuery, maxLength: 255, isRepeatable: true, type: OptionType.String){ }
public LocationQuery() : base(optionNumber: CoapRegisteredOptionNumber.LocationQuery, maxLength: 255, isRepeatable: true){ }
}
}
8 changes: 4 additions & 4 deletions src/CoAPNet/Options/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace CoAPNet.Options
{
public class ProxyUri : CoapOption
public class ProxyUri : CoapStringOption
{
public ProxyUri() : base(optionNumber: CoapRegisteredOptionNumber.ProxyUri, minLength: 1, maxLength: 1034, type: OptionType.String) { }
public ProxyUri() : base(optionNumber: CoapRegisteredOptionNumber.ProxyUri, minLength: 1, maxLength: 1034) { }
}

public class ProxyScheme : CoapOption
public class ProxyScheme : CoapStringOption
{
public ProxyScheme() : base(optionNumber: CoapRegisteredOptionNumber.ProxyScheme, minLength: 1, maxLength: 255, type: OptionType.String) { }
public ProxyScheme() : base(optionNumber: CoapRegisteredOptionNumber.ProxyScheme, minLength: 1, maxLength: 255) { }
}
}
16 changes: 8 additions & 8 deletions src/CoAPNet/Options/Uri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace CoAPNet.Options
{
public class UriHost : CoapOption
public class UriHost : CoapStringOption
{
public UriHost() : base(optionNumber: CoapRegisteredOptionNumber.UriHost, minLength: 1, maxLength: 255, type: OptionType.String)
public UriHost() : base(optionNumber: CoapRegisteredOptionNumber.UriHost, minLength: 1, maxLength: 255)
{
ValueString = null;

Expand All @@ -20,9 +20,9 @@ public UriHost(string value) : this()
}
}

public class UriPort : CoapOption
public class UriPort : CoapUintOption
{
public UriPort() : base(optionNumber: CoapRegisteredOptionNumber.UriPort, minLength: 0, maxLength: 2, type: OptionType.UInt)
public UriPort() : base(optionNumber: CoapRegisteredOptionNumber.UriPort, minLength: 0, maxLength: 2)
{
ValueUInt = 0u;
}
Expand All @@ -33,9 +33,9 @@ public UriPort(ushort value) : this()
}
}

public class UriPath : CoapOption
public class UriPath : CoapStringOption
{
public UriPath() : base(optionNumber: CoapRegisteredOptionNumber.UriPath, minLength: 0, maxLength: 255, isRepeatable: true, type: OptionType.String)
public UriPath() : base(optionNumber: CoapRegisteredOptionNumber.UriPath, minLength: 0, maxLength: 255, isRepeatable: true)
{
ValueString = null;
}
Expand All @@ -46,9 +46,9 @@ public UriPath(string value) : this()
}
}

public class UriQuery : CoapOption
public class UriQuery : CoapStringOption
{
public UriQuery() : base(optionNumber: CoapRegisteredOptionNumber.UriQuery, minLength: 0, maxLength: 255, isRepeatable: true, type: OptionType.String)
public UriQuery() : base(optionNumber: CoapRegisteredOptionNumber.UriQuery, minLength: 0, maxLength: 255, isRepeatable: true)
{
ValueString = null;
}
Expand Down

0 comments on commit 81031ae

Please sign in to comment.