Chromium Code Reviews| 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 analysis.server; | 5 library analysis.server; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/analysis_logger.dart'; | 9 import 'package:analysis_server/src/analysis_logger.dart'; |
| 10 import 'package:analysis_server/src/channel.dart'; | 10 import 'package:analysis_server/src/channel.dart'; |
| 11 import 'package:analysis_server/src/protocol.dart'; | 11 import 'package:analysis_server/src/protocol.dart'; |
| 12 import 'package:analyzer/src/generated/engine.dart'; | 12 import 'package:analyzer/src/generated/engine.dart'; |
| 13 import 'package:analyzer/src/generated/error.dart'; | 13 import 'package:analyzer/src/generated/error.dart'; |
| 14 import 'package:analyzer/src/generated/java_core.dart'; | 14 import 'package:analyzer/src/generated/java_core.dart'; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Instances of the class [AnalysisServer] implement a server that listens on a | 17 * Instances of the class [AnalysisServer] implement a server that listens on a |
| 18 * [CommunicationChannel] for analysis requests and process them. | 18 * [CommunicationChannel] for analysis requests and process them. |
| 19 */ | 19 */ |
| 20 class AnalysisServer { | 20 class AnalysisServer { |
| 21 /** | 21 /** |
| 22 * The name of the notification of new errors associated with a source. | 22 * The name of the notification of new errors associated with a source. |
| 23 */ | 23 */ |
| 24 static const String ERROR_NOTIFICATION_NAME = 'context.errors'; | 24 static const String ERROR_NOTIFICATION_NAME = 'context.errors'; |
| 25 | 25 |
| 26 /** | 26 /** |
| 27 * The name of the notification sent when analysis is complete. | |
| 28 */ | |
| 29 static const String ANALYSIS_COMPLETE = "analysis.complete"; | |
| 30 | |
| 31 /** | |
| 27 * The name of the contextId parameter. | 32 * The name of the contextId parameter. |
| 28 */ | 33 */ |
| 29 static const String CONTEXT_ID_PARAM = 'contextId'; | 34 static const String CONTEXT_ID_PARAM = 'contextId'; |
| 30 | 35 |
| 31 /** | 36 /** |
| 32 * The name of the parameter whose value is a list of errors. | 37 * The name of the parameter whose value is a list of errors. |
| 33 */ | 38 */ |
| 34 static const String ERRORS_PARAM = 'errors'; | 39 static const String ERRORS_PARAM = 'errors'; |
| 35 | 40 |
| 36 /** | 41 /** |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 61 * server. | 66 * server. |
| 62 */ | 67 */ |
| 63 List<RequestHandler> handlers; | 68 List<RequestHandler> handlers; |
| 64 | 69 |
| 65 /** | 70 /** |
| 66 * A table mapping context id's to the analysis contexts associated with them. | 71 * A table mapping context id's to the analysis contexts associated with them. |
| 67 */ | 72 */ |
| 68 final Map<String, AnalysisContext> contextMap = new Map<String, AnalysisContex t>(); | 73 final Map<String, AnalysisContext> contextMap = new Map<String, AnalysisContex t>(); |
| 69 | 74 |
| 70 /** | 75 /** |
| 76 * A table mapping analysis contexts to the context id's associated with them. | |
| 77 */ | |
| 78 final Map<AnalysisContext, String> contextIdMap = new Map<AnalysisContext, Str ing>(); | |
| 79 | |
| 80 /** | |
| 71 * A list of the analysis contexts for which analysis work needs to be | 81 * A list of the analysis contexts for which analysis work needs to be |
| 72 * performed. | 82 * performed. |
| 73 * | 83 * |
| 74 * Invariant: when this list is non-empty, there is exactly one pending call | 84 * Invariant: when this list is non-empty, there is exactly one pending call |
| 75 * to [performTask] on the event queue. When this list is empty, there are | 85 * to [performTask] on the event queue. When this list is empty, there are |
| 76 * no calls to [performTask] on the event queue. | 86 * no calls to [performTask] on the event queue. |
| 77 */ | 87 */ |
| 78 final List<AnalysisContext> contextWorkQueue = new List<AnalysisContext>(); | 88 final List<AnalysisContext> contextWorkQueue = new List<AnalysisContext>(); |
| 79 | 89 |
| 80 /** | 90 /** |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 contextWorkQueue.clear(); | 165 contextWorkQueue.clear(); |
| 156 } | 166 } |
| 157 if (contextWorkQueue.isEmpty) { | 167 if (contextWorkQueue.isEmpty) { |
| 158 // Nothing to do. | 168 // Nothing to do. |
| 159 return; | 169 return; |
| 160 } | 170 } |
| 161 // | 171 // |
| 162 // Look for a context that has work to be done and then perform one task. | 172 // Look for a context that has work to be done and then perform one task. |
| 163 // | 173 // |
| 164 List<ChangeNotice> notices = null; | 174 List<ChangeNotice> notices = null; |
| 175 String contextId; | |
| 165 try { | 176 try { |
| 166 AnalysisContext context = contextWorkQueue[0]; | 177 AnalysisContext context = contextWorkQueue[0]; |
| 178 contextId = contextIdMap[context]; | |
| 167 AnalysisResult result = context.performAnalysisTask(); | 179 AnalysisResult result = context.performAnalysisTask(); |
| 168 notices = result.changeNotices; | 180 notices = result.changeNotices; |
| 169 } finally { | 181 } finally { |
| 170 if (notices == null) { | 182 if (notices == null) { |
| 171 // Either we have no more work to do for this context, or there was an | 183 // Either we have no more work to do for this context, or there was an |
| 172 // unhandled exception trying to perform the analysis. In either case, | 184 // unhandled exception trying to perform the analysis. In either case, |
| 173 // remove the context form the work queue so we won't try to do more | 185 // remove the context form the work queue so we won't try to do more |
| 174 // analysis on it. | 186 // analysis on it. |
| 175 contextWorkQueue.removeAt(0); | 187 AnalysisContext context = contextWorkQueue.removeAt(0); |
| 188 var notification = new Notification(ANALYSIS_COMPLETE); | |
|
Paul Berry
2014/05/20 15:34:58
I'm concerned that this is going to lead to bugs,
| |
| 189 notification.setParameter(CONTEXT_ID_PARAM, contextId); | |
| 190 sendNotification(notification); | |
| 176 } | 191 } |
| 177 // | 192 // |
| 178 // Schedule this method to be run again if there is any more work to be | 193 // Schedule this method to be run again if there is any more work to be |
| 179 // done. | 194 // done. |
| 180 // | 195 // |
| 181 if (!contextWorkQueue.isEmpty) { | 196 if (!contextWorkQueue.isEmpty) { |
| 182 _scheduleTask(); | 197 _scheduleTask(); |
| 183 } | 198 } |
| 184 } | 199 } |
| 185 if (notices != null) { | 200 if (notices != null) { |
| 186 sendNotices(notices); | 201 sendNotices(contextId, notices); |
| 187 } | 202 } |
| 188 } | 203 } |
| 189 | 204 |
| 190 /** | 205 /** |
| 191 * Send the information in the given list of notices back to the client. | 206 * Send the information in the given list of notices back to the client. |
| 192 */ | 207 */ |
| 193 void sendNotices(List<ChangeNotice> notices) { | 208 void sendNotices(String contextId, List<ChangeNotice> notices) { |
| 194 for (int i = 0; i < notices.length; i++) { | 209 for (int i = 0; i < notices.length; i++) { |
| 195 ChangeNotice notice = notices[i]; | 210 ChangeNotice notice = notices[i]; |
| 196 Notification notification = new Notification(ERROR_NOTIFICATION_NAME); | 211 Notification notification = new Notification(ERROR_NOTIFICATION_NAME); |
| 212 notification.setParameter(CONTEXT_ID_PARAM, contextId); | |
| 197 notification.setParameter(SOURCE_PARAM, notice.source.encoding); | 213 notification.setParameter(SOURCE_PARAM, notice.source.encoding); |
| 198 notification.setParameter(ERRORS_PARAM, notice.errors.map( | 214 notification.setParameter(ERRORS_PARAM, notice.errors.map( |
| 199 errorToJson).toList()); | 215 errorToJson).toList()); |
| 200 sendNotification(notification); | 216 sendNotification(notification); |
| 201 } | 217 } |
| 202 } | 218 } |
| 203 | 219 |
| 204 static Map<String, Object> errorToJson(AnalysisError analysisError) { | 220 static Map<String, Object> errorToJson(AnalysisError analysisError) { |
| 205 // TODO(paulberry): move this function into the AnalysisError class. | 221 // TODO(paulberry): move this function into the AnalysisError class. |
| 206 | 222 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 226 void sendNotification(Notification notification) { | 242 void sendNotification(Notification notification) { |
| 227 channel.sendNotification(notification); | 243 channel.sendNotification(notification); |
| 228 } | 244 } |
| 229 | 245 |
| 230 void _scheduleTask() { | 246 void _scheduleTask() { |
| 231 new Future(performTask).catchError((ex, st) { | 247 new Future(performTask).catchError((ex, st) { |
| 232 AnalysisEngine.instance.logger.logError("${ex}\n${st}"); | 248 AnalysisEngine.instance.logger.logError("${ex}\n${st}"); |
| 233 }); | 249 }); |
| 234 } | 250 } |
| 235 } | 251 } |
| OLD | NEW |