Skip to content

Commit

Permalink
feat (FileDownloader.Dispose()): implement the disposable pattern on …
Browse files Browse the repository at this point in the history
…the file-downloader too
  • Loading branch information
ksidirop-laerdal committed Apr 23, 2024
1 parent ddc6f90 commit af522fe
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 10 deletions.
4 changes: 4 additions & 0 deletions Laerdal.McuMgr.Tests/FileDownloader/FileDownloaderTestbed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public void FatalErrorOccurredAdvertisement(string resource, string errorMessage

public void FileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(int progressPercentage, float averageThroughput)
=> _downloaderCallbacksProxy.FileDownloadProgressPercentageAndDataThroughputChangedAdvertisement(progressPercentage, averageThroughput); //raises the actual event

public void Dispose()
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public void FirmwareUploadProgressPercentageAndDataThroughputChangedAdvertisemen
public void CleanupResourcesOfLastInstallation()
{
}

public void Dispose()
{
}
}
}
}
33 changes: 32 additions & 1 deletion Laerdal.McuMgr/Droid/FileDownloader/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,39 @@ internal AndroidFileDownloaderProxy(INativeFileDownloaderCallbacksProxy fileDown
_fileDownloaderCallbacksProxy = fileDownloaderCallbacksProxy ?? throw new ArgumentNullException(nameof(fileDownloaderCallbacksProxy));
}

// public new void Dispose() { ... } dont there is no need to override the base implementation

private bool _alreadyDisposed;
protected override void Dispose(bool disposing)
{
if (_alreadyDisposed)
{
base.Dispose(disposing); //vital
return;
}

if (disposing)
{
CleanupInfrastructure();
}

_alreadyDisposed = true;

base.Dispose(disposing);
}

private void CleanupInfrastructure()
{
try
{
Disconnect();
}
catch
{
// ignored
}
}

#region commands

public new EFileDownloaderVerdict BeginDownload(string remoteFilePath)
Expand All @@ -66,7 +98,6 @@ internal AndroidFileDownloaderProxy(INativeFileDownloaderCallbacksProxy fileDown
}

#endregion commands



#region android callbacks -> csharp event emitters
Expand Down
33 changes: 33 additions & 0 deletions Laerdal.McuMgr/Droid/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ internal AndroidFileUploaderProxy(INativeFileUploaderCallbacksProxy fileUploader
_fileUploaderCallbacksProxy = fileUploaderCallbacksProxy ?? throw new ArgumentNullException(nameof(fileUploaderCallbacksProxy));
}

// public new void Dispose() { ... } dont there is no need to override the base implementation

private bool _alreadyDisposed;
protected override void Dispose(bool disposing)
{
if (_alreadyDisposed)
{
base.Dispose(disposing); //vital
return;
}

if (disposing)
{
CleanupInfrastructure();
}

_alreadyDisposed = true;

base.Dispose(disposing);
}

private void CleanupInfrastructure()
{
try
{
Disconnect();
}
catch
{
// ignored
}
}

public void CleanupResourcesOfLastUpload()
{
//nothing to do in android
Expand Down
33 changes: 33 additions & 0 deletions Laerdal.McuMgr/Droid/FirmwareInstaller/FirmwareInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,39 @@ internal AndroidFirmwareInstallerProxy(INativeFirmwareInstallerCallbacksProxy fi
_firmwareInstallerCallbacksProxy = firmwareInstallerCallbacksProxy ?? throw new ArgumentNullException(nameof(firmwareInstallerCallbacksProxy));
}

// public new void Dispose() { ... } dont there is no need to override the base implementation

private bool _alreadyDisposed;
protected override void Dispose(bool disposing)
{
if (_alreadyDisposed)
{
base.Dispose(disposing); //vital
return;
}

if (disposing)
{
CleanupInfrastructure();
}

_alreadyDisposed = true;

base.Dispose(disposing);
}

private void CleanupInfrastructure()
{
try
{
Disconnect();
}
catch
{
// ignored
}
}

public void CleanupResourcesOfLastInstallation()
{
//nothing to do here for android only ios needs this
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// ReSharper disable UnusedMember.Global
// ReSharper disable EventNeverSubscribedTo.Global

using System;

namespace Laerdal.McuMgr.FileDownloader.Contracts
{
/// <summary>Downloads a file on a specific Nordic-chip-based BLE device</summary>
/// <remarks>For the file-downloading process to even commence you need to be authenticated with the AED device that's being targeted.</remarks>
public interface IFileDownloader : IFileDownloaderCommandable, IFileDownloaderQueryable, IFileDownloaderEventSubscribable
public interface IFileDownloader :
IFileDownloaderCommandable,
IFileDownloaderQueryable,
IFileDownloaderEventSubscribable,
IDisposable
{
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
namespace Laerdal.McuMgr.FileDownloader.Contracts.Native
using System;

namespace Laerdal.McuMgr.FileDownloader.Contracts.Native
{
internal interface INativeFileDownloaderProxy : INativeFileDownloaderQueryableProxy, INativeFileDownloaderCommandableProxy, INativeFileDownloaderCallbacksProxy
internal interface INativeFileDownloaderProxy :
INativeFileDownloaderQueryableProxy,
INativeFileDownloaderCommandableProxy,
INativeFileDownloaderCallbacksProxy,
IDisposable
{
}
}
7 changes: 7 additions & 0 deletions Laerdal.McuMgr/Shared/FileDownloader/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ internal FileDownloader(INativeFileDownloaderProxy nativeFileDownloaderProxy)
_nativeFileDownloaderProxy = nativeFileDownloaderProxy ?? throw new ArgumentNullException(nameof(nativeFileDownloaderProxy));
_nativeFileDownloaderProxy.FileDownloader = this; //vital
}

public void Dispose()
{
_nativeFileDownloaderProxy?.Dispose();

GC.SuppressFinalize(this);
}

public string LastFatalErrorMessage => _nativeFileDownloaderProxy?.LastFatalErrorMessage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ namespace Laerdal.McuMgr.FileUploader.Contracts
{
/// <summary>Uploads a file on a specific Nordic-chip-based BLE device</summary>
/// <remarks>For the file-uploading process to even commence you need to be authenticated with the AED device that's being targeted.</remarks>
public interface IFileUploader : IFileUploaderCommandable, IFileUploaderQueryable, IFileUploaderEventSubscribable, IFileUploaderCleanupable, IDisposable
public interface IFileUploader :
IFileUploaderCommandable,
IFileUploaderQueryable,
IFileUploaderEventSubscribable,
IFileUploaderCleanupable,
IDisposable
{
}
}
9 changes: 7 additions & 2 deletions Laerdal.McuMgr/Shared/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ internal FileUploader(INativeFileUploaderProxy nativeFileUploaderProxy)
_nativeFileUploaderProxy.FileUploader = this; //vital
}

public void Dispose()
{
_nativeFileUploaderProxy?.Dispose();

GC.SuppressFinalize(this);
}

public bool TrySetContext(object context) => _nativeFileUploaderProxy?.TrySetContext(context) ?? false;
public bool TrySetBluetoothDevice(object bluetoothDevice) => _nativeFileUploaderProxy?.TrySetContext(bluetoothDevice) ?? false;
public bool TryInvalidateCachedTransport() => _nativeFileUploaderProxy?.TryInvalidateCachedTransport() ?? false;
Expand Down Expand Up @@ -438,7 +445,5 @@ public void FileUploadProgressPercentageAndDataThroughputChangedAdvertisement(in
progressPercentage: progressPercentage
));
}

public void Dispose() => _nativeFileUploaderProxy?.Dispose();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// ReSharper disable UnusedMember.Global
// ReSharper disable EventNeverSubscribedTo.Global

using System;

namespace Laerdal.McuMgr.FirmwareInstaller.Contracts
{
/// <summary>Upgrades the firmware on a specific Nordic-chip-based BLE device</summary>
public interface IFirmwareInstaller : IFirmwareInstallerQueryable, IFirmwareInstallerEventSubscribable, IFirmwareInstallerCommandable
public interface IFirmwareInstaller :
IFirmwareInstallerQueryable,
IFirmwareInstallerEventSubscribable,
IFirmwareInstallerCommandable,
IDisposable
{
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;

namespace Laerdal.McuMgr.FirmwareInstaller.Contracts.Native
{
internal interface INativeFirmwareInstallerProxy :
INativeFirmwareInstallerCommandableProxy,
INativeFirmwareInstallerQueryableProxy,
INativeFirmwareInstallerCleanupableProxy,
INativeFirmwareInstallerCallbacksProxy
INativeFirmwareInstallerCallbacksProxy,
IDisposable
{
string Nickname { get; set; }
}
Expand Down
7 changes: 7 additions & 0 deletions Laerdal.McuMgr/Shared/FirmwareInstaller/FirmwareInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ internal FirmwareInstaller(INativeFirmwareInstallerProxy nativeFirmwareInstaller
_nativeFirmwareInstallerProxy = nativeFirmwareInstallerProxy ?? throw new ArgumentNullException(nameof(nativeFirmwareInstallerProxy));
_nativeFirmwareInstallerProxy.FirmwareInstaller = this; //vital
}

public void Dispose()
{
_nativeFirmwareInstallerProxy?.Dispose();

GC.SuppressFinalize(this);
}

public EFirmwareInstallationVerdict BeginInstallation(
byte[] data,
Expand Down
2 changes: 1 addition & 1 deletion Laerdal.McuMgr/iOS/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected override void Dispose(bool disposing)
}

_alreadyDisposed = true;

base.Dispose(disposing);
}

Expand Down

0 comments on commit af522fe

Please sign in to comment.