| OLD | NEW |
| 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 } |
| OLD | NEW |