private static void PruneOldBreweries() { // Any brewery not heard from in > 15 minutes is awol, assume it's left. List <Brewery> deadBreweries = new List <Brewery>(); foreach (Brewery brewery in s_breweries.Values) { if (DateTime.Now - brewery.LastMessage > TimeSpan.FromMinutes(15)) { deadBreweries.Add(brewery); } } foreach (Brewery brewery in deadBreweries) { Brewery tmp = null; if (s_breweries.TryRemove(brewery.ID, out tmp)) { Info("{0} has not been heard from in over 15 minutes, pruning", tmp.ID); OnBreweryLeave?.Invoke(null, new BreweryTopologyEventArgs() { Brewery = tmp }); } } }
private static void HandleIncomingBreweryMessage(BreweryMessage message) { IncomingMessage incomingMessage = message as IncomingMessage; if (message != null) { // If the message is goodbye, get and remove from list if (incomingMessage.MessageType == BreweryMessageType.Goodbye) { Info("Goodbye from {0}", incomingMessage.SourceBreweryID); // Only signal if we already knew about it Brewery targetBrewery = null; if (s_breweries.TryRemove(incomingMessage.SourceBreweryID, out targetBrewery)) { targetBrewery.HandleIncomingMessage(message); OnBreweryLeave?.Invoke(null, new BreweryTopologyEventArgs() { Brewery = targetBrewery }); } } else { bool isNew = false; Brewery targetBrewery = s_breweries.GetOrAdd(incomingMessage.SourceBreweryID, (key) => { // We add to the dictionary, but note that its new isNew = true; Debug("Message from previously unknown ID: {0} => {1}", incomingMessage.SourceBreweryID, incomingMessage.MessageType); return(new Brewery(key)); }); // Fire event outside of dictionary add, as delegate may want the dictionary elemetn if (isNew) { Info("New brewery discovered, sending full refresh: {0}", targetBrewery.ID); targetBrewery.OnRefreshComplete += NewBreweryRefreshComplete; targetBrewery.FullRefreshAsync(); } // Deliver message to the target brewery object - TBD targetBrewery.HandleIncomingMessage(message); } } PruneOldBreweries(); }