/// <summary> /// Updates the server info for the specified serverKey /// </summary> /// <param name="serverKeyObj">string serverKey</param> private void UpdateServerInfo(object serverKeyObj) { string serverKey = serverKeyObj as string; if (serverKey == null) { return; } if (!MediaServers.ContainsKey(serverKey)) { return; } ServerInfo original = MediaServers[serverKey]; ServerInfo updated = FetchServerInfo(original.ServerAddress); if (updated != null) { if (MediaServers.ContainsKey(serverKey)) { //HACK this preserves the AdvertisedService name updated.ServerName = original.ServerName; MediaServers[serverKey] = updated; FireServerInfoChanged(serverKey); } } else { Debug.WriteLine("Failed to UpdateServerInfo for " + serverKey + ". Removing..."); RemoveServer(serverKey); } }
/// <summary> /// This is the method the AX Polling Thread executes /// </summary> private void AXPollingBackgroundThread() { while (true) { List <string> currentActiveServers = new List <string>(); List <AdvertisedServiceInfo> servInfos = GetFilteredAdvertisedServices(); if (servInfos.Count > 0) { foreach (AdvertisedServiceInfo servInfo in servInfos) { string serverAddress = servInfo.IP.ToString(); DiscoveryMechanism m = IsServerKnown(serverAddress); if (m == DiscoveryMechanism.Both) { RemoveManualServer(serverAddress); } bool exists = m != DiscoveryMechanism.NotDiscovered; currentActiveServers.Add(serverAddress); if ((exists) && (servInfo.Type == 3)) { ThreadPool.QueueUserWorkItem(new WaitCallback(QueryServerInfoChanged), serverAddress); } else if (!exists) { ThreadPool.QueueUserWorkItem(new WaitCallback(QueryNewServer), servInfo); } } List <string> offlineServers = new List <string>(); lock (_mediaServerListLock) { foreach (KeyValuePair <string, ServerInfo> kvp in MediaServers) { if ((!currentActiveServers.Contains(kvp.Value.ServerAddress)) && (!kvp.Key.Contains(ManualDiscoverySuffix))) { offlineServers.Add(kvp.Value.ServerAddress); } } foreach (string address in offlineServers) { MediaServers.Remove(address); } } foreach (string i in offlineServers) { FireServerNowUnavailable(i); } } Thread.Sleep(this.AXServicesPollInterval); } Debug.WriteLine("AXPollingBackgroundThread END"); }
/// <summary> /// Removes a manually-added server. /// </summary> /// <param name="serverAddress">IP addres to remove</param> public void RemoveManualServer(string serverAddress) { string key = serverAddress + ManualDiscoverySuffix; if (MediaServers.ContainsKey(key)) { lock (_mediaServerListLock) { MediaServers.Remove(key); } } }
/// <summary> /// Removes a server from knowledge, and raises the ServerNowUnavailable event /// </summary> /// <param name="serverKey">server's address/key</param> private void RemoveServer(string serverKey) { bool raiseEvent = false; lock (_mediaServerListLock) { raiseEvent = MediaServers.Remove(serverKey); } if (raiseEvent) { FireServerNowUnavailable(serverKey); } }
/// <summary> /// Determines if we have previously populated the given server (by address) /// </summary> /// <param name="serverAddress">server address to check</param> /// <returns>Returns how the given server IP is known</returns> private DiscoveryMechanism IsServerKnown(string serverAddress) { bool ax = MediaServers.ContainsKey(serverAddress); bool manual = MediaServers.ContainsKey(serverAddress + ManualDiscoverySuffix); if (ax && manual) { return(DiscoveryMechanism.Both); } else if (ax) { return(DiscoveryMechanism.AXServices); } else if (manual) { return(DiscoveryMechanism.Manual); } return(DiscoveryMechanism.NotDiscovered); }
private async void Initialize() { var mediaServers = await mediaServerRepository.GetAllAsync(); mediaServers.ToList().ForEach(x => MediaServers.Add(x)); }