Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(555)

Side by Side Diff: sdk/lib/io/io_resource_info.dart

Issue 1326703004: Add resource metadata for open files. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: address comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« sdk/lib/io/file_impl.dart ('K') | « sdk/lib/io/file_impl.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 const String TCP_STRING = 'TCP';
8 const String UDP_STRING = 'UDP';
9
10
11 abstract class _IOResourceInfo { 7 abstract class _IOResourceInfo {
12 final String type; 8 final String type;
13 final int id; 9 final int id;
14 String get name; 10 String get name;
15 static int _count = 0; 11 static int _count = 0;
16 12
17 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID(); 13 _IOResourceInfo(this.type) : id = _IOResourceInfo.getNextID();
18 14
19 String toJSON(); 15 String toJSON();
20 16
21 /// Get the full set of values for a specific implementation. This is normally 17 /// Get the full set of values for a specific implementation. This is normally
22 /// looked up based on an id from a referenceValueMap. 18 /// looked up based on an id from a referenceValueMap.
23 Map<String, String> get fullValueMap; 19 Map<String, String> get fullValueMap;
24 20
25 /// The reference map, used to return a list of values, e.g., getting 21 /// 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. 22 /// all open sockets. The structure of this is shared among all subclasses.
27 Map<String, String> get referenceValueMap => 23 Map<String, String> get referenceValueMap =>
28 { 24 {
29 // The type for a reference object is prefixed with @ in observatory. 25 // The type for a reference object is prefixed with @ in observatory.
30 'type': '@$type', 26 'type': '@$type',
31 'id': id, 27 'id': id,
32 'name': name, 28 'name': name,
33 }; 29 };
34 30
35 static int getNextID() => _count++; 31 static int getNextID() => _count++;
36 } 32 }
37 33
34 // TODO(ricow): Move stopwatch into this class and use it for both files
Ivan Posva 2015/09/08 18:06:36 Can you please explain what you mean by moving sto
ricow1 2015/09/08 18:33:39 Correct. You can see the follow up here: https://c
35 // and sockets (by using setters on totalRead/totalWritten). Also, consider
36 // setting readCount and writeCount in those setters.
38 abstract class _ReadWriteResourceInfo extends _IOResourceInfo { 37 abstract class _ReadWriteResourceInfo extends _IOResourceInfo {
39 int totalRead; 38 int totalRead;
40 int totalWritten; 39 int totalWritten;
41 int readCount; 40 int readCount;
42 int writeCount; 41 int writeCount;
43 double lastRead; 42 double lastRead;
44 double lastWrite; 43 double lastWrite;
45 44
46 _ReadWriteResourceInfo(String type) : 45 _ReadWriteResourceInfo(String type) :
47 totalRead = 0, 46 totalRead = 0,
(...skipping 15 matching lines...) Expand all
63 'write_count': writeCount, 62 'write_count': writeCount,
64 'last_read': lastRead, 63 'last_read': lastRead,
65 'last_write': lastWrite 64 'last_write': lastWrite
66 }; 65 };
67 66
68 String toJSON() { 67 String toJSON() {
69 return JSON.encode(fullValueMap); 68 return JSON.encode(fullValueMap);
70 } 69 }
71 } 70 }
72 71
72 class _FileResourceInfo extends _ReadWriteResourceInfo {
73 static const String TYPE = '_file';
74
75 final file;
76
77 static Map<int, _FileResourceInfo> openFiles =
78 new Map<int, _FileResourceInfo>();
79
80 _FileResourceInfo(this.file) : super(TYPE) {
81 FileOpened(this);
82 }
83
84 static FileOpened(_FileResourceInfo info) {
85 assert(!openFiles.containsKey(info.id));
86 openFiles[info.id] = info;
87 }
88
89 static FileClosed(_FileResourceInfo info) {
90 assert(openFiles.containsKey(info.id));
91 openFiles.remove(info.id);
92 }
93
94 static Iterable<Map<String, String>> getOpenFilesList() {
95 return new List.from(openFiles.values.map((e) => e.referenceValueMap));
96 }
97
98 static Future<ServiceExtensionResponse> getOpenFiles(function, params) {
99 assert(function == '__getOpenFiles');
100 var data = {'type': '_openfiles', 'data': getOpenFilesList()};
101 var json = JSON.encode(data);
102 return new Future.value(new ServiceExtensionResponse.result(json));
103 }
104
105 Map<String, String> getFileInfoMap() {
106 var result = fullValueMap;
107 return result;
108 }
109
110 static Future<ServiceExtensionResponse> getFileInfoMapByID(function, params) {
111 assert(params.containsKey('id'));
112 var id = int.parse(params['id']);
113 var result =
114 openFiles.containsKey(id) ? openFiles[id].getFileInfoMap() : {};
115 var json = JSON.encode(result);
116 return new Future.value(new ServiceExtensionResponse.result(json));
117 }
118
119 String get name {
120 return '${file.path}';
121 }
122 }
123
73 class _SocketResourceInfo extends _ReadWriteResourceInfo { 124 class _SocketResourceInfo extends _ReadWriteResourceInfo {
125 static const String TCP_STRING = 'TCP';
126 static const String UDP_STRING = 'UDP';
127 static const String TYPE = '_socket';
128
74 final socket; 129 final socket;
75 130
76 static Map<int, _SocketResourceInfo> openSockets = 131 static Map<int, _SocketResourceInfo> openSockets =
77 new Map<int, _SocketResourceInfo>(); 132 new Map<int, _SocketResourceInfo>();
78 133
79 _SocketResourceInfo(this.socket) : super('_socket') { 134 _SocketResourceInfo(this.socket) : super(TYPE) {
80 SocketOpened(this); 135 SocketOpened(this);
81 } 136 }
82 137
83 String get name { 138 String get name {
84 if (socket.isListening) { 139 if (socket.isListening) {
85 return 'listening:${socket.address.host}:${socket.port}'; 140 return 'listening:${socket.address.host}:${socket.port}';
86 } 141 }
87 var remote = ''; 142 var remote = '';
88 try { 143 try {
89 var remoteHost = socket.remoteAddress.host; 144 var remoteHost = socket.remoteAddress.host;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 assert(!openSockets.containsKey(info.id)); 196 assert(!openSockets.containsKey(info.id));
142 openSockets[info.id] = info; 197 openSockets[info.id] = info;
143 } 198 }
144 199
145 static SocketClosed(_SocketResourceInfo info) { 200 static SocketClosed(_SocketResourceInfo info) {
146 assert(openSockets.containsKey(info.id)); 201 assert(openSockets.containsKey(info.id));
147 openSockets.remove(info.id); 202 openSockets.remove(info.id);
148 } 203 }
149 204
150 } 205 }
OLDNEW
« sdk/lib/io/file_impl.dart ('K') | « sdk/lib/io/file_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698