Skip to content

Commit

Permalink
Adding the unadvertising of services
Browse files Browse the repository at this point in the history
Notifying clients about the removal of a published service (goodbye-message, TTL=0).
  • Loading branch information
gilzad committed Jun 7, 2019
1 parent fc20b5a commit 34d9b46
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/ServiceDiscovery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,37 @@ public void Advertise(ServiceProfile service)
}
}

/// <summary>
/// Sends a goodbye message for the provided
/// profile and removes its pointer from the name sever.
/// </summary>
/// <param name="profile">The profile to send a goodbye message for.</param>
public void Unadvertise(ServiceProfile profile)
{
var message = new Message { QR = true };
var ptrRecord = new PTRRecord { Name = profile.QualifiedServiceName, DomainName = profile.FullyQualifiedName };
ptrRecord.TTL = TimeSpan.Zero;

message.Answers.Add(ptrRecord);
profile.Resources.ForEach((resource) =>
{
resource.TTL = TimeSpan.Zero;
message.AdditionalRecords.Add(resource);
});

Mdns.SendAnswer(message);

NameServer.Catalog.TryRemove(profile.QualifiedServiceName, out Node _);
}

/// <summary>
/// Sends a goodbye message for each anounced service.
/// </summary>
public void Unadvertise()
{
profiles.ForEach(profile => Unadvertise(profile));
}

void OnAnswer(object sender, MessageEventArgs e)
{
var msg = e.Message;
Expand Down

0 comments on commit 34d9b46

Please # to comment.