protected void UpdateTreeView(bool showPending) { TreeNodeCollection nodes = tvDeviceTree.Nodes; nodes.Clear(); TreeNode devicesNode = nodes.Add("DevicesKey", "Devices"); IDictionary <string, RootDescriptor> knownDevices = _networkTracker.KnownRootDevices; try { if (knownDevices == null) { if (showPending) { devicesNode.Nodes.Add(null, " - pending -"); } return; } foreach (RootDescriptor rootDescriptor in knownDevices.Values) { if (rootDescriptor.State != RootDescriptorState.Ready) { continue; } string deviceUUID = rootDescriptor.SSDPRootEntry.RootDeviceUUID; DeviceDescriptor deviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); TreeNode deviceNode = devicesNode.Nodes.Add(deviceUUID, BuildDeviceName(deviceDescriptor)); deviceNode.Tag = new UPnPDeviceDescriptor(deviceDescriptor); } } finally { devicesNode.Expand(); } }
/// <summary> /// Establishes the actual device connection by building the control point's proxy device tree corresponding to the /// device contained in the given <paramref name="rootDescriptor"/> specified by its <paramref name="deviceUUID"/>. /// </summary> /// <param name="rootDescriptor">Root descriptor which contains the device to build.</param> /// <param name="deviceUUID">UUID of the device to connect.</param> /// <param name="dataTypeResolver">Delegate method to resolve extended datatypes.</param> private void BuildDeviceProxy(RootDescriptor rootDescriptor, string deviceUUID, DataTypeResolverDlgt dataTypeResolver) { if (rootDescriptor.State != RootDescriptorState.Ready) { throw new ArgumentException("Root descriptor is not ready - cannot connect"); } DeviceDescriptor rootDeviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); DeviceDescriptor deviceDescriptor = rootDeviceDescriptor.FindDevice(deviceUUID); _device = CpDevice.ConnectDevice(this, deviceDescriptor, dataTypeResolver); }
private void OnDeviceDescriptionReceived(IAsyncResult asyncResult) { DescriptionRequestState state = (DescriptionRequestState)asyncResult.AsyncState; RootDescriptor rd = state.RootDescriptor; HttpWebRequest request = state.Request; try { WebResponse response = request.EndGetResponse(asyncResult); if (rd.State != RootDescriptorState.AwaitingDeviceDescription) { return; } try { using (Stream body = CompressionHelper.Decompress(response)) { XPathDocument xmlDeviceDescription = new XPathDocument(body); lock (_cpData.SyncObj) { rd.DeviceDescription = xmlDeviceDescription; DeviceDescriptor rootDeviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rd); if (rootDeviceDescriptor == null) { // No root device description available rd.State = RootDescriptorState.Erroneous; return; } ExtractServiceDescriptorsRecursive(rootDeviceDescriptor, rd.ServiceDescriptors, state.PendingServiceDescriptions); rd.State = RootDescriptorState.AwaitingServiceDescriptions; } } ContinueGetServiceDescription(state); } catch (Exception) // Don't log exceptions at this low protocol level { rd.State = RootDescriptorState.Erroneous; } finally { response.Close(); } } catch (WebException e) { rd.State = RootDescriptorState.Erroneous; if (e.Response != null) { e.Response.Close(); } } }
protected IResourceInformationService TryGetResourceInformationService(RootDescriptor rootDescriptor) { DeviceConnection connection; using (_networkTracker.SharedControlPointData.Lock.EnterRead()) { object service; if (rootDescriptor.SSDPRootEntry.ClientProperties.TryGetValue(KEY_RESOURCE_INFORMATION_SERVICE, out service)) { return(service as IResourceInformationService); } } DeviceDescriptor rootDevice = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); DeviceDescriptor frontendServerDevice = rootDevice.FindFirstDevice( UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION) ?? rootDevice.FindFirstDevice(UPnPTypesAndIds.FRONTEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.FRONTEND_SERVER_DEVICE_TYPE_VERSION); if (frontendServerDevice == null) { return(null); } string deviceUuid = frontendServerDevice.DeviceUUID; try { connection = _controlPoint.Connect(rootDescriptor, deviceUuid, UPnPExtendedDataTypes.ResolveDataType); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("Error connecting to UPnP MP2 device '{0}'", e, deviceUuid); return(null); } try { CpService rasStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.RESOURCE_INFORMATION_SERVICE_ID); if (rasStub == null) { throw new InvalidDataException("ResourceAccess service not found in device '{0}'", deviceUuid); } IResourceInformationService ris = new UPnPResourceInformationServiceProxy(rasStub); using (_networkTracker.SharedControlPointData.Lock.EnterWrite()) rootDescriptor.SSDPRootEntry.ClientProperties[KEY_RESOURCE_INFORMATION_SERVICE] = ris; return(ris); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("Error connecting to services of UPnP MP2 device '{0}'", e, deviceUuid); _controlPoint.Disconnect(deviceUuid); return(null); } }
public static ServerDescriptor GetMPBackendServerDescriptor(RootDescriptor uPnPRootDescriptor) { DeviceDescriptor rootDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(uPnPRootDescriptor); if (rootDescriptor == null) { return(null); } DeviceDescriptor serverDeviceDescriptor = rootDescriptor.FindFirstDevice( UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); return(serverDeviceDescriptor == null ? null : new ServerDescriptor(serverDeviceDescriptor)); }
private void OnUpnpRootDeviceAdded(RootDescriptor rootDescriptor) { if (rootDescriptor == null || rootDescriptor.State != RootDescriptorState.Ready || _knownUpnpDevices.Contains(rootDescriptor.SSDPRootEntry.RootDeviceUUID)) { return; } _knownUpnpDevices.Add(rootDescriptor.SSDPRootEntry.RootDeviceUUID); DeviceDescriptor deviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); IEnumerator <DeviceEntry> childDeviceEn = rootDescriptor.SSDPRootEntry.Devices.Values.GetEnumerator(); bool isFirst = true; while (childDeviceEn.MoveNext()) { foreach (string serviceUrn in childDeviceEn.Current.Services) { // Supported device? if (serviceUrn.Equals("urn:schemas-opencable-com:service:Tuner:1")) { if (isFirst) { isFirst = false; Log.Log.Info("Detected new OCUR/DRI device {0}", deviceDescriptor.FriendlyName); } // Find the corresponding DeviceDescriptor. IEnumerator <DeviceDescriptor> childDeviceDescriptorEn = deviceDescriptor.ChildDevices.GetEnumerator(); while (childDeviceDescriptorEn.MoveNext()) { if (childDeviceDescriptorEn.Current.DeviceUUID == childDeviceEn.Current.UUID) { break; } } Log.Log.Info(" add {0} {1}", childDeviceDescriptorEn.Current.FriendlyName, childDeviceDescriptorEn.Current.DeviceUDN); _deviceEventListener.OnDeviceAdded(new TunerDri(childDeviceDescriptorEn.Current, _upnpControlPoint)); break; } } } }
private void OnUpnpRootDeviceRemoved(RootDescriptor rootDescriptor) { if (rootDescriptor == null) { return; } _knownUpnpDevices.Remove(rootDescriptor.SSDPRootEntry.RootDeviceUUID); DeviceDescriptor deviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); IEnumerator <DeviceEntry> childDeviceEn = rootDescriptor.SSDPRootEntry.Devices.Values.GetEnumerator(); bool isFirst = true; while (childDeviceEn.MoveNext()) { foreach (string serviceUrn in childDeviceEn.Current.Services) { if (serviceUrn.Equals("urn:schemas-opencable-com:service:Tuner:1")) { if (isFirst) { isFirst = false; Log.Log.Info("UPnP device {0} removed", deviceDescriptor.FriendlyName); } IEnumerator <DeviceDescriptor> childDeviceDescriptorEn = deviceDescriptor.ChildDevices.GetEnumerator(); while (childDeviceDescriptorEn.MoveNext()) { if (childDeviceDescriptorEn.Current.DeviceUUID == childDeviceEn.Current.UUID) { break; } } Log.Log.Info(" remove {0} {1}", childDeviceDescriptorEn.Current.FriendlyName, childDeviceDescriptorEn.Current.DeviceUDN); _deviceEventListener.OnDeviceRemoved(childDeviceDescriptorEn.Current.DeviceUDN); break; } } } }
protected void TryConnect(RootDescriptor rootDescriptor) { DeviceConnection connection; string deviceUuid; lock (_networkTracker.SharedControlPointData.SyncObj) { if (_connection != null) { return; } DeviceDescriptor rootDeviceDescriptor = DeviceDescriptor.CreateRootDeviceDescriptor(rootDescriptor); DeviceDescriptor backendServerDescriptor = rootDeviceDescriptor.FindFirstDevice( UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); if (backendServerDescriptor == null) { return; } deviceUuid = backendServerDescriptor.DeviceUUID; string friendlyName = backendServerDescriptor.FriendlyName; SystemName system = new SystemName(new Uri(rootDescriptor.SSDPRootEntry.PreferredLink.DescriptionLocation).Host); if (deviceUuid == _homeServerSystemId) { ServiceRegistration.Get <ILogger>().Debug("UPnPClientControlPoint: Found MP2 home server '{0}' (system ID '{1}') at host '{2}' (IP address: '{3}')", friendlyName, deviceUuid, system.HostName, system.Address); } else { ServiceRegistration.Get <ILogger>().Debug("UPnPClientControlPoint: Found foreign MP2 server '{0}' (system ID '{1}') at host '{2}' (IP address: '{3}')", friendlyName, deviceUuid, system.HostName, system.Address); return; } try { connection = _connection = _controlPoint.Connect(rootDescriptor, deviceUuid, UPnPExtendedDataTypes.ResolveDataType); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error connecting to UPnP MP2 backend server '{0}'", e, deviceUuid); return; } } connection.DeviceDisconnected += OnUPnPDeviceDisconnected; try { CpService cdsStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.CONTENT_DIRECTORY_SERVICE_ID); if (cdsStub == null) { throw new InvalidDataException("ContentDirectory service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService risStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.RESOURCE_INFORMATION_SERVICE_ID); if (risStub == null) { throw new InvalidDataException("ResourceAccess service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService scsStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.SERVER_CONTROLLER_SERVICE_ID); if (scsStub == null) { throw new InvalidDataException("ServerController service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } CpService updmStub = connection.Device.FindServiceByServiceId(UPnPTypesAndIds.USER_PROFILE_DATA_MANAGEMENT_SERVICE_ID); if (updmStub == null) { throw new InvalidDataException("UserProfileDataManagement service not found in device '{0}' of type '{1}:{2}'", deviceUuid, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE, UPnPTypesAndIds.BACKEND_SERVER_DEVICE_TYPE_VERSION); } lock (_networkTracker.SharedControlPointData.SyncObj) { _contentDirectoryService = new UPnPContentDirectoryServiceProxy(cdsStub); _resourceInformationService = new UPnPResourceInformationServiceProxy(risStub); _serverControllerService = new UPnPServerControllerServiceProxy(scsStub); _userProfileDataManagementService = new UPnPUserProfileDataManagementServiceProxy(updmStub); } ICollection <UPnPServiceProxyBase> additionalServices = new List <UPnPServiceProxyBase>(); foreach (AdditionalServiceRegisterDlgt additionalServiceRegistration in _additionalServiceRegistrations) { try { additionalServices.Add(additionalServiceRegistration(connection)); } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error registering user service for UPnP MP2 backend server '{0}'", e, deviceUuid); } } lock (_networkTracker.SharedControlPointData.SyncObj) _additionalServices = additionalServices; } catch (Exception e) { ServiceRegistration.Get <ILogger>().Warn("UPnPClientControlPoint: Error connecting to services of UPnP MP2 backend server '{0}'", e, deviceUuid); connection.DeviceDisconnected -= OnUPnPDeviceDisconnected; _controlPoint.Disconnect(deviceUuid); return; } InvokeBackendServerDeviceConnected(connection); }