コード例 #1
0
        public override bool unregisterService(ServiceName serviceName)
        {
            DomainName        dnsName  = serviceName.toDnsName();
            DomainName        typeName = DomainName.Parse(serviceName.getType().toDnsString()) + registrationDomain;
            List <DomainName> subtypes = new List <DomainName>(serviceName.getType().getSubtypes().Count);

            foreach (String subtype in serviceName.getType().toDnsStringsWithSubtype())
            {
                subtypes.Add(DomainName.Parse(subtype) + registrationDomain);
            }

            DnsUpdateMessage msg = new DnsUpdateMessage();

            msg.ZoneName = registrationDomain; // XXX Should really be the zone (SOA) for the RRs we are about to add

            msg.Prequisites.Add(new RecordExistsPrequisite(dnsName, RecordType.Any));

            msg.Updates.Add(new DeleteRecordUpdate(new PtrRecord(typeName, timeToLive, dnsName)));


            foreach (DomainName subtype in subtypes)
            {
                msg.Updates.Add(new DeleteRecordUpdate(new PtrRecord(subtype, timeToLive, dnsName)));
            }

            msg.Updates.Add(new DeleteAllRecordsUpdate(dnsName));

            TSigRecord tsigRecord = generateTSigRecord(msg.TransactionID);

            if (tsigRecord != null)
            {
                msg.TSigOptions = tsigRecord;
            }


            DnsUpdateMessage response = resolver.SendUpdate(msg);

            if (response != null)
            {
                switch (response.ReturnCode)
                {
                case ReturnCode.NoError:
                    //flushCache(update);
                    break;

                case ReturnCode.YXDomain:        // Prerequisite failed, the service already exists.
                    return(false);

                default:
                    throw new DnsSDException("Failed to send DNS update to server. Server returned error code: " + response.ReturnCode.ToString());
                }
            }
            else
            {
                throw new DnsSDException("Failed to send DNS update to server. Server time out");
            }

            // Remove the service type if there are no instances left
            msg          = new DnsUpdateMessage();
            msg.ZoneName = registrationDomain; // XXX Should really be the zone (SOA) for the RRs we are about to add

            msg.Prequisites.Add(new RecordNotExistsPrequisite(typeName, RecordType.Any));
            msg.Updates.Add(new DeleteRecordUpdate(new PtrRecord(servicesName, timeToLive, typeName)));

            tsigRecord = generateTSigRecord(msg.TransactionID);
            if (tsigRecord != null)
            {
                msg.TSigOptions = tsigRecord;
            }


            response = resolver.SendUpdate(msg);
            if (response != null)
            {
                switch (response.ReturnCode)
                {
                case ReturnCode.NoError:
                    //flushCache(update);
                    Log.Info(String.Format("Removed service type record {0}", typeName));
                    break;

                case ReturnCode.YXDomain:        // Prerequisite failed, service instances exists
                    Log.Info(String.Format("Did not remove service type record {0}, instances left.", typeName));
                    break;

                default:
                    Log.Error(String.Format("Failed to remove service type {0}, server returned status {1}", typeName, response.ReturnCode.ToString()));
                    break;
                }
            }
            else
            {
                throw new DnsSDException("Failed to send DNS update to server. Server time out");
            }



            return(true);
        }
コード例 #2
0
        public override bool registerService(ServiceData serviceData)
        {
            ServiceName       serviceName = serviceData.getName();
            DomainName        dnsName     = serviceName.toDnsName();
            DomainName        typeName    = DomainName.Parse(serviceName.getType().toDnsString()) + registrationDomain;
            List <DomainName> subtypes    = new List <DomainName>(serviceName.getType().getSubtypes().Count);

            foreach (String subtype in serviceName.getType().toDnsStringsWithSubtype())
            {
                subtypes.Add(DomainName.Parse(subtype) + registrationDomain);
            }
            DomainName target = DomainName.Parse(serviceData.getHost());

            List <String> strings = new List <String>();

            foreach (var entry in serviceData.getProperties())
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(entry.Key);
                if (entry.Key != null)
                {
                    sb.Append('=').Append(entry.Value);
                }
                strings.Add(sb.ToString());
            }
            if (strings.Count <= 0)
            {
                // Must not be empty
                strings.Add("");
            }

            DnsUpdateMessage msg = new DnsUpdateMessage();

            msg.ZoneName = registrationDomain; // XXX Should really be the zone (SOA) for the RRs we are about to add

            msg.Prequisites.Add(new RecordNotExistsPrequisite(dnsName, RecordType.Any));
            msg.Updates.Add(new AddRecordUpdate(new PtrRecord(servicesName, timeToLive, typeName)));
            msg.Updates.Add(new AddRecordUpdate(new PtrRecord(typeName, timeToLive, dnsName)));


            foreach (DomainName subtype in subtypes)
            {
                msg.Updates.Add(new AddRecordUpdate(new PtrRecord(subtype, timeToLive, dnsName)));
            }

            msg.Updates.Add(new AddRecordUpdate(new SrvRecord(dnsName, timeToLive, 0, 0, Convert.ToUInt16(serviceData.getPort()), target)));

            msg.Updates.Add(new AddRecordUpdate(new TxtRecord(dnsName, timeToLive, strings)));

            TSigRecord tsigRecord = generateTSigRecord(msg.TransactionID);

            if (tsigRecord != null)
            {
                msg.TSigOptions = tsigRecord;
            }


            DnsUpdateMessage response = resolver.SendUpdate(msg);


            if (response != null)
            {
                switch (response.ReturnCode)
                {
                case ReturnCode.NoError:
                    //flushCache(update);
                    return(true);

                case ReturnCode.YXDomain:        // Prerequisite failed, the service already exists.
                    return(false);

                default:
                    throw new DnsSDException("Failed to send DNS update to server. Server returned error code: " + response.ReturnCode.ToString());
                }
            }
            else
            {
                throw new DnsSDException("Failed to send DNS update to server. Server time out");
            }
        }