From 86de29bccedc91ddb4ba3303965cb9d434fe9533 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Tue, 21 Sep 2021 14:36:16 +0200 Subject: [PATCH] del Controller and ClientHandler / msg send to ui --- PolyChat/Controller.cs | 88 ------------------------- PolyChat/Models/Client.cs | 49 +++++--------- PolyChat/Models/ClientHandler.cs | 53 --------------- PolyChat/Models/NetworkingController.cs | 34 +++++----- PolyChat/PolyChat.csproj | 2 - 5 files changed, 31 insertions(+), 195 deletions(-) delete mode 100644 PolyChat/Controller.cs delete mode 100644 PolyChat/Models/ClientHandler.cs diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs deleted file mode 100644 index c296840..0000000 --- a/PolyChat/Controller.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; -using System.Net; -using EngineIOSharp.Common.Enum; -using PolyChat.Models; -using PolyChat.Models.Exceptions; -using SocketIOSharp.Client; -using SocketIOSharp.Common; -using SocketIOSharp.Common.Packet; -using SocketIOSharp.Server.Client; - -namespace PolyChat -{ - public class Controller - { - public static string ip; - private readonly MainPage UIController; - - private ClientHandler clientHandler; - /* - public Controller(MainPage uiController) - { - UIController = uiController; - clientHandler = new ClientHandler(); - Socket s = new Socket(this); - } - - public void Connect(string ip) - { - clientHandler.connectNewClient(ip); - } - - public void sendMessage(String ip, String name, String msg) - { - clientHandler.getClient(ip).send Message(SendCode.Message, msg, DateTime.Now); - } - */ - /// - /// prints out ip. on server side automatticaly finds 10.... ip (which is the correct one) - /// - /// users ip - private String getIP() - { - while (true) - { - string input = Console.ReadLine(); - if (input.Equals("server")) - { - Console.WriteLine("starting as server..."); - for (short i = 0; i < GetIPs().Length; i++) - { - if (GetIPs()[i].ToString().Substring(0, 3).Equals("10.")) - { - Controller.ip = GetIPs()[i]; - Console.WriteLine(GetIPs()[i]); - //get ip as byte array - byte[] ba = System.Text.Encoding.ASCII.GetBytes(GetIPs()[i].ToString()); - foreach (var item in ba) - { - Console.Write(item.ToString() + ","); - } - break; - } - } - } - else if (input.Equals("client")) - { - ClientHandler cl = new ClientHandler(); - Console.WriteLine("Enter IP:"); - cl.connectNewClient(Console.ReadLine()); - } - } - } - - - static string[] GetIPs() - { - IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); - IPAddress[] addr = ipEntry.AddressList; - string[] ips = new string[addr.Length]; - for (int i = 0; i < addr.Length; i++) - { - ips[i] = addr.ToString(); - } - return ips; - } - - } -} diff --git a/PolyChat/Models/Client.cs b/PolyChat/Models/Client.cs index b09b5e5..daf0256 100644 --- a/PolyChat/Models/Client.cs +++ b/PolyChat/Models/Client.cs @@ -16,18 +16,18 @@ namespace PolyChat.Models private Boolean connected = true; private String ipSelf; - public Client(SocketIOClient connection, String ip) + public Client(SocketIOClient connection, String ip, MainPage uiController) { this.ipSelf = ip; this.connection_client = connection; - InitEventHandlers(this, connection); + InitEventHandlers(this, connection, uiController); } - public Client(SocketIOSocket connection, String ip) + public Client(SocketIOSocket connection, String ip, MainPage uiController) { this.ipSelf = ip; this.connection_server = connection; - InitEventHandlers(this, connection); + InitEventHandlers(this, connection, uiController); } //Sending @@ -47,7 +47,7 @@ namespace PolyChat.Models { Debug.WriteLine($"connected is {connected}"); //create msg - Message msg = new Message(chatMessage, false, Controller.ip); + Message msg = new Message(chatMessage, false, ipSelf); //convert msg String petJson = JsonNet.Serialize(msg); @@ -73,48 +73,30 @@ namespace PolyChat.Models } }).Start(); } - /* - /// - /// Sends Message with new name - /// - /// - /// - /// - public void sendNameChange(SendCode code, String nameChange) - { - new Thread(() => - { - //create msg - Message msg = new Message( Controller.ip); - - //convert msg - String petJson = JsonNet.Serialize(msg); - - //send msg - connection_client.Emit(code.ToString(), petJson); - }).Start(); - } - */ //================================================================================== //EventHandeling //=================================================================================== /// - /// handles all events of client to server communiation + /// handles all events of client /// /// self /// - private static void InitEventHandlers(Client client, SocketIOClient connection) + private static void InitEventHandlers(Client client, SocketIOClient connection, MainPage uiController) { connection.On(SendCode.Message.ToString(), (Data) => { - Message pet = new Message(Data[0]); + Message msg = new Message(Data[0]); + uiController.OnIncomingMessage(msg); + //TODO: send message to GUI }); + connection.On(SendCode.Command.ToString(), (Data) => { Console.WriteLine("Command recieved!" + Data[0]); + }); connection.On(SocketIOEvent.CONNECTION, () => @@ -124,15 +106,16 @@ namespace PolyChat.Models } /// - /// handles all events of server to client communiation + /// handles all events of server /// /// self /// - private static void InitEventHandlers(Client client, SocketIOSocket connection) + private static void InitEventHandlers(Client client, SocketIOSocket connection, MainPage uiController) { connection.On(SendCode.Message.ToString(), (Data) => { - Message pet = new Message(Data[0]); + Message msg = new Message(Data[0]); + uiController.OnIncomingMessage(msg); //TODO: send message to GUI }); diff --git a/PolyChat/Models/ClientHandler.cs b/PolyChat/Models/ClientHandler.cs deleted file mode 100644 index b2d079b..0000000 --- a/PolyChat/Models/ClientHandler.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using SocketIOSharp.Common; -using SocketIOSharp.Server; -using SocketIOSharp.Client; -using SocketIOSharp.Server.Client; -using EngineIOSharp.Common.Enum; -using Json.Net; - -namespace PolyChat.Models -{ - class ClientHandler - { - public List clients = new List(); - public ClientHandler() - { - } - - /// - /// connects new clients and saves them in list - /// - /// ip adress of parter - public void connectNewClient(String clientCode) - { - //Todo: convert code into ip - - SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, clientCode, 8050)); - connection.Connect(); - clients.Add(new Client(connection, clientCode)); - } - - /// - /// returns client that fits to ip adress - /// - /// - /// - public Client getClient(String ip) - { - foreach (Client cl in clients) - { - if (cl.getIP().Equals(ip)) - { - return cl; - } - } - return null; - } - } - -} diff --git a/PolyChat/Models/NetworkingController.cs b/PolyChat/Models/NetworkingController.cs index 2d00836..4827fb5 100644 --- a/PolyChat/Models/NetworkingController.cs +++ b/PolyChat/Models/NetworkingController.cs @@ -27,11 +27,7 @@ namespace PolyChat.Models this.uiController = uiController; this.Port = Port; ownIP = getIP(); - Server = new SocketIOServer(new SocketIOServerOption(Port)); - Server.OnConnection((socket) => connectNewClient(socket)); - Server.Start(); - Debug.WriteLine($"Your ip is: {ownIP}"); - Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection..."); + startServer(); } //EXTERNAL METHODS @@ -45,7 +41,7 @@ namespace PolyChat.Models { SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050)); connection.Connect(); - clients.Add(new Client(connection, ip)); + clients.Add(new Client(connection, ip, uiController)); } /// @@ -56,8 +52,9 @@ namespace PolyChat.Models { socket.On(SendCode.Initial.ToString(), (JToken[] Data) => { + Debug.WriteLine("Client connected!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Message m = new Message(Data[0]); - clients.Add(new Client(socket,m.Ip)); + clients.Add(new Client(socket,m.Ip, uiController)); }); } @@ -90,24 +87,23 @@ namespace PolyChat.Models return null; } - /* - /// - /// changes name of self and sends new name to all chats - /// - /// - public void changeName(String newName) + public MainPage getUIController() { - this.ownName = newName; - foreach(Client cl in clients) - { - cl.sendNameChange(SendCode.NameChange, newName); - } + return this.uiController; } - */ + //========================================================================================================================================================================================= //INTERNAL METHODS //========================================================================================================================================================================================= + private void startServer() + { + Server = new SocketIOServer(new SocketIOServerOption(Port)); + Server.OnConnection((socket) => connectNewClient(socket)); + Server.Start(); + Debug.WriteLine($"Your ip is: {ownIP}"); + Debug.WriteLine($"Server started, binding to port {Port}, waiting for connection..."); + } /// /// returns client that fit to ip address diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 4b9f3db..3410aba 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -127,10 +127,8 @@ - -