| 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 part of dart.io; | |
| 6 | |
| 7 const String TCP_STRING = 'TCP'; | |
| 8 const String UDP_STRING = 'UDP'; | |
| 9 | |
| 10 | |
| 11 abstract class _IOResourceInfo { | |
| 12 final String type; | |
| 13 final int id; | |
| 14 String get name; | |
| 15 static int _count = 0; | |
| 16 | |
| 17 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); | |
| 18 | |
| 19 String toJSON(); | |
| 20 | |
| 21 /// Get the full set of values for a specific implementation. This is normally | |
| 22 /// looked up based on an id from a referenceValueMap. | |
| 23 Map<String, String> get fullValueMap; | |
| 24 | |
| 25 /// The reference map, used to return a list of values, e.g., getting | |
| 26 /// all open sockets. The structure of this is shared among all subclasses. | |
| 27 Map<String, String> get referenceValueMap => | |
| 28 { | |
| 29 // The type for a reference object is prefixed with @ in observatory. | |
| 30 'type': '@$type', | |
| 31 'id': id, | |
| 32 'name': name, | |
| 33 }; | |
| 34 | |
| 35 static int getNextID() => _count++; | |
| 36 } | |
| 37 | |
| 38 abstract class _ReadWriteResourceInfo extends _IOResourceInfo { | |
| 39 int totalRead; | |
| 40 int totalWritten; | |
| 41 int readCount; | |
| 42 int writeCount; | |
| 43 double lastRead; | |
| 44 double lastWrite; | |
| 45 | |
| 46 _ReadWriteResourceInfo(String type) : | |
| 47 totalRead = 0, | |
| 48 totalWritten = 0, | |
| 49 readCount = 0, | |
| 50 writeCount = 0, | |
| 51 lastRead = 0.0, | |
| 52 lastWrite = 0.0, | |
| 53 super(type); | |
| 54 | |
| 55 Map<String, String> get fullValueMap => | |
| 56 { | |
| 57 'type': type, | |
| 58 'id': id, | |
| 59 'name': name, | |
| 60 'total_read': totalRead, | |
| 61 'total_written': totalWritten, | |
| 62 'read_count': readCount, | |
| 63 'write_count': writeCount, | |
| 64 'last_read': lastRead, | |
| 65 'last_write': lastWrite | |
| 66 }; | |
| 67 | |
| 68 String toJSON() { | |
| 69 return JSON.encode(fullValueMap); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 class _SocketResourceInfo extends _ReadWriteResourceInfo { | |
| 74 final _NativeSocket socket; | |
| 75 | |
| 76 static Map<int, _SocketResourceInfo> openSockets = | |
| 77 new Map<int, _SocketResourceInfo>(); | |
| 78 | |
| 79 _SocketResourceInfo(this.socket) : super('_socket') { | |
| 80 SocketOpened(this); | |
| 81 } | |
| 82 | |
| 83 String get name { | |
| 84 if (socket.isListening) { | |
| 85 return 'listening:${socket.address.host}:${socket.port}'; | |
| 86 } | |
| 87 var remote = ''; | |
| 88 try { | |
| 89 var remoteHost = socket.remoteAddress.host; | |
| 90 var remotePort = socket.remotePort; | |
| 91 remote = ' -> $remoteHost:$remotePort'; | |
| 92 } catch (e) { } // ignored if we can't get the information | |
| 93 return '${socket.address.host}:${socket.port}$remote'; | |
| 94 } | |
| 95 | |
| 96 static Iterable<Map<String, String>> getOpenSocketsList() { | |
| 97 return new List.from(openSockets.values.map((e) => e.referenceValueMap)); | |
| 98 } | |
| 99 | |
| 100 Map<String, String> getSocketInfoMap() { | |
| 101 var result = fullValueMap; | |
| 102 result['socket_type'] = socket.isTcp ? TCP_STRING : UDP_STRING; | |
| 103 result['listening'] = socket.isListening; | |
| 104 result['host'] = socket.address.host; | |
| 105 result['port'] = socket.port; | |
| 106 if (!socket.isListening) { | |
| 107 try { | |
| 108 result['remote_host'] = socket.remoteAddress.host; | |
| 109 result['remote_port'] = socket.remotePort; | |
| 110 } catch (e) { | |
| 111 // UDP. | |
| 112 result['remote_port'] = 'NA'; | |
| 113 result['remote_host'] = 'NA'; | |
| 114 } | |
| 115 } else { | |
| 116 result['remote_port'] = 'NA'; | |
| 117 result['remote_host'] = 'NA'; | |
| 118 } | |
| 119 result['address_type'] = socket.address.type.name; | |
| 120 return result; | |
| 121 } | |
| 122 | |
| 123 static Future<ServiceExtensionResponse> getSocketInfoMapByID( | |
| 124 String function, Map<String, String> params) { | |
| 125 assert(params.containsKey('id')); | |
| 126 var id = int.parse(params['id']); | |
| 127 var result = | |
| 128 openSockets.containsKey(id) ? openSockets[id].getSocketInfoMap() : {}; | |
| 129 var json = JSON.encode(result); | |
| 130 return new Future.value(new ServiceExtensionResponse.result(json)); | |
| 131 } | |
| 132 | |
| 133 static Future<ServiceExtensionResponse> getOpenSockets(function, params) { | |
| 134 assert(function == '__getOpenSockets'); | |
| 135 var data = {'type': '_opensockets', 'data': getOpenSocketsList()}; | |
| 136 var json = JSON.encode(data); | |
| 137 return new Future.value(new ServiceExtensionResponse.result(json)); | |
| 138 } | |
| 139 | |
| 140 static Future<ServiceExtensionResponse> getSocketInfo(function, params) { | |
| 141 assert(function == '__getSocketInfo'); | |
| 142 assert(params.containsKey('id')); | |
| 143 var result = openSockets.containsKey(params['id']) != null | |
| 144 ? openSockets[params['id']].getSocketInfoMap() : {}; | |
| 145 return | |
| 146 Future.value(new ServiceExtensionResponse.result(JSON.encode(result))); | |
| 147 } | |
| 148 | |
| 149 static SocketOpened(_SocketResourceInfo info) { | |
| 150 assert(!openSockets.containsKey(info.id)); | |
| 151 openSockets[info.id] = info; | |
| 152 } | |
| 153 | |
| 154 static SocketClosed(_SocketResourceInfo info) { | |
| 155 assert(openSockets.containsKey(info.id)); | |
| 156 openSockets.remove(info.id); | |
| 157 } | |
| 158 | |
| 159 } | |
| OLD | NEW |