Skip to content

Commit be10902

Browse files
sgjessednfield
authored andcommitted
Update the mDNS command line tools in the mdns package
Add a lookup tool. Add a timeout option to both tools. [email protected], [email protected] BUG= Review URL: https://codereview.chromium.org/1432973002 .
1 parent 5eae734 commit be10902

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

pkg/mdns/bin/mdns-resolve.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE.md file.
4+
5+
// Example script to illustrate how to use the mdns package to lookup names
6+
// on the local network.
7+
8+
import 'package:args/args.dart';
9+
10+
import '../lib/mdns.dart';
11+
12+
main(List<String> args) async {
13+
// Parse the command line arguments.
14+
var parser = new ArgParser();
15+
parser.addOption('timeout', abbr: 't', defaultsTo: '5');
16+
var arguments = parser.parse(args);
17+
18+
if (arguments.rest.length != 1) {
19+
print('''
20+
Please provide an address as argument.
21+
22+
For example:
23+
dart mdns-resolve.dart [--timeout <timeout>] fletch.local''');
24+
return;
25+
}
26+
27+
var name = arguments.rest[0];
28+
29+
MDnsClient client = new MDnsClient();
30+
await client.start();
31+
var timeout;
32+
timeout = new Duration(seconds: int.parse(arguments['timeout']));
33+
await for (ResourceRecord record in
34+
client.lookup(RRType.A, name, timeout: timeout)) {
35+
print('Found address (${record.address}).');
36+
}
37+
client.stop();
38+
}

pkg/mdns/bin/mdns-sd.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@
55
// Example script to illustrate how to use the mdns package to discover services
66
// on the local network.
77

8-
import 'mdns.dart';
8+
import 'package:args/args.dart';
9+
10+
import '../lib/mdns.dart';
911

1012
main(List<String> args) async {
11-
if (args.length != 1) {
13+
// Parse the command line arguments.
14+
var parser = new ArgParser();
15+
parser.addOption('timeout', abbr: 't', defaultsTo: '5');
16+
var arguments = parser.parse(args);
17+
18+
if (arguments.rest.length != 1) {
1219
print('''
1320
Please provide the name of a service as argument.
1421
1522
For example:
16-
dart mdns-sd.dart _workstation._tcp.local''');
23+
dart mdns-sd.dart [--timeout <timeout>] _workstation._tcp.local''');
1724
return;
1825
}
26+
27+
var name = arguments.rest[0];
28+
1929
MDnsClient client = new MDnsClient();
2030
await client.start();
21-
await for (ResourceRecord ptr in client.lookup(RRType.PTR, args[0])) {
31+
await for (ResourceRecord ptr in client.lookup(RRType.PTR, name)) {
2232
String domain = ptr.domainName;
2333
await for (ResourceRecord srv in client.lookup(RRType.SRV, domain)) {
2434
String target = srv.target;
2535
await for (ResourceRecord ip in client.lookup(RRType.A, target)) {
26-
print('Service instance found at $target ($ip).');
36+
print('Service instance found at $target (${ip.address}).');
2737
}
2838
}
2939
}

pkg/mdns/lib/mdns.dart

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,3 @@ abstract class MDnsClient {
5959
String name,
6060
{Duration timeout: const Duration(seconds: 5)});
6161
}
62-
63-
// Simple standalone test.
64-
Future main(List<String> args) async {
65-
var client = new MDnsClient();
66-
await client.start();
67-
ResourceRecord resource = await client.lookup(RRType.A, args[0]).first;
68-
print(address);
69-
client.stop();
70-
}

0 commit comments

Comments
 (0)