Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(291)

Side by Side Diff: pkg/mdns/lib/src/lookup_resolver.dart

Issue 1412063015: Improve resource record implementation in the mdns package. (Closed) Base URL: https://github.com/dart-lang/fletch.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE.md file.
4 4
5 library mdns.src.lookup_resolver; 5 library mdns.src.lookup_resolver;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:collection'; 8 import 'dart:collection';
9 9
10 import 'package:mdns/src/packet.dart'; 10 import 'package:mdns/src/packet.dart';
11 11
12 class PendingRequest extends LinkedListEntry { 12 class PendingRequest extends LinkedListEntry {
13 final String hostname; 13 final int type;
14 final Completer completer; 14 final String name;
15 PendingRequest(this.hostname, this.completer); 15 final StreamController controller;
16
17 PendingRequest(this.type, this.name, this.controller);
16 } 18 }
17 19
18 /// Class for keeping track of pending lookups and process incoming 20 /// Class for keeping track of pending lookups and process incoming
19 /// query responses. 21 /// query responses.
20 ///
21 /// Currently the responses are no cached.
22 class LookupResolver { 22 class LookupResolver {
23 LinkedList pendingRequests = new LinkedList(); 23 LinkedList pendingRequests = new LinkedList();
24 24
25 Future addPendingRequest(String hostname, Duration timeout) { 25 Stream<ResourceRecord> addPendingRequest(
26 var completer = new Completer(); 26 int type,
27 var request = new PendingRequest(hostname, completer); 27 String name,
28 Duration timeout) {
29 StreamController controller = new StreamController();
30 PendingRequest request = new PendingRequest(type, name, controller);
28 pendingRequests.add(request); 31 pendingRequests.add(request);
29 return completer.future.timeout(timeout, onTimeout: () { 32 Timer timer = new Timer(timeout, () {
30 request.unlink(); 33 request.unlink();
31 return null; 34 controller.close();
32 }); 35 });
36 return controller.stream;
33 } 37 }
34 38
35 void handleResponse(List<DecodeResult> response) { 39 void handleResponse(List<ResourceRecord> response) {
36 for (var r in response) { 40 for (ResourceRecord r in response) {
37 var name = r.name.toLowerCase(); 41 int type = r.type;
42 String name = r.name.toLowerCase();
38 if (name.endsWith('.')) name = name.substring(0, name.length - 1); 43 if (name.endsWith('.')) name = name.substring(0, name.length - 1);
39 pendingRequests 44
40 .where((pendingRequest) { 45 bool responseMatches(PendingRequest request) {
41 return pendingRequest.hostname.toLowerCase() == name; 46 return request.name.toLowerCase() == name &&
42 }) 47 request.type == type;
43 .forEach((pendingRequest) { 48 }
44 pendingRequest.completer.complete(r.address); 49
45 pendingRequest.unlink(); 50 pendingRequests.where(responseMatches).forEach((pendingRequest) {
46 }); 51 if (pendingRequest.controller.isClosed) return;
52 pendingRequest.controller.add(r);
53 });
47 } 54 }
48 } 55 }
49 } 56 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698