Skip to content

Commit

Permalink
First release
Browse files Browse the repository at this point in the history
  • Loading branch information
omidmohajers committed Jan 30, 2023
1 parent c2aaa24 commit 2ba7b6a
Show file tree
Hide file tree
Showing 8 changed files with 459 additions and 53 deletions.
22 changes: 12 additions & 10 deletions PA.SSH.Wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:viewmodels="clr-namespace:PA.SSH.Wpf.ViewModels"
d:DataContext="{d:DesignInstance Type=viewmodels:SshControlViewModel}"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="MainWindow" Height="450" Width="800" WindowStartupLocation="CenterScreen" WindowState="Maximized">
<Window.Resources>
<local:StatusTypeToBackgroudConverter x:Key="TypeToBackgroudConverter"/>
</Window.Resources>
Expand All @@ -23,18 +23,20 @@
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ToolBar>
<Button Content="Load" Margin="5" Padding="5" Command="{Binding LoadCommand}"/>
<Button Content="Save" Margin="5" Padding="5" Command="{Binding SaveProfilesCommand}"/>
<Button Content="Load" Margin="5" Padding="5" Command="{Binding LoadCommand}" IsEnabled="{Binding IsIdle}"/>
<Button Content="Save" Margin="5" Padding="5" Command="{Binding SaveProfilesCommand}" IsEnabled="{Binding IsIdle}" />
<Button Content="SaveAs" Margin="5" Padding="5" Command="{Binding SaveAsProfilesCommand}" IsEnabled="{Binding IsIdle}"/>
<Separator/>
<Button Content="New" Margin="5" Padding="5" Command="{Binding AddCommand}"/>
<Button Content="Edit" Margin="5" Padding="5" Command="{Binding EditCommand}"/>
<Button Content="Delete" Margin="5" Padding="5" Command="{Binding RemoveCommand}"/>
<Button Content="Delete" Margin="5" Padding="5" Command="{Binding RemoveCommand}" IsEnabled="{Binding IsIdle}"/>
</ToolBar>
<ToolBar Grid.Column="1">
<CheckBox IsChecked="{Binding RunAsAnyPortMethod}" Content="Any Port Response Method" Margin="5" Padding="5" />
<Button Content="Start" Margin="5" Padding="5" Command="{Binding StartCommand}"/>
<Button Content="Save (Best Ping)" Margin="5" Padding="5" Command="{Binding SaveSuccessListByPingCommand}"/>
<Button Content="Save (Best Response)" Margin="5" Padding="5" Command="{Binding SaveSuccessListByResponseCommand}"/>
<CheckBox IsChecked="{Binding RunAsAnyPortMethod}" Content="Any Port Response Method" Margin="5" Padding="5" IsEnabled="{Binding IsIdle}"/>
<Separator/>
<Button Content="Start" Margin="5" Padding="5" Command="{Binding StartCommand}" IsEnabled="{Binding IsIdle}"/>
<Button Content="Stop" Margin="5" Padding="5" Command="{Binding StopCommand}" IsEnabled="{Binding IsListenToCancel}"/>
<Separator/>
<Button Content="Save (Best Ping)" Margin="5" Padding="5" Command="{Binding SaveSuccessListByPingCommand}" IsEnabled="{Binding IsFinished}"/>
<Button Content="Save (Best Response)" Margin="5" Padding="5" Command="{Binding SaveSuccessListByResponseCommand}" IsEnabled="{Binding IsFinished}"/>
</ToolBar>
<ListView Grid.Row="2" ItemsSource="{Binding PublicLog}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListView.ItemTemplate>
Expand Down
5 changes: 5 additions & 0 deletions PA.SSH.Wpf/PA.SSH.Wpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,10 @@
<Name>PA.SSH</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Content Include="input.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion PA.SSH.Wpf/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PA.SSH.Wpf")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyCopyright("Copyright © omidmohajers 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
129 changes: 102 additions & 27 deletions PA.SSH.Wpf/ViewModels/SshControlViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,141 @@
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace PA.SSH.Wpf.ViewModels
{
public class SshControlViewModel : BindableBase
{
#region Fileds
private SshConnectionChecker connectionChecker = null;
private double passCount;
private bool runAsAnyPortMethod;
private bool runAsAnyPortMethod;
private bool isRunning;
private bool isListenToCancel;
private List<SshConnectionChecker> checkers;
private const string db = "input.txt";
#endregion Fileds

#region Properties
public bool IsListenToCancel
{
get => isListenToCancel;
set
{
SetProperty(ref isListenToCancel, value);
}
}
public bool IsRunning
{
get => isRunning;
set
{
SetProperty(ref isRunning, value);
RaisePropertyChanged("IsIdle");
RaisePropertyChanged("IsFinished");
}
}
public bool IsIdle { get => !IsRunning; }
public double PassCount { get => passCount; set => SetProperty(ref passCount, value); }
public bool RunAsAnyPortMethod { get => runAsAnyPortMethod; set => SetProperty(ref runAsAnyPortMethod, value); }
public DelegateCommand StopCommand { get; private set; }
public DelegateCommand LoadCommand { get; private set; }
public DelegateCommand SaveProfilesCommand { get; private set; }
public DelegateCommand AddCommand { get; private set; }
public DelegateCommand EditCommand { get; private set; }
public DelegateCommand SaveAsProfilesCommand { get; private set; }
public DelegateCommand RemoveCommand { get; private set; }
public DelegateCommand StartCommand { get; private set; }
public DelegateCommand SaveSuccessListByPingCommand { get; private set; }
public DelegateCommand SaveSuccessListByResponseCommand { get; private set; }

public ObservableCollection<SshProfile> SshProfiles { get; private set; }
public ObservableCollection<SshConnectionStatus> PublicLog { get; private set; }
public ObservableCollection<SshConnectionStatus> SuccessLog { get; private set; }
public SshProfile SelectedProfile { get; set; }
public bool IsFinished
{
get
{
return PassCount == SshProfiles.Count || IsCanceled;
}
}
public bool IsCanceled { get; private set; }
#endregion Properties

public SshControlViewModel()
{
IsListenToCancel = false;
PassCount = 0;
IsCanceled = false;
IsRunning = false;
SshProfiles = new ObservableCollection<SshProfile>();
PublicLog = new ObservableCollection<SshConnectionStatus>();
SuccessLog = new ObservableCollection<SshConnectionStatus>();
LoadCommand = new DelegateCommand(LoadProfiles, () => true);
AddCommand = new DelegateCommand(Add, () => true);
EditCommand = new DelegateCommand(Edit, () => true);
SaveAsProfilesCommand = new DelegateCommand(SaveAsProfiles, () => true);
RemoveCommand = new DelegateCommand(Remove, () => true);
SaveProfilesCommand = new DelegateCommand(SaveProfiles, () => true);
SaveSuccessListByPingCommand = new DelegateCommand(SaveSuccessListByPing, () => true);
SaveSuccessListByResponseCommand = new DelegateCommand(SaveSuccessListByResponse, () => true);
StartCommand = new DelegateCommand(StartAsync, () => true);
StopCommand = new DelegateCommand(Stop, () => true);
InitilizeData();
}

#region Private Methods
private void SaveAsProfiles()
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Text Files|*.txt";
if (dlg.ShowDialog() ?? false)
SaveToFile(dlg.FileName);
}
private void StartAsync()
{
if (SshProfiles.Count == 0)
return;
IsListenToCancel = true;
PassCount = 0;
PublicLog.Clear();
SuccessLog.Clear();
IsCanceled = false;
IsRunning = true;
checkers = new List<SshConnectionChecker>();
foreach(SshProfile prof in SshProfiles)
{
if (IsCanceled)
{
IsRunning = false;
break;
}
connectionChecker = new SshConnectionChecker();
connectionChecker.AnyPort = RunAsAnyPortMethod;
connectionChecker.Passed += ConnectionChecker_Passed;
connectionChecker.LogChanged += ConnectionChecker_LogChanged;
connectionChecker.Profile = prof;
connectionChecker.ConnectAsync();
checkers.Add(connectionChecker);
}
}

private void Stop()
{
BroadcastCancelation();
IsCanceled = true;
IsListenToCancel = false;
MessageBox.Show("Process Canceled By User!!!\nMaybe last Process only countinue working...");
}
private void BroadcastCancelation()
{
foreach (SshConnectionChecker checker in checkers)
checker.CancelOperation();
}
private void ConnectionChecker_Passed(object sender, EventArgs e)
{
if (++PassCount == SshProfiles.Count)
{
MessageBox.Show("All Profiles Checked!!!");
IsRunning = false;
}
}

private void ConnectionChecker_LogChanged(object sender, SshConnectionStatus e)
{
switch (e.Type)
Expand All @@ -95,7 +168,6 @@ private void ConnectionChecker_LogChanged(object sender, SshConnectionStatus e)
break;
}
}

private void LoadProfiles()
{
OpenFileDialog dlg = new OpenFileDialog();
Expand All @@ -114,13 +186,11 @@ private void LoadProfiles()
SelectedProfile = SshProfiles[0];
}
}

private void SaveSuccessListByResponse()
{
var data = SuccessLog.Where(log => log.Type == StatusType.Done).OrderBy(o => o.Duration);
SaveSuccessList(data);
}

private void SaveSuccessList(IOrderedEnumerable<SshConnectionStatus> data)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -134,36 +204,41 @@ private void SaveSuccessList(IOrderedEnumerable<SshConnectionStatus> data)
if (dlg.ShowDialog() ?? false)
File.WriteAllText(dlg.FileName, sb.ToString());
}

private void SaveSuccessListByPing()
{
var data = SuccessLog.Where(log => log.Type == StatusType.Done).OrderBy(o => o.PingAvrage);
SaveSuccessList(data);
}

private void SaveProfiles()
{
throw new NotImplementedException();
SaveToFile(db);
}

private void Remove()
{
throw new NotImplementedException();
}

private void Edit()
private void SaveToFile(string fileName)
{
throw new NotImplementedException();
StringBuilder sb = new StringBuilder();
foreach (SshProfile profile in SshProfiles)
sb.AppendLine(profile.Serialize());
File.WriteAllText(fileName, sb.ToString());
}

private void Add()
private void Remove()
{
throw new NotImplementedException();
if (SelectedProfile == null)
return;
SshProfiles.Remove(SelectedProfile);
}

private void InitilizeData()
{

string[] lines = File.ReadAllLines(db);
SshProfiles.Clear();
foreach (string line in lines)
{
SshProfile prof = new SshProfile();
prof.Deserialize(line);
SshProfiles.Add(prof);
}
if (SshProfiles.Count > 0)
SelectedProfile = SshProfiles[0];
}
#endregion Private methods
}
}
Loading

0 comments on commit 2ba7b6a

Please sign in to comment.