Skip to content

Commit

Permalink
refa (AndroidFileUploader.java, IOSFileUploader.swift): we now suppor…
Browse files Browse the repository at this point in the history
…t setting a reason for the cancellation

   in the same context we now also raise the new event 'Cancelling'
  • Loading branch information
ksidirop-laerdal committed Oct 18, 2024
1 parent ae9942e commit 304492e
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,14 @@ public void disconnect() {
mcuMgrTransporter.release();
}

public void cancel()
private String _cancellationReason = "";
public void cancel(final String reason)
{
setState(EAndroidFileUploaderState.CANCELLING); //order
_cancellationReason = reason;

final TransferController transferController = _uploadController;
cancellingAdvertisement(reason); //order
setState(EAndroidFileUploaderState.CANCELLING); //order
final TransferController transferController = _uploadController; //order
if (transferController == null)
return;

Expand Down Expand Up @@ -434,7 +437,12 @@ public void fileUploadedAdvertisement(final String remoteFilePath)
//this method is intentionally empty its meant to be overridden by csharp binding libraries to intercept updates
}

public void cancelledAdvertisement()
public void cancellingAdvertisement(final String reason)
{
//this method is intentionally empty its meant to be overridden by csharp binding libraries to intercept updates
}

public void cancelledAdvertisement(final String reason)
{
//this method is intentionally empty its meant to be overridden by csharp binding libraries to intercept updates
}
Expand Down Expand Up @@ -501,7 +509,7 @@ public void onUploadCanceled()
{
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0);
setState(EAndroidFileUploaderState.CANCELLED);
cancelledAdvertisement();
cancelledAdvertisement(_cancellationReason);
setLoggingEnabled(true);
busyStateChangedAdvertisement(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class IOSFileUploader: NSObject {
private var _cbPeripheral: CBPeripheral!
private var _currentState: EIOSFileUploaderState = .none
private var _fileSystemManager: FileSystemManager!
private var _cancellationReason: String = ""
private var _lastFatalErrorMessage: String = ""
private var _remoteFilePathSanitized: String!

Expand Down Expand Up @@ -186,7 +187,10 @@ public class IOSFileUploader: NSObject {
}

@objc
public func cancel() {
public func cancel(_ reason: String) {
_cancellationReason = reason

cancellingAdvertisement(reason)
setState(.cancelling) //order

_fileSystemManager?.cancelTransfer() //order
Expand Down Expand Up @@ -293,8 +297,13 @@ public class IOSFileUploader: NSObject {
}

//@objc dont
private func cancelledAdvertisement() {
_listener.cancelledAdvertisement()
private func cancellingAdvertisement(_ reason: String) {
_listener.cancellingAdvertisement(reason)
}

//@objc dont
private func cancelledAdvertisement(_ reason: String) {
_listener.cancelledAdvertisement(reason)
}

//@objc dont
Expand Down Expand Up @@ -365,7 +374,7 @@ extension IOSFileUploader: FileUploadDelegate {
setState(EIOSFileUploaderState.cancelled)
busyStateChangedAdvertisement(false)
fileUploadProgressPercentageAndDataThroughputChangedAdvertisement(0, 0)
cancelledAdvertisement()
cancelledAdvertisement(_cancellationReason)
}

public func uploadDidFinish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ public protocol IOSListenerForFileUploader {
func logMessageAdvertisement(_ message: String, _ category: String, _ level: String, _ resource: String)
func fatalErrorOccurredAdvertisement(_ resource: String, _ errorMessage: String, _ errorCode: Int)

func cancelledAdvertisement()
func cancelledAdvertisement(_ reason: String)
func cancellingAdvertisement(_ reason: String)

func stateChangedAdvertisement(_ resource: String, _ oldState: EIOSFileUploaderState, _ newState: EIOSFileUploaderState)
func fileUploadedAdvertisement(_ resource: String)
func busyStateChangedAdvertisement(_ busyNotIdle: Bool)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public async Task SingleFileUploadAsync_ShouldThrowUploadCancelledException_Give
// Arrange
var mockedFileData = new byte[] { 1, 2, 3 };
const string remoteFilePath = "/path/to/file.bin";
const string cancellationReason = "blah blah foobar";

var mockedNativeFileUploaderProxy = new MockedGreenNativeFileUploaderProxySpy3(new GenericNativeFileUploaderCallbacksProxy_(), isCancellationLeadingToSoftLanding);
var fileUploader = new McuMgr.FileUploader.FileUploader(mockedNativeFileUploaderProxy);
Expand All @@ -31,14 +32,16 @@ public async Task SingleFileUploadAsync_ShouldThrowUploadCancelledException_Give
{
await Task.Delay(500);

fileUploader.Cancel();
fileUploader.Cancel(reason: cancellationReason);
});
var work = new Func<Task>(() => fileUploader.UploadAsync(mockedFileData, remoteFilePath));

// Assert
await work.Should().ThrowExactlyAsync<UploadCancelledException>().WithTimeoutInMs((int)5.Seconds().TotalMilliseconds);

mockedNativeFileUploaderProxy.CancelCalled.Should().BeTrue();
mockedNativeFileUploaderProxy.CancellationReason.Should().Be(cancellationReason);

mockedNativeFileUploaderProxy.DisconnectCalled.Should().BeFalse(); //00
mockedNativeFileUploaderProxy.BeginUploadCalled.Should().BeTrue();

Expand Down Expand Up @@ -67,19 +70,20 @@ public MockedGreenNativeFileUploaderProxySpy3(INativeFileUploaderCallbacksProxy
_isCancellationLeadingToSoftLanding = isCancellationLeadingToSoftLanding;
}

public override void Cancel()
public override void Cancel(string reason = "")
{
base.Cancel();
base.Cancel(reason);

Task.Run(async () => // under normal circumstances the native implementation will bubble up these events in this exact order
{
CancellingAdvertisement(reason); // order
StateChangedAdvertisement(_currentRemoteFilePath, oldState: EFileUploaderState.Idle, newState: EFileUploaderState.Cancelling); // order

await Task.Delay(100);
if (_isCancellationLeadingToSoftLanding) //00
{
StateChangedAdvertisement(_currentRemoteFilePath, oldState: EFileUploaderState.Idle, newState: EFileUploaderState.Cancelled); // order
CancelledAdvertisement(); // order
CancelledAdvertisement(reason); // order
}
});

Expand Down
11 changes: 8 additions & 3 deletions Laerdal.McuMgr.Tests/FileUploader/FileUploaderTestbed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public partial class FileUploaderTestbed
public bool CancelCalled { get; private set; }
public bool DisconnectCalled { get; private set; }
public bool BeginUploadCalled { get; private set; }
public string CancellationReason { get; private set; }

public string LastFatalErrorMessage => "";

Expand Down Expand Up @@ -43,18 +44,22 @@ public virtual EFileUploaderVerdict BeginUpload(
return EFileUploaderVerdict.Success;
}

public virtual void Cancel()
public virtual void Cancel(string reason = "")
{
CancelCalled = true;
CancellationReason = reason;
}

public virtual void Disconnect()
{
DisconnectCalled = true;
}

public void CancelledAdvertisement()
=> _uploaderCallbacksProxy.CancelledAdvertisement(); //raises the actual event
public void CancellingAdvertisement(string reason = "")
=> _uploaderCallbacksProxy.CancellingAdvertisement(reason); //raises the actual event

public void CancelledAdvertisement(string reason = "")
=> _uploaderCallbacksProxy.CancelledAdvertisement(reason); //raises the actual event

public void LogMessageAdvertisement(string message, string category, ELogLevel level, string resource)
=> _uploaderCallbacksProxy.LogMessageAdvertisement(message, category, level, resource); //raises the actual event
Expand Down
20 changes: 16 additions & 4 deletions Laerdal.McuMgr/Droid/FileUploader/FileUploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static private INativeFileUploaderProxy ValidateArgumentsAndConstructProxy(Bluet
);
}

private sealed class AndroidFileUploaderProxy : AndroidFileUploader, INativeFileUploaderProxy
private sealed class AndroidFileUploaderProxy : AndroidFileUploader, INativeFileUploaderProxy
{
private readonly INativeFileUploaderCallbacksProxy _fileUploaderCallbacksProxy;

Expand Down Expand Up @@ -117,6 +117,11 @@ public EFileUploaderVerdict BeginUpload(
memoryAlignment: memoryAlignment ?? -1
));
}

public new void Cancel(string reason = "")
{
base.Cancel(reason);
}

public bool TrySetContext(object context) //the parameter must be of type 'object' so that it wont cause problems in platforms other than android
{
Expand Down Expand Up @@ -182,12 +187,19 @@ public void LogMessageAdvertisement(string message, string category, ELogLevel l
resource: resource //essentially the remote filepath
);
}

public override void CancellingAdvertisement(string reason)
{
base.CancellingAdvertisement(reason); //just in case

_fileUploaderCallbacksProxy?.CancellingAdvertisement(reason);
}

public override void CancelledAdvertisement()
public override void CancelledAdvertisement(string reason)
{
base.CancelledAdvertisement(); //just in case
base.CancelledAdvertisement(reason); //just in case

_fileUploaderCallbacksProxy?.CancelledAdvertisement();
_fileUploaderCallbacksProxy?.CancelledAdvertisement(reason);
}

public override void FileUploadedAdvertisement(string resource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@ namespace Laerdal.McuMgr.FileUploader.Contracts.Events
{
public readonly struct CancelledEventArgs : IMcuMgrEventArgs
{
public string Reason { get; }

public CancelledEventArgs(string reason)
{
Reason = reason;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Laerdal.McuMgr.Common.Events;

namespace Laerdal.McuMgr.FileUploader.Contracts.Events
{
public readonly struct CancellingEventArgs : IMcuMgrEventArgs
{
public string Reason { get; }

public CancellingEventArgs(string reason)
{
Reason = reason;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace Laerdal.McuMgr.FileUploader.Contracts.Exceptions
{
public class UploadCancelledException : Exception, IUploadException
{
public UploadCancelledException() : base("Upload was cancelled")
public UploadCancelledException(string optionalReason = "")
: base($"Upload was cancelled{(string.IsNullOrWhiteSpace(optionalReason) ? "" : $": {optionalReason}")}")
{
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ EFileUploaderVerdict BeginUpload(
bool TrySetBluetoothDevice(object bluetoothDevice);

/// <summary>Cancels the file-uploading process</summary>
void Cancel();
/// <param name="reason">(optional) The reason for the cancellation</param>
void Cancel(string reason = "");

/// <summary>Disconnects the file-uploader from the targeted device</summary>
void Disconnect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Laerdal.McuMgr.FileUploader.Contracts
internal interface IFileUploaderEventEmittable
{
void OnCancelled(CancelledEventArgs ea);
void OnCancelling(CancellingEventArgs ea);
void OnLogEmitted(LogEmittedEventArgs ea);
void OnStateChanged(StateChangedEventArgs ea);
void OnFileUploaded(FileUploadedEventArgs ea);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ internal interface INativeFileUploaderCallbacksProxy
{
public IFileUploaderEventEmittable FileUploader { get; set; }

void CancelledAdvertisement();
void CancelledAdvertisement(string reason = "");
void CancellingAdvertisement(string reason = "");

void LogMessageAdvertisement(string message, string category, ELogLevel level, string resource);
void StateChangedAdvertisement(string resource, EFileUploaderState oldState, EFileUploaderState newState);
void BusyStateChangedAdvertisement(bool busyNotIdle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Laerdal.McuMgr.FileUploader.Contracts.Native
{
internal interface INativeFileUploaderCommandableProxy
{
void Cancel();
void Cancel(string reason = "");
void Disconnect();

EFileUploaderVerdict BeginUpload(
Expand Down
Loading

0 comments on commit 304492e

Please sign in to comment.