/// <summary> /// Reprioritize all of the currently queued events /// </summary> public void Reprioritize() { lock (m_syncRoot) { List <QueuedInterestListEvent> removedEvents = new List <QueuedInterestListEvent>(0); foreach (C5.PriorityQueueHandle handle in m_eventIDs.Values) { QueuedInterestListEvent qile = m_eventHeap[handle]; double?priority = qile.Handler.PriorityCallback(qile.Event, m_presence); if (priority.HasValue) { // Update the event with its new priority and reinsert qile.Priority = priority.Value; m_eventHeap.Replace(handle, qile); } else { // Delete this event from the priority queue and mark // it for removal from the dictionary m_eventHeap.Delete(handle); removedEvents.Add(qile); } } // Erase any removed events from the m_eventIDs dictionary for (int i = 0; i < removedEvents.Count; i++) { m_eventIDs.Remove(removedEvents[i].Event.ID); } } }
/// <summary> /// Adds an event to the interest list /// </summary> /// <param name="eventData">Event to add</param> /// <param name="handler">Collection of callbacks that will handle this /// event</param> public void EnqueueEvent(InterestListEvent eventData, InterestListEventHandler handler) { double?priority = handler.PriorityCallback(eventData, m_presence); // If this event has a non-null priority for this presence, enqueue it if (priority.HasValue) { QueuedInterestListEvent qile = new QueuedInterestListEvent(eventData, priority.Value, handler); lock (m_syncRoot) { C5.PriorityQueueHandle handle; if (m_eventIDs.TryGetValue(eventData.ID, out handle)) { // An event with the same ID already exists in the priority queue. Combine this update with the previous one qile = handler.CombineCallback(m_eventHeap[handle], qile); m_eventHeap.Replace(handle, qile); } else { // This event ID is new m_eventHeap.Add(ref handle, qile); m_eventIDs.Add(eventData.ID, handle); } } } }
/// <summary> /// Dequeues the highest priority events /// </summary> /// <param name="count">Maximum number of events to dequeue</param> public void DequeueEvents(int count) { Dictionary <string, List <QueuedInterestListEvent> > dequeued = new Dictionary <string, List <QueuedInterestListEvent> >(); List <QueuedInterestListEvent> events; // Fetch the requested number of events, or as many as are available. // Put them in collections sorted by event type lock (m_syncRoot) { while (!m_eventHeap.IsEmpty && count-- > 0) { QueuedInterestListEvent qile = m_eventHeap.DeleteMin(); m_eventIDs.Remove(qile.Event.ID); if (!dequeued.TryGetValue(qile.Event.Type, out events)) { events = new List <QueuedInterestListEvent>(); dequeued.Add(qile.Event.Type, events); } events.Add(qile); } } // Fire a SendEventCallback for each unique event type foreach (List <QueuedInterestListEvent> eventsList in dequeued.Values) { QueuedInterestListEvent[] eventsArray = eventsList.ToArray(); InterestListEventHandler handler = eventsArray[0].Handler; handler.SendCallback(eventsArray, m_presence); } }
/// <summary> /// The default combiner for interest list events. Returns newData /// </summary> private static QueuedInterestListEvent DefaultCombiner(QueuedInterestListEvent currentData, QueuedInterestListEvent newData) { // Default behavior is to overwrite old events with new ones return(newData); }