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 channel.web_socket; | 5 library channel.web_socket; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 * [ServerCommunicationChannel] that uses a [WebSocket] to communicate with | 66 * [ServerCommunicationChannel] that uses a [WebSocket] to communicate with |
67 * clients. | 67 * clients. |
68 */ | 68 */ |
69 class WebSocketServerChannel implements ServerCommunicationChannel { | 69 class WebSocketServerChannel implements ServerCommunicationChannel { |
70 /** | 70 /** |
71 * The socket being wrapped. | 71 * The socket being wrapped. |
72 */ | 72 */ |
73 final WebSocket socket; | 73 final WebSocket socket; |
74 | 74 |
75 /** | 75 /** |
76 * The instrumentation server that is to be used by this analysis server. | 76 * The instrumentation service that is to be used by this analysis server. |
77 */ | 77 */ |
78 final InstrumentationServer instrumentationServer; | 78 final InstrumentationService instrumentationService; |
79 | 79 |
80 /** | 80 /** |
81 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. | 81 * Initialize a newly create [WebSocket] wrapper to wrap the given [socket]. |
82 */ | 82 */ |
83 WebSocketServerChannel(this.socket, this.instrumentationServer); | 83 WebSocketServerChannel(this.socket, this.instrumentationService); |
84 | 84 |
85 @override | 85 @override |
86 void close() { | 86 void close() { |
87 socket.close(WebSocketStatus.NORMAL_CLOSURE); | 87 socket.close(WebSocketStatus.NORMAL_CLOSURE); |
88 } | 88 } |
89 | 89 |
90 @override | 90 @override |
91 void listen(void onRequest(Request request), {void onError(), void | 91 void listen(void onRequest(Request request), {void onError(), void |
92 onDone()}) { | 92 onDone()}) { |
93 socket.listen( | 93 socket.listen( |
94 (data) => readRequest(data, onRequest), | 94 (data) => readRequest(data, onRequest), |
95 onError: onError, | 95 onError: onError, |
96 onDone: onDone); | 96 onDone: onDone); |
97 } | 97 } |
98 | 98 |
99 /** | 99 /** |
100 * Read a request from the given [data] and use the given function to handle | 100 * Read a request from the given [data] and use the given function to handle |
101 * the request. | 101 * the request. |
102 */ | 102 */ |
103 void readRequest(Object data, void onRequest(Request request)) { | 103 void readRequest(Object data, void onRequest(Request request)) { |
104 if (data is String) { | 104 if (data is String) { |
105 instrumentationServer.log(data); | 105 instrumentationService.logRequest(data); |
106 // Parse the string as a JSON descriptor and process the resulting | 106 // Parse the string as a JSON descriptor and process the resulting |
107 // structure as a request. | 107 // structure as a request. |
108 ServerCommunicationChannel.FromJson.start(); | 108 ServerCommunicationChannel.FromJson.start(); |
109 Request request = new Request.fromString(data); | 109 Request request = new Request.fromString(data); |
110 ServerCommunicationChannel.FromJson.stop(); | 110 ServerCommunicationChannel.FromJson.stop(); |
111 if (request == null) { | 111 if (request == null) { |
112 sendResponse(new Response.invalidRequestFormat()); | 112 sendResponse(new Response.invalidRequestFormat()); |
113 return; | 113 return; |
114 } | 114 } |
115 onRequest(request); | 115 onRequest(request); |
116 } else if (data is List<int>) { | 116 } else if (data is List<int>) { |
117 // TODO(brianwilkerson) Implement a more efficient protocol. | 117 // TODO(brianwilkerson) Implement a more efficient protocol. |
118 sendResponse(new Response.invalidRequestFormat()); | 118 sendResponse(new Response.invalidRequestFormat()); |
119 } else { | 119 } else { |
120 sendResponse(new Response.invalidRequestFormat()); | 120 sendResponse(new Response.invalidRequestFormat()); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 @override | 124 @override |
125 void sendNotification(Notification notification) { | 125 void sendNotification(Notification notification) { |
126 ServerCommunicationChannel.ToJson.start(); | 126 ServerCommunicationChannel.ToJson.start(); |
127 String jsonEncoding = JSON.encode(notification.toJson()); | 127 String jsonEncoding = JSON.encode(notification.toJson()); |
128 ServerCommunicationChannel.ToJson.stop(); | 128 ServerCommunicationChannel.ToJson.stop(); |
129 socket.add(jsonEncoding); | 129 socket.add(jsonEncoding); |
130 instrumentationServer.log(jsonEncoding); | 130 instrumentationService.logNotification(jsonEncoding); |
131 } | 131 } |
132 | 132 |
133 @override | 133 @override |
134 void sendResponse(Response response) { | 134 void sendResponse(Response response) { |
135 ServerCommunicationChannel.ToJson.start(); | 135 ServerCommunicationChannel.ToJson.start(); |
136 String jsonEncoding = JSON.encode(response.toJson()); | 136 String jsonEncoding = JSON.encode(response.toJson()); |
137 ServerCommunicationChannel.ToJson.stop(); | 137 ServerCommunicationChannel.ToJson.stop(); |
138 socket.add(jsonEncoding); | 138 socket.add(jsonEncoding); |
139 instrumentationServer.log(jsonEncoding); | 139 instrumentationService.logResponse(jsonEncoding); |
140 } | 140 } |
141 } | 141 } |
OLD | NEW |