Annotation of /trunk/AssetServer.cs
Parent Directory
|
Revision Log
Revision 5 - (view) (download)
| 1 : | jhurliman | 2 | /* |
| 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.Net; | ||
| 33 : | using System.IO; | ||
| 34 : | using System.Xml; | ||
| 35 : | jhurliman | 4 | using System.Reflection; |
| 36 : | using ExtensionLoader; | ||
| 37 : | jhurliman | 2 | using OpenMetaverse; |
| 38 : | using OpenMetaverse.Capabilities; | ||
| 39 : | using OpenMetaverse.StructuredData; | ||
| 40 : | |||
| 41 : | namespace AssetServer | ||
| 42 : | { | ||
| 43 : | public class AssetServer | ||
| 44 : | { | ||
| 45 : | public int HttpPort = 9001; | ||
| 46 : | public HttpServer HttpServer; | ||
| 47 : | |||
| 48 : | jhurliman | 4 | public IMetadataProvider MethodMetadata; |
| 49 : | public IDataProvider MethodData; | ||
| 50 : | public ICreateAssetProvider MethodCreateAsset; | ||
| 51 : | |||
| 52 : | jhurliman | 2 | public AssetServer() |
| 53 : | { | ||
| 54 : | } | ||
| 55 : | |||
| 56 : | public bool Start(int port, bool ssl) | ||
| 57 : | { | ||
| 58 : | HttpPort = port; | ||
| 59 : | |||
| 60 : | jhurliman | 4 | try |
| 61 : | { | ||
| 62 : | // Load all of the extensions | ||
| 63 : | List<string> references = new List<string>(); | ||
| 64 : | references.Add("OpenMetaverseTypes.dll"); | ||
| 65 : | references.Add("OpenMetaverse.dll"); | ||
| 66 : | references.Add("AssetServer.exe"); | ||
| 67 : | |||
| 68 : | Dictionary<Type, FieldInfo> assignables = GetInterfaces(); | ||
| 69 : | ExtensionLoader<AssetServer>.LoadAllExtensions(Assembly.GetExecutingAssembly(), | ||
| 70 : | AppDomain.CurrentDomain.BaseDirectory, this, references, | ||
| 71 : | "AssetServer.*.dll", "AssetServer.*.cs", this, assignables); | ||
| 72 : | } | ||
| 73 : | catch (ExtensionException ex) | ||
| 74 : | { | ||
| 75 : | Logger.Log("Interface loading failed, shutting down: " + ex.Message, Helpers.LogLevel.Error); | ||
| 76 : | Stop(); | ||
| 77 : | return false; | ||
| 78 : | } | ||
| 79 : | |||
| 80 : | jhurliman | 2 | InitHttpServer(HttpPort, ssl); |
| 81 : | jhurliman | 5 | |
| 82 : | foreach (IExtension extension in ExtensionLoader<AssetServer>.Extensions) | ||
| 83 : | { | ||
| 84 : | Logger.DebugLog("Starting extension " + extension.GetType().Name); | ||
| 85 : | extension.Start(); | ||
| 86 : | } | ||
| 87 : | |||
| 88 : | jhurliman | 2 | return true; |
| 89 : | } | ||
| 90 : | |||
| 91 : | public void Stop() | ||
| 92 : | { | ||
| 93 : | jhurliman | 5 | foreach (IExtension extension in ExtensionLoader<AssetServer>.Extensions) |
| 94 : | { | ||
| 95 : | Logger.DebugLog("Stopping extension " + extension.GetType().Name); | ||
| 96 : | extension.Stop(); | ||
| 97 : | } | ||
| 98 : | |||
| 99 : | jhurliman | 2 | HttpServer.Stop(); |
| 100 : | } | ||
| 101 : | |||
| 102 : | jhurliman | 4 | Dictionary<Type, FieldInfo> GetInterfaces() |
| 103 : | { | ||
| 104 : | Dictionary<Type, FieldInfo> interfaces = new Dictionary<Type, FieldInfo>(); | ||
| 105 : | |||
| 106 : | foreach (FieldInfo field in this.GetType().GetFields()) | ||
| 107 : | { | ||
| 108 : | if (field.FieldType.IsInterface) | ||
| 109 : | interfaces.Add(field.FieldType, field); | ||
| 110 : | } | ||
| 111 : | |||
| 112 : | return interfaces; | ||
| 113 : | } | ||
| 114 : | |||
| 115 : | jhurliman | 2 | void InitHttpServer(int port, bool ssl) |
| 116 : | { | ||
| 117 : | HttpServer = new HttpServer(HttpPort, ssl); | ||
| 118 : | |||
| 119 : | // Texture request | ||
| 120 : | HttpRequestSignature signature = new HttpRequestSignature(); | ||
| 121 : | signature.Method = "get"; | ||
| 122 : | signature.Path = "/"; | ||
| 123 : | HttpServer.HttpRequestCallback callback = new HttpServer.HttpRequestCallback(RequestHandler); | ||
| 124 : | HttpServer.HttpRequestHandler handler = new HttpServer.HttpRequestHandler(signature, callback); | ||
| 125 : | handler.Signature = signature; | ||
| 126 : | handler.Callback = callback; | ||
| 127 : | HttpServer.AddHandler(handler); | ||
| 128 : | |||
| 129 : | HttpServer.Start(); | ||
| 130 : | jhurliman | 5 | |
| 131 : | Logger.Log("Asset server is listening on port " + port, Helpers.LogLevel.Info); | ||
| 132 : | jhurliman | 2 | } |
| 133 : | |||
| 134 : | void RequestHandler(HttpRequestSignature signature, ref HttpListenerContext context) | ||
| 135 : | { | ||
| 136 : | // Split the URL up into an AssetID and a method | ||
| 137 : | string[] rawUrl = context.Request.RawUrl.Split('/'); | ||
| 138 : | if (rawUrl.Length >= 3) | ||
| 139 : | { | ||
| 140 : | UUID assetID; | ||
| 141 : | if (UUID.TryParse(rawUrl[1], out assetID)) | ||
| 142 : | { | ||
| 143 : | string method = rawUrl[2]; | ||
| 144 : | string[] authHeader = context.Request.Headers.GetValues("Authorization"); | ||
| 145 : | |||
| 146 : | jhurliman | 5 | switch (method) |
| 147 : | { | ||
| 148 : | case "metadata": | ||
| 149 : | MethodMetadata.FetchMetadata(assetID, context); | ||
| 150 : | break; | ||
| 151 : | case "data": | ||
| 152 : | MethodData.FetchData(assetID, context); | ||
| 153 : | break; | ||
| 154 : | } | ||
| 155 : | jhurliman | 2 | |
| 156 : | return; | ||
| 157 : | } | ||
| 158 : | } | ||
| 159 : | |||
| 160 : | context.Response.StatusCode = (int)HttpStatusCode.NotFound; | ||
| 161 : | context.Response.StatusDescription = "Requests take the form of /AssetID/method"; | ||
| 162 : | context.Response.Close(); | ||
| 163 : | } | ||
| 164 : | } | ||
| 165 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

