diff options
Diffstat (limited to 'OpenSim/Grid/AssetServer/Main.cs')
-rw-r--r-- | OpenSim/Grid/AssetServer/Main.cs | 170 |
1 files changed, 108 insertions, 62 deletions
diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs index 9f1f9a2..dabacd4 100644 --- a/OpenSim/Grid/AssetServer/Main.cs +++ b/OpenSim/Grid/AssetServer/Main.cs | |||
@@ -28,15 +28,18 @@ | |||
28 | 28 | ||
29 | using System; | 29 | using System; |
30 | using System.IO; | 30 | using System.IO; |
31 | using System.Reflection; | ||
31 | 32 | ||
32 | using libsecondlife; | 33 | using libsecondlife; |
34 | using Nini.Config; | ||
35 | |||
36 | using OpenSim.Framework.Types; | ||
33 | using OpenSim.Framework.Communications.Cache; | 37 | using OpenSim.Framework.Communications.Cache; |
34 | using OpenSim.Framework.Configuration; | 38 | using OpenSim.Framework.Configuration; |
35 | using OpenSim.Framework.Console; | 39 | using OpenSim.Framework.Console; |
36 | using OpenSim.Framework.Servers; | ||
37 | using OpenSim.Framework.Configuration; | ||
38 | using OpenSim.Framework.Interfaces; | 40 | using OpenSim.Framework.Interfaces; |
39 | using OpenSim.Framework.Utilities; | 41 | using OpenSim.Framework.Utilities; |
42 | using OpenSim.Framework.Servers; | ||
40 | 43 | ||
41 | /* | 44 | /* |
42 | using System.Text; | 45 | using System.Text; |
@@ -57,7 +60,7 @@ namespace OpenSim.Grid.AssetServer | |||
57 | public static OpenAsset_Main assetserver; | 60 | public static OpenAsset_Main assetserver; |
58 | 61 | ||
59 | private LogBase m_console; | 62 | private LogBase m_console; |
60 | private IAssetServer m_assetServer; | 63 | private IAssetProvider m_assetProvider; |
61 | 64 | ||
62 | [STAThread] | 65 | [STAThread] |
63 | public static void Main(string[] args) | 66 | public static void Main(string[] args) |
@@ -97,11 +100,14 @@ namespace OpenSim.Grid.AssetServer | |||
97 | m_console.Verbose("ASSET", "Setting up asset DB"); | 100 | m_console.Verbose("ASSET", "Setting up asset DB"); |
98 | setupDB(m_config); | 101 | setupDB(m_config); |
99 | 102 | ||
103 | m_console.Verbose("ASSET", "Loading default asset set.."); | ||
104 | LoadDefaultAssets(); | ||
105 | |||
100 | m_console.Verbose("ASSET", "Starting HTTP process"); | 106 | m_console.Verbose("ASSET", "Starting HTTP process"); |
101 | BaseHttpServer httpServer = new BaseHttpServer((int)m_config.HttpPort); | 107 | BaseHttpServer httpServer = new BaseHttpServer((int)m_config.HttpPort); |
102 | 108 | ||
103 | httpServer.AddStreamHandler(new GetAssetStreamHandler(this)); | 109 | httpServer.AddStreamHandler(new GetAssetStreamHandler(this, m_assetProvider)); |
104 | httpServer.AddStreamHandler(new PostAssetStreamHandler( this )); | 110 | httpServer.AddStreamHandler(new PostAssetStreamHandler(this, m_assetProvider)); |
105 | 111 | ||
106 | httpServer.Start(); | 112 | httpServer.Start(); |
107 | } | 113 | } |
@@ -111,14 +117,49 @@ namespace OpenSim.Grid.AssetServer | |||
111 | return null; | 117 | return null; |
112 | } | 118 | } |
113 | 119 | ||
120 | |||
121 | public IAssetProvider LoadDatabasePlugin(string FileName) | ||
122 | { | ||
123 | MainLog.Instance.Verbose("ASSET SERVER", "LoadDatabasePlugin: Attempting to load " + FileName); | ||
124 | Assembly pluginAssembly = Assembly.LoadFrom(FileName); | ||
125 | IAssetProvider assetPlugin = null; | ||
126 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
127 | { | ||
128 | if (!pluginType.IsAbstract) | ||
129 | { | ||
130 | Type typeInterface = pluginType.GetInterface("IAssetProvider", true); | ||
131 | |||
132 | if (typeInterface != null) | ||
133 | { | ||
134 | IAssetProvider plug = (IAssetProvider)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
135 | assetPlugin = plug; | ||
136 | assetPlugin.Initialise(); | ||
137 | |||
138 | MainLog.Instance.Verbose("ASSET SERVER", "Added " + assetPlugin.Name + " " + assetPlugin.Version); | ||
139 | break; | ||
140 | } | ||
141 | |||
142 | typeInterface = null; | ||
143 | } | ||
144 | } | ||
145 | |||
146 | pluginAssembly = null; | ||
147 | return assetPlugin; | ||
148 | } | ||
149 | |||
114 | public void setupDB(AssetConfig config) | 150 | public void setupDB(AssetConfig config) |
115 | { | 151 | { |
116 | try | 152 | try |
117 | { | 153 | { |
118 | SQLAssetServer assetServer = new SQLAssetServer(config.DatabaseProvider ); | 154 | m_assetProvider = LoadDatabasePlugin(config.DatabaseProvider); |
119 | assetServer.LoadDefaultAssets(); | 155 | if (m_assetProvider == null) |
156 | { | ||
157 | MainLog.Instance.Error("ASSET", "Failed to load a database plugin, server halting"); | ||
158 | Environment.Exit(-1); | ||
159 | } | ||
160 | // assetServer.LoadDefaultAssets(); | ||
120 | 161 | ||
121 | m_assetServer = assetServer; | 162 | // m_assetServer = assetServer; |
122 | } | 163 | } |
123 | catch (Exception e) | 164 | catch (Exception e) |
124 | { | 165 | { |
@@ -127,84 +168,89 @@ namespace OpenSim.Grid.AssetServer | |||
127 | } | 168 | } |
128 | } | 169 | } |
129 | 170 | ||
130 | public void RunCmd(string cmd, string[] cmdparams) | 171 | public void LoadAsset(AssetBase info, bool image, string filename) |
131 | { | 172 | { |
132 | switch (cmd) | 173 | //should request Asset from storage manager |
133 | { | 174 | //but for now read from file |
134 | case "help": | ||
135 | m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); | ||
136 | break; | ||
137 | |||
138 | case "shutdown": | ||
139 | m_console.Close(); | ||
140 | Environment.Exit(0); | ||
141 | break; | ||
142 | } | ||
143 | } | ||
144 | 175 | ||
145 | public void Show(string ShowWhat) | 176 | string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; |
146 | { | 177 | string fileName = Path.Combine(dataPath, filename); |
178 | FileInfo fInfo = new FileInfo(fileName); | ||
179 | long numBytes = fInfo.Length; | ||
180 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
181 | byte[] idata = new byte[numBytes]; | ||
182 | BinaryReader br = new BinaryReader(fStream); | ||
183 | idata = br.ReadBytes((int)numBytes); | ||
184 | br.Close(); | ||
185 | fStream.Close(); | ||
186 | info.Data = idata; | ||
187 | //info.loaded=true; | ||
147 | } | 188 | } |
148 | } | ||
149 | 189 | ||
150 | public class GetAssetStreamHandler : BaseStreamHandler | 190 | public AssetBase CreateAsset(string assetIdStr, string name, string filename, bool isImage) |
151 | { | ||
152 | OpenAsset_Main m_assetManager; | ||
153 | |||
154 | override public byte[] Handle(string path, Stream request) | ||
155 | { | 191 | { |
156 | string param = GetParam(path); | 192 | AssetBase asset = new AssetBase( |
193 | new LLUUID(assetIdStr), | ||
194 | name | ||
195 | ); | ||
157 | 196 | ||
158 | byte[] assetdata = m_assetManager.GetAssetData(new LLUUID(param), false); | 197 | if (!String.IsNullOrEmpty(filename)) |
159 | if (assetdata != null) | ||
160 | { | 198 | { |
161 | return assetdata; | 199 | MainLog.Instance.Verbose("ASSETS", "Loading: [{0}][{1}]", name, filename); |
200 | |||
201 | LoadAsset(asset, isImage, filename); | ||
162 | } | 202 | } |
163 | else | 203 | else |
164 | { | 204 | { |
165 | return new byte[]{}; | 205 | MainLog.Instance.Verbose("ASSETS", "Instantiated: [{0}]", name); |
166 | } | 206 | } |
167 | } | ||
168 | 207 | ||
169 | public GetAssetStreamHandler(OpenAsset_Main assetManager) : base("/assets/", "GET") | 208 | return asset; |
170 | { | ||
171 | m_assetManager = assetManager; | ||
172 | } | 209 | } |
173 | } | ||
174 | |||
175 | public class PostAssetStreamHandler : BaseStreamHandler | ||
176 | { | ||
177 | OpenAsset_Main m_assetManager; | ||
178 | 210 | ||
179 | override public byte[] Handle(string path, Stream request) | 211 | public void LoadDefaultAssets() |
180 | { | 212 | { |
181 | string param = GetParam(path); | 213 | string filePath = Path.Combine(Util.configDir(), "OpenSimAssetSet.xml"); |
182 | LLUUID assetId = new LLUUID(param); | 214 | if (File.Exists(filePath)) |
183 | byte[] txBuffer = new byte[4096]; | ||
184 | |||
185 | using( BinaryReader binReader = new BinaryReader( request ) ) | ||
186 | { | 215 | { |
187 | using (MemoryStream memoryStream = new MemoryStream(4096)) | 216 | XmlConfigSource source = new XmlConfigSource(filePath); |
217 | |||
218 | for (int i = 0; i < source.Configs.Count; i++) | ||
188 | { | 219 | { |
189 | int count; | 220 | string assetIdStr = source.Configs[i].GetString("assetID", LLUUID.Random().ToStringHyphenated()); |
190 | while ((count = binReader.Read(txBuffer, 0, 4096)) > 0) | 221 | string name = source.Configs[i].GetString("name", ""); |
191 | { | 222 | sbyte type = (sbyte)source.Configs[i].GetInt("assetType", 0); |
192 | memoryStream.Write(txBuffer, 0, count); | 223 | sbyte invType = (sbyte)source.Configs[i].GetInt("inventoryType", 0); |
193 | } | 224 | string fileName = source.Configs[i].GetString("fileName", ""); |
225 | |||
226 | AssetBase newAsset = CreateAsset(assetIdStr, name, fileName, false); | ||
194 | 227 | ||
195 | byte[] assetData = memoryStream.ToArray(); | 228 | newAsset.Type = type; |
229 | newAsset.InvType = invType; | ||
196 | 230 | ||
197 | // m_assetManager.CreateAsset(assetId, assetData); | 231 | m_assetProvider.CreateAsset(newAsset); |
198 | } | 232 | } |
199 | } | 233 | } |
200 | |||
201 | return new byte[]{}; | ||
202 | } | 234 | } |
203 | 235 | ||
204 | public PostAssetStreamHandler( OpenAsset_Main assetManager ) | 236 | |
205 | : base("/assets/", "POST") | 237 | public void RunCmd(string cmd, string[] cmdparams) |
238 | { | ||
239 | switch (cmd) | ||
240 | { | ||
241 | case "help": | ||
242 | m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); | ||
243 | break; | ||
244 | |||
245 | case "shutdown": | ||
246 | m_console.Close(); | ||
247 | Environment.Exit(0); | ||
248 | break; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | public void Show(string ShowWhat) | ||
206 | { | 253 | { |
207 | m_assetManager = assetManager; | ||
208 | } | 254 | } |
209 | } | 255 | } |
210 | } | 256 | } |