Skip to content

Commit

Permalink
Add comments and simplify some of the new code
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil committed Jun 14, 2023
1 parent f9268a5 commit ac2082d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 29 deletions.
28 changes: 25 additions & 3 deletions c-sharp/NetworkObjects/DataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>
{
// A parameterless constructor is required for serialization to work
// ReSharper disable once UnusedMember.Local
public MessageEventDataUpdated()
: base(Enum<EventType>.Null) { }

public MessageEventDataUpdated(Enum<EventType> 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<EventType>($"{DataProviderName}:onDidUpdate");

_dataUpdatedEvent = new MessageEventDataUpdated(eventType);
}

protected string DataProviderName { get; }

/// <summary>
/// Register this data provider on the network so that other services can use it
/// </summary>
public void RegisterDataProvider()
{
RegisterNetworkObject(DataProviderName, FunctionHandler);
Expand Down Expand Up @@ -47,10 +70,9 @@ private ResponseToRequest FunctionHandler(dynamic? request)
/// <summary>
/// Notify all processes on the network that this data provider has new data
/// </summary>
protected void ReportDataUpdate()
protected void SendDataUpdateEvent()
{
var dataUpdateEventType = new Enum<EventType>($"{DataProviderName}:onDidUpdate");
PapiClient.SendEvent(new DataUpdateEvent(dataUpdateEventType, true));
PapiClient.SendEvent(_dataUpdatedEvent);
}

/// <summary>
Expand Down
16 changes: 0 additions & 16 deletions c-sharp/NetworkObjects/DataUpdateEvent.cs

This file was deleted.

7 changes: 7 additions & 0 deletions c-sharp/NetworkObjects/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ protected NetworkObject(PapiClient papiClient)

protected PapiClient PapiClient { get; }

/// <summary>
/// Notify PAPI services we have a new network object they can use
/// </summary>
/// <param name="networkObjectName">Services access this network object using this name</param>
/// <param name="requestHandler">Function that will handle calls from services to this network object</param>
/// <exception cref="Exception">Throws if the network object could not be registered properly</exception>
protected void RegisterNetworkObject(
string networkObjectName,
Func<dynamic, ResponseToRequest> requestHandler
)
{
// PAPI requires network objects to expose "get" and "function" requests
var getReqType = new Enum<RequestType>($"object:{networkObjectName}.get");
var functionReqType = new Enum<RequestType>($"object:{networkObjectName}.function");

Expand Down
19 changes: 9 additions & 10 deletions c-sharp/NetworkObjects/TimeDataProvider.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using System.Timers;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.MessageTransports;
using SIL.Extensions;

namespace Paranext.DataProvider.NetworkObjects
{
/// <summary>
/// This is a sample data provider for demonstration purposes
/// </summary>
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;
}
Expand Down
11 changes: 11 additions & 0 deletions c-sharp/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"ParanextDataProvider": {
"commandName": "Project"
},
"WSL": {
"commandName": "WSL2",
"distributionName": ""
}
}
}

0 comments on commit ac2082d

Please sign in to comment.