OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart 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 file. |
| 4 |
| 5 library mdns.src.lookup_resolver; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 9 |
| 10 import 'package:mdns/src/packet.dart'; |
| 11 |
| 12 class PendingRequest extends LinkedListEntry { |
| 13 final String hostname; |
| 14 final Completer completer; |
| 15 PendingRequest(this.hostname, this.completer); |
| 16 } |
| 17 |
| 18 /// Class for keeping track of pending lookups and process incoming |
| 19 /// query responses. |
| 20 /// |
| 21 /// Currently the responses are no cached. |
| 22 class LookupResolver { |
| 23 LinkedList pendingRequests = new LinkedList(); |
| 24 |
| 25 Future addPendingRequest(String hostname, Duration timeout) { |
| 26 var completer = new Completer(); |
| 27 var request = new PendingRequest(hostname, completer); |
| 28 pendingRequests.add(request); |
| 29 return completer.future.timeout(timeout, onTimeout: () { |
| 30 request.unlink(); |
| 31 return null; |
| 32 }); |
| 33 } |
| 34 |
| 35 void handleResponse(List<DecodeResult> response) { |
| 36 for (var r in response) { |
| 37 pendingRequests |
| 38 .where((pendingRequest) => pendingRequest.hostname == r.name) |
| 39 .forEach((pendingRequest) { |
| 40 pendingRequest.completer.complete(r.address); |
| 41 pendingRequest.unlink(); |
| 42 }); |
| 43 } |
| 44 } |
| 45 } |
| 46 |
OLD | NEW |