Chromium Code Reviews| Index: sdk/lib/io/io_resource_info.dart |
| diff --git a/sdk/lib/io/io_resource_info.dart b/sdk/lib/io/io_resource_info.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2d1c7b4c765f7edcad4a88850d59e14d3e82cf6d |
| --- /dev/null |
| +++ b/sdk/lib/io/io_resource_info.dart |
| @@ -0,0 +1,154 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of dart.io; |
| + |
| +abstract class _IOResourceInfo { |
| + String type; |
|
Søren Gjesse
2015/09/02 09:07:49
Can't type and id and be final?
ricow1
2015/09/02 10:45:42
Done.
|
| + int id; |
| + String get name; |
| + static int _count = 0; |
| + |
| + _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); |
| + |
| + String toJSON(); |
| + |
| + /// Get the full set of values for a specific implementation. This is normally |
| + /// looked up based on an id from a referenceValueMap. |
| + Map<String, String> get fullValueMap; |
| + |
| + /// The reference map, used to return a list of values, e.g., getting |
| + /// all open sockets. The structure of this is shared among all subclasses. |
| + Map<String, String> get referenceValueMap => |
|
Søren Gjesse
2015/09/02 09:07:49
Cache this value?
ricow1
2015/09/02 10:45:42
Imagine how few times this will be called compared
Søren Gjesse
2015/09/02 12:24:50
Good point. Lets leave it as is.
|
| + { |
| + 'type': '@$type', |
|
Søren Gjesse
2015/09/02 09:07:49
What is the reason for the @?
ricow1
2015/09/02 10:45:42
John told me that the reference objects have that
Søren Gjesse
2015/09/02 12:24:50
Thought so :-) - maybe add a comment.
ricow1
2015/09/02 12:45:50
Done.
|
| + 'id': id, |
| + 'name': name, |
| + }; |
| + |
| + static int getNextID() => _count++; |
| +} |
| + |
| +abstract class _ReadWriteResourceInfo extends _IOResourceInfo { |
| + int totalRead; |
| + int totalWritten; |
| + int readCount; |
| + int writeCount; |
| + double lastRead; |
| + double lastWrite; |
| + |
| + _ReadWriteResourceInfo(String type) : |
| + totalRead = 0, |
| + totalWritten = 0, |
| + readCount = 0, |
| + writeCount = 0, |
| + lastRead = 0.0, |
| + lastWrite = 0.0, |
| + super(type); |
| + |
| + Map<String, String> get fullValueMap => |
| + { |
| + 'type': type, |
| + 'id': id, |
| + 'name': name, |
| + 'total_read': totalRead, |
| + 'total_written': totalWritten, |
| + 'read_count': readCount, |
| + 'write_count': writeCount, |
| + 'last_read': lastRead, |
| + 'last_write': lastWrite |
| + }; |
| + |
| + String toJSON() { |
| + return JSON.encode(fullValueMap); |
| + } |
| +} |
| + |
| +class _SocketResourceInfo extends _ReadWriteResourceInfo{ |
|
Søren Gjesse
2015/09/02 09:07:49
nit: missing space before {.
ricow1
2015/09/02 10:45:42
Done.
|
| + final _NativeSocket socket; |
| + |
| + static Map<int, _SocketResourceInfo> openSockets = |
| + new Map<int, _SocketResourceInfo>(); |
| + |
| + _SocketResourceInfo(this.socket) : super('_socket') { |
| + SocketOpened(this); |
| + } |
| + |
| + String get name { |
|
Søren Gjesse
2015/09/02 09:07:49
Cache the String generated here?
ricow1
2015/09/02 10:45:42
Same answer as above, happy to add caching though
Søren Gjesse
2015/09/02 12:24:50
ditto answer.
|
| + if (socket.isListening) { |
| + return 'listening:${socket.address.host}:${socket.port}'; |
| + } |
| + var remote = ''; |
| + try { |
| + var remoteHost = socket.remoteAddress.host; |
| + var remotePort = socket.remotePort; |
| + remote = ' -> $remoteHost:$remotePort'; |
| + } catch (e) { } // ignored if we can't get the information |
| + return '${socket.address.host}:${socket.port}$remote'; |
| + } |
| + |
| + static Iterable<Map<String, String>> getOpenSocketsList() { |
| + return new List.from(openSockets.values.map((e) => e.referenceValueMap)); |
| + } |
| + |
| + Map<String, String> getSocketInfoMap() { |
| + var result = fullValueMap; |
| + result['socket_type'] = socket.isTcp ? 'TCP' : 'UDP'; |
|
Søren Gjesse
2015/09/02 09:07:49
Please make constants for these string keys.
ricow1
2015/09/02 10:45:42
Done.
|
| + result['listening'] = socket.isListening; |
| + result['host'] = socket.address.host; |
| + result['port'] = socket.port; |
| + if (!socket.isListening) { |
| + try { |
| + result['remote_host'] = socket.remoteAddress.host; |
| + result['remote_port'] = socket.remotePort; |
| + } catch (e) { |
| + // UDP. |
| + result['remote_port'] = 'NA'; |
| + result['remote_host'] = 'NA'; |
| + } |
| + } else { |
| + result['remote_port'] = 'NA'; |
| + result['remote_host'] = 'NA'; |
| + } |
| + result['address_type'] = socket.address.type.name; |
| + return result; |
| + } |
| + |
| + static Future<ServiceExtensionResponse> getSocketInfoMapByID( |
| + String function, Map<String, String> params) { |
| + assert(params.containsKey('id')); |
| + var id = int.parse(params['id']); |
| + var result = |
| + openSockets.containsKey(id) ? openSockets[id].getSocketInfoMap() : {}; |
| + var json = JSON.encode(result); |
| + return new Future.value(new ServiceExtensionResponse.result(json)); |
| + } |
| + |
| + static Future<ServiceExtensionResponse> getOpenSockets(function, params) { |
| + assert(function == '__getOpenSockets'); |
| + var data = {'type': '_opensockets', 'data': getOpenSocketsList()}; |
| + var json = JSON.encode(data); |
| + return new Future.value(new ServiceExtensionResponse.result(json)); |
| + } |
| + |
| + static Future<ServiceExtensionResponse> getSocketInfo(function, params) { |
| + assert(function == '__getSocketInfo'); |
| + assert(params.containsKey('id')); |
| + var result = openSockets.containsKey(params['id']) != null |
| + ? openSockets[params['id']].getSocketInfoMap() : {}; |
| + return |
| + Future.value(new ServiceExtensionResponse.result(JSON.encode(result))); |
| + } |
| + |
| + static SocketOpened(_SocketResourceInfo info) { |
| + assert(!openSockets.containsKey(info.id)); |
| + openSockets[info.id] = info; |
| + } |
| + |
| + static SocketClosed(_SocketResourceInfo info) { |
| + assert(openSockets.containsKey(info.id)); |
| + openSockets.remove(info.id); |
| + } |
| + |
| +} |