상속: Service, IRegisterService, IDisposable
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceDiscovery"/> class.
        /// </summary>
        public DeviceDiscovery(short port, string messageToCast)
        {
            try
            {
                Port = port;
                MessageToCast = messageToCast;

                // Register all providers //
                foreach (var assembly in Directory.GetFiles(".", "*Mono.Zeroconf.Providers.*.dll", SearchOption.AllDirectories))
                    new Publish().GacInstall(Path.GetFullPath(assembly));

                _service = new RegisterService()
                {
                    Name = MessageToCast ?? Environment.MachineName,
                    RegType = "_test._tcp",
                    ReplyDomain = "local.",
                    Port = Port
                };
            }
            catch (Exception e)
            {
                throw e;
            }
        }
예제 #2
0
        /// <summary>
        /// Start new broadcast service
        /// </summary>
        /// <param name="type">Type of discovery</param>
        /// <param name="nameToBroadcast">The name of the service that needs to be broadcasted</param>
        /// <param name="physicalLocation">The physical location of the service that needs to be broadcasted</param>
        /// <param name="code">code to be broadcasted (e.g. device id)</param>
        /// <param name="addressToBroadcast">The address of the service that needs to be broadcasted</param>
        public void Start( DiscoveryType type, string nameToBroadcast, string physicalLocation, string code, Uri addressToBroadcast)
        {
            DiscoveryType = type;

            switch ( DiscoveryType )
            {
                case DiscoveryType.WsDiscovery:
                {
                    Ip = Net.GetIp( IpType.All );
                    Port = 7985;
                    Address = "http://" + Ip + ":" + Port + "/";

                    _discoveryHost = new ServiceHost( new DiscoveyService() );

                    var serviceEndpoint = _discoveryHost.AddServiceEndpoint( typeof( IDiscovery ), new WebHttpBinding(),
                                                                             Net.GetUrl( Ip, Port, "" ) );
                    serviceEndpoint.Behaviors.Add( new WebHttpBehavior() );

                    var broadcaster = new EndpointDiscoveryBehavior();

                    broadcaster.Extensions.Add( nameToBroadcast.ToXElement<string>() );
                    broadcaster.Extensions.Add( physicalLocation.ToXElement<string>() );
                    broadcaster.Extensions.Add( addressToBroadcast.ToString().ToXElement<string>() );
                    broadcaster.Extensions.Add( code.ToXElement<string>() );

                    serviceEndpoint.Behaviors.Add( broadcaster );
                    _discoveryHost.Description.Behaviors.Add( new ServiceDiscoveryBehavior() );
                    _discoveryHost.Description.Endpoints.Add( new UdpDiscoveryEndpoint() );
                    _discoveryHost.Open();

                    IsRunning = true;
                    Debug.WriteLine( DiscoveryType.ToString() + " is started" );
                }
                    break;
                case DiscoveryType.Zeroconf:
                {
                    _service = new RegisterService { Name = nameToBroadcast, RegType = "_am._tcp", ReplyDomain = "local", Port = 3689 };

                    // TxtRecords are optional
                    var txtRecord = new TxtRecord
                    {
                        { "name", nameToBroadcast },
                        { "addr", addressToBroadcast.ToString() },
                        { "loc", physicalLocation },
                        { "code", code }
                    };
                    _service.TxtRecord = txtRecord;
                    _service.Response += service_Response;
                    _service.Register();
                    Debug.WriteLine(DiscoveryType.ToString() + " is started");
                }
                    break;
            }
        }