/// <summary> /// Converts current state to sorted list of BoardShapes, sorted in increasing order of timestamp /// </summary> /// <returns>Sorted list of BoardShape</returns> private List <BoardShape> GetOrderedList() { List <QueueElement> queueElements = new(); List <BoardShape> boardShapes = new(); // Getting all elements from the queue while (_priorityQueue.GetSize() != 0) { queueElements.Add(_priorityQueue.Extract()); } // Adding elements in the list in inceasing order of their timestamp for (var i = queueElements.Count - 1; i >= 0; i--) { boardShapes.Add(_mapIdToBoardShape[queueElements[i].Id]); } // inserting element back in the priority queue // reverse order is better in terms of better average time-complexity for (var i = 0; i < queueElements.Count; i++) { _priorityQueue.Insert(queueElements[i]); } return(boardShapes); }
/// <summary> /// Updates local state on Fetch State from server. /// </summary> /// <param name="boardServerShape">BoardServerShape object having the whole update.</param> /// <returns>List of UXShape to notify client.</returns> private List <UXShape> UpdateStateOnFetch(BoardServerShape boardServerShape) { try { var boardShapes = boardServerShape.ShapeUpdates; List <UXShape> uXShapes = new(); // Sorting boardShapes boardShapes.Sort(delegate(BoardShape boardShape1, BoardShape boardShape2) { return(boardShape1.LastModifiedTime.CompareTo(boardShape2.LastModifiedTime)); }); // updating checkpoint number _checkpointsNumber = boardServerShape.CheckpointNumber; _clientCheckPointHandler.CheckpointNumber = _checkpointsNumber; // updating state for (var i = 0; i < boardShapes.Count; i++) { var boardShapeId = boardShapes[i].Uid; // insert in id to BoardShape map if (_mapIdToBoardShape.ContainsKey(boardShapeId)) { // if already there is some reference present, removing it _mapIdToBoardShape.Remove(boardShapeId); GC.Collect(); } _mapIdToBoardShape.Add(boardShapeId, boardShapes[i]); // insert in priority queue and id to QueueElement map var queueElement = new QueueElement(boardShapeId, boardShapes[i].LastModifiedTime); if (_mapIdToQueueElement.ContainsKey(boardShapeId)) { // if already there is some reference present, removing it var tempQueueElement = _mapIdToQueueElement[boardShapeId]; _priorityQueue.DeleteElement(tempQueueElement); _mapIdToQueueElement.Remove(boardShapeId); GC.Collect(); } _mapIdToQueueElement.Add(boardShapeId, queueElement); _priorityQueue.Insert(queueElement); // converting BoardShape to UXShape and adding it in the list uXShapes.Add(new UXShape(UXOperation.CREATE, boardShapes[i].MainShapeDefiner, boardShapeId, _checkpointsNumber, boardServerShape.OperationFlag)); } return(uXShapes); } catch (Exception e) { Trace.WriteLine("ClientBoardStateManager.UpdateStateOnFetch: Exception occurred."); Trace.WriteLine(e.Message); } return(null); }