/// <summary>
 ///     serializes the shape objects and passes it to communicator.send()
 /// </summary>
 /// <param name="clientUpdate"> the object to be passed to client</param>
 public void Send(BoardServerShape clientUpdate, string clientId = "all")
 {
     // TODO: Correct this error
     // var xml_obj = serializer.Serialize(clientUpdate);
     // if (clientId == "all")
     //     communicator.Send(xml_obj, moduleIdentifier);
     // else
     //     communicator.Send(xml_obj, moduleIdentifier, clientId);
 }
 /// <summary>
 ///     Manages state and notifies UX on receiving an update from ClientBoardCommunicator.
 /// </summary>
 /// <param name="serverUpdate">BoardServerShapes signifying the update.</param>
 public void OnMessageReceived(BoardServerShape serverUpdate)
 {
     // a case of state fetching for newly joined client
     if (serverUpdate.OperationFlag == Operation.FETCH_STATE && serverUpdate.RequesterId == _currentUserId)
     {
         // converting network update to UXShapes and sending them to UX
         Trace.WriteLine(
             "ClientBoardStateManager.OnMessageReceived: FETCH_STATE (subscribe) request's result arrived.");
         var uXShapes = UpdateStateOnFetch(serverUpdate);
         NotifyClients(uXShapes);
         Trace.WriteLine("ClientBoardStateManager.OnMessageReceived: Clients Notified.");
     }
     else
     {
         throw new NotImplementedException();
     }
 }
 /// <summary>
 ///     Saves the updates on state at the server.
 /// </summary>
 /// <param name="boardServerShape">Object containing the update information for shape.</param>
 /// <returns>Boolean to indicate success status of update.</returns>
 public bool SaveUpdate(BoardServerShape boardServerShape)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 ///     serializes the shape objects and passes it to communicator.send()
 /// </summary>
 /// <param name="clientUpdate"> the object to be passed to server</param>
 public void Send(BoardServerShape clientUpdate)
 {
     // TODO: Correct this error
     // var xml_obj = serializer.Serialize(clientUpdate);
     // communicator.Send(xml_obj, moduleIdentifier);
 }
        /// <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);
        }