Skip to content

Commit

Permalink
feat: example browser
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jul 22, 2018
1 parent 003370e commit 1b976bb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
16 changes: 16 additions & 0 deletions Browser/Browser.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\Mdns.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
80 changes: 80 additions & 0 deletions Browser/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Makaretu.Dns;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Browser
{
class Program
{
public static void Main(string[] args)
{
var mdns = new MulticastService();
var sd = new ServiceDiscovery(mdns);

mdns.NetworkInterfaceDiscovered += (s, e) =>
{
foreach (var nic in e.NetworkInterfaces)
{
Console.WriteLine($"NIC '{nic.Name}'");
}

// Ask for the name of all services.
sd.QueryAllServices();
};

sd.ServiceDiscovered += (s, serviceName) =>
{
Console.WriteLine($"service '{serviceName}'");

// Ask for the name of instances of the service.
mdns.SendQuery(serviceName, type: DnsType.PTR);
};

mdns.AnswerReceived += (s, e) =>
{
// Is this an answer to a instances query?
var pointers = e.Message.Answers.OfType<PTRRecord>().Where(p => p.Name != ServiceDiscovery.ServiceName);
foreach (var pointer in pointers)
{
Console.WriteLine($"service instance '{pointer.DomainName}'");

// Ask for the service instance details.
mdns.SendQuery(pointer.DomainName, type: DnsType.SRV);
}

// Is this an answer to a service instance details?
var servers = e.Message.Answers.OfType<SRVRecord>();
foreach (var server in servers)
{
Console.WriteLine($"host '{server.Target}' for '{server.Name}'");

// Ask for the host IP addresses.
mdns.SendQuery(server.Target, type: DnsType.A);
mdns.SendQuery(server.Target, type: DnsType.AAAA);
}

// Is this an answer to host addresses?
var addresses = e.Message.Answers.OfType<AddressRecord>();
foreach (var address in addresses)
{
Console.WriteLine($"host '{address.Name}' at {address.Address}");
}

};

try
{
mdns.Start();
Console.ReadKey();
}
finally
{
sd.Dispose();
mdns.Stop();
}
}
}
}
10 changes: 8 additions & 2 deletions Mdns.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Documentation", "doc\Documentation.csproj", "{F3A32EA9-0B2F-46A3-B47A-33B4C04BD423}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Documentation", "doc\Documentation.csproj", "{F3A32EA9-0B2F-46A3-B47A-33B4C04BD423}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mdns", "src\Mdns.csproj", "{F3C81C57-C283-4E07-B765-DEABCFB22136}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MdnsTests", "test\MdnsTests.csproj", "{B459FBC7-4A28-4170-AD83-7348A403407F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Spike", "Spike\Spike.csproj", "{B7019D35-A153-4143-979C-C493A74FA7EA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Spike", "Spike\Spike.csproj", "{B7019D35-A153-4143-979C-C493A74FA7EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Browser", "Browser\Browser.csproj", "{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -41,6 +43,10 @@ Global
{B7019D35-A153-4143-979C-C493A74FA7EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7019D35-A153-4143-979C-C493A74FA7EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7019D35-A153-4143-979C-C493A74FA7EA}.Release|Any CPU.Build.0 = Release|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A77BEF8C-440E-46F7-ACFC-5EF06EFCA4BA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var sd = new ServiceDiscovery();
sd.Advertise(service);
```

See the [example advertiser](Spike/Program.cs) for a working program.

### Discovery

Find all services running on the local link.
Expand All @@ -51,6 +53,8 @@ var sd = new ServiceDiscovery();
sd.ServiceDiscovered += (s, serviceName) => { // Do something };
```

See the [example browser](Browser/Program.cs) for a working program.

## Usage Multicast

### Event Based Queries
Expand Down

0 comments on commit 1b976bb

Please # to comment.