Пример #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;
            }
        }
    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();
    }
Пример #3
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;
        }
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
                                    ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen,
                                    IntPtr txtRecord, IntPtr contex)
        {
            is_resolved     = true;
            resolve_pending = false;

            InterfaceIndex = interfaceIndex;
            FullName       = fullname;
            HostTarget     = hosttarget;
            this.port      = (short)port;
            TxtRecord      = new TxtRecord(txtLen, txtRecord);

            EventHandler handler = Resolved;

            if (handler != null)
            {
                handler(this, new EventArgs());
            }

            sdRef.Deallocate();
        }
Пример #5
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;
        }
        private void PublishZeroconf()
        {
            var name = "Lightswitch " + Environment.MachineName;
            _netservice = new Mono.Zeroconf.Providers.Bonjour.RegisterService
            {
                Name = name,
                RegType = "_lightswitch._tcp",
                ReplyDomain = "",
                Port = (short)PortSetting
            };

            _netservice.Response += netservice_Response;

            // TxtRecords are optional
            var txtRecord = new TxtRecord
            {
                {"txtvers", "1"},
                {"ServiceName", name},
                {"MachineName", Environment.MachineName},
                {"OS", Environment.OSVersion.ToString()},
                {"IPAddress", "127.0.0.1"},
                {"Version", Utils.ApplicationNameAndVersion}
            };
            //txt_record.Add("Password", "false");
            _netservice.TxtRecord = txtRecord;

            _netservice.Register();
        }
Пример #7
0
        private void AdvertiseService () 
		{
            try {
				Logger.Debug("Adding Zeroconf Service _giver._tcp");
				TxtRecord txt = new TxtRecord();
				
				txt.Add("User Name", Application.Preferences.UserName);
				txt.Add("Machine Name", Environment.MachineName);
				txt.Add("Version", Defines.Version);
				
				if( Application.Preferences.PhotoType.CompareTo(Preferences.Local) == 0) {					
					txt.Add("PhotoType", Preferences.Local);
					txt.Add("Photo", "none");
				} else if( Application.Preferences.PhotoType.CompareTo(Preferences.Gravatar) == 0) {
					txt.Add("PhotoType", Preferences.Gravatar);
					txt.Add("Photo", Giver.Utilities.GetMd5Sum(Application.Preferences.PhotoLocation));	
				} else if( Application.Preferences.PhotoType.CompareTo(Preferences.Uri) == 0) {
					txt.Add("PhotoType", Preferences.Uri);
					txt.Add("Photo", Application.Preferences.PhotoLocation);
				} else {
					txt.Add("PhotoType", Preferences.None);
					txt.Add("Photo", "none");
				}
				
				client.Name = "giver on " + Application.Preferences.UserName + "@" + Environment.MachineName; 
				client.RegType = "_giver._tcp";
				client.ReplyDomain = "local.";
				client.Port = (short)port;
				client.TxtRecord = txt;

                client.Register();
				Logger.Debug("Avahi Service  _giver._tcp is added");
            } catch (Exception e) {
				Logger.Debug("Exception adding service: {0}", e.Message);
				Logger.Debug("Exception is: {0}", e);
            }
        }
Пример #8
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();
    }
 public TxtRecordEnumerator(TxtRecord record)
 {
     this.record = record;
 }
        private void OnResolveReply(ServiceRef sdRef, ServiceFlags flags, uint interfaceIndex,
            ServiceError errorCode, string fullname, string hosttarget, ushort port, ushort txtLen, 
            IntPtr txtRecord, IntPtr contex)
        {
            is_resolved = true;
            resolve_pending = false;

            InterfaceIndex = interfaceIndex;
            FullName = fullname;
            HostTarget = hosttarget;
            this.port = (short)port;
            TxtRecord = new TxtRecord(txtLen, txtRecord);

            EventHandler handler = Resolved;
            if(handler != null) {
                handler(this, new EventArgs());
            }

            sdRef.Deallocate();
        }
Пример #11
0
 public TxtRecordEnumerator(TxtRecord record)
 {
     this.record = record;
 }