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

Side by Side Diff: runtime/bin/vmservice/client/lib/service_html.dart

Issue 305603004: Observatory now uses websockets to communicate with the vm. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: gen js Created 6 years, 6 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 | Annotate | Revision Log
OLDNEW
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 service_html; 5 library service_html;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:html'; 9 import 'dart:html';
10 10
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 'response': error.target.responseText, 43 'response': error.target.responseText,
44 'kind': 'NetworkException', 44 'kind': 'NetworkException',
45 'message': 'Could not connect to service. Check that you started the' 45 'message': 'Could not connect to service. Check that you started the'
46 ' VM with the following flags:\n --enable-vm-service' 46 ' VM with the following flags:\n --enable-vm-service'
47 ' --pause-isolates-on-exit' 47 ' --pause-isolates-on-exit'
48 }); 48 });
49 }); 49 });
50 } 50 }
51 } 51 }
52 52
53 class WebSocketVM extends VM {
54 final Map<int, Completer> _pendingRequests =
55 new Map<int, Completer>();
56 int _requestSerial = 0;
57
58 String _host;
59 Future<WebSocket> _socketFuture;
60
61 bool runningInJavaScript() => identical(1.0, 1);
62
63 WebSocketVM() : super() {
64 if (runningInJavaScript()) {
65 // When we are running as JavaScript use the same hostname:port
66 // that the Observatory is loaded from.
67 _host = 'ws://${window.location.host}/ws';
68 } else {
69 // Otherwise, assume we are running from the Dart Editor and
70 // want to connect on the default port.
71 _host = 'ws://127.0.0.1:8181/ws';
72 }
73
74 var completer = new Completer<WebSocket>();
75 _socketFuture = completer.future;
76 var socket = new WebSocket(_host);
77 socket.onOpen.first.then((_) {
78 socket.onMessage.listen(_handleMessage);
79 socket.onClose.first.then((_) {
80 _socketFuture = null;
81 });
82 completer.complete(socket);
83 });
84 socket.onError.first.then((_) {
85 _socketFuture = null;
86 });
87 }
88
89 void _handleMessage(MessageEvent event) {
90 var map = JSON.decode(event.data);
91 int seq = map['seq'];
92 var response = map['response'];
93 var completer = _pendingRequests.remove(seq);
94 if (completer == null) {
95 Logger.root.severe('Received unexpected message: ${map}');
96 } else {
97 completer.complete(response);
98 }
99 }
100
101 Future<String> getString(String id) {
102 if (_socketFuture == null) {
103 var errorResponse = JSON.encode({
104 'type': 'ServiceException',
105 'id': '',
106 'response': '',
107 'kind': 'NetworkException',
108 'message': 'Could not connect to service. Check that you started t he'
109 ' VM with the following flags:\n --enable-vm-service'
110 ' --pause-isolates-on-exit'
111 });
112 return new Future.value(errorResponse);
113 }
114 return _socketFuture.then((socket) {
115 int seq = _requestSerial++;
116 if (!id.endsWith('/profile/tag')) {
117 Logger.root.info('Fetching $id from $_host');
118 }
119 var completer = new Completer<String>();
120 _pendingRequests[seq] = completer;
121 var message = JSON.encode({'seq': seq, 'request': id});
122 socket.send(message);
123 return completer.future;
124 });
125 }
126 }
127
53 class DartiumVM extends VM { 128 class DartiumVM extends VM {
54 final Map _outstandingRequests = new Map(); 129 final Map<String, Completer> _pendingRequests =
130 new Map<String, Completer>();
55 int _requestSerial = 0; 131 int _requestSerial = 0;
56 132
57 DartiumVM() : super() { 133 DartiumVM() : super() {
58 window.onMessage.listen(_messageHandler); 134 window.onMessage.listen(_messageHandler);
59 Logger.root.info('Connected to DartiumVM'); 135 Logger.root.info('Connected to DartiumVM');
60 } 136 }
61 137
62 void _messageHandler(msg) { 138 void _messageHandler(msg) {
63 var id = msg.data['id']; 139 var id = msg.data['id'];
64 var name = msg.data['name']; 140 var name = msg.data['name'];
65 var data = msg.data['data']; 141 var data = msg.data['data'];
66 if (name != 'observatoryData') { 142 if (name != 'observatoryData') {
67 return; 143 return;
68 } 144 }
69 var completer = _outstandingRequests[id]; 145 var completer = _pendingRequests[id];
70 assert(completer != null); 146 assert(completer != null);
71 _outstandingRequests.remove(id); 147 _pendingRequests.remove(id);
72 completer.complete(data); 148 completer.complete(data);
73 } 149 }
74 150
75 Future<String> getString(String path) { 151 Future<String> getString(String path) {
76 var idString = '$_requestSerial'; 152 var idString = '$_requestSerial';
77 Map message = {}; 153 Map message = {};
78 message['id'] = idString; 154 message['id'] = idString;
79 message['method'] = 'observatoryQuery'; 155 message['method'] = 'observatoryQuery';
80 message['query'] = '/$path'; 156 message['query'] = '/$path';
81 _requestSerial++; 157 _requestSerial++;
82 var completer = new Completer(); 158 var completer = new Completer();
83 _outstandingRequests[idString] = completer; 159 _pendingRequests[idString] = completer;
84 window.parent.postMessage(JSON.encode(message), '*'); 160 window.parent.postMessage(JSON.encode(message), '*');
85 return completer.future; 161 return completer.future;
86 } 162 }
87 } 163 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698