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

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: Remove unnecessary await. 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
« no previous file with comments | « pkg/mdns/lib/src/constants.dart ('k') | pkg/mdns/lib/src/native_extension_client.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 Timer timer;
17
18 PendingRequest(this.type, this.name, this.controller);
16 } 19 }
17 20
18 /// Class for keeping track of pending lookups and process incoming 21 /// Class for keeping track of pending lookups and process incoming
19 /// query responses. 22 /// query responses.
20 ///
21 /// Currently the responses are no cached.
22 class LookupResolver { 23 class LookupResolver {
23 LinkedList pendingRequests = new LinkedList(); 24 LinkedList pendingRequests = new LinkedList();
24 25
25 Future addPendingRequest(String hostname, Duration timeout) { 26 Stream<ResourceRecord> addPendingRequest(
26 var completer = new Completer(); 27 int type,
27 var request = new PendingRequest(hostname, completer); 28 String name,
29 Duration timeout) {
30 StreamController controller = new StreamController();
31 PendingRequest request = new PendingRequest(type, name, controller);
32 Timer timer = new Timer(timeout, () {
33 request.unlink();
34 controller.close();
35 });
36 request.timer = timer;
28 pendingRequests.add(request); 37 pendingRequests.add(request);
29 return completer.future.timeout(timeout, onTimeout: () { 38 return controller.stream;
30 request.unlink();
31 return null;
32 });
33 } 39 }
34 40
35 void handleResponse(List<DecodeResult> response) { 41 void handleResponse(List<ResourceRecord> response) {
36 for (var r in response) { 42 for (ResourceRecord r in response) {
37 var name = r.name.toLowerCase(); 43 int type = r.type;
44 String name = r.name.toLowerCase();
38 if (name.endsWith('.')) name = name.substring(0, name.length - 1); 45 if (name.endsWith('.')) name = name.substring(0, name.length - 1);
39 pendingRequests 46
40 .where((pendingRequest) { 47 bool responseMatches(PendingRequest request) {
41 return pendingRequest.hostname.toLowerCase() == name; 48 return request.name.toLowerCase() == name &&
42 }) 49 request.type == type;
43 .forEach((pendingRequest) { 50 }
44 pendingRequest.completer.complete(r.address); 51
45 pendingRequest.unlink(); 52 pendingRequests.where(responseMatches).forEach((pendingRequest) {
46 }); 53 if (pendingRequest.controller.isClosed) return;
54 pendingRequest.controller.add(r);
55 });
56 }
57 }
58
59 void clearPendingRequests() {
60 while (pendingRequests.isNotEmpty) {
61 PendingRequest request = pendingRequests.first;
62 request.unlink();
63 request.timer.cancel();
64 request.controller.close();
47 } 65 }
48 } 66 }
49 } 67 }
OLDNEW
« no previous file with comments | « pkg/mdns/lib/src/constants.dart ('k') | pkg/mdns/lib/src/native_extension_client.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698