diff --git a/ErgometerServer/ClientThread.cs b/ErgometerServer/ClientThread.cs new file mode 100644 index 0000000..5e86fa0 --- /dev/null +++ b/ErgometerServer/ClientThread.cs @@ -0,0 +1,86 @@ +using ErgometerLibrary; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ErgometerServer +{ + class ClientThread + { + TcpClient client; + Server server; + + string name; + int session; + + bool running; + bool loggedin; + + List metingen; + List chat; + + + public ClientThread(TcpClient client, Server server) + { + this.client = client; + this.server = server; + this.name = "Unknown"; + this.session = 0; + this.running = false; + this.loggedin = false; + + metingen = new List(); + chat = new List(); + } + + public void run() + { + running = true; + + session = FileHandler.GenerateSession(); + NetHelper.SendNetCommand(client, new NetCommand(session)); + + while (running) + { + NetCommand input = NetHelper.ReadNetCommand(client); + + switch(input.Type) + { + case NetCommand.CommandType.SESSION: + break; + case NetCommand.CommandType.LOGIN: + if(input.IsDoctor) + { + server.ChangeClientToDoctor(client, this); + running = false; + } + else + { + name = input.DisplayName; + loggedin = true; + } + break; + case NetCommand.CommandType.DATA: + metingen.Add(input.Meting); + break; + case NetCommand.CommandType.CHAT: + chat.Add(new ChatMessage(name, input.ChatMessage)); + break; + case NetCommand.CommandType.LOGOUT: + loggedin = false; + running = false; + client.Close(); + break; + default: + throw new FormatException("Unknown command"); + } + } + } + + } +} diff --git a/ErgometerServer/DoctorThread.cs b/ErgometerServer/DoctorThread.cs new file mode 100644 index 0000000..1681935 --- /dev/null +++ b/ErgometerServer/DoctorThread.cs @@ -0,0 +1,32 @@ +using System.Net.Sockets; + +namespace ErgometerServer +{ + class DoctorThread + { + TcpClient client; + Server server; + + string name; + int session; + + bool running; + bool loggedin; + + + public DoctorThread(TcpClient client, Server server) + { + this.client = client; + this.server = server; + this.name = "Unknown"; + this.session = 0; + this.running = false; + this.loggedin = false; + } + + public void run() + { + + } + } +} \ No newline at end of file diff --git a/ErgometerServer/Program.cs b/ErgometerServer/Server.cs similarity index 75% rename from ErgometerServer/Program.cs rename to ErgometerServer/Server.cs index b6ae4a1..e779ef5 100644 --- a/ErgometerServer/Program.cs +++ b/ErgometerServer/Server.cs @@ -1,50 +1,52 @@ -using System; +using ErgometerLibrary; +using System; using System.Collections; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; -using System.Threading.Tasks; -using ErgometerLibrary; namespace ErgometerServer { - class Program + class Server { static void Main(string[] args) { - new Program(); + new Server(); } - public Program() + private List clients; + private DoctorThread doctor; + + public Server() { FileHandler.CheckDataFolder(); - IPAddress ipAddress; //= IPAddress.Parse("127.0.0.1"); - bool ipIsOk = IPAddress.TryParse("127.0.0.1", out ipAddress); //GetIp() - if (!ipIsOk) { Console.WriteLine("ip adres kan niet geparsed worden."); Environment.Exit(1); } - - TcpListener listener = new System.Net.Sockets.TcpListener(ipAddress, 8888); + TcpListener listener = new TcpListener(NetHelper.GetIP("127.0.0.1"), 8888); listener.Start(); while (true) { - Console.WriteLine("Waiting for connection with client"); + Console.WriteLine("Waiting for connection with client..."); //AcceptTcpClient waits for a connection from the client TcpClient client = listener.AcceptTcpClient(); - Console.WriteLine("Connection established"); + Console.WriteLine("Client connected"); - Thread thread = new Thread(HandleClientThread); - thread.Start(client); + //Start new client + var cl = new ClientThread(client, this); + clients.Add(cl); + + //Run client on new thread + Thread thread = new Thread(new ThreadStart(cl.run)); + thread.Start(); } } - public static String GetIp() + public static string GetIp() { IPHostEntry host; string localIP = "?"; @@ -59,7 +61,19 @@ namespace ErgometerServer return localIP; } - static void HandleClientThread(object obj) + public void ChangeClientToDoctor(TcpClient client, ClientThread clth) + { + clients.Remove(clth); + doctor = new DoctorThread(client, this); + Thread thread = new Thread(new ThreadStart(doctor.run)); + thread.Start(); + } + + + + + //OLD DO NOT USE + static void OldThread(object obj) { TcpClient client = obj as TcpClient; StreamReader reader = new StreamReader(client.GetStream(), Encoding.Unicode);