Annotation of /trunk/AssetServer/AssetServer.cs
Parent Directory
|
Revision Log
Revision 57 - (view) (download)
| 1 : | jhurliman | 48 | /* |
| 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 : | jhurliman | 57 | using System.Net; |
| 34 : | jhurliman | 48 | using System.Reflection; |
| 35 : | jhurliman | 57 | using System.Security.Cryptography.X509Certificates; |
| 36 : | using System.ServiceProcess; | ||
| 37 : | jhurliman | 48 | using ExtensionLoader; |
| 38 : | using ExtensionLoader.Config; | ||
| 39 : | jhurliman | 57 | using HttpServer; |
| 40 : | jhurliman | 48 | |
| 41 : | namespace AssetServer | ||
| 42 : | { | ||
| 43 : | jhurliman | 57 | public class AssetServer : ServiceBase |
| 44 : | jhurliman | 48 | { |
| 45 : | public const string CONFIG_FILE = "AssetServer.ini"; | ||
| 46 : | |||
| 47 : | jhurliman | 57 | public WebServer HttpServer; |
| 48 : | jhurliman | 48 | public IniConfigSource ConfigFile; |
| 49 : | |||
| 50 : | public IStorageProvider StorageProvider; | ||
| 51 : | public IInventoryProvider InventoryProvider; | ||
| 52 : | public IAuthenticationProvider AuthenticationProvider; | ||
| 53 : | public IAuthorizationProvider AuthorizationProvider; | ||
| 54 : | public IMetricsProvider MetricsProvider; | ||
| 55 : | |||
| 56 : | public AssetServer() | ||
| 57 : | { | ||
| 58 : | jhurliman | 57 | this.ServiceName = "CableBeachAssetServer"; |
| 59 : | |||
| 60 : | jhurliman | 48 | } |
| 61 : | |||
| 62 : | public bool Start() | ||
| 63 : | { | ||
| 64 : | List<string> extensionList = null; | ||
| 65 : | jhurliman | 57 | int port = 0; |
| 66 : | X509Certificate2 serverCert = null; | ||
| 67 : | jhurliman | 48 | |
| 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 : | jhurliman | 57 | // 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 : | jhurliman | 48 | } |
| 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("ExtensionLoader.dll"); | ||
| 112 : | references.Add("AssetServer.exe"); | ||
| 113 : | |||
| 114 : | // Get a list of all of the members of AssetServer that are interfaces | ||
| 115 : | List<FieldInfo> assignables = ExtensionLoader<AssetServer>.GetInterfaces(this); | ||
| 116 : | |||
| 117 : | // Load all of the extensions | ||
| 118 : | ExtensionLoader<AssetServer>.LoadAllExtensions( | ||
| 119 : | Assembly.GetExecutingAssembly(), | ||
| 120 : | Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), | ||
| 121 : | extensionList, | ||
| 122 : | references, | ||
| 123 : | "AssetServer.*.dll", | ||
| 124 : | "AssetServer.*.cs", | ||
| 125 : | this, | ||
| 126 : | assignables); | ||
| 127 : | } | ||
| 128 : | catch (ExtensionException ex) | ||
| 129 : | { | ||
| 130 : | Logger.Log.Error("Interface loading failed, shutting down: " + ex.Message); | ||
| 131 : | if (ex.InnerException != null) | ||
| 132 : | Logger.Log.Error(ex.InnerException.Message, ex.InnerException); | ||
| 133 : | Stop(); | ||
| 134 : | return false; | ||
| 135 : | } | ||
| 136 : | |||
| 137 : | try | ||
| 138 : | { | ||
| 139 : | jhurliman | 57 | InitHttpServer(port, serverCert); |
| 140 : | jhurliman | 48 | } |
| 141 : | catch (Exception ex) | ||
| 142 : | { | ||
| 143 : | Logger.Log.Error("Initializing the HTTP server failed, shutting down: " + ex.Message); | ||
| 144 : | Stop(); | ||
| 145 : | return false; | ||
| 146 : | } | ||
| 147 : | |||
| 148 : | // Start all of the extensions | ||
| 149 : | foreach (IExtension<AssetServer> extension in ExtensionLoader<AssetServer>.Extensions) | ||
| 150 : | { | ||
| 151 : | Logger.Log.Info("Starting extension " + extension.GetType().Name); | ||
| 152 : | extension.Start(this); | ||
| 153 : | } | ||
| 154 : | |||
| 155 : | return true; | ||
| 156 : | } | ||
| 157 : | |||
| 158 : | jhurliman | 57 | public void Shutdown() |
| 159 : | jhurliman | 48 | { |
| 160 : | foreach (IExtension<AssetServer> extension in ExtensionLoader<AssetServer>.Extensions) | ||
| 161 : | { | ||
| 162 : | Logger.Log.Debug("Stopping extension " + extension.GetType().Name); | ||
| 163 : | try { extension.Stop(); } | ||
| 164 : | catch (Exception ex) | ||
| 165 : | { Logger.Log.ErrorFormat("Failure shutting down extension {0}: {1}", extension.GetType().Name, ex.Message); } | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | if (HttpServer != null) | ||
| 169 : | HttpServer.Stop(); | ||
| 170 : | } | ||
| 171 : | |||
| 172 : | jhurliman | 57 | void InitHttpServer(int port, X509Certificate serverCert) |
| 173 : | jhurliman | 48 | { |
| 174 : | jhurliman | 57 | if (serverCert != null) |
| 175 : | HttpServer = new WebServer(IPAddress.Any, port, serverCert, null, false); | ||
| 176 : | else | ||
| 177 : | HttpServer = new WebServer(IPAddress.Any, port); | ||
| 178 : | |||
| 179 : | jhurliman | 48 | HttpServer.Start(); |
| 180 : | |||
| 181 : | jhurliman | 57 | Logger.Log.Info("Asset server is listening on port " + port); |
| 182 : | } | ||
| 183 : | jhurliman | 48 | |
| 184 : | jhurliman | 57 | #region ServiceBase Overrides |
| 185 : | |||
| 186 : | protected override void OnStart(string[] args) | ||
| 187 : | { | ||
| 188 : | Start(); | ||
| 189 : | jhurliman | 48 | } |
| 190 : | jhurliman | 57 | protected override void OnStop() |
| 191 : | { | ||
| 192 : | Shutdown(); | ||
| 193 : | } | ||
| 194 : | |||
| 195 : | #endregion | ||
| 196 : | jhurliman | 48 | } |
| 197 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

