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