From f5c666b21ee49d248a5d6c019977e22f17b3ff3c Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 10 Jul 2018 10:19:16 +1200 Subject: [PATCH] feat(ServiceProfile): AddProperty --- Spike/Program.cs | 10 ++++++---- src/ServiceProfile.cs | 20 ++++++++++++++++++++ test/ServiceProfileTest.cs | 15 +++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Spike/Program.cs b/Spike/Program.cs index b06480e..1a44b7f 100644 --- a/Spike/Program.cs +++ b/Spike/Program.cs @@ -13,10 +13,12 @@ static void Main(string[] args) Console.WriteLine("Multicast DNS spike"); // set logger factory - var properties = new Common.Logging.Configuration.NameValueCollection(); - properties["level"] = "TRACE"; - properties["showLogName"] = "true"; - properties["showDateTime"] = "true"; + var properties = new Common.Logging.Configuration.NameValueCollection + { + ["level"] = "TRACE", + ["showLogName"] = "true", + ["showDateTime"] = "true" + }; LogManager.Adapter = new ConsoleOutLoggerFactoryAdapter(properties); var mdns = new MulticastService diff --git a/src/ServiceProfile.cs b/src/ServiceProfile.cs index 409ed19..7098fea 100644 --- a/src/ServiceProfile.cs +++ b/src/ServiceProfile.cs @@ -144,5 +144,25 @@ public ServiceProfile(string instanceName, string serviceName, ushort port, IEnu /// /// public List Resources { get; set; } = new List(); + + /// + /// Add a property of the service to the TXT record. + /// + /// + /// The name of the property. + /// + /// + /// The value of the property. + /// + public void AddProperty(string key, string value) + { + var txt = Resources.OfType().FirstOrDefault(); + if (txt == null) + { + txt = new TXTRecord { Name = FullyQualifiedName }; + Resources.Add(txt); + } + txt.Strings.Add(key + "=" + value); + } } } diff --git a/test/ServiceProfileTest.cs b/test/ServiceProfileTest.cs index ab377fd..1795c01 100644 --- a/test/ServiceProfileTest.cs +++ b/test/ServiceProfileTest.cs @@ -72,5 +72,20 @@ public void TXTRecords() CollectionAssert.Contains(txt.Strings, "a=1"); CollectionAssert.Contains(txt.Strings, "b=2"); } + + [TestMethod] + public void AddProperty() + { + var service = new ServiceProfile + { + InstanceName = "x", + ServiceName = "_sdtest._udp" + }; + service.AddProperty("a", "1"); + + var txt = service.Resources.OfType().First(); + Assert.AreEqual(service.FullyQualifiedName, txt.Name); + CollectionAssert.Contains(txt.Strings, "a=1"); + } } }