public Message(User from, User to, string MessageContent) { this.FromUser = from; this.ToUser = to; this.MessageContent = MessageContent; this.MessageType = ""; }
/// <summary> /// Reads the configuration file, catches all errors and returns a null user /// </summary> /// <returns>The user which read out the file. Occuring error returning a null Object.</returns> internal User ReadUserCfgFile() { try { using (StreamReader sr = new StreamReader("user.cfg")) { // Read the stream to a string, and write the string to the console. String line = sr.ReadToEnd(); if (line.Length > 0) { String[] split = line.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); long id = long.Parse(split[1]); User user = new User(split[0]); user.Id = id; sr.Close(); return user; } sr.Close(); } } catch (FileNotFoundException ex) { Logger.LogException("File could not be found by the application.", ex); } catch (IOException ex) { Logger.LogException("File could not be read by the application.", ex); } catch (Exception ex) { Logger.LogException("File could not be read by the application.", ex); } return null; }
/// <summary> /// Check the equality of the user with an other one, depending on their name and their id /// </summary> /// <param name="user"></param> /// <returns>true for equality</returns> public bool Equals(User user) { if (this.name.Equals(user.name) && this.id.Equals(user.id)) { return true; } return false; }
/// <summary> /// Generates a message of type TCPConnectTo;fromUserName;fromUserId;;;message /// </summary> /// <param name="message"></param> /// <returns></returns> public static string GenerateConnectMessage(User user, IPAddress ipAddress, int port) { return "TCPConnectTo;" + user.Name + ";" + user.Id + ";;;" + ipAddress.ToString() + ";" + port; }
public Message(User from, User to, string MessageContent, string MessageType) : this(from, to, MessageContent) { this.MessageType = MessageType; }
/// <summary> /// Parses a string to a message, awaits a correct formated string, can be tested with method <see cref="IsNotifyMessage"/>. /// </summary> /// /// <param name="content">a string formed like /// TCPNotify;user.name;user.id;message /// </param> /// <returns>A new message object or null when exception occurs</returns> internal static Message ParseTCPNotifyMessage(string content) { string[] temp = content.Split(';'); if (temp.Length >= 3) { try { User from = new User(temp[1]); from.Id = long.Parse(temp[2]); string message = ""; for (int i = 5; i < temp.Length; i++) { message += temp[i]; } Logger.LogInfo("Notfiy Message: " + message); return new Message(from, null, message, temp[0]); } catch (Exception ex) { Logger.LogException("ParseTCPMessage", ex); } } return null; }
/// <summary> /// Parses a string to a message, awaits a correct formated string, can be tested with method <see cref="IsTCPMessage"/>. /// </summary> /// /// <param name="content">a string formed like /// TCPMessage;user.name;user.id;user.name;user.id;message /// </param> /// <returns>A new message object or null when exception occurs</returns> internal static Message ParseTCPMessage(string content) { string[] temp = content.Split(';'); if (temp.Length >= 6) { try { User from = new User(temp[1]); from.Id = long.Parse(temp[2]); User to = new User(temp[3]); to.Id = long.Parse(temp[4]); string message = ""; for (int i = 5; i < temp.Length; i++) { message += temp[i]; } return new Message(from, to, message, temp[0]); } catch (Exception ex) { Logger.LogException("ParseTCPMessage", ex); } } return null; }
/// <summary> /// Reads a message out of a well formated NewContactMessage, test before if string is correct with method <see cref="IsNewContactMessage"/>. /// Format for a new contact message: TCPConnectTo;username;userid;;;ipaddress;port /// </summary> /// <param name="content"></param> /// <returns></returns> internal static Message ParseNewContactMessage(string content) { string[] temp = content.Split(';'); if (temp.Length >= 6) { try { User from = new User(temp[1]); from.Id = long.Parse(temp[2]); string message = temp[5] + ";" + temp[6]; return new Message(from, null, message, temp[0]); } catch (Exception ex) { Logger.LogException("ParseNewContactMessage", ex); } } return null; }