Skip to content

Commit

Permalink
Compatibility updates for Service (#18)
Browse files Browse the repository at this point in the history
Tested universal Service with a complex implementation of `IAdapterSource` that required many supplementary libraries and made updates based on that real-world implementation.

 - Added listener to `IAdapterSource.OnAdapterSourceStarted` within the `Adapter` when the `Adapter` starts
 - Removed listener to `IAdapterSource.OnAdapterSourceStarted` within the `Adapter` when the `IAdapterSource` stops
 - Fixed bug with `TcpAdapter` not bubbling-up `OnStop` exceptions
 - Updated Service worker to listen to `Adapter.OnStopped` event
 - Removed scope of Service to x86 platform
 - Converted Assembly load to `UnsafeLoadFrom` just because it worked. Discussions are definitely welcome on this change.
  • Loading branch information
tbm0115 authored Feb 8, 2023
1 parent 608b891 commit b0df93e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 7 deletions.
7 changes: 7 additions & 0 deletions AdapterInterface/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,19 @@ public void Start(IEnumerable<IAdapterSource> sources, bool begin = true, Cancel
foreach (var source in sources)
{
_sources.Add(source);
source.OnAdapterSourceStarted += Source_OnAdapterSourceStarted;
source.OnAdapterSourceStopped += Source_OnAdapterSourceStopped;
source.OnDataReceived += _source_OnDataReceived;
source.Start(token);
}
TriggerOnStartedEvent();
}

private void Source_OnAdapterSourceStarted(IAdapterSource sender, AdapterSourceStartedEventArgs e)
{
_logger?.LogInformation("Adapter's Source {AdapterSourceType} has started", sender.GetType().FullName);
}

private void Source_OnAdapterSourceStopped(IAdapterSource sender, AdapterSourceStoppedEventArgs e)
{
bool removedSource = false;
Expand Down Expand Up @@ -417,6 +423,7 @@ public virtual void Stop(Exception ex = null)
foreach (var source in _sources)
{
source.Stop();
source.OnAdapterSourceStarted -= Source_OnAdapterSourceStarted;
source.OnAdapterSourceStopped -= Source_OnAdapterSourceStopped;
source.OnDataReceived -= _source_OnDataReceived;
}
Expand Down
2 changes: 1 addition & 1 deletion AdapterInterface/AdapterInterface.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<ApplicationIcon>icon.ico</ApplicationIcon>
<PackageProjectUrl>https://github.com/TrueAnalyticsSolutions/MtconnectCore.Adapter</PackageProjectUrl>
<RepositoryUrl>$(ProjectUrl)</RepositoryUrl>
<Version>1.0.15</Version>
<Version>1.0.16</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions MtconnectCore.TcpAdapter/TcpAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ protected override void Start(bool begin = true, CancellationToken token = defau
/// <inheritdoc />
public override void Stop(Exception ex = null)
{
base.Stop();
base.Stop(ex);

if (State > AdapterStates.NotStarted)
{
_logger?.LogInformation("Stopping Adapter");
_logger?.LogInformation("Stopping TcpAdapter");
State = AdapterStates.Stopping;

// Queue the _listerThread to cancel
Expand Down
2 changes: 1 addition & 1 deletion MtconnectCore.TcpAdapter/TcpAdapter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>Mtconnect;Adapter;TCP;TAMS;</PackageTags>
<PackageReleaseNotes>Added extra logging and refined TcpConnections.</PackageReleaseNotes>
<Version>1.0.15</Version>
<Version>1.0.16</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
16 changes: 16 additions & 0 deletions Service/AdapterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,26 @@ public void Start(CancellationToken token = default)
_logger?.LogWarning(ex, ex.Message + " in Adapter type '{AdapterType}'", adapterType.FullName);
continue;
}
adapterInstance.Instance.OnStopped += Instance_OnStopped;
adapterInstance.Instance.Start(adapterInstance.Sources, token: token);
_logger?.LogInformation("Started Adapter {AdapterType}", adapterType.FullName);
}
}

private void Instance_OnStopped(Adapter sender, AdapterStoppedEventArgs e)
{
if (e.Exception != null)
{
_logger?.LogError(e.Exception, "Adapter Instance stopped due to error");
} else if (e.WasCancelled)
{
_logger?.LogWarning("Adapter Instance was cancelled");
} else
{
_logger?.LogInformation("Adapter Instance stopped");
}
}

/// <summary>
/// Stops all <see cref="Adapter"/>s.
/// </summary>
Expand All @@ -118,6 +133,7 @@ public void Stop()
}

adapterInstance.Instance.Stop();
adapterInstance.Instance.OnStopped -= Instance_OnStopped;
}
}

Expand Down
2 changes: 0 additions & 2 deletions Service/Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
<OutputType>exe</OutputType>
<PublishSingleFile Condition="'$(Configuration)' == 'Release'">true</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Release'">true</PublishReadyToRun>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PlatformTarget>x86</PlatformTarget>
<Version>1.0.0.1</Version>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion Service/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private void _importDlls()
continue;
}

var dll = Assembly.Load(File.ReadAllBytes(dllFilename));
var dll = Assembly.UnsafeLoadFrom(dllFilename);
if (dll == null)
{
_workerLogger?.LogError(new FileLoadException("Failed to load Adapter DLL", dllFilename), "Failed to load Adapter DLL {AdapterFilename}", dllFilename);
Expand Down

0 comments on commit b0df93e

Please sign in to comment.