OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 namespace NativeClientVSAddIn |
| 6 { |
| 7 using System; |
| 8 |
| 9 using EnvDTE; |
| 10 using Microsoft.VisualStudio.VCProjectEngine; |
| 11 |
| 12 /// <summary> |
| 13 /// This class contains the functionality related to the web server which host
s the web page |
| 14 /// during debugging. |
| 15 /// </summary> |
| 16 public class WebServer : IDisposable |
| 17 { |
| 18 /// <summary> |
| 19 /// The web server port to default to if the user does not specify one. |
| 20 /// </summary> |
| 21 private const int DefaultWebServerPort = 5103; |
| 22 |
| 23 /// <summary> |
| 24 /// Holds the main web server process. |
| 25 /// </summary> |
| 26 private System.Diagnostics.Process webServer_; |
| 27 |
| 28 /// <summary> |
| 29 /// Captures output from the web server. |
| 30 /// </summary> |
| 31 private OutputWindowPane webServerOutputPane_; |
| 32 |
| 33 /// <summary> |
| 34 /// Keeps track of if dispose has been called. |
| 35 /// </summary> |
| 36 private bool disposed_ = false; |
| 37 |
| 38 /// <summary> |
| 39 /// Constructs the WebServer, starts the web server process. |
| 40 /// </summary> |
| 41 /// <param name="outputWindowPane">Existing output pane to send web server o
utput to.</param> |
| 42 /// <param name="properties">PropertyManager that is set to a valid project/
platform.</param> |
| 43 public WebServer(OutputWindowPane outputWindowPane, PropertyManager properti
es) |
| 44 { |
| 45 if (outputWindowPane == null) |
| 46 { |
| 47 throw new ArgumentNullException("outputWindowPane"); |
| 48 } |
| 49 |
| 50 if (properties == null) |
| 51 { |
| 52 throw new ArgumentNullException("properties"); |
| 53 } |
| 54 |
| 55 webServerOutputPane_ = outputWindowPane; |
| 56 |
| 57 // Read port from properties, if invalid port then set to default value. |
| 58 int webServerPort; |
| 59 if (!int.TryParse(properties.WebServerPort, out webServerPort)) |
| 60 { |
| 61 webServerPort = DefaultWebServerPort; |
| 62 } |
| 63 |
| 64 string webServerExecutable = "python.exe"; |
| 65 string webServerArguments = string.Format( |
| 66 "{0}\\examples\\httpd.py --no_dir_check {1}", properties.SDKRootDirect
ory, webServerPort); |
| 67 |
| 68 // Start the web server process. |
| 69 try |
| 70 { |
| 71 webServer_ = new System.Diagnostics.Process(); |
| 72 webServer_.StartInfo.CreateNoWindow = true; |
| 73 webServer_.StartInfo.UseShellExecute = false; |
| 74 webServer_.StartInfo.RedirectStandardOutput = true; |
| 75 webServer_.StartInfo.RedirectStandardError = true; |
| 76 webServer_.StartInfo.FileName = webServerExecutable; |
| 77 webServer_.StartInfo.Arguments = webServerArguments; |
| 78 webServer_.StartInfo.WorkingDirectory = properties.ProjectDirectory; |
| 79 webServer_.OutputDataReceived += WebServerMessageReceive; |
| 80 webServer_.ErrorDataReceived += WebServerMessageReceive; |
| 81 webServer_.Start(); |
| 82 webServer_.BeginOutputReadLine(); |
| 83 webServer_.BeginErrorReadLine(); |
| 84 } |
| 85 catch (Exception e) |
| 86 { |
| 87 webServerOutputPane_.OutputString(Strings.WebServerStartFail + "\n"); |
| 88 webServerOutputPane_.OutputString("Exception: " + e.Message + "\n"); |
| 89 webServerOutputPane_.Activate(); |
| 90 } |
| 91 |
| 92 webServerOutputPane_.Clear(); |
| 93 webServerOutputPane_.OutputString(Strings.WebServerStartMessage + "\n"); |
| 94 webServerOutputPane_.Activate(); |
| 95 } |
| 96 |
| 97 /// <summary> |
| 98 /// Finalizer. Should clean up unmanaged resources. Should not be overriden
in derived classes. |
| 99 /// </summary> |
| 100 ~WebServer() |
| 101 { |
| 102 Dispose(false); |
| 103 } |
| 104 |
| 105 /// <summary> |
| 106 /// Disposes the object when called by user code (not directly by garbage co
llector). |
| 107 /// </summary> |
| 108 public void Dispose() |
| 109 { |
| 110 Dispose(true); |
| 111 GC.SuppressFinalize(this); |
| 112 } |
| 113 |
| 114 /// <summary> |
| 115 /// Disposes the object. If disposing is false then this has been called by
garbage collection, |
| 116 /// and we shouldn't reference managed objects. |
| 117 /// </summary> |
| 118 /// <param name="disposing">True if user called Dispose, false if garbage co
llection.</param> |
| 119 protected virtual void Dispose(bool disposing) |
| 120 { |
| 121 if (!disposed_ && disposing) |
| 122 { |
| 123 // Managed resource clean up. |
| 124 Utility.EnsureProcessKill(ref webServer_); |
| 125 webServerOutputPane_.OutputString(Strings.WebServerStopMessage); |
| 126 webServerOutputPane_.Activate(); |
| 127 } |
| 128 |
| 129 disposed_ = true; |
| 130 } |
| 131 |
| 132 /// <summary> |
| 133 /// Receives output from the web server process to display in the Visual Stu
dio UI. |
| 134 /// </summary> |
| 135 /// <param name="sender">The parameter is not used.</param> |
| 136 /// <param name="e">Contains the data to display.</param> |
| 137 private void WebServerMessageReceive(object sender, System.Diagnostics.DataR
eceivedEventArgs e) |
| 138 { |
| 139 webServerOutputPane_.OutputString(e.Data + "\n"); |
| 140 } |
| 141 } |
| 142 } |
OLD | NEW |