diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index d377df8..43f856e 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using Newtonsoft.Json.Linq; using System.Net; +using PolyChat.Models; namespace PolyChat { diff --git a/PolyChat/MainPage.xaml b/PolyChat/MainPage.xaml index 5484d94..ebdfd13 100644 --- a/PolyChat/MainPage.xaml +++ b/PolyChat/MainPage.xaml @@ -99,10 +99,10 @@ - + - - + + diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index ba84679..67f6688 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -50,7 +50,7 @@ namespace PolyChat public void OnSendMessage(object sender = null, RoutedEventArgs e = null) { - selectedPartner.AddMessage(new Message(inputSend.Text,false)); + selectedPartner.AddMessage(new ChatMessage(username, "message" , inputSend.Text)); Controller.SendMessage(selectedPartner.Code, inputSend.Text); // clear input inputSend.Text = ""; @@ -100,14 +100,10 @@ namespace PolyChat /// Adds an message to the UI, based on .sender if known /// /// ChatMessage - public void OnIncomingMessage(Message message) + public void OnIncomingMessage(string origin, string json) { - ChatPartner sendingPartner = Partners.First(p => p.Code == message.Ip); - sendingPartner.AddMessage(new Message( - message.Msg, - true, - message.Sender - )); + ChatPartner sendingPartner = Partners.First(p => p.Code == origin); + sendingPartner.AddMessage(new ChatMessage(origin, json)); } private void OnDeleteChat(object sender = null, RoutedEventArgs e = null) diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 4c9f48f..30b3ea2 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -6,20 +6,18 @@ namespace PolyChat.Models { public class ChatMessage { - private string Origin; - private string Type; - private string Content; - private DateTime TimeStamp; + public string Origin; + public string Type; + public string Content; + public DateTime TimeStamp; public readonly bool Foreign; // - public readonly string Ip; - - public ChatMessage(string content = "", string origin = "Unknown", string ip = "127.0.0.1") + public ChatMessage(string origin, string type, string content) { Origin = origin; TimeStamp = DateTime.Now; + Type = type; Content = content; - Ip = ip; // no json = my messages Foreign = false; Debug.WriteLine("Created Message: " + ToString()); diff --git a/PolyChat/Models/ChatPartner.cs b/PolyChat/Models/ChatPartner.cs index 8b93455..020433d 100644 --- a/PolyChat/Models/ChatPartner.cs +++ b/PolyChat/Models/ChatPartner.cs @@ -7,20 +7,20 @@ namespace PolyChat.Models { public string Name; public string Code; - public ObservableCollection Messages; + public ObservableCollection Messages; private SocketIOSocket socketIOSocket; - public ChatPartner(string name, string code, ObservableCollection messages = null) + public ChatPartner(string name, string code, ObservableCollection messages = null) { Name = name; Code = code; - if (messages == null) Messages = new ObservableCollection(); + if (messages == null) Messages = new ObservableCollection(); else Messages = messages; } public SocketIOSocket SocketIOSocket { get => socketIOSocket; set => socketIOSocket = value; } - public void AddMessage(Message message) + public void AddMessage(ChatMessage message) { Messages.Add(message); } diff --git a/PolyChat/Models/Client.cs b/PolyChat/Models/Client.cs deleted file mode 100644 index daf0256..0000000 --- a/PolyChat/Models/Client.cs +++ /dev/null @@ -1,144 +0,0 @@ -using System; -using SocketIOSharp.Common; -using SocketIOSharp.Client; -using SocketIOSharp.Server.Client; -using Json.Net; -using System.Threading; -using PolyChat.Models.Exceptions; -using System.Diagnostics; - -namespace PolyChat.Models -{ - class Client - { - private SocketIOClient connection_client = null; - private SocketIOSocket connection_server = null; - private Boolean connected = true; - private String ipSelf; - - public Client(SocketIOClient connection, String ip, MainPage uiController) - { - this.ipSelf = ip; - this.connection_client = connection; - InitEventHandlers(this, connection, uiController); - } - - public Client(SocketIOSocket connection, String ip, MainPage uiController) - { - this.ipSelf = ip; - this.connection_server = connection; - InitEventHandlers(this, connection, uiController); - } - - //Sending - //=================================================================================== - /// - /// converts String message into json file and sends it to the server. - /// - /// - /// gets called by gui if someone wants to send Message - /// - /// Sender of Message - /// the accual text the user wants to send - /// current time - public void sendMessage(SendCode code, String chatMessage) - { - new Thread(() => - { - Debug.WriteLine($"connected is {connected}"); - //create msg - Message msg = new Message(chatMessage, false, ipSelf); - - //convert msg - String petJson = JsonNet.Serialize(msg); - - //wait if not connected and send msg - int i=0; - int sleeptimer = 2000; - while(!this.connected) - { - Thread.Sleep(sleeptimer); - i++; - if(i>=10) - { - throw new MessageTimedOutException(i*sleeptimer); - } - } - if (connection_client != null) - { - connection_client.Emit(code.ToString(), petJson); - }else if (connection_server != null) - { - connection_server.Emit(code.ToString(), petJson); - } - }).Start(); - } - - //================================================================================== - //EventHandeling - //=================================================================================== - - /// - /// handles all events of client - /// - /// self - /// - private static void InitEventHandlers(Client client, SocketIOClient connection, MainPage uiController) - { - connection.On(SendCode.Message.ToString(), (Data) => - { - 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, () => - { - client.connected = true; - }); - } - - /// - /// handles all events of server - /// - /// self - /// - private static void InitEventHandlers(Client client, SocketIOSocket connection, MainPage uiController) - { - connection.On(SendCode.Message.ToString(), (Data) => - { - 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]); - }); - - client.connected = true; - } - //================================================================================== - //Getter and Setter - //================================================================================== - - public String getIP() - { - return this.ipSelf; - } - - public Boolean isConnected() - { - return this.connected; - } - } - -} diff --git a/PolyChat/Connection.cs b/PolyChat/Models/Connection.cs similarity index 99% rename from PolyChat/Connection.cs rename to PolyChat/Models/Connection.cs index f32592c..32daad4 100644 --- a/PolyChat/Connection.cs +++ b/PolyChat/Models/Connection.cs @@ -7,7 +7,7 @@ using SocketIOSharp.Common; using SocketIOSharp.Server; using SocketIOSharp.Server.Client; -namespace PolyChat +namespace PolyChat.Models { public class Connection { diff --git a/PolyChat/Models/MSG.cs b/PolyChat/Models/MSG.cs deleted file mode 100644 index 637fc3b..0000000 --- a/PolyChat/Models/MSG.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using System.Threading.Tasks; - -namespace PolyChat.Models -{ - /// - /// dumy class for json converter - /// - public class MSG - { - public String sender = "unknown"; - public DateTime timestamp = new DateTime(2000, 01, 01); - public String msg = "empty"; - public IPAddress ip = new IPAddress(new byte[] { 49,48,46,49,46,50,49,49,46,50,54 }); - - - public MSG(IPAddress ip, String msg, DateTime timestamp) - { - this.sender = sender; - this.ip = ip; - this.timestamp = timestamp; - this.msg = msg; - } - } -} diff --git a/PolyChat/Models/Message.cs b/PolyChat/Models/Message.cs deleted file mode 100644 index 6e01ea2..0000000 --- a/PolyChat/Models/Message.cs +++ /dev/null @@ -1,55 +0,0 @@ -using Newtonsoft.Json.Linq; -using System; - -namespace PolyChat.Models -{ - public class Message - { - public readonly string Sender; - public readonly DateTime Timestamp = new DateTime(1970, 01, 01); - public readonly string Msg = "empty"; - public readonly string Ip; - public readonly bool Foreign; - public readonly string StringTimeStamp; - - /// - /// create new Message object from parameters - /// - /// - /// - /// - /// - public Message(string Msg = "", bool Foreign = true, string Sender= "Unknown", string Ip = "127.0.0.1") - { - this.Sender = Sender; - this.Timestamp = DateTime.Now; - StringTimeStamp = Timestamp.ToString(); - this.Msg = Msg; - this.Foreign = Foreign; - this.Ip = Ip; - } - - /// - /// create new Message object from JToken (json) - /// - /// - public Message(JToken data) - { - Message m = (Message) data[0].ToObject(); - Sender = m.Sender; - Timestamp = m.Timestamp; - StringTimeStamp = Timestamp.ToString(); - Msg = m.Msg; - Ip = m.Ip; - Foreign = m.Foreign; - } - - - override - public string ToString() - { - string prefix = Foreign ? "Other" : "Me"; - return $"{prefix}: {Msg}({Sender})"; - } - } -} \ No newline at end of file diff --git a/PolyChat/Models/NetworkingController.cs b/PolyChat/Models/NetworkingController.cs deleted file mode 100644 index d938d50..0000000 --- a/PolyChat/Models/NetworkingController.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using System.Diagnostics; -using System.Collections.Generic; -using SocketIOSharp.Client; -using EngineIOSharp.Common.Enum; -using System.Net; -using PolyChat.Models.Exceptions; - -//dependencies for server functionality -using SocketIOSharp.Server; -using SocketIOSharp.Server.Client; -using Newtonsoft.Json.Linq; -using System.Threading; - -namespace PolyChat.Models -{ - class NetworkingController - { - public List clients = new List(); - private String ownName = ""; - private IPAddress ownIP; - private readonly ushort Port; - private SocketIOServer Server; - private readonly MainPage uiController; - - public NetworkingController (MainPage uiController, ushort Port = 8050) - { - this.uiController = uiController; - this.Port = Port; - ownIP = getIP(); - startServer(); - } - - //EXTERNAL METHODS - //========================================================================================================================================================================================= - - /// - /// connects self to server with given ip - /// - /// server to connect to - public void connectNewClient(String ip) - { - SocketIOClient connection = new SocketIOClient(new SocketIOClientOption(EngineIOScheme.http, ip, 8050)); - connection.Connect(); - clients.Add(new Client(connection, ip, uiController)); - } - - /// - /// handle incomming connection - /// - /// server to connect to - private void connectNewClient(SocketIOSocket socket) - { - socket.On(SendCode.Initial.ToString(), (JToken[] Data) => - { - Debug.WriteLine("Client connected!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - Message m = new Message(Data[0]); - clients.Add(new Client(socket,m.Ip, uiController)); - }); - } - - /// - /// sends Message to given ip - /// - /// partner to send to - /// to send - public void sendMessage(String ip, String msg) - { - this.getClient(ip).sendMessage(SendCode.Initial, msg); - } - - /// - /// returns own ip adress - /// - /// - public IPAddress getIP() - { - IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName()); - IPAddress[] addrList = ipEntry.AddressList; - - for (short i = 0; i < addrList.Length; i++) - { - if (addrList[i].ToString().Substring(0, 3).Equals("10.")) - { - return addrList[i]; - } - } - return null; - } - - public MainPage getUIController() - { - 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 - /// - /// - /// - private Client getClient(String ip) - { - foreach (Client cl in clients) - { - if (cl.getIP().Equals(ip)) - { - return cl; - } - } - return null; - } - } -} diff --git a/PolyChat/Models/SendCode.cs b/PolyChat/Models/SendCode.cs deleted file mode 100644 index 0995534..0000000 --- a/PolyChat/Models/SendCode.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace PolyChat.Models -{ - enum SendCode - { - Message, - Command, - NameChange, - Initial - } -} \ No newline at end of file diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 41da7e3..3f479cf 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -119,19 +119,15 @@ App.xaml - + MainPage.xaml - - - -