| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library protocol; | 5 library protocol; |
| 6 | 6 |
| 7 import 'dart:convert' show JsonDecoder; | 7 import 'dart:convert' show JsonDecoder; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Instances of the class [Request] represent a request that was received. | 10 * Instances of the class [Request] represent a request that was received. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 * pairs. | 68 * pairs. |
| 69 */ | 69 */ |
| 70 factory Request.fromString(String data) { | 70 factory Request.fromString(String data) { |
| 71 try { | 71 try { |
| 72 var result = DECODER.convert(data); | 72 var result = DECODER.convert(data); |
| 73 if (result is! Map) { | 73 if (result is! Map) { |
| 74 return null; | 74 return null; |
| 75 } | 75 } |
| 76 String id = result[Request.ID]; | 76 String id = result[Request.ID]; |
| 77 String method = result[Request.METHOD]; | 77 String method = result[Request.METHOD]; |
| 78 Map<String, Object> params = result[Request.PARAMS]; | 78 var params = result[Request.PARAMS]; |
| 79 Request request = new Request(id, method); | 79 Request request = new Request(id, method); |
| 80 if (params != null) { | 80 if (params is Map) { |
| 81 params.forEach((String key, Object value) { | 81 params.forEach((String key, Object value) { |
| 82 request.setParameter(key, value); | 82 request.setParameter(key, value); |
| 83 }); | 83 }); |
| 84 } | 84 } |
| 85 return request; | 85 return request; |
| 86 } catch (exception) { | 86 } catch (exception) { |
| 87 return null; | 87 return null; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 }); | 146 }); |
| 147 } | 147 } |
| 148 throw new RequestFailure(new Response.expectedInteger(this, value)); | 148 throw new RequestFailure(new Response.expectedInteger(this, value)); |
| 149 } | 149 } |
| 150 | 150 |
| 151 /** | 151 /** |
| 152 * Return a table representing the structure of the Json object that will be | 152 * Return a table representing the structure of the Json object that will be |
| 153 * sent to the client to represent this response. | 153 * sent to the client to represent this response. |
| 154 */ | 154 */ |
| 155 Map<String, Object> toJson() { | 155 Map<String, Object> toJson() { |
| 156 Map jsonObject = new Map(); | 156 Map<String, Object> jsonObject = new Map<String, Object>(); |
| 157 jsonObject[ID] = id; | 157 jsonObject[ID] = id; |
| 158 jsonObject[METHOD] = method; | 158 jsonObject[METHOD] = method; |
| 159 params.forEach((String key, Object value) { | 159 if (params.isNotEmpty) { |
| 160 jsonObject[key] = value; | 160 jsonObject[PARAMS] = params; |
| 161 }); | 161 } |
| 162 return jsonObject; | 162 return jsonObject; |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 /** | 166 /** |
| 167 * Instances of the class [Response] represent a response to a request. | 167 * Instances of the class [Response] represent a response to a request. |
| 168 */ | 168 */ |
| 169 class Response { | 169 class Response { |
| 170 /** | 170 /** |
| 171 * The name of the JSON attribute containing the id of the request for which | 171 * The name of the JSON attribute containing the id of the request for which |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 : this(request.id, new RequestError(-6, 'Unknown analysis option: "$optionNa
me"')); | 254 : this(request.id, new RequestError(-6, 'Unknown analysis option: "$optionNa
me"')); |
| 255 | 255 |
| 256 /** | 256 /** |
| 257 * Initialize a newly created instance to represent an error condition caused | 257 * Initialize a newly created instance to represent an error condition caused |
| 258 * by a [request] that cannot be handled by any known handlers. | 258 * by a [request] that cannot be handled by any known handlers. |
| 259 */ | 259 */ |
| 260 Response.unknownRequest(Request request) | 260 Response.unknownRequest(Request request) |
| 261 : this(request.id, new RequestError(-7, 'Unknown request')); | 261 : this(request.id, new RequestError(-7, 'Unknown request')); |
| 262 | 262 |
| 263 /** | 263 /** |
| 264 * Initialize a newly created instance based upon the given JSON data |
| 265 */ |
| 266 factory Response.fromJson(Map<String, Object> json) { |
| 267 try { |
| 268 // TODO process result |
| 269 String id = json[Response.ID]; |
| 270 var error = json[Response.ERROR]; |
| 271 var result = json[Response.RESULT]; |
| 272 Response response; |
| 273 if (error is Map) { |
| 274 response = new Response(id, new RequestError.fromJson(error)); |
| 275 } else { |
| 276 response = new Response(id); |
| 277 } |
| 278 if (result is Map) { |
| 279 result.forEach((String key, Object value) { |
| 280 response.setResult(key, value); |
| 281 }); |
| 282 } |
| 283 return response; |
| 284 } catch (exception) { |
| 285 return null; |
| 286 } |
| 287 } |
| 288 |
| 289 /** |
| 264 * Return the value of the result field with the given [name]. | 290 * Return the value of the result field with the given [name]. |
| 265 */ | 291 */ |
| 266 Object getResult(String name) { | 292 Object getResult(String name) { |
| 267 return result[name]; | 293 return result[name]; |
| 268 } | 294 } |
| 269 | 295 |
| 270 /** | 296 /** |
| 271 * Set the value of the result field with the given [name] to the given [value
]. | 297 * Set the value of the result field with the given [name] to the given [value
]. |
| 272 */ | 298 */ |
| 273 void setResult(String name, Object value) { | 299 void setResult(String name, Object value) { |
| 274 result[name] = value; | 300 result[name] = value; |
| 275 } | 301 } |
| 276 | 302 |
| 277 /** | 303 /** |
| 278 * Return a table representing the structure of the Json object that will be | 304 * Return a table representing the structure of the Json object that will be |
| 279 * sent to the client to represent this response. | 305 * sent to the client to represent this response. |
| 280 */ | 306 */ |
| 281 Map<String, Object> toJson() { | 307 Map<String, Object> toJson() { |
| 282 Map jsonObject = new Map(); | 308 Map<String, Object> jsonObject = new Map<String, Object>(); |
| 283 jsonObject[ID] = id; | 309 jsonObject[ID] = id; |
| 284 if (error == null) { | 310 if (error == null) { |
| 285 jsonObject[ERROR] = null; | 311 jsonObject[ERROR] = null; |
| 286 } else { | 312 } else { |
| 287 jsonObject[ERROR] = error.toJson(); | 313 jsonObject[ERROR] = error.toJson(); |
| 288 } | 314 } |
| 289 if (!result.isEmpty) { | 315 if (!result.isEmpty) { |
| 290 jsonObject[RESULT] = result; | 316 jsonObject[RESULT] = result; |
| 291 } | 317 } |
| 292 return jsonObject; | 318 return jsonObject; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 * parameters. | 418 * parameters. |
| 393 */ | 419 */ |
| 394 RequestError.invalidParameters() : this(CODE_INVALID_PARAMS, "Invalid paramete
rs"); | 420 RequestError.invalidParameters() : this(CODE_INVALID_PARAMS, "Invalid paramete
rs"); |
| 395 | 421 |
| 396 /** | 422 /** |
| 397 * Initialize a newly created [Error] to indicate an internal error. | 423 * Initialize a newly created [Error] to indicate an internal error. |
| 398 */ | 424 */ |
| 399 RequestError.internalError() : this(CODE_INTERNAL_ERROR, "Internal error"); | 425 RequestError.internalError() : this(CODE_INTERNAL_ERROR, "Internal error"); |
| 400 | 426 |
| 401 /** | 427 /** |
| 428 * Initialize a newly created [Error] from the given JSON. |
| 429 */ |
| 430 factory RequestError.fromJson(Map<String, Object> json) { |
| 431 try { |
| 432 int code = json[RequestError.CODE]; |
| 433 String message = json[RequestError.MESSAGE]; |
| 434 Map<String, Object> data = json[RequestError.DATA]; |
| 435 RequestError requestError = new RequestError(code, message); |
| 436 if (data != null) { |
| 437 data.forEach((String key, Object value) { |
| 438 requestError.setData(key, value); |
| 439 }); |
| 440 } |
| 441 return requestError; |
| 442 } catch (exception) { |
| 443 return null; |
| 444 } |
| 445 } |
| 446 |
| 447 /** |
| 402 * Return the value of the data with the given [name], or `null` if there is | 448 * Return the value of the data with the given [name], or `null` if there is |
| 403 * no such data associated with this error. | 449 * no such data associated with this error. |
| 404 */ | 450 */ |
| 405 Object getData(String name) => data[name]; | 451 Object getData(String name) => data[name]; |
| 406 | 452 |
| 407 /** | 453 /** |
| 408 * Set the value of the data with the given [name] to the given [value]. | 454 * Set the value of the data with the given [name] to the given [value]. |
| 409 */ | 455 */ |
| 410 void setData(String name, Object value) { | 456 void setData(String name, Object value) { |
| 411 data[name] = value; | 457 data[name] = value; |
| 412 } | 458 } |
| 413 | 459 |
| 414 /** | 460 /** |
| 415 * Return a table representing the structure of the Json object that will be | 461 * Return a table representing the structure of the Json object that will be |
| 416 * sent to the client to represent this response. | 462 * sent to the client to represent this response. |
| 417 */ | 463 */ |
| 418 Map<String, Object> toJson() { | 464 Map<String, Object> toJson() { |
| 419 Map jsonObject = new Map(); | 465 Map<String, Object> jsonObject = new Map<String, Object>(); |
| 420 jsonObject[CODE] = code; | 466 jsonObject[CODE] = code; |
| 421 jsonObject[MESSAGE] = message; | 467 jsonObject[MESSAGE] = message; |
| 422 if (!data.isEmpty) { | 468 if (!data.isEmpty) { |
| 423 jsonObject[DATA] = data; | 469 jsonObject[DATA] = data; |
| 424 } | 470 } |
| 425 return jsonObject; | 471 return jsonObject; |
| 426 } | 472 } |
| 427 } | 473 } |
| 428 | 474 |
| 429 /** | 475 /** |
| (...skipping 21 matching lines...) Expand all Loading... |
| 451 * A table mapping the names of notification parameters to their values. | 497 * A table mapping the names of notification parameters to their values. |
| 452 */ | 498 */ |
| 453 final Map<String, Object> params = new Map<String, Object>(); | 499 final Map<String, Object> params = new Map<String, Object>(); |
| 454 | 500 |
| 455 /** | 501 /** |
| 456 * Initialize a newly created [Notification] to have the given [event] name. | 502 * Initialize a newly created [Notification] to have the given [event] name. |
| 457 */ | 503 */ |
| 458 Notification(this.event); | 504 Notification(this.event); |
| 459 | 505 |
| 460 /** | 506 /** |
| 507 * Initialize a newly created instance based upon the given JSON data |
| 508 */ |
| 509 factory Notification.fromJson(Map<String, Object> json) { |
| 510 try { |
| 511 String event = json[Notification.EVENT]; |
| 512 var params = json[Notification.PARAMS]; |
| 513 Notification notification = new Notification(event); |
| 514 if (params is Map) { |
| 515 params.forEach((String key, Object value) { |
| 516 notification.setParameter(key, value); |
| 517 }); |
| 518 } |
| 519 return notification; |
| 520 } catch (exception) { |
| 521 return null; |
| 522 } |
| 523 } |
| 524 |
| 525 /** |
| 461 * Return the value of the parameter with the given [name], or `null` if there | 526 * Return the value of the parameter with the given [name], or `null` if there |
| 462 * is no such parameter associated with this notification. | 527 * is no such parameter associated with this notification. |
| 463 */ | 528 */ |
| 464 Object getParameter(String name) => params[name]; | 529 Object getParameter(String name) => params[name]; |
| 465 | 530 |
| 466 /** | 531 /** |
| 467 * Set the value of the parameter with the given [name] to the given [value]. | 532 * Set the value of the parameter with the given [name] to the given [value]. |
| 468 */ | 533 */ |
| 469 void setParameter(String name, Object value) { | 534 void setParameter(String name, Object value) { |
| 470 params[name] = value; | 535 params[name] = value; |
| 471 } | 536 } |
| 472 | 537 |
| 473 /** | 538 /** |
| 474 * Return a table representing the structure of the Json object that will be | 539 * Return a table representing the structure of the Json object that will be |
| 475 * sent to the client to represent this response. | 540 * sent to the client to represent this response. |
| 476 */ | 541 */ |
| 477 Map<String, Object> toJson() { | 542 Map<String, Object> toJson() { |
| 478 Map jsonObject = new Map(); | 543 Map<String, Object> jsonObject = new Map<String, Object>(); |
| 479 jsonObject[EVENT] = event; | 544 jsonObject[EVENT] = event; |
| 480 if (!params.isEmpty) { | 545 if (!params.isEmpty) { |
| 481 jsonObject[PARAMS] = params; | 546 jsonObject[PARAMS] = params; |
| 482 } | 547 } |
| 483 return jsonObject; | 548 return jsonObject; |
| 484 } | 549 } |
| 485 } | 550 } |
| 486 | 551 |
| 487 /** | 552 /** |
| 488 * Instances of the class [RequestHandler] implement a handler that can handle | 553 * Instances of the class [RequestHandler] implement a handler that can handle |
| (...skipping 18 matching lines...) Expand all Loading... |
| 507 /** | 572 /** |
| 508 * The response to be returned as a result of the failure. | 573 * The response to be returned as a result of the failure. |
| 509 */ | 574 */ |
| 510 final Response response; | 575 final Response response; |
| 511 | 576 |
| 512 /** | 577 /** |
| 513 * Initialize a newly created exception to return the given reponse. | 578 * Initialize a newly created exception to return the given reponse. |
| 514 */ | 579 */ |
| 515 RequestFailure(this.response); | 580 RequestFailure(this.response); |
| 516 } | 581 } |
| OLD | NEW |