private void ProcessMessage(int messageId) { if (messageId == ELEMENT_CHANGE_MESSAGE_ID) { var stream = new NetworkStream(_socket); var bytes = stream.ReadNBytes(ELEMENT_CHANGE_MESSAGE_SIZE); var args = ElementChangingEventArgs.Deserialize(this, bytes); ElementChanging?.Invoke(this, args); } }
/// <summary> /// One Http responces changes (random from add, remove or change) /// </summary> /// <returns>Change description</returns> public string RandomUpdateHttpResponce() { var updateLog = new StringBuilder(DateTime.Now.ToShortTimeString()); var rand = new Random(); var httpResponceChangingList = new List <ServerListChanging <HttpResponce> >(); var position = rand.Next(_httpResponces.Count); switch (rand.Next(4)) { case 0: // Add new Http responce { var currIdx = _idCount; _idCount++; var newHttpResponce = GetHttpResponce(_increment); _httpResponces.Insert(position, new KeyValuePair <long, HttpResponce>(currIdx, newHttpResponce)); _increment++; updateLog.Append(" ADD (pos: "); updateLog.Append(position); var realPosition = GetIndexAfterSortAndFilter(currIdx); updateLog.Append("; r_pos: "); updateLog.Append(realPosition); updateLog.Append(") - "); updateLog.Append(newHttpResponce.GetType().ToString()); if (realPosition >= 0) { var httpResponceChanging = new ElementChanging <HttpResponce>(realPosition, newHttpResponce, CollectionChangeAction.Add); httpResponceChangingList.Add(httpResponceChanging); } else { updateLog.Append(" (NOT IN VIEW) "); } break; } case 1: // Add collection Http responce { const int count = 5; updateLog.Append(" ADD LIST (pos: "); updateLog.Append(position); updateLog.Append(" count: "); updateLog.Append(count); updateLog.Append(") - "); for (int i = 0; i < count; i++) { var currIdx = _idCount; _idCount++; var newHttpResponce = GetHttpResponce(_increment); _httpResponces.Insert(position, new KeyValuePair <long, HttpResponce>(currIdx, newHttpResponce)); _increment++; var realPosition = GetIndexAfterSortAndFilter(currIdx); if (realPosition >= 0) { var httpResponceChanging = new ElementChanging <HttpResponce>(realPosition, newHttpResponce, CollectionChangeAction.Add); httpResponceChangingList.Add(httpResponceChanging); } else { updateLog.Append(" (NOT IN VIEW) "); } updateLog.Append(newHttpResponce.GetType().ToString()); if (i != count - 1) { updateLog.Append(", "); } } break; } case 2: // Remove Http responce { if (_httpResponces.Count == 0) { break; } updateLog.Append(" REMOVE ("); updateLog.Append(position); updateLog.Append(")"); var currIdx = _httpResponces[position].Key; var realPosition = GetIndexAfterSortAndFilter(currIdx); updateLog.Append("; r_pos: "); updateLog.Append(realPosition); if (realPosition >= 0) { var httpResponceChanging = new ElementChanging <HttpResponce>(realPosition, null, CollectionChangeAction.Remove); httpResponceChangingList.Add(httpResponceChanging); } else { updateLog.Append(" (NOT IN VIEW) "); } _httpResponces.RemoveAt(position); break; } case 3: // Change Http responce { if (_httpResponces.Count == 0) { break; } var newHttpResponce = GetHttpResponce(_httpResponces[position].Value.Id); newHttpResponce.DetectTime = _httpResponces[position].Value.DetectTime; updateLog.Append(" CHANGE ("); updateLog.Append(position); var currIdx = _httpResponces[position].Key; var realPosition = GetIndexAfterSortAndFilter(currIdx); updateLog.Append("; r_pos: "); updateLog.Append(realPosition); if (realPosition >= 0) { var httpResponceChanging = new ElementChanging <HttpResponce>(realPosition, newHttpResponce, CollectionChangeAction.Refresh); httpResponceChangingList.Add(httpResponceChanging); } else { updateLog.Append(" (NOT IN VIEW) "); } _httpResponces[position] = new KeyValuePair <long, HttpResponce>(_httpResponces[position].Key, newHttpResponce); updateLog.Append(") - "); updateLog.Append(newHttpResponce.GetType().ToString()); break; } } if (ListUpdates != null) { ListUpdates(httpResponceChangingList); } return(updateLog.ToString()); }
/// <summary> /// List changes handler /// </summary> /// <param name="serverListChangings">List changes</param> private void ListChangesHandler(List <ServerListChanging <T> > serverListChangings) { bool hasCountChanging = serverListChangings.Any(changing => changing is RequestedData <T>) || serverListChangings.Where(changing => changing is ElementChanging <T>).Cast <ElementChanging <T> >() .Any(changing => changing.ChangeAction == CollectionChangeAction.Add || changing.ChangeAction == CollectionChangeAction.Remove); //Rise clear selection handler if (hasCountChanging) { OnClearSelection(); } var aplliedChangesList = new List <NotifyCollectionChangedEventArgs>(); for (int i = 0; i < serverListChangings.Count; i++) { if (serverListChangings[i] is RequestedData <T> ) { var requestedData = serverListChangings[i] as RequestedData <T>; //If priority changes, clear list and apply it if (requestedData.IsPriority) { ClearList(); } int pageIndex = requestedData.StartIndex / PageSize; PopulatePage(pageIndex, requestedData.RequestedDataList); AccumulatedCount = requestedData.OverallCount; Trace.WriteLine("Request Data on page " + pageIndex + " overall count = " + requestedData.OverallCount); //Undate selected item if (requestedData.StartIndex <= SelectedIndex) { bool findUpdated = false; foreach (var key in _pages.Keys) { for (int j = 0; j < _pages[key].Items.Count; j++) { if (_pages[key].Items[j].Data != null && _pages[key].Items[j].Data.Equals(SelectedItem)) { SelectedIndex = _pages[key].Items[j].Index; findUpdated = true; break; } } if (findUpdated) { break; } } if (!findUpdated) { Trace.WriteLine("Selected Item is wrong"); } } aplliedChangesList.Add(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } else if (serverListChangings[i] is ElementChanging <T> ) { var elementChanging = serverListChangings[i] as ElementChanging <T>; NotifyCollectionChangedEventArgs appliedChanged = null; switch (elementChanging.ChangeAction) { case CollectionChangeAction.Add: { appliedChanged = AddItem(elementChanging.Position, elementChanging.ChangedElement); break; } case CollectionChangeAction.Refresh: { ElementChanging <T> refreshChange = null; while (refreshChange == null) { if (serverListChangings.Count > i + 1 && serverListChangings[i + 1] is ElementChanging <T> && ((ElementChanging <T>)serverListChangings[i + 1]).ChangeAction == CollectionChangeAction.Refresh && elementChanging.Position == ((ElementChanging <T>)serverListChangings[i + 1]).Position) { serverListChangings.RemoveAt(i); } else { refreshChange = serverListChangings[i] as ElementChanging <T>; } } bool isRemovedItemLate = false; //Remove change if go after remove change for (int j = i + 1; j < serverListChangings.Count; j++) { if (serverListChangings[j] is ElementChanging <T> && ((ElementChanging <T>)serverListChangings[j]).ChangeAction == CollectionChangeAction.Remove && ((ElementChanging <T>)serverListChangings[j]).Position == refreshChange.Position) { isRemovedItemLate = true; Trace.WriteLine("Remove Change because Remove Item late"); break; } } if (!isRemovedItemLate) { appliedChanged = ChangeItem(refreshChange.Position, refreshChange.ChangedElement); } break; } case CollectionChangeAction.Remove: { appliedChanged = RemoveItem(elementChanging.Position, elementChanging.ChangedElement); break; } } if (appliedChanged != null) { aplliedChangesList.Add(appliedChanged); } } } VerifyUnloadedPages(); //Notify collection changed if (aplliedChangesList.Count > 0) { Trace.WriteLine("FireCollectionChanged - RESET"); FireCollectionChanged(new List <NotifyCollectionChangedEventArgs> { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) }); } if (hasCountChanging) { OnResetSelection(); } }