aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/AssetServer/Main.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Grid/AssetServer/Main.cs406
1 files changed, 406 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs
new file mode 100644
index 0000000..3e302d8
--- /dev/null
+++ b/OpenSim/Grid/AssetServer/Main.cs
@@ -0,0 +1,406 @@
1/*
2* Copyright (c) Contributors, http://www.openmetaverse.org/
3* See CONTRIBUTORS.TXT for a full list of copyright holders.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above copyright
10* notice, this list of conditions and the following disclaimer in the
11* documentation and/or other materials provided with the distribution.
12* * Neither the name of the OpenSim Project nor the
13* names of its contributors may be used to endorse or promote products
14* derived from this software without specific prior written permission.
15*
16* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
17* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*
27*/
28
29using System;
30using System.IO;
31using System.Text;
32using Db4objects.Db4o;
33using libsecondlife;
34using OpenSim.Framework.Console;
35using OpenSim.Framework.Types;
36using OpenSim.Framework.Servers;
37
38namespace OpenSim.Grid.AssetServer
39{
40 /// <summary>
41 /// An asset server
42 /// </summary>
43 public class OpenAsset_Main : conscmd_callback
44 {
45 private IObjectContainer db;
46
47 public static OpenAsset_Main assetserver;
48
49 private LogBase m_console;
50
51 [STAThread]
52 public static void Main(string[] args)
53 {
54 Console.WriteLine("Starting...\n");
55
56 assetserver = new OpenAsset_Main();
57 assetserver.Startup();
58
59 assetserver.Work();
60 }
61
62 private void Work()
63 {
64 m_console.Notice("Enter help for a list of commands");
65
66 while (true)
67 {
68 m_console.MainLogPrompt();
69 }
70 }
71
72 private OpenAsset_Main()
73 {
74 m_console = new LogBase("opengrid-AssetServer-console.log", "OpenAsset", this, false);
75 MainLog.Instance = m_console;
76 }
77
78 public void Startup()
79 {
80 m_console.Verbose("Main.cs:Startup() - Setting up asset DB");
81 setupDB();
82
83 m_console.Verbose("Main.cs:Startup() - Starting HTTP process");
84 BaseHttpServer httpServer = new BaseHttpServer(8003);
85
86 httpServer.AddStreamHandler( new GetAssetStreamHandler(this));
87 httpServer.AddStreamHandler(new PostAssetStreamHandler( this ));
88
89 //httpServer.AddRestHandler("GET", "/assets/", this.assetGetMethod);
90 //httpServer.AddRestHandler("POST", "/assets/", this.assetPostMethod);
91
92 httpServer.Start();
93
94 }
95
96 //public string AssetPostMethod(string requestBody, string path, string param)
97 //{
98 // AssetBase asset = new AssetBase();
99 // asset.Name = "";
100 // asset.FullID = new LLUUID(param);
101 // Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
102 // byte[] buffer = Windows1252Encoding.GetBytes(requestBody);
103 // asset.Data = buffer;
104 // AssetStorage store = new AssetStorage();
105 // store.Data = asset.Data;
106 // store.Name = asset.Name;
107 // store.UUID = asset.FullID;
108 // db.Set(store);
109 // db.Commit();
110 // return "";
111 //}
112
113 //public string AssetGetMethod(string request, string path, string param)
114 //{
115 // Console.WriteLine("got a request " + param);
116 // byte[] assetdata = GetAssetData(new LLUUID(param), false);
117 // if (assetdata != null)
118 // {
119 // Encoding Windows1252Encoding = Encoding.GetEncoding(1252);
120 // string ret = Windows1252Encoding.GetString(assetdata);
121 // //string ret = System.Text.Encoding.Unicode.GetString(assetdata);
122
123 // return ret;
124
125 // }
126 // else
127 // {
128 // return "";
129 // }
130
131 //}
132
133 public byte[] GetAssetData(LLUUID assetID, bool isTexture)
134 {
135 bool found = false;
136 AssetStorage foundAsset = null;
137
138 IObjectSet result = db.Get(new AssetStorage(assetID));
139 if (result.Count > 0)
140 {
141 foundAsset = (AssetStorage)result.Next();
142 found = true;
143 }
144
145 if (found)
146 {
147 return foundAsset.Data;
148 }
149 else
150 {
151 return null;
152 }
153 }
154
155 public void setupDB()
156 {
157 bool yapfile = File.Exists("assets.yap");
158 try
159 {
160 db = Db4oFactory.OpenFile("assets.yap");
161 MainLog.Instance.Verbose("Main.cs:setupDB() - creation");
162 }
163 catch (Exception e)
164 {
165 db.Close();
166 MainLog.Instance.Warn("Main.cs:setupDB() - Exception occured");
167 MainLog.Instance.Warn(e.ToString());
168 }
169 if (!yapfile)
170 {
171 this.LoadDB();
172 }
173 }
174
175 public void LoadDB()
176 {
177 try
178 {
179
180 Console.WriteLine("setting up Asset database");
181
182 AssetBase Image = new AssetBase();
183 Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001");
184 Image.Name = "Bricks";
185 this.LoadAsset(Image, true, "bricks.jp2");
186 AssetStorage store = new AssetStorage();
187 store.Data = Image.Data;
188 store.Name = Image.Name;
189 store.UUID = Image.FullID;
190 db.Set(store);
191 db.Commit();
192
193 Image = new AssetBase();
194 Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002");
195 Image.Name = "Plywood";
196 this.LoadAsset(Image, true, "plywood.jp2");
197 store = new AssetStorage();
198 store.Data = Image.Data;
199 store.Name = Image.Name;
200 store.UUID = Image.FullID;
201 db.Set(store);
202 db.Commit();
203
204 Image = new AssetBase();
205 Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003");
206 Image.Name = "Rocks";
207 this.LoadAsset(Image, true, "rocks.jp2");
208 store = new AssetStorage();
209 store.Data = Image.Data;
210 store.Name = Image.Name;
211 store.UUID = Image.FullID;
212 db.Set(store);
213 db.Commit();
214
215 Image = new AssetBase();
216 Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004");
217 Image.Name = "Granite";
218 this.LoadAsset(Image, true, "granite.jp2");
219 store = new AssetStorage();
220 store.Data = Image.Data;
221 store.Name = Image.Name;
222 store.UUID = Image.FullID;
223 db.Set(store);
224 db.Commit();
225
226 Image = new AssetBase();
227 Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005");
228 Image.Name = "Hardwood";
229 this.LoadAsset(Image, true, "hardwood.jp2");
230 store = new AssetStorage();
231 store.Data = Image.Data;
232 store.Name = Image.Name;
233 store.UUID = Image.FullID;
234 db.Set(store);
235 db.Commit();
236
237 Image = new AssetBase();
238 Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005");
239 Image.Name = "Prim Base Texture";
240 this.LoadAsset(Image, true, "plywood.jp2");
241 store = new AssetStorage();
242 store.Data = Image.Data;
243 store.Name = Image.Name;
244 store.UUID = Image.FullID;
245 db.Set(store);
246 db.Commit();
247
248 Image = new AssetBase();
249 Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
250 Image.Name = "Shape";
251 this.LoadAsset(Image, false, "base_shape.dat");
252 store = new AssetStorage();
253 store.Data = Image.Data;
254 store.Name = Image.Name;
255 store.UUID = Image.FullID;
256 db.Set(store);
257 db.Commit();
258 }
259 catch (Exception e)
260 {
261 Console.WriteLine(e.Message);
262 }
263 }
264
265 private void LoadAsset(AssetBase info, bool image, string filename)
266 {
267
268
269 string dataPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder;
270 string fileName = Path.Combine(dataPath, filename);
271 FileInfo fInfo = new FileInfo(fileName);
272 long numBytes = fInfo.Length;
273 FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
274 byte[] idata = new byte[numBytes];
275 BinaryReader br = new BinaryReader(fStream);
276 idata = br.ReadBytes((int)numBytes);
277 br.Close();
278 fStream.Close();
279 info.Data = idata;
280 //info.loaded=true;
281 }
282
283 /*private GridConfig LoadConfigDll(string dllName)
284 {
285 Assembly pluginAssembly = Assembly.LoadFrom(dllName);
286 GridConfig config = null;
287
288 foreach (Type pluginType in pluginAssembly.GetTypes())
289 {
290 if (pluginType.IsPublic)
291 {
292 if (!pluginType.IsAbstract)
293 {
294 Type typeInterface = pluginType.GetInterface("IGridConfig", true);
295
296 if (typeInterface != null)
297 {
298 IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
299 config = plug.GetConfigObject();
300 break;
301 }
302
303 typeInterface = null;
304 }
305 }
306 }
307 pluginAssembly = null;
308 return config;
309 }*/
310
311 public void CreateAsset(LLUUID assetId, byte[] assetData)
312 {
313 AssetBase asset = new AssetBase();
314 asset.Name = "";
315 asset.FullID = assetId;
316 asset.Data = assetData;
317
318 AssetStorage store = new AssetStorage();
319 store.Data = asset.Data;
320 store.Name = asset.Name;
321 store.UUID = asset.FullID;
322 db.Set(store);
323 db.Commit();
324 }
325
326 public void RunCmd(string cmd, string[] cmdparams)
327 {
328 switch (cmd)
329 {
330 case "help":
331 m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)");
332 break;
333
334 case "shutdown":
335 m_console.Close();
336 Environment.Exit(0);
337 break;
338 }
339 }
340
341 public void Show(string ShowWhat)
342 {
343 }
344 }
345
346 public class GetAssetStreamHandler : BaseStreamHandler
347 {
348 OpenAsset_Main m_assetManager;
349
350 override public byte[] Handle(string path, Stream request)
351 {
352 string param = GetParam(path);
353
354 byte[] assetdata = m_assetManager.GetAssetData(new LLUUID(param), false);
355 if (assetdata != null)
356 {
357 return assetdata;
358 }
359 else
360 {
361 return new byte[]{};
362 }
363 }
364
365 public GetAssetStreamHandler(OpenAsset_Main assetManager):base( "/assets/", "GET")
366 {
367 m_assetManager = assetManager;
368 }
369 }
370
371 public class PostAssetStreamHandler : BaseStreamHandler
372 {
373 OpenAsset_Main m_assetManager;
374
375 override public byte[] Handle(string path, Stream request)
376 {
377 string param = GetParam(path);
378 LLUUID assetId = new LLUUID(param);
379 byte[] txBuffer = new byte[4096];
380
381 using( BinaryReader binReader = new BinaryReader( request ) )
382 {
383 using (MemoryStream memoryStream = new MemoryStream(4096))
384 {
385 int count;
386 while ((count = binReader.Read(txBuffer, 0, 4096)) > 0)
387 {
388 memoryStream.Write(txBuffer, 0, count);
389 }
390
391 byte[] assetData = memoryStream.ToArray();
392
393 m_assetManager.CreateAsset(assetId, assetData);
394 }
395 }
396
397 return new byte[]{};
398 }
399
400 public PostAssetStreamHandler( OpenAsset_Main assetManager )
401 : base("/assets/", "POST")
402 {
403 m_assetManager = assetManager;
404 }
405 }
406}