Annotation of /trunk/GridServer/GridServer.cs
Parent Directory
|
Revision Log
Revision 76 - (view) (download)
| 1 : | jhurliman | 65 | /* |
| 2 : | * Copyright (c) 2008 Intel Corporation | ||
| 3 : | * All rights reserved. | ||
| 4 : | * Redistribution and use in source and binary forms, with or without | ||
| 5 : | * modification, are permitted provided that the following conditions | ||
| 6 : | * are met: | ||
| 7 : | * | ||
| 8 : | * -- Redistributions of source code must retain the above copyright | ||
| 9 : | * notice, this list of conditions and the following disclaimer. | ||
| 10 : | * -- Redistributions in binary form must reproduce the above copyright | ||
| 11 : | * notice, this list of conditions and the following disclaimer in the | ||
| 12 : | * documentation and/or other materials provided with the distribution. | ||
| 13 : | * -- Neither the name of the Intel Corporation nor the names of its | ||
| 14 : | * contributors may be used to endorse or promote products derived from | ||
| 15 : | * this software without specific prior written permission. | ||
| 16 : | * | ||
| 17 : | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| 18 : | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| 19 : | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
| 20 : | * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR ITS | ||
| 21 : | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| 22 : | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| 23 : | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| 24 : | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
| 25 : | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
| 26 : | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
| 27 : | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 28 : | */ | ||
| 29 : | |||
| 30 : | using System; | ||
| 31 : | using System.Collections.Generic; | ||
| 32 : | using System.IO; | ||
| 33 : | using System.Net; | ||
| 34 : | using System.Reflection; | ||
| 35 : | using System.Security.Cryptography.X509Certificates; | ||
| 36 : | using System.ServiceProcess; | ||
| 37 : | using ExtensionLoader; | ||
| 38 : | using ExtensionLoader.Config; | ||
| 39 : | using HttpServer; | ||
| 40 : | jhurliman | 74 | using HttpListener = HttpServer.HttpListener; |
| 41 : | jhurliman | 65 | using log4net; |
| 42 : | |||
| 43 : | namespace GridServer | ||
| 44 : | { | ||
| 45 : | public class GridServer : ServiceBase | ||
| 46 : | { | ||
| 47 : | public const string CONFIG_FILE = "GridServer.ini"; | ||
| 48 : | |||
| 49 : | jhurliman | 74 | public HttpListener HttpServer; |
| 50 : | jhurliman | 65 | public IniConfigSource ConfigFile; |
| 51 : | |||
| 52 : | public IAvatarProvider AvatarProvider; | ||
| 53 : | public ISessionProvider SessionProvider; | ||
| 54 : | jhurliman | 69 | public IAuthorizationProvider AuthorizationProvider; |
| 55 : | public IGridProvider GridProvider; | ||
| 56 : | jhurliman | 65 | |
| 57 : | public GridServer() | ||
| 58 : | { | ||
| 59 : | this.ServiceName = "CableBeachGridServer"; | ||
| 60 : | } | ||
| 61 : | |||
| 62 : | public bool Start() | ||
| 63 : | { | ||
| 64 : | List<string> extensionList = null; | ||
| 65 : | int port = 0; | ||
| 66 : | X509Certificate2 serverCert = null; | ||
| 67 : | |||
| 68 : | try { ConfigFile = new IniConfigSource(CONFIG_FILE); } | ||
| 69 : | catch (Exception) | ||
| 70 : | { | ||
| 71 : | Logger.Log.Error("Failed to load the config file " + CONFIG_FILE); | ||
| 72 : | return false; | ||
| 73 : | } | ||
| 74 : | |||
| 75 : | try | ||
| 76 : | { | ||
| 77 : | IConfig extensionConfig = ConfigFile.Configs["Config"]; | ||
| 78 : | |||
| 79 : | // Load the port number to listen on | ||
| 80 : | port = extensionConfig.GetInt("ListenPort"); | ||
| 81 : | |||
| 82 : | // Load the server certificate file | ||
| 83 : | string certFile = extensionConfig.GetString("SSLCertFile"); | ||
| 84 : | if (!String.IsNullOrEmpty(certFile)) | ||
| 85 : | serverCert = new X509Certificate2(certFile); | ||
| 86 : | } | ||
| 87 : | catch (Exception) | ||
| 88 : | { | ||
| 89 : | Logger.Log.Error("Failed to load [Config] section from " + CONFIG_FILE); | ||
| 90 : | return false; | ||
| 91 : | } | ||
| 92 : | |||
| 93 : | try | ||
| 94 : | { | ||
| 95 : | // Load the extension list (and ordering) from our config file | ||
| 96 : | IConfig extensionConfig = ConfigFile.Configs["Extensions"]; | ||
| 97 : | extensionList = new List<string>(extensionConfig.GetKeys()); | ||
| 98 : | } | ||
| 99 : | catch (Exception) | ||
| 100 : | { | ||
| 101 : | Logger.Log.Error("Failed to load [Extensions] section from " + CONFIG_FILE); | ||
| 102 : | return false; | ||
| 103 : | } | ||
| 104 : | |||
| 105 : | try | ||
| 106 : | { | ||
| 107 : | // Create a reference list for C# extensions compiled at runtime | ||
| 108 : | List<string> references = new List<string>(); | ||
| 109 : | references.Add("OpenMetaverseTypes.dll"); | ||
| 110 : | references.Add("OpenMetaverse.dll"); | ||
| 111 : | references.Add("OpenMetaverse.StructuredData.dll"); | ||
| 112 : | references.Add("OpenMetaverse.Http.dll"); | ||
| 113 : | references.Add("ExtensionLoader.dll"); | ||
| 114 : | references.Add("GridServer.exe"); | ||
| 115 : | |||
| 116 : | // Get a list of all of the members of GridServer that are interfaces | ||
| 117 : | List<FieldInfo> assignables = ExtensionLoader<GridServer>.GetInterfaces(this); | ||
| 118 : | |||
| 119 : | // Load all of the extensions | ||
| 120 : | ExtensionLoader<GridServer>.LoadAllExtensions( | ||
| 121 : | Assembly.GetExecutingAssembly(), | ||
| 122 : | Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), | ||
| 123 : | extensionList, | ||
| 124 : | references, | ||
| 125 : | "GridServer.*.dll", | ||
| 126 : | "GridServer.*.cs", | ||
| 127 : | this, | ||
| 128 : | assignables); | ||
| 129 : | } | ||
| 130 : | catch (ExtensionException ex) | ||
| 131 : | { | ||
| 132 : | Logger.Log.Error("Interface loading failed, shutting down: " + ex.Message); | ||
| 133 : | if (ex.InnerException != null) | ||
| 134 : | Logger.Log.Error(ex.InnerException.Message, ex.InnerException); | ||
| 135 : | Stop(); | ||
| 136 : | return false; | ||
| 137 : | } | ||
| 138 : | |||
| 139 : | try | ||
| 140 : | { | ||
| 141 : | InitHttpServer(port, serverCert); | ||
| 142 : | } | ||
| 143 : | catch (Exception ex) | ||
| 144 : | { | ||
| 145 : | Logger.Log.Error("Initializing the HTTP server failed, shutting down: " + ex.Message); | ||
| 146 : | Stop(); | ||
| 147 : | return false; | ||
| 148 : | } | ||
| 149 : | |||
| 150 : | // Start all of the extensions | ||
| 151 : | foreach (IExtension<GridServer> extension in ExtensionLoader<GridServer>.Extensions) | ||
| 152 : | { | ||
| 153 : | Logger.Log.Info("Starting extension " + extension.GetType().Name); | ||
| 154 : | extension.Start(this); | ||
| 155 : | } | ||
| 156 : | |||
| 157 : | return true; | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | public void Shutdown() | ||
| 161 : | { | ||
| 162 : | foreach (IExtension<GridServer> extension in ExtensionLoader<GridServer>.Extensions) | ||
| 163 : | { | ||
| 164 : | Logger.Log.Debug("Stopping extension " + extension.GetType().Name); | ||
| 165 : | try { extension.Stop(); } | ||
| 166 : | catch (Exception ex) | ||
| 167 : | { Logger.Log.ErrorFormat("Failure shutting down extension {0}: {1}", extension.GetType().Name, ex.Message); } | ||
| 168 : | } | ||
| 169 : | |||
| 170 : | if (HttpServer != null) | ||
| 171 : | HttpServer.Stop(); | ||
| 172 : | } | ||
| 173 : | |||
| 174 : | void InitHttpServer(int port, X509Certificate serverCert) | ||
| 175 : | { | ||
| 176 : | if (serverCert != null) | ||
| 177 : | jhurliman | 76 | HttpServer = HttpListener.Create(log4netLogWriter.Instance, IPAddress.Any, port, serverCert); |
| 178 : | jhurliman | 65 | else |
| 179 : | jhurliman | 76 | HttpServer = HttpListener.Create(log4netLogWriter.Instance, IPAddress.Any, port); |
| 180 : | jhurliman | 65 | |
| 181 : | HttpServer.Set404Handler( | ||
| 182 : | delegate(IHttpClientContext client, IHttpRequest request, IHttpResponse response) | ||
| 183 : | { | ||
| 184 : | Logger.Log.Warn("Requested page was not found: " + request.Uri.PathAndQuery); | ||
| 185 : | |||
| 186 : | string notFoundString = "<html><head><title>Page Not Found</title></head><body>The requested page or method was not found</body></html>"; | ||
| 187 : | byte[] buffer = System.Text.Encoding.UTF8.GetBytes(notFoundString); | ||
| 188 : | response.Body.Write(buffer, 0, buffer.Length); | ||
| 189 : | response.Status = HttpStatusCode.NotFound; | ||
| 190 : | return true; | ||
| 191 : | } | ||
| 192 : | ); | ||
| 193 : | |||
| 194 : | jhurliman | 74 | HttpServer.Start(10); |
| 195 : | jhurliman | 65 | |
| 196 : | Logger.Log.Info("Grid server is listening on port " + port); | ||
| 197 : | } | ||
| 198 : | |||
| 199 : | #region ServiceBase Overrides | ||
| 200 : | |||
| 201 : | protected override void OnStart(string[] args) | ||
| 202 : | { | ||
| 203 : | Start(); | ||
| 204 : | } | ||
| 205 : | protected override void OnStop() | ||
| 206 : | { | ||
| 207 : | Shutdown(); | ||
| 208 : | } | ||
| 209 : | |||
| 210 : | #endregion | ||
| 211 : | } | ||
| 212 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

