From ac2082dbe4e30dd75b28167388bda6f74d4d2f9a Mon Sep 17 00:00:00 2001 From: Matt Lyons Date: Wed, 14 Jun 2023 10:46:16 -0500 Subject: [PATCH] Add comments and simplify some of the new code --- c-sharp/NetworkObjects/DataProvider.cs | 28 +++++++++++++++++++--- c-sharp/NetworkObjects/DataUpdateEvent.cs | 16 ------------- c-sharp/NetworkObjects/NetworkObject.cs | 7 ++++++ c-sharp/NetworkObjects/TimeDataProvider.cs | 19 +++++++-------- c-sharp/Properties/launchSettings.json | 11 +++++++++ 5 files changed, 52 insertions(+), 29 deletions(-) delete mode 100644 c-sharp/NetworkObjects/DataUpdateEvent.cs create mode 100644 c-sharp/Properties/launchSettings.json diff --git a/c-sharp/NetworkObjects/DataProvider.cs b/c-sharp/NetworkObjects/DataProvider.cs index 943c21e7bc..7ed9a298e1 100644 --- a/c-sharp/NetworkObjects/DataProvider.cs +++ b/c-sharp/NetworkObjects/DataProvider.cs @@ -8,14 +8,37 @@ namespace Paranext.DataProvider.NetworkObjects { internal abstract class DataProvider : NetworkObject { + // This is an internal class because nothing else should be instantiating it directly + private class MessageEventDataUpdated : MessageEventGeneric + { + // A parameterless constructor is required for serialization to work + // ReSharper disable once UnusedMember.Local + public MessageEventDataUpdated() + : base(Enum.Null) { } + + public MessageEventDataUpdated(Enum eventType) + : base(eventType, true) { } + } + + private readonly MessageEventDataUpdated _dataUpdatedEvent; + protected DataProvider(string name, PapiClient papiClient) : base(papiClient) { + // "-data" is the prefix used by PAPI for data provider names DataProviderName = name + "-data"; + + // "onDidUpdate" is the event name used by PAPI for data providers to notify consumers of updates + var eventType = new Enum($"{DataProviderName}:onDidUpdate"); + + _dataUpdatedEvent = new MessageEventDataUpdated(eventType); } protected string DataProviderName { get; } + /// + /// Register this data provider on the network so that other services can use it + /// public void RegisterDataProvider() { RegisterNetworkObject(DataProviderName, FunctionHandler); @@ -47,10 +70,9 @@ private ResponseToRequest FunctionHandler(dynamic? request) /// /// Notify all processes on the network that this data provider has new data /// - protected void ReportDataUpdate() + protected void SendDataUpdateEvent() { - var dataUpdateEventType = new Enum($"{DataProviderName}:onDidUpdate"); - PapiClient.SendEvent(new DataUpdateEvent(dataUpdateEventType, true)); + PapiClient.SendEvent(_dataUpdatedEvent); } /// diff --git a/c-sharp/NetworkObjects/DataUpdateEvent.cs b/c-sharp/NetworkObjects/DataUpdateEvent.cs deleted file mode 100644 index 5b17a9b09d..0000000000 --- a/c-sharp/NetworkObjects/DataUpdateEvent.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Paranext.DataProvider.Messages; -using PtxUtils; - -namespace Paranext.DataProvider.NetworkObjects -{ - internal sealed class DataUpdateEvent : MessageEventGeneric - { - // A parameterless constructor is required for serialization to work, but we never need to deserialize this particular event. So just use a null event type. - // Because the event types are dynamic based on data provider names, we can't create every possible event type ahead of time. - public DataUpdateEvent() - : base(Enum.Null) { } - - public DataUpdateEvent(Enum eventType, bool eventContents) - : base(eventType, eventContents) { } - } -} diff --git a/c-sharp/NetworkObjects/NetworkObject.cs b/c-sharp/NetworkObjects/NetworkObject.cs index 969ed78215..26d73f8c4f 100644 --- a/c-sharp/NetworkObjects/NetworkObject.cs +++ b/c-sharp/NetworkObjects/NetworkObject.cs @@ -14,11 +14,18 @@ protected NetworkObject(PapiClient papiClient) protected PapiClient PapiClient { get; } + /// + /// Notify PAPI services we have a new network object they can use + /// + /// Services access this network object using this name + /// Function that will handle calls from services to this network object + /// Throws if the network object could not be registered properly protected void RegisterNetworkObject( string networkObjectName, Func requestHandler ) { + // PAPI requires network objects to expose "get" and "function" requests var getReqType = new Enum($"object:{networkObjectName}.get"); var functionReqType = new Enum($"object:{networkObjectName}.function"); diff --git a/c-sharp/NetworkObjects/TimeDataProvider.cs b/c-sharp/NetworkObjects/TimeDataProvider.cs index c9629a8d0d..44113bfb6f 100644 --- a/c-sharp/NetworkObjects/TimeDataProvider.cs +++ b/c-sharp/NetworkObjects/TimeDataProvider.cs @@ -1,27 +1,26 @@ -using System.Timers; using Paranext.DataProvider.MessageHandlers; using Paranext.DataProvider.MessageTransports; using SIL.Extensions; namespace Paranext.DataProvider.NetworkObjects { + /// + /// This is a sample data provider for demonstration purposes + /// internal class TimeDataProvider : DataProvider { - private readonly System.Timers.Timer _timer = new System.Timers.Timer( - TimeSpan.FromSeconds(1) - ); + // Fire an event that says our "time data" updated once per second + private readonly System.Timers.Timer _timer = new(TimeSpan.FromSeconds(1)); public TimeDataProvider(PapiClient papiClient) : base("current-time", papiClient) { } - private void TimerFired(object? state, ElapsedEventArgs args) - { - ReportDataUpdate(); - } - protected override void StartDataProvider() { - _timer.Elapsed += TimerFired; + _timer.Elapsed += (_, _) => + { + SendDataUpdateEvent(); + }; _timer.AutoReset = true; _timer.Enabled = true; } diff --git a/c-sharp/Properties/launchSettings.json b/c-sharp/Properties/launchSettings.json new file mode 100644 index 0000000000..24eb035021 --- /dev/null +++ b/c-sharp/Properties/launchSettings.json @@ -0,0 +1,11 @@ +{ + "profiles": { + "ParanextDataProvider": { + "commandName": "Project" + }, + "WSL": { + "commandName": "WSL2", + "distributionName": "" + } + } +} \ No newline at end of file