Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactored callback as tuple for labels #55

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions source/Kangaroo.UI/Services/IScannerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using Kangaroo.UI.Controls;

namespace Kangaroo.UI.Services;

/// <summary>
/// Creates scanners from configuration options.
/// </summary>
public interface IScannerFactory
{
/// <summary>
/// Provides the scan and the required contextual data to run and display a scan.
/// </summary>
Action<(IScanner? scanner, ScanConfiguration? configuration, bool valid)>? OnScannerCreated { get; set; }

/// <summary>
/// Creates the correct scanner for the data provided
/// </summary>
/// <param name="options">scan configuration data</param>
void CreateScanner(ScanConfiguration options);
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,36 @@
using Kangaroo.UI.Models;
using Kangaroo.UI.Controls;
using Kangaroo.UI.Models;
using System;

namespace Kangaroo.UI.Controls;

public interface IScannerFactory
{
Action<IScanner?, ScanConfiguration?, bool>? OnScannerCreated { get; set; }

void CreateScanner(ScanConfiguration options);
}

namespace Kangaroo.UI.Services;

/// <inheritdoc />
public sealed class ScannerFactory : IScannerFactory
{
/// <inheritdoc />
public Action<(IScanner? scanner, ScanConfiguration? configuration, bool valid)>? OnScannerCreated { get; set; }

public Action<IScanner?, ScanConfiguration?, bool>? OnScannerCreated { get; set; }

/// <inheritdoc />
public void CreateScanner(ScanConfiguration options)
{
switch (options.ScanMode)
var scanData = options.ScanMode switch
{
case ScanMode.AddressRange:
CreateRangeScanner(options);
break;
case ScanMode.NetworkSubnet:
CreateSubnetScanner(options);
break;
case ScanMode.NetworkAdapter:
CreateAdapterScanner(options);
break;
case ScanMode.SingleAddress:
CreateSingleScanner(options);
break;
case ScanMode.SpecifiedAddresses:
break;
default:
throw new ArgumentOutOfRangeException();
}
ScanMode.AddressRange => CreateRangeScanner(options),
ScanMode.NetworkSubnet => CreateSubnetScanner(options),
ScanMode.NetworkAdapter => CreateAdapterScanner(options),
ScanMode.SingleAddress => CreateSingleScanner(options),
ScanMode.SpecifiedAddresses => throw new ArgumentOutOfRangeException(),
_ => throw new ArgumentOutOfRangeException()
};

OnScannerCreated?.Invoke(scanData);
}

private void CreateRangeScanner(ScanConfiguration options)
private (IScanner scanner, ScanConfiguration config, bool isValid) CreateRangeScanner(ScanConfiguration options)
{
ArgumentNullException.ThrowIfNull(options.StartAddress);
ArgumentNullException.ThrowIfNull(options.EndAddress);

var scanner = new ScannerBuilder()
.WithRange(options.StartAddress, options.EndAddress)
.WithOptions(ops =>
Expand All @@ -55,13 +42,13 @@ private void CreateRangeScanner(ScanConfiguration options)
.WithParallelism()
.Build();

OnScannerCreated?.Invoke(scanner, options, true);
return (scanner, options, true);
}

private void CreateSingleScanner(ScanConfiguration options)
private (IScanner scanner, ScanConfiguration config, bool isValid) CreateSingleScanner(ScanConfiguration options)
{
ArgumentNullException.ThrowIfNull(options.SpecificAddress);

var scanner = new ScannerBuilder()
.WithAddress(options.SpecificAddress)
.WithOptions(ops =>
Expand All @@ -73,13 +60,13 @@ private void CreateSingleScanner(ScanConfiguration options)
.WithParallelism()
.Build();

OnScannerCreated?.Invoke(scanner, options, true);
return (scanner, options, true);
}
private void CreateSubnetScanner(ScanConfiguration options)
private (IScanner scanner, ScanConfiguration config, bool isValid) CreateSubnetScanner(ScanConfiguration options)
{
ArgumentNullException.ThrowIfNull(options.SpecificAddress);
ArgumentNullException.ThrowIfNull(options.NetmaskAddress);

var scanner = new ScannerBuilder()
.WithSubnet(options.SpecificAddress, options.NetmaskAddress)
.WithOptions(ops =>
Expand All @@ -91,10 +78,10 @@ private void CreateSubnetScanner(ScanConfiguration options)
.WithParallelism()
.Build();

OnScannerCreated?.Invoke(scanner, options, true);
return (scanner, options, true);
}

private void CreateAdapterScanner(ScanConfiguration options)
private (IScanner scanner, ScanConfiguration config, bool isValid) CreateAdapterScanner(ScanConfiguration options)
{
ArgumentNullException.ThrowIfNull(options.NetworkInterface);

Expand All @@ -109,6 +96,6 @@ private void CreateAdapterScanner(ScanConfiguration options)
.WithParallelism()
.Build();

OnScannerCreated?.Invoke(scanner, options, true);
return (scanner, options, true);
}
}
9 changes: 5 additions & 4 deletions source/Kangaroo.UI/ViewModels/IpScannerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CommunityToolkit.Mvvm.Input;
using Kangaroo.UI.Controls;
using Kangaroo.UI.Models;
using Kangaroo.UI.Services;
using Kangaroo.UI.Services.Database;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView;
Expand Down Expand Up @@ -37,12 +38,12 @@ public IpScannerViewModel(RecentScansRepository recentScans, IScannerFactory fac
{
_recentScans = recentScans;
_factory = factory;
factory.OnScannerCreated = (scanner, options, valid) =>
factory.OnScannerCreated = scannerData =>
{
_scanner?.Dispose();
ScanEnabled = valid && scanner is not null;
_scanner = scanner;
_configuration = options;
ScanEnabled = scannerData is { valid: true, scanner: not null };
_scanner = scannerData.scanner;
_configuration = scannerData.configuration;
};
LoadRecent().SafeFireAndForget();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Net;
using System.Net.Sockets;
using Kangaroo.UI.Services;

namespace Kangaroo.UI.Controls;
public partial class ScanConfiguratorViewModel : ViewModelBase
Expand Down