| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.io; | 5 part of dart.io; |
| 6 | 6 |
| 7 abstract class _IOResourceInfo { | 7 abstract class _IOResourceInfo { |
| 8 final String type; | 8 final String type; |
| 9 final int id; | 9 final int id; |
| 10 String get name; | 10 String get name; |
| 11 static int _count = 0; | 11 static int _count = 0; |
| 12 | 12 |
| 13 static final Stopwatch _sw = new Stopwatch()..start(); | 13 static final Stopwatch _sw = new Stopwatch()..start(); |
| 14 static final _startTime = new DateTime.now().millisecondsSinceEpoch; | 14 static final _startTime = new DateTime.now().millisecondsSinceEpoch; |
| 15 | 15 |
| 16 static double get timestamp => _startTime + _sw.elapsedMicroseconds/1000; | 16 static double get timestamp => _startTime + _sw.elapsedMicroseconds/1000; |
| 17 | 17 |
| 18 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); | 18 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); |
| 19 | 19 |
| 20 /// Get the full set of values for a specific implementation. This is normally | 20 /// Get the full set of values for a specific implementation. This is normally |
| 21 /// looked up based on an id from a referenceValueMap. | 21 /// looked up based on an id from a referenceValueMap. |
| 22 Map<String, dynamic> get fullValueMap; | 22 Map<String, String> get fullValueMap; |
| 23 | 23 |
| 24 /// The reference map, used to return a list of values, e.g., getting | 24 /// The reference map, used to return a list of values, e.g., getting |
| 25 /// all open sockets. The structure of this is shared among all subclasses. | 25 /// all open sockets. The structure of this is shared among all subclasses. |
| 26 Map<String, dynamic> get referenceValueMap => | 26 Map<String, String> get referenceValueMap => |
| 27 { | 27 { |
| 28 // The type for a reference object is prefixed with @ in observatory. | 28 // The type for a reference object is prefixed with @ in observatory. |
| 29 'type': '@$type', | 29 'type': '@$type', |
| 30 'id': id, | 30 'id': id, |
| 31 'name': name, | 31 'name': name, |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 static int getNextID() => _count++; | 34 static int getNextID() => _count++; |
| 35 } | 35 } |
| 36 | 36 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 64 | 64 |
| 65 _ReadWriteResourceInfo(String type) : | 65 _ReadWriteResourceInfo(String type) : |
| 66 totalRead = 0, | 66 totalRead = 0, |
| 67 totalWritten = 0, | 67 totalWritten = 0, |
| 68 readCount = 0, | 68 readCount = 0, |
| 69 writeCount = 0, | 69 writeCount = 0, |
| 70 lastRead = 0.0, | 70 lastRead = 0.0, |
| 71 lastWrite = 0.0, | 71 lastWrite = 0.0, |
| 72 super(type); | 72 super(type); |
| 73 | 73 |
| 74 Map<String, dynamic> get fullValueMap => | 74 Map<String, String> get fullValueMap => |
| 75 { | 75 { |
| 76 'type': type, | 76 'type': type, |
| 77 'id': id, | 77 'id': id, |
| 78 'name': name, | 78 'name': name, |
| 79 'totalRead': totalRead, | 79 'totalRead': totalRead, |
| 80 'totalWritten': totalWritten, | 80 'totalWritten': totalWritten, |
| 81 'readCount': readCount, | 81 'readCount': readCount, |
| 82 'writeCount': writeCount, | 82 'writeCount': writeCount, |
| 83 'lastRead': lastRead, | 83 'lastRead': lastRead, |
| 84 'lastWrite': lastWrite | 84 'lastWrite': lastWrite |
| (...skipping 26 matching lines...) Expand all Loading... |
| 111 return new List.from(openFiles.values.map((e) => e.referenceValueMap)); | 111 return new List.from(openFiles.values.map((e) => e.referenceValueMap)); |
| 112 } | 112 } |
| 113 | 113 |
| 114 static Future<ServiceExtensionResponse> getOpenFiles(function, params) { | 114 static Future<ServiceExtensionResponse> getOpenFiles(function, params) { |
| 115 assert(function == 'ext.dart.io.getOpenFiles'); | 115 assert(function == 'ext.dart.io.getOpenFiles'); |
| 116 var data = {'type': '_openfiles', 'data': getOpenFilesList()}; | 116 var data = {'type': '_openfiles', 'data': getOpenFilesList()}; |
| 117 var json = JSON.encode(data); | 117 var json = JSON.encode(data); |
| 118 return new Future.value(new ServiceExtensionResponse.result(json)); | 118 return new Future.value(new ServiceExtensionResponse.result(json)); |
| 119 } | 119 } |
| 120 | 120 |
| 121 Map<String, dynamic> getFileInfoMap() { | 121 Map<String, String> getFileInfoMap() { |
| 122 return fullValueMap; | 122 var result = fullValueMap; |
| 123 return result; |
| 123 } | 124 } |
| 124 | 125 |
| 125 static Future<ServiceExtensionResponse> getFileInfoMapByID(function, params) { | 126 static Future<ServiceExtensionResponse> getFileInfoMapByID(function, params) { |
| 126 assert(params.containsKey('id')); | 127 assert(params.containsKey('id')); |
| 127 var id = int.parse(params['id']); | 128 var id = int.parse(params['id']); |
| 128 var result = | 129 var result = |
| 129 openFiles.containsKey(id) ? openFiles[id].getFileInfoMap() : {}; | 130 openFiles.containsKey(id) ? openFiles[id].getFileInfoMap() : {}; |
| 130 var json = JSON.encode(result); | 131 var json = JSON.encode(result); |
| 131 return new Future.value(new ServiceExtensionResponse.result(json)); | 132 return new Future.value(new ServiceExtensionResponse.result(json)); |
| 132 } | 133 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 147 _ProcessResourceInfo(this.process) : | 148 _ProcessResourceInfo(this.process) : |
| 148 startedAt = _IOResourceInfo.timestamp, | 149 startedAt = _IOResourceInfo.timestamp, |
| 149 super(TYPE) { | 150 super(TYPE) { |
| 150 ProcessStarted(this); | 151 ProcessStarted(this); |
| 151 } | 152 } |
| 152 | 153 |
| 153 String get name => process._path; | 154 String get name => process._path; |
| 154 | 155 |
| 155 void stopped() { ProcessStopped(this); } | 156 void stopped() { ProcessStopped(this); } |
| 156 | 157 |
| 157 Map<String, dynamic> get fullValueMap => | 158 Map<String, String> get fullValueMap => |
| 158 { | 159 { |
| 159 'type': type, | 160 'type': type, |
| 160 'id': id, | 161 'id': id, |
| 161 'name': name, | 162 'name': name, |
| 162 'pid': process.pid, | 163 'pid': process.pid, |
| 163 'startedAt': startedAt, | 164 'startedAt': startedAt, |
| 164 'arguments': process._arguments, | 165 'arguments': process._arguments, |
| 165 'workingDirectory': | 166 'workingDirectory': |
| 166 process._workingDirectory == null ? '.' : process._workingDirectory, | 167 process._workingDirectory == null ? '.' : process._workingDirectory, |
| 167 }; | 168 }; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 196 var json = JSON.encode(result); | 197 var json = JSON.encode(result); |
| 197 return new Future.value(new ServiceExtensionResponse.result(json)); | 198 return new Future.value(new ServiceExtensionResponse.result(json)); |
| 198 } | 199 } |
| 199 } | 200 } |
| 200 | 201 |
| 201 class _SocketResourceInfo extends _ReadWriteResourceInfo { | 202 class _SocketResourceInfo extends _ReadWriteResourceInfo { |
| 202 static const String TCP_STRING = 'TCP'; | 203 static const String TCP_STRING = 'TCP'; |
| 203 static const String UDP_STRING = 'UDP'; | 204 static const String UDP_STRING = 'UDP'; |
| 204 static const String TYPE = '_socket'; | 205 static const String TYPE = '_socket'; |
| 205 | 206 |
| 206 final /*_NativeSocket|*/ socket; | 207 final socket; |
| 207 | 208 |
| 208 static Map<int, _SocketResourceInfo> openSockets = | 209 static Map<int, _SocketResourceInfo> openSockets = |
| 209 new Map<int, _SocketResourceInfo>(); | 210 new Map<int, _SocketResourceInfo>(); |
| 210 | 211 |
| 211 _SocketResourceInfo(this.socket) : super(TYPE) { | 212 _SocketResourceInfo(this.socket) : super(TYPE) { |
| 212 SocketOpened(this); | 213 SocketOpened(this); |
| 213 } | 214 } |
| 214 | 215 |
| 215 String get name { | 216 String get name { |
| 216 if (socket.isListening) { | 217 if (socket.isListening) { |
| 217 return 'listening:${socket.address.host}:${socket.port}'; | 218 return 'listening:${socket.address.host}:${socket.port}'; |
| 218 } | 219 } |
| 219 var remote = ''; | 220 var remote = ''; |
| 220 try { | 221 try { |
| 221 var remoteHost = socket.remoteAddress.host; | 222 var remoteHost = socket.remoteAddress.host; |
| 222 var remotePort = socket.remotePort; | 223 var remotePort = socket.remotePort; |
| 223 remote = ' -> $remoteHost:$remotePort'; | 224 remote = ' -> $remoteHost:$remotePort'; |
| 224 } catch (e) { } // ignored if we can't get the information | 225 } catch (e) { } // ignored if we can't get the information |
| 225 return '${socket.address.host}:${socket.port}$remote'; | 226 return '${socket.address.host}:${socket.port}$remote'; |
| 226 } | 227 } |
| 227 | 228 |
| 228 static Iterable<Map<String, String>> getOpenSocketsList() { | 229 static Iterable<Map<String, String>> getOpenSocketsList() { |
| 229 return new List.from(openSockets.values.map((e) => e.referenceValueMap)); | 230 return new List.from(openSockets.values.map((e) => e.referenceValueMap)); |
| 230 } | 231 } |
| 231 | 232 |
| 232 Map<String, dynamic> getSocketInfoMap() { | 233 Map<String, String> getSocketInfoMap() { |
| 233 var result = fullValueMap; | 234 var result = fullValueMap; |
| 234 result['socketType'] = socket.isTcp ? TCP_STRING : UDP_STRING; | 235 result['socketType'] = socket.isTcp ? TCP_STRING : UDP_STRING; |
| 235 result['listening'] = socket.isListening; | 236 result['listening'] = socket.isListening; |
| 236 result['host'] = socket.address.host; | 237 result['host'] = socket.address.host; |
| 237 result['port'] = socket.port; | 238 result['port'] = socket.port; |
| 238 if (!socket.isListening) { | 239 if (!socket.isListening) { |
| 239 try { | 240 try { |
| 240 result['remoteHost'] = socket.remoteAddress.host; | 241 result['remoteHost'] = socket.remoteAddress.host; |
| 241 result['remotePort'] = socket.remotePort; | 242 result['remotePort'] = socket.remotePort; |
| 242 } catch (e) { | 243 } catch (e) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 271 | 272 |
| 272 static SocketOpened(_SocketResourceInfo info) { | 273 static SocketOpened(_SocketResourceInfo info) { |
| 273 assert(!openSockets.containsKey(info.id)); | 274 assert(!openSockets.containsKey(info.id)); |
| 274 openSockets[info.id] = info; | 275 openSockets[info.id] = info; |
| 275 } | 276 } |
| 276 | 277 |
| 277 static SocketClosed(_SocketResourceInfo info) { | 278 static SocketClosed(_SocketResourceInfo info) { |
| 278 assert(openSockets.containsKey(info.id)); | 279 assert(openSockets.containsKey(info.id)); |
| 279 openSockets.remove(info.id); | 280 openSockets.remove(info.id); |
| 280 } | 281 } |
| 282 |
| 281 } | 283 } |
| OLD | NEW |