Annotation of /trunk/AssetServer/AssetServer.cs
Parent Directory
|
Revision Log
Revision 48 - (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 : | using System.Reflection; | ||
| 34 : | using ExtensionLoader; | ||
| 35 : | using ExtensionLoader.Config; | ||
| 36 : | using OpenMetaverse; | ||
| 37 : | |||
| 38 : | namespace AssetServer | ||
| 39 : | { | ||
| 40 : | public class AssetServer | ||
| 41 : | { | ||
| 42 : | public const string CONFIG_FILE = "AssetServer.ini"; | ||
| 43 : | |||
| 44 : | public HttpServer HttpServer; | ||
| 45 : | public IniConfigSource ConfigFile; | ||
| 46 : | |||
| 47 : | public IStorageProvider StorageProvider; | ||
| 48 : | public IInventoryProvider InventoryProvider; | ||
| 49 : | public IAuthenticationProvider AuthenticationProvider; | ||
| 50 : | public IAuthorizationProvider AuthorizationProvider; | ||
| 51 : | public IMetricsProvider MetricsProvider; | ||
| 52 : | |||
| 53 : | public AssetServer() | ||
| 54 : | { | ||
| 55 : | } | ||
| 56 : | |||
| 57 : | public bool Start() | ||
| 58 : | { | ||
| 59 : | List<string> extensionList = null; | ||
| 60 : | List<string> listenPrefixes = null; | ||
| 61 : | |||
| 62 : | try { ConfigFile = new IniConfigSource(CONFIG_FILE); } | ||
| 63 : | catch (Exception) | ||
| 64 : | { | ||
| 65 : | Logger.Log.Error("Failed to load the config file " + CONFIG_FILE); | ||
| 66 : | return false; | ||
| 67 : | } | ||
| 68 : | |||
| 69 : | try | ||
| 70 : | { | ||
| 71 : | IConfig extensionConfig = ConfigFile.Configs["Config"]; | ||
| 72 : | |||
| 73 : | // Load the addresses to listen at | ||
| 74 : | string[] prefixes = extensionConfig.GetString("ListenAddress").Split(','); | ||
| 75 : | listenPrefixes = new List<string>(prefixes.Length); | ||
| 76 : | foreach (string prefix in prefixes) | ||
| 77 : | listenPrefixes.Add(prefix.Trim()); | ||
| 78 : | } | ||
| 79 : | catch (Exception) | ||
| 80 : | { | ||
| 81 : | Logger.Log.Error("Failed to load [Config] section from " + CONFIG_FILE); | ||
| 82 : | return false; | ||
| 83 : | } | ||
| 84 : | |||
| 85 : | try | ||
| 86 : | { | ||
| 87 : | // Load the extension list (and ordering) from our config file | ||
| 88 : | IConfig extensionConfig = ConfigFile.Configs["Extensions"]; | ||
| 89 : | extensionList = new List<string>(extensionConfig.GetKeys()); | ||
| 90 : | } | ||
| 91 : | catch (Exception) | ||
| 92 : | { | ||
| 93 : | Logger.Log.Error("Failed to load [Extensions] section from " + CONFIG_FILE); | ||
| 94 : | return false; | ||
| 95 : | } | ||
| 96 : | |||
| 97 : | try | ||
| 98 : | { | ||
| 99 : | // Create a reference list for C# extensions compiled at runtime | ||
| 100 : | List<string> references = new List<string>(); | ||
| 101 : | references.Add("OpenMetaverseTypes.dll"); | ||
| 102 : | references.Add("OpenMetaverse.dll"); | ||
| 103 : | references.Add("ExtensionLoader.dll"); | ||
| 104 : | references.Add("AssetServer.exe"); | ||
| 105 : | |||
| 106 : | // Get a list of all of the members of AssetServer that are interfaces | ||
| 107 : | List<FieldInfo> assignables = ExtensionLoader<AssetServer>.GetInterfaces(this); | ||
| 108 : | |||
| 109 : | // Load all of the extensions | ||
| 110 : | ExtensionLoader<AssetServer>.LoadAllExtensions( | ||
| 111 : | Assembly.GetExecutingAssembly(), | ||
| 112 : | Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), | ||
| 113 : | extensionList, | ||
| 114 : | references, | ||
| 115 : | "AssetServer.*.dll", | ||
| 116 : | "AssetServer.*.cs", | ||
| 117 : | this, | ||
| 118 : | assignables); | ||
| 119 : | } | ||
| 120 : | catch (ExtensionException ex) | ||
| 121 : | { | ||
| 122 : | Logger.Log.Error("Interface loading failed, shutting down: " + ex.Message); | ||
| 123 : | if (ex.InnerException != null) | ||
| 124 : | Logger.Log.Error(ex.InnerException.Message, ex.InnerException); | ||
| 125 : | Stop(); | ||
| 126 : | return false; | ||
| 127 : | } | ||
| 128 : | |||
| 129 : | try | ||
| 130 : | { | ||
| 131 : | InitHttpServer(listenPrefixes); | ||
| 132 : | } | ||
| 133 : | catch (Exception ex) | ||
| 134 : | { | ||
| 135 : | Logger.Log.Error("Initializing the HTTP server failed, shutting down: " + ex.Message); | ||
| 136 : | Stop(); | ||
| 137 : | return false; | ||
| 138 : | } | ||
| 139 : | |||
| 140 : | // Start all of the extensions | ||
| 141 : | foreach (IExtension<AssetServer> extension in ExtensionLoader<AssetServer>.Extensions) | ||
| 142 : | { | ||
| 143 : | Logger.Log.Info("Starting extension " + extension.GetType().Name); | ||
| 144 : | extension.Start(this); | ||
| 145 : | } | ||
| 146 : | |||
| 147 : | return true; | ||
| 148 : | } | ||
| 149 : | |||
| 150 : | public void Stop() | ||
| 151 : | { | ||
| 152 : | foreach (IExtension<AssetServer> extension in ExtensionLoader<AssetServer>.Extensions) | ||
| 153 : | { | ||
| 154 : | Logger.Log.Debug("Stopping extension " + extension.GetType().Name); | ||
| 155 : | try { extension.Stop(); } | ||
| 156 : | catch (Exception ex) | ||
| 157 : | { Logger.Log.ErrorFormat("Failure shutting down extension {0}: {1}", extension.GetType().Name, ex.Message); } | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | if (HttpServer != null) | ||
| 161 : | HttpServer.Stop(); | ||
| 162 : | } | ||
| 163 : | |||
| 164 : | void InitHttpServer(List<string> listenPrefixes) | ||
| 165 : | { | ||
| 166 : | HttpServer = new HttpServer(listenPrefixes); | ||
| 167 : | HttpServer.Start(); | ||
| 168 : | |||
| 169 : | System.Text.StringBuilder output = new System.Text.StringBuilder("Asset server is listening at: "); | ||
| 170 : | for (int i = 0; i < listenPrefixes.Count; i++) | ||
| 171 : | { | ||
| 172 : | output.Append(listenPrefixes[i]); | ||
| 173 : | if (i != listenPrefixes.Count - 1) output.Append(", "); | ||
| 174 : | } | ||
| 175 : | |||
| 176 : | Logger.Log.Info(output.ToString()); | ||
| 177 : | } | ||
| 178 : | } | ||
| 179 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

