Пример #1
0
        /// <summary>
        /// Looks for devices that host a service identified by its type and namespace. 
        /// It returns the transport address of the first matching service.
        /// </summary>
        public static string FindFirst(DpwsDiscoveryClient discoveryClient, string serviceTypeName, string namespaceUri)
        {
            if (discoveryClient == null)
                throw new ArgumentNullException();
            if (serviceTypeName == null)
                throw new ArgumentNullException();
            if (namespaceUri == null)
                throw new ArgumentNullException();

            Debug.Print("Discovering service devices...");
            // Define search criterias
            DpwsServiceType serviceType = new DpwsServiceType(serviceTypeName, namespaceUri);
            DpwsServiceTypes filters = new DpwsServiceTypes();
            filters.Add(serviceType);
            // Probe for devices
            DpwsServiceDescriptions probeMatches = discoveryClient.Probe(filters);
            if (probeMatches != null && probeMatches.Count > 0)
            {
                // Remember transport address of the first device
                string deviceTransportAddress = probeMatches[0].XAddrs[0];
                // Request metadata to get the desired service and its ID
                DpwsMexClient mexClient = new DpwsMexClient();
                DpwsMetadata metadata = mexClient.Get(deviceTransportAddress);
                // Check host service
                DpwsMexService host = metadata.Relationship.Host;
                if (host != null) // has host service
                {
                    if (host.ServiceTypes[serviceTypeName] != null)
                        return host.EndpointRefs[0].Address.AbsoluteUri;
                }
                // Check hosted services
                DpwsMexServices hostedServices = metadata.Relationship.HostedServices;
                if (hostedServices != null)
                {
                    for (int i = 0; i < hostedServices.Count; ++i)
                    {
                        DpwsMexService hostedService = hostedServices[i];
                        if (hostedService.ServiceTypes[serviceTypeName] != null)
                            return hostedService.EndpointRefs[0].Address.AbsoluteUri;
                    }
                }
            }
            Debug.Print("No service found.");
            return null;
        }
Пример #2
0
        /// <summary>
        /// Creates an instance of a DpwsClient class with a specified eventing callback port number.
        /// </summary>
        public DpwsClient(Binding localBinding, ProtocolVersion v)
        {
            m_threadLock               = new object();
            m_eventClient              = new DpwsEventingClient(v);
            m_mexClient                = new DpwsMexClient(v);
            m_eventCallbacks           = new WsServiceOperations();
            m_callbackServiceEndpoints = new WsServiceEndpoints();
            m_blockingCall             = true;
            m_ignoreRequestFromThisIP  = true;
            m_localBinding             = localBinding;
            m_version                  = v;

            m_discoClient = new DpwsDiscoveryClient(this, v);

            // Add the Hello and Bye discovery disco services
            ClientDiscoveryService = new DpwsDiscoClientService(this, v);

            // Start a Udp discovery service host
            WsUdpServiceHost.Instance.AddServiceEndpoint(ClientDiscoveryService);
            WsUdpServiceHost.Instance.IgnoreRequestFromThisIP = m_ignoreRequestFromThisIP;
            WsUdpServiceHost.Instance.MaxThreadCount = 5;
            WsUdpServiceHost.Instance.Start(new ServerBindingContext(v));
        }
        /// <summary>
        /// Creates an instance of a DpwsClient class with a specified eventing callback port number.
        /// </summary>
        public DpwsClient(Binding localBinding, ProtocolVersion v)
        {
            m_threadLock               = new object();
            m_eventClient              = new DpwsEventingClient(v);
            m_mexClient                = new DpwsMexClient(v);
            m_eventCallbacks           = new WsServiceOperations();
            m_callbackServiceEndpoints = new WsServiceEndpoints();
            m_blockingCall             = true;
            m_ignoreRequestFromThisIP  = true;
            m_localBinding             = localBinding;
            m_version                  = v;

            m_discoClient = new DpwsDiscoveryClient(this, v);

            // Add the Hello and Bye discovery disco services
            ClientDiscoveryService = new DpwsDiscoClientService(this, v);

            // Start a Udp discovery service host
            WsUdpServiceHost.Instance.AddServiceEndpoint(ClientDiscoveryService);
            WsUdpServiceHost.Instance.IgnoreRequestFromThisIP = m_ignoreRequestFromThisIP;
            WsUdpServiceHost.Instance.MaxThreadCount = 5;
            WsUdpServiceHost.Instance.Start(new ServerBindingContext(v));

            // Add eventing SubscriptionEnd ServiceOperations. By default Subscription End call back
            // to this client
            ServiceOperations.Add(new WsServiceOperation(WsWellKnownUri.WseNamespaceUri, "SubscriptionEnd"));

            // Add callbacks implemented by this client
            m_callbackServiceEndpoints.Add(this);

            // Start the Http service host
            m_httpServiceHost = new WsHttpServiceHost(m_localBinding, m_callbackServiceEndpoints);
            m_httpServiceHost.MaxThreadCount = m_callbackServiceEndpoints.Count;
            m_httpServiceHost.Start(new ServerBindingContext(v));
            System.Ext.Console.Write("Http service host started...");
        }
        public void PrintMetadataInfo()
        {
            this.IgnoreRequestFromThisIP = true;

            // This is the endpoint (logical) address of the target device
            // we want to obtain the metadata
            string deviceEndpointAddr = "urn:uuid:bde0943a-0516-c8ca-80a6-000000b525ed";

            Debug.Print("Resolving the device...");
            // We need to resolve the device to get the transport address
            DpwsServiceDescription resolveMatch = this.DiscoveryClient.Resolve(deviceEndpointAddr);
            if (resolveMatch != null)
            {
                // Device was located
                string deviceTransportAddr = resolveMatch.XAddrs[0];

                // Get metadata
                DpwsMexClient mexClient = new DpwsMexClient();
                DpwsMetadata metadata = mexClient.Get(deviceTransportAddr);
                if (metadata != null)
                {
                    Debug.Print("");
                    Debug.Print("Metadata:");
                    Debug.Print("ThisModel:");
                    Debug.Print("\tManufacturer: " + metadata.ThisModel.Manufacturer);
                    Debug.Print("\tManufacturerUrl: " + metadata.ThisModel.ManufacturerUrl);
                    Debug.Print("\tModelName: " + metadata.ThisModel.ModelName);
                    Debug.Print("\tModelNumber: " + metadata.ThisModel.ModelNumber);
                    Debug.Print("\tModelUrl: " + metadata.ThisModel.ModelUrl);
                    Debug.Print("\tPresentationUrl: " + metadata.ThisModel.PresentationUrl);
                    Debug.Print("ThisDevice:");
                    Debug.Print("\tFirmwareVersion: " + metadata.ThisDevice.FirmwareVersion);
                    Debug.Print("\tFriendlyName: " + metadata.ThisDevice.FriendlyName);
                    Debug.Print("\tSerialNumber: " + metadata.ThisDevice.SerialNumber);
                    DpwsMexService host = metadata.Relationship.Host;
                    if (host != null)
                    {
                        Debug.Print("Host:");
                        Debug.Print("\tServiceID: " + host.ServiceID);
                        Debug.Print("\tAddress: " + host.EndpointRefs[0].Address.AbsoluteUri);
                        Debug.Print("\tTypes:");
                        for (int t = 0; t < host.ServiceTypes.Count; ++t)
                        {
                            DpwsServiceType serviceType = host.ServiceTypes[t];
                            Debug.Print("\t\tName = " + serviceType.TypeName);
                            Debug.Print("\t\tNamespace = " + serviceType.NamespaceUri);
                            Debug.Print("");
                        }
                    }
                    DpwsMexServices hostedServices = metadata.Relationship.HostedServices;
                    if (hostedServices != null)
                    {
                        Debug.Print("HostedServices:");
                        for (int i = 0; i < hostedServices.Count; i++)
                        {
                            DpwsMexService hostedService = hostedServices[i];
                            Debug.Print("\tService ID: " + hostedService.ServiceID);
                            Debug.Print("\tAddress: " + hostedService.EndpointRefs[0].Address.AbsoluteUri);
                            Debug.Print("\tTypes:");
                            for (int t = 0; t < hostedService.ServiceTypes.Count; ++t)
                            {
                                DpwsServiceType serviceType = hostedService.ServiceTypes[t];
                                Debug.Print("\t\tName = " + serviceType.TypeName);
                                Debug.Print("\t\tNamespace = " + serviceType.NamespaceUri);
                                Debug.Print("");
                            }
                        }
                    }
                }
                else
                    Debug.Print("Did not get metadata from device.");
            }
            else
                Debug.Print("Device cannot be resolved.");
        }
        public void PrintMetadataInfo()
        {
            Debug.Print("Resolving the device...");
            // We need to resolve the device to get the transport address
            DpwsServiceDescription resolveMatch = m_simpleServiceClient.DiscoveryClient.Resolve(m_deviceEndpointAddress);
            //if(true)
            if (resolveMatch != null)
            {
                // Device was located
                string deviceTransportAddr = resolveMatch.XAddrs[0];

                // If transport address ends with '/' remove it
                int length = deviceTransportAddr.Length;
                if (deviceTransportAddr[length - 1] == '/')
                    deviceTransportAddr = deviceTransportAddr.Substring(0, length - 1);

                // Get metadata
                DpwsMexClient mexClient = new DpwsMexClient(m_version);
                DpwsMetadata metadata = mexClient.Get(deviceTransportAddr);
                if (metadata != null)
                {
                    Debug.Print("");
                    Debug.Print("Metadata:");
                    Debug.Print("ThisModel:");
                    Debug.Print("\tManufacturer: " + metadata.ThisModel.Manufacturer);
                    Debug.Print("\tManufacturerUrl: " + metadata.ThisModel.ManufacturerUrl);
                    Debug.Print("\tModelName: " + metadata.ThisModel.ModelName);
                    Debug.Print("\tModelNumber: " + metadata.ThisModel.ModelNumber);
                    Debug.Print("\tModelUrl: " + metadata.ThisModel.ModelUrl);
                    Debug.Print("\tPresentationUrl: " + metadata.ThisModel.PresentationUrl);
                    Debug.Print("ThisDevice:");
                    Debug.Print("\tFirmwareVersion: " + metadata.ThisDevice.FirmwareVersion);
                    Debug.Print("\tFriendlyName: " + metadata.ThisDevice.FriendlyName);
                    Debug.Print("\tSerialNumber: " + metadata.ThisDevice.SerialNumber);
                    DpwsMexService host = metadata.Relationship.Host;
                    if (host != null)
                    {
                        Debug.Print("Host:");
                        Debug.Print("\tServiceID: " + host.ServiceID);
                        Debug.Print("\tAddress: " + host.EndpointRefs[0].Address.AbsoluteUri);
                        Debug.Print("\tTypes:");
                        for (int t = 0; t < host.ServiceTypes.Count; ++t)
                        {
                            DpwsServiceType serviceType = host.ServiceTypes[t];
                            Debug.Print("\t\tName = " + serviceType.TypeName);
                            Debug.Print("\t\tNamespace = " + serviceType.NamespaceUri);
                            Debug.Print("");
                        }
                    }
                    DpwsMexServices hostedServices = metadata.Relationship.HostedServices;
                    if (hostedServices != null)
                    {
                        Debug.Print("HostedServices:");
                        for (int i = 0; i < hostedServices.Count; i++)
                        {
                            DpwsMexService hostedService = hostedServices[i];
                            Debug.Print("\tService ID: " + hostedService.ServiceID);
                            Debug.Print("\tAddress: " + hostedService.EndpointRefs[0].Address.AbsoluteUri);
                            Debug.Print("\tTypes:");
                            for (int t = 0; t < hostedService.ServiceTypes.Count; ++t)
                            {
                                DpwsServiceType serviceType = hostedService.ServiceTypes[t];
                                Debug.Print("\t\tName = " + serviceType.TypeName);
                                Debug.Print("\t\tNamespace = " + serviceType.NamespaceUri);
                                Debug.Print("");
                            }
                        }
                    }
                }
                else
                    Debug.Print("Did not get metadata from device.");
            }
            else
                Debug.Print("Device cannot be resolved.");
        }
Пример #6
0
        /// <summary>
        /// Stop tranport services and releases the managed resources used by this class.
        /// </summary>
        /// <param name="disposing">True to release managed resources</param>
        internal void Dispose(bool disposing)
        {
            if (disposing)
            {
                // Stop the transport services
                m_httpServiceHost.Stop();
                m_udpSeviceHost.Stop();

                m_threadLock = null;
                m_discoClient = null;
                m_endpointAddress = null;
                m_eventClient = null;
                m_mexClient = null;
                m_eventCallbacks = null;
                m_transportAddress = null;
                m_bodyParts = null;
                m_discoServiceEndpoints = null;
                m_callbackServiceEndpoints = null;
                m_udpSeviceHost = null;
                m_httpServiceHost = null;
            }
        }