Connections shit

This commit is contained in:
fabjuuuh
2020-10-14 13:05:56 +02:00
parent 301f6b447f
commit 25176dbaeb
13 changed files with 186 additions and 34 deletions

View File

@@ -14,6 +14,9 @@
<views:LoginView />
<!-- This is a UserControl -->
</DataTemplate>
</Application.Resources>
<DataTemplate DataType="{x:Type viewModels:ClientInfoViewModel}">
<views:ClientInfoView/>
</DataTemplate>
</Application.Resources>
</Application>

View File

@@ -20,6 +20,8 @@ namespace DoctorApp.Utils
private bool sessionRunning = false;
private IHandler handler = null;
private LoginViewModel LoginViewModel;
private MainViewModel MainViewModel;
private ClientInfoViewModel ClientInfoViewModel;
public Client() : this("localhost", 5555)
@@ -119,6 +121,12 @@ namespace DoctorApp.Utils
sendMessage(DataParser.getSetResistanceResponseJson(true));
}
break;
case DataParser.NEW_CONNECTION:
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
case DataParser.DISCONNECT:
this.MainViewModel.NewConnectedUser(DataParser.getUsernameFromResponseJson(payloadbytes));
break;
default:
Console.WriteLine($"Received json with identifier {identifier}:\n{Encoding.ASCII.GetString(payloadbytes)}");
break;
@@ -247,5 +255,15 @@ namespace DoctorApp.Utils
{
this.LoginViewModel = loginViewModel;
}
internal void SetMainViewModel(MainViewModel mainViewModel)
{
this.MainViewModel = mainViewModel;
}
internal void SetClientInfoViewModel(ClientInfoViewModel clientInfoViewModel)
{
this.ClientInfoViewModel = clientInfoViewModel;
}
}
}

View File

@@ -15,6 +15,8 @@ namespace DoctorApp.Utils
public const string START_SESSION = "START SESSION";
public const string STOP_SESSION = "STOP SESSION";
public const string SET_RESISTANCE = "SET RESISTANCE";
public const string NEW_CONNECTION = "NEW CONNECTION";
public const string DISCONNECT = "DISCONNECT";
/// <summary>
/// makes the json object with LOGIN identifier and username and password
/// </summary>
@@ -191,6 +193,24 @@ namespace DoctorApp.Utils
return getJsonMessage(SET_RESISTANCE, data);
}
public static byte[] getNewConnectionJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(NEW_CONNECTION, data);
}
public static byte[] getDisconnectJson(string user)
{
dynamic data = new
{
username = user
};
return getJsonMessage(DISCONNECT, data);
}
public static float getResistanceFromJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.resistance;
@@ -201,6 +221,11 @@ namespace DoctorApp.Utils
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.worked;
}
public static string getUsernameFromResponseJson(byte[] json)
{
return ((dynamic)JsonConvert.DeserializeObject(Encoding.ASCII.GetString(json))).data.username;
}
}
}

View File

@@ -4,7 +4,10 @@ using System.Text;
namespace DoctorApp.ViewModels
{
class ClientInfoViewModel
{
public string Username { get; set; }
public string TabName { get; set; }
}
}

View File

@@ -37,7 +37,9 @@ namespace DoctorApp.ViewModels
this.InvertedLoginStatus = !status;
if (status)
{
this.mainWindowViewModel.SelectedViewModel = new MainViewModel(mainWindowViewModel);
MainViewModel mainViewModel = new MainViewModel(mainWindowViewModel);
this.mainWindowViewModel.client.SetMainViewModel(mainViewModel);
this.mainWindowViewModel.SelectedViewModel = mainViewModel;
}
}
}

View File

@@ -1,12 +1,17 @@
using DoctorApp.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;
using System.Windows.Controls;
namespace DoctorApp.ViewModels
{
class MainViewModel : ObservableObject
{
public ObservableCollection<object> Tabs { get; set; }
public int Selected { get; set; }
public MainWindowViewModel MainWindowViewModel { get; set; }
Client client;
@@ -15,6 +20,26 @@ namespace DoctorApp.ViewModels
{
this.MainWindowViewModel = mainWindowViewModel;
client = this.MainWindowViewModel.client;
Tabs= new ObservableCollection<object>();
}
public void NewConnectedUser(string username)
{
App.Current.Dispatcher.Invoke((Action)delegate
{
Tabs.Add(new ClientInfoViewModel
{
Username = username,
TabName = username
});
});
}
public void DisconnectedUser(string username)
{
}
}
}

View File

@@ -1,14 +1,68 @@
<Page x:Class="DoctorApp.Views.ClientInfoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DoctorApp.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="ClientInfoView">
<Grid>
<UserControl x:Class="DoctorApp.Views.ClientInfoView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DoctorApp.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
<Grid Margin="15,5,15,15">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="43*"/>
<RowDefinition Height="47*"/>
<RowDefinition Height="180*"/>
<RowDefinition Height="180*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.RowSpan="2" Margin="0,0,0,22">
<StackPanel.Resources>
<Style TargetType="{x:Type Label}">
<Setter Property="Margin" Value="0,0,20,0"/>
</Style>
</StackPanel.Resources>
<Label Content="UserName" Name="Username_Label"/>
<Label Content="Status: " Name="Status_Label"/>
</StackPanel>
<StackPanel Margin="0,10,0,0" Grid.RowSpan="2" Grid.Row="1">
<StackPanel.Resources>
<Style TargetType="{x:Type DockPanel}">
<Setter Property="Margin" Value="0,20,0,0"/>
</Style>
</StackPanel.Resources>
<DockPanel Height="26" LastChildFill="False" HorizontalAlignment="Stretch">
<Label Content="Resistance" Width="110" DockPanel.Dock="Right"/>
<Label Content="Current Speed" Width="110" DockPanel.Dock="Left"/>
<Label Content="Current BPM" Width="110" DockPanel.Dock="Top"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False" HorizontalAlignment="Stretch">
<TextBox Name="textBox_Resistance" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentSpeed" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentBPM" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False">
<Label Content="Distance Covered" Width="110" DockPanel.Dock="Right"/>
<Label Content="Current Power" Width="110" DockPanel.Dock="Left"/>
<Label Content="Acc. Power" Width="110" DockPanel.Dock="Top"/>
</DockPanel>
<DockPanel Height="26" LastChildFill="False">
<TextBox Name="textBox_DistanceCovered" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Right" IsReadOnly="true"/>
<TextBox Name="textBox_CurrentPower" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Left" IsReadOnly="true"/>
<TextBox Name="textBox_AccPower" Text="" TextWrapping="Wrap" Width="110" DockPanel.Dock="Top" Height="26" IsReadOnly="true"/>
</DockPanel>
</StackPanel>
<ListBox Name="ChatBox" Grid.Column="1" Margin="59,41,0,0" Grid.RowSpan="3"/>
<TextBox Name="textBox_Chat" Grid.Column="1" HorizontalAlignment="Left" Margin="59,10,0,0" Grid.Row="3" Text="TextBox" TextWrapping="Wrap" VerticalAlignment="Top" Width="235"/>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="59,33,0,0" Grid.Row="3" VerticalAlignment="Top" />
<Button Content="Start Session" Grid.Column="1" HorizontalAlignment="Left" Margin="69,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97"/>
<Button Content="Stop Session" Grid.Column="1" HorizontalAlignment="Left" Margin="187,86,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" />
<TextBox x:Name="textBox_SetResistance" Grid.Column="1" HorizontalAlignment="Left" Margin="69,128,0,0" Grid.Row="3" TextWrapping="Wrap" VerticalAlignment="Top" Width="97"/>
<Button Content="Set Resistance" Grid.Column="1" HorizontalAlignment="Left" Margin="187,128,0,0" Grid.Row="3" VerticalAlignment="Top" Width="97" Height="18"/>
<Canvas Grid.Row="3" Background="White" Margin="0,33,0,0"/>
<ComboBox Name="DropBox" HorizontalAlignment="Left" Margin="0,6,0,0" Grid.Row="3" VerticalAlignment="Top" Width="190"/>
<Button Content="Client Info" Grid.Column="1" HorizontalAlignment="Left" Margin="207,6,0,0" VerticalAlignment="Top" Height="26" Width="82"/>
</Grid>
</Page>
</UserControl>

View File

@@ -16,7 +16,7 @@ namespace DoctorApp.Views
/// <summary>
/// Interaction logic for ClientInfoView.xaml
/// </summary>
public partial class ClientInfoView : Page
public partial class ClientInfoView : UserControl
{
public ClientInfoView()
{

View File

@@ -5,10 +5,20 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DoctorApp.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
>
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="231,132,0,0" VerticalAlignment="Top"/>
<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding Selected}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TabName}"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<ContentControl Content="{Binding}"/>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</UserControl>

View File

@@ -5,9 +5,10 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DoctorApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<Label Content="gemaakt door: mensen" DockPanel.Dock="Bottom" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontStyle="Italic" Foreground="Gray"/>
Title="MainWindow" Height="450" Width="800"
WindowState="Maximized">
<Grid>
<ContentControl HorizontalAlignment="Center" VerticalAlignment="Center" Content="{Binding SelectedViewModel}" Focusable="False" />
</DockPanel>
<Label Content="gemaakt door: mensen" DockPanel.Dock="Bottom" HorizontalAlignment="Right" VerticalAlignment="Bottom" FontStyle="Italic" Foreground="Gray"/>
</Grid>
</Window>

View File

@@ -16,14 +16,10 @@ namespace Server
private byte[] totalBuffer = new byte[1024];
private int totalBufferReceived = 0;
private SaveData saveData;
private string username = null;
public string username = null;
private DateTime sessionStart;
private string fileName;
public string Username { get; set; }
public Client(Communication communication, TcpClient tcpClient)
{
this.sessionStart = DateTime.Now;
@@ -39,6 +35,7 @@ namespace Server
int receivedBytes = this.stream.EndRead(ar);
if (totalBufferReceived + receivedBytes > 1024)
if (totalBufferReceived + receivedBytes > 1024)
{
throw new OutOfMemoryException("buffer too small");
@@ -107,6 +104,7 @@ namespace Server
this.username = username;
sendMessage(DataParser.getLoginResponse("OK"));
sendMessage(DataParser.getStartSessionJson());
communication.NewLogin(this);
}
else
{

View File

@@ -4,7 +4,7 @@ using System.IO.Pipes;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using ClientApp.Utils;
using DoctorApp.Utils;
namespace Server
{
@@ -13,7 +13,6 @@ namespace Server
private TcpListener listener;
private List<Client> clients;
private Client doctor;
public Communication(TcpListener listener)
{
this.listener = listener;
@@ -35,15 +34,15 @@ namespace Server
var tcpClient = listener.EndAcceptTcpClient(ar);
Console.WriteLine($"Client connected from {tcpClient.Client.RemoteEndPoint}");
clients.Add(new Client(this, tcpClient));
if (doctor == null)
/*if (doctor == null)
{
doctor = clients.ElementAt(0);
}
else
{
doctor.sendMessage(DataParser.getLoginResponse("new client"));
}
doctor.sendMessage(DataParser.getNewConnectionJson("jan"));
}*/
listener.BeginAcceptTcpClient(new AsyncCallback(OnConnect), null);
}
@@ -51,5 +50,18 @@ namespace Server
{
clients.Remove(client);
}
public void NewLogin(Client client)
{
if (doctor == null)
{
doctor = client;
}
else
{
doctor.sendMessage(DataParser.getNewConnectionJson(client.username));
}
}
}
}

View File

@@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\ClientApp\ClientApp.csproj" />
<ProjectReference Include="..\DoctorApp\DoctorApp.csproj" />
</ItemGroup>
<Import Project="..\Hashing\Hashing.projitems" Label="Shared" />