| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 | |
| 6 final Map _servicePathMap = { | |
| 7 'http' : { | |
| 8 'servers' : _httpServersServiceObject, | |
| 9 'serverconnections' : _httpServerConnectionsServiceObject, | |
| 10 }, | |
| 11 'sockets' : _socketsServiceObject, | |
| 12 'websockets' : _webSocketsServiceObject, | |
| 13 'file' : { | |
| 14 'randomaccessfiles' : _randomAccessFilesServiceObject | |
| 15 }, | |
| 16 'processes' : _processesServiceObject, | |
| 17 }; | |
| 18 | |
| 19 String _getServicePath(obj) => obj._servicePath; | |
| 20 | |
| 21 String _serviceObjectHandler(List<String> paths, | |
| 22 List<String> keys, | |
| 23 List<String> values) { | |
| 24 assert(keys.length == values.length); | |
| 25 if (paths.isEmpty) { | |
| 26 return JSON.encode(_ioServiceObject()); | |
| 27 } | |
| 28 int i = 0; | |
| 29 var current = _servicePathMap; | |
| 30 do { | |
| 31 current = current[paths[i]]; | |
| 32 i++; | |
| 33 } while (i < paths.length && current is Map); | |
| 34 if (current is! Function) { | |
| 35 return JSON.encode(_makeServiceError('Unrecognized path', paths, keys, | |
| 36 values)); | |
| 37 } | |
| 38 var query = new Map(); | |
| 39 for (int i = 0; i < keys.length; i++) { | |
| 40 query[keys[i]] = values[i]; | |
| 41 } | |
| 42 return JSON.encode(current(paths.sublist(i))); | |
| 43 } | |
| 44 | |
| 45 Map _makeServiceError(String message, | |
| 46 List<String> paths, | |
| 47 List<String> keys, | |
| 48 List<String> values, | |
| 49 [String kind]) { | |
| 50 var error = { | |
| 51 'type': 'Error', | |
| 52 'id': '', | |
| 53 'message': message, | |
| 54 'request': { | |
| 55 'arguments': paths, | |
| 56 'option_keys': keys, | |
| 57 'option_values': values, | |
| 58 } | |
| 59 }; | |
| 60 if (kind != null) { | |
| 61 error['kind'] = kind; | |
| 62 } | |
| 63 return error; | |
| 64 } | |
| 65 | |
| 66 Map _ioServiceObject() { | |
| 67 return { | |
| 68 'id': 'io', | |
| 69 'type': 'IO', | |
| 70 'name': 'io', | |
| 71 'user_name': 'io', | |
| 72 }; | |
| 73 } | |
| 74 | |
| 75 Map _httpServersServiceObject(args) { | |
| 76 if (args.length == 1) { | |
| 77 var server = _HttpServer._servers[int.parse(args.first)]; | |
| 78 if (server == null) { | |
| 79 return {}; | |
| 80 } | |
| 81 return server._toJSON(false); | |
| 82 } | |
| 83 return { | |
| 84 'id': 'io/http/servers', | |
| 85 'type': 'HttpServerList', | |
| 86 'members': _HttpServer._servers.values | |
| 87 .map((server) => server._toJSON(true)).toList(), | |
| 88 }; | |
| 89 } | |
| 90 | |
| 91 Map _httpServerConnectionsServiceObject(args) { | |
| 92 if (args.length == 1) { | |
| 93 var connection = _HttpConnection._connections[int.parse(args.first)]; | |
| 94 if (connection == null) { | |
| 95 return {}; | |
| 96 } | |
| 97 return connection._toJSON(false); | |
| 98 } | |
| 99 return _makeServiceError("http/serverconnections can not be listed"); | |
| 100 } | |
| 101 | |
| 102 Map _socketsServiceObject(args) { | |
| 103 if (args.length == 1) { | |
| 104 var socket = _NativeSocket._sockets[int.parse(args.first)]; | |
| 105 if (socket == null) { | |
| 106 return {}; | |
| 107 } | |
| 108 return socket._toJSON(false); | |
| 109 } | |
| 110 return { | |
| 111 'id': 'io/sockets', | |
| 112 'type': 'SocketList', | |
| 113 'members': _NativeSocket._sockets.values | |
| 114 .map((socket) => socket._toJSON(true)).toList(), | |
| 115 }; | |
| 116 } | |
| 117 | |
| 118 Map _webSocketsServiceObject(args) { | |
| 119 if (args.length == 1) { | |
| 120 var webSocket = _WebSocketImpl._webSockets[int.parse(args.first)]; | |
| 121 if (webSocket == null) { | |
| 122 return {}; | |
| 123 } | |
| 124 return webSocket._toJSON(false); | |
| 125 } | |
| 126 return { | |
| 127 'id': 'io/websockets', | |
| 128 'type': 'WebSocketList', | |
| 129 'members': _WebSocketImpl._webSockets.values | |
| 130 .map((webSocket) => webSocket._toJSON(true)).toList(), | |
| 131 }; | |
| 132 } | |
| 133 | |
| 134 Map _randomAccessFilesServiceObject(args) { | |
| 135 if (args.length == 1) { | |
| 136 var raf = _RandomAccessFile._files[int.parse(args.first)]; | |
| 137 if (raf == null) { | |
| 138 return {}; | |
| 139 } | |
| 140 return raf._toJSON(false); | |
| 141 } | |
| 142 return { | |
| 143 'id': 'io/file/randomaccessfiles', | |
| 144 'type': 'RandomAccessFileList', | |
| 145 'members': _RandomAccessFile._files.values | |
| 146 .map((raf) => raf._toJSON(true)).toList(), | |
| 147 }; | |
| 148 } | |
| 149 | |
| 150 Map _processesServiceObject(args) { | |
| 151 if (args.length == 1) { | |
| 152 var process = _ProcessImpl._processes[int.parse(args.first)]; | |
| 153 if (process == null) { | |
| 154 return {}; | |
| 155 } | |
| 156 return process._toJSON(false); | |
| 157 } | |
| 158 return { | |
| 159 'id': 'io/processes', | |
| 160 'type': 'ProcessList', | |
| 161 'members': _ProcessImpl._processes.values | |
| 162 .map((p) => p._toJSON(true)).toList(), | |
| 163 }; | |
| 164 } | |
| OLD | NEW |