Skip to content

Commit

Permalink
Merge pull request #1 from GyroGearl00se/presetservice
Browse files Browse the repository at this point in the history
Presetservice
  • Loading branch information
GyroGearl00se authored Jul 20, 2024
2 parents 5e376df + 876d148 commit 860dd8d
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 65 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,7 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd

trustedca/*
!trustedca/.keep
91 changes: 73 additions & 18 deletions Components/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
@inject SolaceSubscribeService SubscribeService
@inject Blazored.Toast.Services.IToastService toastService
@implements IDisposable
@inject PresetService PresetService

<h3>Solace PubSub+</h3>

<div class="form-container">
<div class="form-section">
<h4>Publish Message</h4>
<div class="form-group">
<label>Select Preset</label>
<select class="form-select-sm" @onchange="LoadPresetPublish">
<option value="">Select a preset</option>
@foreach (var preset in presets)
{
<option value="@preset.Name">@preset.Name</option>
}
</select>
</div>
<div class="form-group">
<div class="input-with-checkbox">
<div class="input-container">
<label>Hostname:Port</label>
<input @bind="publishHost" name="publishHost" autocomplete="on" placeholder="tcps://broker.domain:55443" />
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="publishUseTcps" id="publishUseTcps" />
<label for="publishUseTcps">TCPS</label>
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="publishSslVerify" id="publishSslVerify" />
<label for="publishSslVerify">SSL Verify</label>
Expand Down Expand Up @@ -52,16 +59,22 @@

<div class="form-section">
<h4>Subscribe to Topic</h4>
<div class="form-group">
<label>Select Preset</label>
<select class="form-select-sm" @onchange="LoadPresetSubscribe">
<option value="">Select a preset</option>
@foreach (var preset in presets)
{
<option value="@preset.Name">@preset.Name</option>
}
</select>
</div>
<div class="form-group">
<div class="input-with-checkbox">
<div class="input-container">
<label>Hostname:Port</label>
<input @bind="subscribeHost" name="subscribeHost" autocomplete="on" placeholder="tcps://broker.domain:55443" />
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="subscribeUseTcps" id="subscribeUseTcps" />
<label for="subscribeUseTcps">TCPS</label>
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="subscribeSslVerify" id="subscribeSslVerify" />
<label for="subscribeSslVerify">SSL Verify</label>
Expand Down Expand Up @@ -105,33 +118,76 @@
private string publishPassword;
private string publishTopic;
private string publishMessage;
private bool publishUseTcps = true;
private bool publishSslVerify = true;
private string subscribeHost;
private string subscribeVpnName;
private string subscribeUsername;
private string subscribePassword;
private string subscribeTopic;
private bool subscribeSslVerify = true;
private bool subscribeUseTcps = true;
private string selectedPresetName;
private string presetName;
private List<PresetModel> presets = new List<PresetModel>();

private List<string> messages = new List<string>();

private string FormatHost(string host, bool useTcps)
protected override async Task OnInitializedAsync()
{
presets = await PresetService.GetPresetsAsync();
}

private async Task LoadPresetPublish(ChangeEventArgs e)
{
if (useTcps && !host.StartsWith("tcps://"))
selectedPresetName = e.Value?.ToString();
Console.WriteLine($"Selected Preset Name: {selectedPresetName}");
if (!string.IsNullOrEmpty(selectedPresetName))
{
return "tcps://" + host;
var preset = presets.FirstOrDefault(p => p.Name == selectedPresetName);
if (preset != null)
{
publishHost = preset.Host;
publishVpnName = preset.VpnName;
publishUsername = preset.Username;
publishTopic = preset.Topic;

Console.WriteLine($"Loaded Preset: {preset.Name}");
}
else
{
Console.WriteLine($"Preset with name '{selectedPresetName}' not found.");
}
}
}

private async Task LoadPresetSubscribe(ChangeEventArgs e)
{
selectedPresetName = e.Value?.ToString();
Console.WriteLine($"Selected Preset Name: {selectedPresetName}");
if (!string.IsNullOrEmpty(selectedPresetName))
{
var preset = presets.FirstOrDefault(p => p.Name == selectedPresetName);
if (preset != null)
{
subscribeHost = preset.Host;
subscribeVpnName = preset.VpnName;
subscribeUsername = preset.Username;
subscribeTopic = preset.Topic;

Console.WriteLine($"Loaded Preset: {preset.Name}");
}
else
{
Console.WriteLine($"Preset with name '{selectedPresetName}' not found.");
}
}
return host;
}


private void ConnectAndPublish()
{
try
{
string formattedHost = FormatHost(publishHost, publishUseTcps);
PublishService.PublishMessage(formattedHost, publishVpnName, publishUsername, publishPassword, publishTopic, publishMessage, publishSslVerify);
PublishService.PublishMessage(publishHost, publishVpnName, publishUsername, publishPassword, publishTopic, publishMessage, publishSslVerify);
toastService.ShowSuccess("Successfully published message");
}
catch (SolaceSystems.Solclient.Messaging.OperationErrorException ex)
Expand All @@ -144,8 +200,7 @@
{
try
{
string formattedHost = FormatHost(subscribeHost, subscribeUseTcps);
SubscribeService.SubscribeToTopic(formattedHost, subscribeVpnName, subscribeUsername, subscribePassword, subscribeTopic, subscribeSslVerify, message =>
SubscribeService.SubscribeToTopic(subscribeHost, subscribeVpnName, subscribeUsername, subscribePassword, subscribeTopic, subscribeSslVerify, message =>
{
InvokeAsync(() =>
{
Expand Down
87 changes: 56 additions & 31 deletions Components/Pages/QueueBrowser.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@
@inject ILogger<QueueBrowser> Logger
@inject Blazored.Toast.Services.IToastService toastService
@inject NavigationManager Navigation
@inject PresetService PresetService

<Layout>
<Title>Queue Browser</Title>

<div class="form-group">
<label>Select Preset</label>
<select class="form-select-sm" @onchange="LoadPreset">
<option value="">Select a preset</option>
@foreach (var preset in presets)
{
<option value="@preset.Name">@preset.Name</option>
}
</select>
</div>

<div class="overlay" style="display: @(isDeleting ? "block" : "none")">
<div class="loading-message-delete">
<p>Deleting Message</p>
Expand All @@ -28,10 +40,6 @@
<label>Hostname:Port</label>
<input @bind="host" name="host" autocomplete="on" placeholder="tcps://broker.domain:55443" />
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="useTcps" id="useTcps" />
<label for="useTcps">TCPS</label>
</div>
<div class="checkbox-container">
<input type="checkbox" @bind="sslVerify" id="sslVerify" />
<label for="sslVerify">SSL Verify</label>
Expand Down Expand Up @@ -61,19 +69,17 @@
<button class="btn btn-primary" @onclick="BrowseQueueAsync">Browse Queue</button>

<h4>Messages</h4>
<DataGrid
TItem="MessageDetails"
Data="@messages"
@ref="dataGrid"
RowOverlayBackground="Background.Transparent"
DetailRowStartsVisible="false"
Filterable
FilterMethod="DataGridFilterMethod.Contains"
ShowPager
ShowPageSizes
PagerPosition="DataGridPagerPosition.TopAndBottom"
PagerOptions="new(){ ButtonSize=Size.Small}"
>
<DataGrid TItem="MessageDetails"
Data="@messages"
@ref="dataGrid"
RowOverlayBackground="Background.Transparent"
DetailRowStartsVisible="false"
Filterable
FilterMethod="DataGridFilterMethod.StartsWith"
ShowPager
ShowPageSizes
PagerPosition="DataGridPagerPosition.TopAndBottom"
PagerOptions="new(){ ButtonSize=Size.Small}">
<TotalItemsTemplate><Badge Color="Color.Success">@context.TotalItems total items</Badge></TotalItemsTemplate>
<TotalItemsShortTemplate><Badge Color="Color.Success">@context.TotalItems</Badge></TotalItemsShortTemplate>
<DataGridColumns>
Expand Down Expand Up @@ -139,29 +145,21 @@
private string queueName;
private int maxMessages = 50;
private bool sslVerify = true;
private bool useTcps = true;
private List<MessageDetails> messages = new List<MessageDetails>();
private DataGrid<MessageDetails> dataGrid;
private string selectedPresetName;
private string presetName;
private List<PresetModel> presets = new List<PresetModel>();

private bool isBrowsing = false;

private string FormatHost(string host, bool useTcps)
{
if (useTcps && !host.StartsWith("tcps://"))
{
return "tcps://" + host;
}
return host;
}

private async Task BrowseQueueAsync()
{
Logger.LogInformation("BrowseQueue method called.");
try
{
string formattedHost = FormatHost(host, useTcps);
isBrowsing = true;
messages = await QueueBrowserService.BrowseQueueAsync(formattedHost, vpnName, username, password, queueName, sslVerify, maxMessages);
messages = await QueueBrowserService.BrowseQueueAsync(host, vpnName, username, password, queueName, sslVerify, maxMessages);
Logger.LogInformation("Messages retrieved: {count}", messages.Count);
}
catch (SolaceSystems.Solclient.Messaging.OperationErrorException ex)
Expand All @@ -184,10 +182,9 @@
{
try
{
string formattedHost = FormatHost(host, useTcps);
isDeleting = true;
deletingMessageId = adMessageId.ToString();
await QueueBrowserService.DeleteMessage(formattedHost, vpnName, username, password, queueName, sslVerify, adMessageId);
await QueueBrowserService.DeleteMessage(host, vpnName, username, password, queueName, sslVerify, adMessageId);
toastService.ShowSuccess("Deleting message with ID: " + deletingMessageId);
await BrowseQueueAsync();
}
Expand All @@ -203,6 +200,11 @@
}
}

protected override async Task OnInitializedAsync()
{
presets = await PresetService.GetPresetsAsync();
}

protected override void OnInitialized()
{
var uri = Navigation.ToAbsoluteUri(Navigation.Uri);
Expand Down Expand Up @@ -235,4 +237,27 @@

password = string.Empty;
}

private async Task LoadPreset(ChangeEventArgs e)
{
selectedPresetName = e.Value?.ToString();
Console.WriteLine($"Selected Preset Name: {selectedPresetName}");
if (!string.IsNullOrEmpty(selectedPresetName))
{
var preset = presets.FirstOrDefault(p => p.Name == selectedPresetName);
if (preset != null)
{
host = preset.Host;
vpnName = preset.VpnName;
username = preset.Username;
queueName = preset.QueueName;

Console.WriteLine($"Loaded Preset: {preset.Name}");
}
else
{
Console.WriteLine($"Preset with name '{selectedPresetName}' not found.");
}
}
}
}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
COPY trustedca ./trustedca
COPY presets ./presets
ENTRYPOINT ["dotnet", "SolaceWebClient.dll"]
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
builder.Services.AddSingleton<SolaceSubscribeService>();

builder.Services.AddSingleton<QueueBrowserService>();
builder.Services.AddSingleton<PresetService>();

builder.Services.AddBlazoredToast();
builder.Services.AddHttpClient<SEMPService>();
Expand Down
50 changes: 50 additions & 0 deletions Services/PresetService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Blazored.Toast.Services;
using Blazorise;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;

namespace SolaceWebClient.Services
{
public class PresetModel
{
public string Name { get; set; }
public string Host { get; set; }
public string VpnName { get; set; }
public string Username { get; set; }
public string QueueName { get; set; }
public string Topic { get; set; }
}

public class PresetService
{
private readonly string presetsFilePath = Path.Combine("presets", "presets.json");

public async Task<List<PresetModel>> GetPresetsAsync()
{
if (!File.Exists(presetsFilePath))
{
return new List<PresetModel>();
}

var json = await File.ReadAllTextAsync(presetsFilePath);
var presets = JsonSerializer.Deserialize<List<PresetModel>>(json);

return presets ?? new List<PresetModel>();
}

public async Task SavePresetsAsync(List<PresetModel> presets)
{
var json = JsonSerializer.Serialize(presets, new JsonSerializerOptions { WriteIndented = true });
var directory = Path.GetDirectoryName(presetsFilePath);

if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

await File.WriteAllTextAsync(presetsFilePath, json);
}
}
}
Loading

0 comments on commit 860dd8d

Please sign in to comment.