/// <summary> /// Creates a new conversation from a conversation update. /// </summary> /// <remarks>Currently unused.</remarks> /// <param name="conv">Conversation update</param> public Conversation(ConversationUpdates conv) : base() { this.id = conv.ID; this.name = conv.Name; this.Users = conv.Users; this.messages = conv.getMessagesFull(); foreach (var(guid, message) in messages) { message.Conversation = this; } }
/// <summary> /// Updates the conversation based on received object. /// </summary> /// <param name="updt">Received update</param> public void ApplyUpdates(ConversationUpdates updt) { this.ChatSystem.Users = ChatSystem.Users.Union(updt.getUsersFull()); users.AddRange(updt.getUsersFull().Select(u => u.ID)); foreach (var user in updt.Users) { user.ChatSystem = ChatSystem; } foreach (var mess in updt.Messages) { addMessageUnsafe(mess); mess.Conversation = this; } }
/// <summary> /// Gets updates from the time after parameter. /// </summary> /// <param name="time">Time of last known conversation state</param> /// <returns>Updates to conversation.</returns> public ConversationUpdates GetUpdates(DateTime?time) { var updates = new ConversationUpdates(Name, ID); //creates new updates object updates.Users = Users; //adds to it all users foreach (var message in messages.Values) { if (message.SentTime > time) //and all messages sent after specified time { //must use unsafe version as messages can be added in wrong order updates.addMessageUnsafe(new Message(message)); } } return(updates); }
public void handleRequest(List <IClientHandler> allHandlers, IServerChatSystem chatSystem, IClientHandler handlerThread, byte[] messageBytes) { Console.WriteLine("DEBUG: {0} request received", "add new conversation"); //decoding request - first 4 bytes are length of proposed conversation name, next there is the name List <string> namesOfParticipants = new List <string>(); int index = 0; int stringLength = BitConverter.ToInt32(messageBytes, 0); index += 4; string proposedConversationName = Encoding.UTF8.GetString(messageBytes, index, stringLength); index += stringLength; //the rest of the request are some number of pairs: (4 bytes describing length of user name, name of user to add to conversation) while (index < messageBytes.Length) { stringLength = BitConverter.ToInt32(messageBytes, index); index += 4; namesOfParticipants.Add(Encoding.UTF8.GetString(messageBytes, index, stringLength)); index += stringLength; } Console.WriteLine("DEBUG: trying to add conversation"); byte[] reply = new byte[1]; lock (allHandlers) { Conversation newConversation = chatSystem.AddConversation(proposedConversationName, namesOfParticipants.ToArray()); if (newConversation == null) { reply[0] = 0; //conversation could not be created } else { reply[0] = 1; ConversationUpdates conversationUpdates = newConversation.GetUpdates(DateTime.MinValue); byte[] msg = conversationUpdates.Serialize(new ConcreteSerializer()).ToArray(); //if conversation created successfully, send it to all participating users that are currently connected foreach (var handler in allHandlers.FindAll(h => namesOfParticipants.Contains(h.HandledUserName))) { handler.sendMessage(5, msg); } } } handlerThread.sendMessage(1, reply); }