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.native_extension_client; | 5 library mdns.src.native_extension_client; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
11 import 'package:mdns/mdns.dart'; | 11 import 'package:mdns/mdns.dart'; |
12 import 'package:mdns/src/lookup_resolver.dart'; | 12 import 'package:mdns/src/lookup_resolver.dart'; |
13 import 'package:mdns/src/native_extension_api.dart' | 13 import 'package:mdns/src/native_extension_api.dart' |
14 deferred as native_extension_api; | 14 deferred as native_extension_api; |
15 import 'package:mdns/src/packet.dart'; | 15 import 'package:mdns/src/packet.dart'; |
16 import 'package:mdns/src/constants.dart'; | |
16 | 17 |
17 // Requests Ids. This should be aligned with the C code. | 18 // Requests Ids. This should be aligned with the C code. |
18 enum RequestType { | 19 enum RequestType { |
19 echoRequest, // 0 | 20 echoRequest, // 0 |
20 lookupRequest, // 1 | 21 lookupRequest, // 1 |
21 } | 22 } |
22 | 23 |
23 // Implementation of mDNS client using a native extension. | 24 // Implementation of mDNS client using a native extension. |
24 class NativeExtensionMDnsClient implements MDnsClient { | 25 class NativeExtensionMDnsClient implements MDnsClient { |
25 bool _starting = false; | 26 bool _starting = false; |
(...skipping 22 matching lines...) Expand all Loading... | |
48 if (!_started) return; | 49 if (!_started) return; |
49 if (_starting) { | 50 if (_starting) { |
50 throw new StateError('Cannot stop mDNS client wile it is starting'); | 51 throw new StateError('Cannot stop mDNS client wile it is starting'); |
51 } | 52 } |
52 | 53 |
53 _incoming.close(); | 54 _incoming.close(); |
54 | 55 |
55 _started = false; | 56 _started = false; |
56 } | 57 } |
57 | 58 |
58 Future<InternetAddress> lookup( | 59 Future<InternetAddress> lookup( |
Søren Gjesse
2015/11/06 08:44:48
This should be a Stream of ResourceRecord now.
karlklose
2015/11/06 12:20:00
Done.
| |
59 String hostname, {Duration timeout: const Duration(seconds: 5)}) { | 60 int type, |
61 String name, | |
62 {Duration timeout: const Duration(seconds: 5)}) { | |
60 if (!_started) { | 63 if (!_started) { |
61 throw new StateError('mDNS client is not started'); | 64 throw new StateError('mDNS client is not started'); |
62 } | 65 } |
63 | 66 |
67 if (type != RRType.A) { | |
68 // TODO(karlklose): add support. | |
69 throw 'RR type $type not supported.'; | |
70 } | |
71 | |
64 // Add the pending request before sending the query. | 72 // Add the pending request before sending the query. |
65 var future = _resolver.addPendingRequest(hostname, timeout); | 73 var future = _resolver.addPendingRequest(type, name, timeout); |
66 | 74 |
67 // Send the request. | 75 // Send the request. |
68 _service.send([_incoming.sendPort, | 76 _service.send([_incoming.sendPort, |
69 RequestType.lookupRequest.index, | 77 RequestType.lookupRequest.index, |
70 hostname]); | 78 name]); |
71 | 79 |
72 return future; | 80 return future; |
73 } | 81 } |
74 | 82 |
75 // Process incoming responses. | 83 // Process incoming responses. |
76 _handleIncoming(response) { | 84 _handleIncoming(response) { |
77 // Right not the only response we can get is the response to a | 85 // Right now the only response we can get is the response to a |
78 // lookupRequest where the response looks like this: | 86 // lookupRequest where the response looks like this: |
79 // | 87 // |
80 // response[0]: hostname (String) | 88 // response[0]: hostname (String) |
81 // response[1]: IPv4 address (Uint8List) | 89 // response[1]: IPv4 address (Uint8List) |
82 if (response is List && response.length == 2) { | 90 if (response is List && response.length == 2) { |
83 if (response[0] is String && | 91 if (response[0] is String && |
84 response[1] is List && response[1].length == 4) { | 92 response[1] is List && response[1].length == 4) { |
85 response = new DecodeResult(response[0], | 93 response = new ResourceRecord( |
86 new InternetAddress(response[1].join('.'))); | 94 RRType.A, |
95 response[0], | |
96 response[1].codeUnits, | |
97 new DateTime.now().millisecondsSinceEpoch + 2000); | |
87 _resolver.handleResponse([response]); | 98 _resolver.handleResponse([response]); |
88 } else { | 99 } else { |
89 // TODO(sgjesse): Improve the error handling. | 100 // TODO(sgjesse): Improve the error handling. |
90 print('mDNS Response not understood'); | 101 print('mDNS Response not understood'); |
91 } | 102 } |
92 } | 103 } |
93 } | 104 } |
94 } | 105 } |
95 | 106 |
96 Future nativeExtensionEchoTest(dynamic message) async { | 107 Future nativeExtensionEchoTest(dynamic message) async { |
97 await native_extension_api.loadLibrary(); | 108 await native_extension_api.loadLibrary(); |
98 SendPort service = native_extension_api.servicePort(); | 109 SendPort service = native_extension_api.servicePort(); |
99 ReceivePort port = new ReceivePort(); | 110 ReceivePort port = new ReceivePort(); |
100 try { | 111 try { |
101 service.send([port.sendPort, RequestType.echoRequest.index, message]); | 112 service.send([port.sendPort, RequestType.echoRequest.index, message]); |
102 return await port.first; | 113 return await port.first; |
103 } finally { | 114 } finally { |
104 port.close(); | 115 port.close(); |
105 } | 116 } |
106 } | 117 } |
OLD | NEW |