Inheritance: IRegisterService
Esempio n. 1
1
        /// <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="addressToBroadcast">The address of the service that needs to be broadcasted</param>
        /// <param name="broadcastPort">The port of the broadcast service. Default=56789</param>
        public void Start(DiscoveryType type,string nameToBroadcast,string physicalLocation,string code,Uri addressToBroadcast,int broadcastPort=7892)
        {
            DiscoveryType = type;

            switch (DiscoveryType)
            {
                case DiscoveryType.WSDiscovery:
                    {
                        Ip = Net.GetIp(IPType.All);
                        Port = broadcastPort;
                        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;
                    }
                    break;
                case DiscoveryType.Zeroconf:
                    {
                        var 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.Register();
                    }
                    break;
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Mono.Zeroconf.RegisterService bonjourBrowserService = null;

            //-------------------------------------------------
            //begin setup zeroconf
            bonjourBrowserService             = new RegisterService();
            bonjourBrowserService.Name        = "CNCTouch Wi-fi Server";
            bonjourBrowserService.RegType     = "_cnctouch._tcp";
            bonjourBrowserService.ReplyDomain = "local.";
            bonjourBrowserService.Port        = 2030;

            // TxtRecords are optional
            TxtRecord txt_record = new TxtRecord();

            txt_record.Add("Password", "false");
            bonjourBrowserService.TxtRecord = txt_record;

            bonjourBrowserService.Register();

            // End setup zeroconf
            //---------------------------------------------------

            Console.ReadKey();
        }
Esempio n. 3
0
        public static void Start(short port)
        {
            service = new RegisterService
            {
                Name = Bonjour.ServiceName,
                RegType = Bonjour.ServiceType,
                // NOTE: port may have been dynamically determined by the broker
                Port = port,
                UPort = (ushort)port,
                ReplyDomain = Bonjour.ReplyDomain,
                //TxtRecord = new TxtRecord
                //{
                //    {
                //        Bonjour.IpRecord,
                //        Dns.GetHostEntry(Dns.GetHostName()).AddressList.First(ip => ip.AddressFamily == AddressFamily.InterNetwork)
                //        .ToString()
                //    },
                //    {
                //        Bonjour.PortRecord,
                //        port.ToString()
                //    }
                //}
            };

            service.Response += OnRegisterServiceResponse;
            service.Register();
        }
    public static void Main()
    {
        Application.Init();

        // Register a sample service
        RegisterService service = new RegisterService("Fruity Music", null, "_daap._tcp");
        TxtRecord record = new TxtRecord();
        record.Add("A", "Apples");
        record.Add("B", "Bananas");
        record.Add("C", "Carrots");
        service.Port = 8080;
        service.TxtRecord = record;
        service.RegisterAsync();

        // Listen for events of some service type
        ServiceBrowser browser = new ServiceBrowser("_daap._tcp");
        browser.ServiceAdded += OnServiceAdded;
        browser.ServiceRemoved += OnServiceRemoved;
        browser.StartAsync();

        // Unregister our service in 10 seconds
        GLib.Timeout.Add(10000, delegate {
            service.Dispose();
            return false;
        });

        // Stop browsing and quit in 15 seconds
        GLib.Timeout.Add(15000, delegate {
            browser.Dispose();
            Application.Quit();
            return false;
        });

        Application.Run();
    }
        public void RegisterServer(P2PDictionary dict)
        {
            if (service != null)
                throw new NotImplementedException();

            service = new RegisterService();
            service.Name = "com.rhfung.P2PDictionary " + dict.Description;
            service.RegType = ZEROCONF_NAME;
            service.ReplyDomain = "local";
            service.Port = (short)((System.Net.IPEndPoint)dict.LocalEndPoint).Port;
            service.TxtRecord = new TxtRecord();
            service.TxtRecord.Add("uid", dict.LocalID.ToString());
            service.Response += new RegisterServiceEventHandler(service_Response);

            service.Register();
        }
Esempio n. 6
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <remarks>
        /// TODO: Need to provide a way to configure this for a given
        /// machine (so the end user can name it, etc.). Eventually it
        /// might be useful for cleaning up zombied xsp2/apache2 processes,
        /// etc.
        /// </remarks>
        static void Main()
        {
			Configuration.Initialize();

			RegisterService service = new RegisterService();
			service.Name = Configuration.ServiceName;
			service.RegType = "_http._tcp";
			service.Port = 12321;
			service.TxtRecord = new TxtRecord();
			service.TxtRecord.Add(new TxtRecordItem("servicetype", "cloverleaf"));
			service.TxtRecord.Add(new TxtRecordItem("systemtype", Environment.MachineName + ", " +
					Environment.OSVersion.ToString()));

			service.Register();

			while (true) System.Threading.Thread.Sleep(5000);
        }
Esempio n. 7
0
        public static RegisterService ToRegisterService(this Service service)
        {
            var regService = new RegisterService {
                Name = service.Name,
                Port = service.Port,
                RegType = service.Type,
                ReplyDomain = service.Domain
            };

            if (service.Records != null && service.Records.Count > 0) {
                regService.TxtRecord = new TxtRecord();
                foreach (var txtRecord in service.Records) {
                    regService.TxtRecord.Add(txtRecord.Name, txtRecord.Value);
                }
            }

            return regService;
        }
Esempio n. 8
0
        public ZeroshareServer(string pin)
            : base(pin)
        {
            service = new RegisterService ();
            service.Name = "Zeroshare";
            service.RegType = "_zeroshare._tcp";
            service.ReplyDomain = "local.";
            service.Port = 5566;

            Console.Error.WriteLine ("PIN: {0}", pin);

            // TxtRecords are optional
            TxtRecord txt_record = new TxtRecord ();
            txt_record.Add ("PIN", pin);
            service.TxtRecord = txt_record;

            service.Response += HandleServiceResponse;
        }
Esempio n. 9
0
        /// <summary>
        /// Publish ZeroConf, so that WaveBox may advertise itself using mDNS to capable devices
        /// </summary>
        public bool Start()
        {
            string serverUrl = ServerUtility.GetServerUrl();
            if ((object)serverUrl == null)
            {
                logger.Error("Could not start ZeroConf service, due to null ServerUrl");
                return false;
            }

            // If we're already registered, dispose of it and create a new one
            if ((object)ZeroConf != null)
            {
                this.Stop();
            }

            // Create and register the service
            try
            {
                ZeroConf = new RegisterService();
                ZeroConf.Name = Hostname;
                ZeroConf.RegType = RegType;
                ZeroConf.ReplyDomain = ReplyDomain;
                ZeroConf.Port = (short)Injection.Kernel.Get<IServerSettings>().Port;

                TxtRecord record = new TxtRecord();
                record.Add(ServerUrlKey, serverUrl);
                ZeroConf.TxtRecord = record;

                ZeroConf.Register();
            }
            catch (Exception e)
            {
                logger.Error(e);
                this.Stop();
                return false;
            }

            return true;
        }
Esempio n. 10
0
 public async void Start()
 {
     if (mServer != null) throw new InvalidOperationException();
     lock (knownHosts) knownHosts.Clear();
     mServer = new HttpSelfHostServer(Configure());
     var serverTask = mServer.OpenAsync();
     RegisterService rs = new RegisterService();
     rs.RegType = "_tincer._tcp";
     rs.ReplyDomain = "local.";
     rs.Name = Environment.MachineName;
     rs.Port = (short)(ushort)Arguments.Global.ListenPort;
     rs.TxtRecord = new TxtRecord();
     rs.TxtRecord.Add("NODEID", ApiHost.Id.ToByteArray());
     rs.Register();
     rs.Response += rs_Response;
     serviceRegister = rs;
     sb.ServiceAdded += sb_ServiceAdded;
     sb.ServiceRemoved += sb_ServiceRemoved;
     sb.Browse("_tincer._tcp", "local.");
     await serverTask;
     timer.Change(30000, 30000);
 }
Esempio n. 11
0
    /// <summary>
    /// Start broadcasting service using zeroconf.
    /// </summary>
    private void RegisterZeroConf()
    {
        Debug.Log("Registering Zeroconf");
        service = new RegisterService();
        service.Name = "Openhab Kinect";
        service.RegType = "_openhab-kinect._tcp";
        service.ReplyDomain = "local.";
        service.Port = SERVER_PORT;

        service.Register();
        Debug.Log("Registered Zeroconf " + service.RegType);

        Mono.Zeroconf.ServiceBrowser browser = new Mono.Zeroconf.ServiceBrowser();
        browser.ServiceAdded += delegate (object o, Mono.Zeroconf.ServiceBrowseEventArgs args)
        {
            Debug.Log("Found Service: " + args.Service.Name);
        };
        browser.Browse("_openhab-kinect._tcp", "local");
    }
Esempio n. 12
0
        protected virtual void UnregisterService()
        {
            lock (eglock) {
                if (zc_service == null) {
                    return;
                }

                try {
                    zc_service.Dispose ();
                } catch {
                }
                zc_service = null;
            }
        }
Esempio n. 13
0
        protected virtual void RegisterService()
        {
            lock (eglock) {
                if (zc_service != null) {
                    UnregisterService ();
                }

                string auth = serverInfo.AuthenticationMethod == AuthenticationMethod.None ? "false" : "true";

                zc_service = new RegisterService ();
                zc_service.Name = serverInfo.Name;
                zc_service.RegType = ZeroconfType;
                zc_service.Port = (short)ws.BoundPort;
                zc_service.TxtRecord = new TxtRecord ();
                zc_service.TxtRecord.Add ("Password", auth);
                zc_service.TxtRecord.Add ("Machine Name", serverInfo.Name);

                AddTxtRecords (zc_service.TxtRecord);

                if (machineId != null) {
                    zc_service.TxtRecord.Add ("Machine ID", machineId);
                }

                zc_service.TxtRecord.Add ("txtvers", "1");
                zc_service.Response += OnRegisterServiceResponse;
                zc_service.Register ();
            }
        }
Esempio n. 14
0
        private void UnregisterService()
        {
            lock (eglock) {
                if (zc_service == null) {
                    return;
                }

                try {
                    zc_service.Dispose ();
                } catch {
                }
                zc_service = null;
            }
        }
Esempio n. 15
0
        private void RegisterService()
        {
            lock (eglock) {
                if (zc_service != null) {
                    UnregisterService ();
                }

                string auth = serverInfo.AuthenticationMethod == AuthenticationMethod.None ? "false" : "true";

                zc_service = new RegisterService ();
                zc_service.Name = serverInfo.Name;
                zc_service.RegType = "_dpap._tcp";
                zc_service.Port = 8770; // (short)ws.BoundPort;
                zc_service.TxtRecord = new TxtRecord ();
                zc_service.TxtRecord.Add ("Password", auth);
                zc_service.TxtRecord.Add ("Machine Name", serverInfo.Name);

                if (machineId != null) {
                    zc_service.TxtRecord.Add ("Machine ID", machineId);
                }

                zc_service.TxtRecord.Add ("txtvers", "1");
                zc_service.TxtRecord.Add ("Version", "65537");
                zc_service.ReplyDomain = "local.";
                zc_service.Response += OnRegisterServiceResponse;
                zc_service.Register ();
            }
        }
Esempio n. 16
0
		public GiverService()
		{
			Logger.Debug("New GiverService was created");
			running = true;

			int desired_port_num = Application.Preferences.PortNumber;
			if (desired_port_num < 0)
				//any port
				desired_port_num = 0;
			if (desired_port_num < IPEndPoint.MinPort || desired_port_num > IPEndPoint.MaxPort) {
				Logger.Debug ("Error: Port number must be between 0 and 65535. Trying to bind to any available port.");
				desired_port_num = 0;
			}
			port = desired_port_num;

			if (desired_port_num == 0 && !TryBindFixedOrAnyPort (desired_port_num, out port))
				ThrowUnableToBindAnyPort ();

			try {
				Logger.Debug ("Starting listener on port {0}", port);
				listener = CreateListener (port);
				listener.Start ();
			} catch (HttpListenerException hle) {
				Logger.Debug ("Error starting a http listener on port {0} : {1}", port, hle.Message);
				listener = TryBindAndListenAgain (desired_port_num, out port);
			} catch (SocketException se) {
				Logger.Debug ("Error starting a http listener on port {0} : {1}", port, se.Message);
				listener = TryBindAndListenAgain (desired_port_num, out port);
			}

			if (port != Application.Preferences.PortNumber)
				Logger.Debug ("We have the port : {0}", port);

			listnerThread  = new Thread(TcpServerThread);
			listnerThread.Start();
			Logger.Debug("About to start the Zeroconf Service");
            client = new RegisterService();
			AdvertiseService();
		}
Esempio n. 17
0
        public void Stop ()
		{
			running = false;
			listener.Stop();
			//server.Stop();

            if (client != null) {
                client.Dispose ();
                client = null;
            }
        }
Esempio n. 18
0
    private static void RegisterService(string serviceDescription)
    {
        Match match = Regex.Match(serviceDescription, @"(_[a-z]+._tcp|udp)\s*(\d+)\s*(.*)");
        if(match.Groups.Count < 4) {
            throw new ApplicationException("Invalid service description syntax");
        }

        string type = match.Groups[1].Value.Trim();
        short port = Convert.ToInt16(match.Groups[2].Value);
        string name = match.Groups[3].Value.Trim();
        
        int txt_pos = name.IndexOf("TXT");
        string txt_data = null;
        
        if(txt_pos > 0) {
            txt_data = name.Substring(txt_pos).Trim();
            name = name.Substring(0, txt_pos).Trim();
            
            if(txt_data == String.Empty) {
                txt_data = null;
            }
        }
                
        RegisterService service = new RegisterService();
        service.Name = name;
        service.RegType = type;
        service.ReplyDomain = "local.";
        service.Port = port;

        TxtRecord record = null;
        
        if(txt_data != null) {
            Match tmatch = Regex.Match(txt_data, @"TXT\s*\[(.*)\]");

            if(tmatch.Groups.Count != 2) {
                throw new ApplicationException("Invalid TXT record definition syntax");
            }
            
            txt_data = tmatch.Groups[1].Value;
        
            foreach(string part in Regex.Split(txt_data, @"'\s*,")) {
                string expr = part.Trim();
                if(!expr.EndsWith("'")) {
                    expr += "'";
                }
                
                Match pmatch = Regex.Match(expr, @"(\w+\s*\w*)\s*=\s*['](.*)[']\s*");
                string key = pmatch.Groups[1].Value.Trim();
                string val = pmatch.Groups[2].Value.Trim();
                
                if(key == null || key == String.Empty || val == null || val == String.Empty) {
                    throw new ApplicationException("Invalid key = 'value' syntax for TXT record item");
                }
                
                if(record == null) {
                    record = new TxtRecord();
                }
                
                record.Add(key, val);
            }
        }
        
        if(record != null) {
            service.TxtRecord = record;
        }
        
        Console.WriteLine("*** Registering name = '{0}', type = '{1}', domain = '{2}'", 
            service.Name,
            service.RegType,
            service.ReplyDomain);
            
        service.Response += OnRegisterServiceResponse;
        service.Register();
    }
Esempio n. 19
0
 public async void Stop()
 {
     if (mServer == null) throw new InvalidOperationException();
     timer.Change(Timeout.Infinite, Timeout.Infinite);
     var closeTask = mServer.CloseAsync();
     mServer = null;
     mConfig = null;
     try
     {
         serviceRegister.Dispose();
     }
     catch { }
     serviceRegister = null;
     try
     {
         sb.Dispose();
     }
     catch { }
     sb = null;
     await closeTask;
     lock (knownHosts) knownHosts.Clear();
 }