diff options
author | MW | 2007-06-27 15:28:52 +0000 |
---|---|---|
committer | MW | 2007-06-27 15:28:52 +0000 |
commit | 646bbbc84b8010e0dacbeed5342cdb045f46cc49 (patch) | |
tree | 770b34d19855363c3c113ab9a0af9a56d821d887 /OpenSim/Grid/AssetServer/Main.cs | |
download | opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.zip opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.gz opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.bz2 opensim-SC-646bbbc84b8010e0dacbeed5342cdb045f46cc49.tar.xz |
Some work on restructuring the namespaces / project names. Note this doesn't compile yet as not all the code has been changed to use the new namespaces. Am committing it now for feedback on the namespaces.
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Grid/AssetServer/Main.cs | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/OpenSim/Grid/AssetServer/Main.cs b/OpenSim/Grid/AssetServer/Main.cs new file mode 100644 index 0000000..96c9dba --- /dev/null +++ b/OpenSim/Grid/AssetServer/Main.cs | |||
@@ -0,0 +1,337 @@ | |||
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 | |||
29 | using System; | ||
30 | using System.IO; | ||
31 | using System.Text; | ||
32 | using System.Timers; | ||
33 | using System.Net; | ||
34 | using System.Reflection; | ||
35 | using System.Threading; | ||
36 | using libsecondlife; | ||
37 | using OpenSim.Framework; | ||
38 | using OpenSim.Framework.Sims; | ||
39 | using OpenSim.Framework.Console; | ||
40 | using OpenSim.Framework.Types; | ||
41 | using OpenSim.Framework.Interfaces; | ||
42 | using OpenSim.Framework.Utilities; | ||
43 | using OpenSim.Servers; | ||
44 | using Db4objects.Db4o; | ||
45 | using Db4objects.Db4o.Query; | ||
46 | |||
47 | namespace OpenGridServices.AssetServer | ||
48 | { | ||
49 | /// <summary> | ||
50 | /// An asset server | ||
51 | /// </summary> | ||
52 | public class OpenAsset_Main : conscmd_callback | ||
53 | { | ||
54 | private IObjectContainer db; | ||
55 | |||
56 | public static OpenAsset_Main assetserver; | ||
57 | |||
58 | private LogBase m_console; | ||
59 | |||
60 | [STAThread] | ||
61 | public static void Main(string[] args) | ||
62 | { | ||
63 | Console.WriteLine("Starting...\n"); | ||
64 | |||
65 | assetserver = new OpenAsset_Main(); | ||
66 | assetserver.Startup(); | ||
67 | |||
68 | assetserver.Work(); | ||
69 | } | ||
70 | |||
71 | private void Work() | ||
72 | { | ||
73 | m_console.Notice("Enter help for a list of commands"); | ||
74 | |||
75 | while (true) | ||
76 | { | ||
77 | m_console.MainLogPrompt(); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | private OpenAsset_Main() | ||
82 | { | ||
83 | m_console = new LogBase("opengrid-AssetServer-console.log", "OpenAsset", this, false); | ||
84 | OpenSim.Framework.Console.MainLog.Instance = m_console; | ||
85 | } | ||
86 | |||
87 | public void Startup() | ||
88 | { | ||
89 | m_console.Verbose( "Main.cs:Startup() - Setting up asset DB"); | ||
90 | setupDB(); | ||
91 | |||
92 | m_console.Verbose( "Main.cs:Startup() - Starting HTTP process"); | ||
93 | AssetHttpServer httpServer = new AssetHttpServer(8003); | ||
94 | |||
95 | |||
96 | httpServer.AddRestHandler("GET", "/assets/", this.assetGetMethod); | ||
97 | httpServer.AddRestHandler("POST", "/assets/", this.assetPostMethod); | ||
98 | |||
99 | httpServer.Start(); | ||
100 | |||
101 | } | ||
102 | |||
103 | public string assetPostMethod(string requestBody, string path, string param) | ||
104 | { | ||
105 | AssetBase asset = new AssetBase(); | ||
106 | asset.Name = ""; | ||
107 | asset.FullID = new LLUUID(param); | ||
108 | Encoding Windows1252Encoding = Encoding.GetEncoding(1252); | ||
109 | byte[] buffer = Windows1252Encoding.GetBytes(requestBody); | ||
110 | asset.Data = buffer; | ||
111 | AssetStorage store = new AssetStorage(); | ||
112 | store.Data = asset.Data; | ||
113 | store.Name = asset.Name; | ||
114 | store.UUID = asset.FullID; | ||
115 | db.Set(store); | ||
116 | db.Commit(); | ||
117 | return ""; | ||
118 | } | ||
119 | |||
120 | public string assetGetMethod(string request, string path, string param) | ||
121 | { | ||
122 | Console.WriteLine("got a request " + param); | ||
123 | byte[] assetdata = getAssetData(new LLUUID(param), false); | ||
124 | if (assetdata != null) | ||
125 | { | ||
126 | Encoding Windows1252Encoding = Encoding.GetEncoding(1252); | ||
127 | string ret = Windows1252Encoding.GetString(assetdata); | ||
128 | //string ret = System.Text.Encoding.Unicode.GetString(assetdata); | ||
129 | |||
130 | return ret; | ||
131 | |||
132 | } | ||
133 | else | ||
134 | { | ||
135 | return ""; | ||
136 | } | ||
137 | |||
138 | } | ||
139 | |||
140 | public byte[] getAssetData(LLUUID assetID, bool isTexture) | ||
141 | { | ||
142 | bool found = false; | ||
143 | AssetStorage foundAsset = null; | ||
144 | |||
145 | IObjectSet result = db.Get(new AssetStorage(assetID)); | ||
146 | if (result.Count > 0) | ||
147 | { | ||
148 | foundAsset = (AssetStorage)result.Next(); | ||
149 | found = true; | ||
150 | } | ||
151 | |||
152 | if (found) | ||
153 | { | ||
154 | return foundAsset.Data; | ||
155 | } | ||
156 | else | ||
157 | { | ||
158 | return null; | ||
159 | } | ||
160 | } | ||
161 | |||
162 | public void setupDB() | ||
163 | { | ||
164 | bool yapfile = System.IO.File.Exists("assets.yap"); | ||
165 | try | ||
166 | { | ||
167 | db = Db4oFactory.OpenFile("assets.yap"); | ||
168 | OpenSim.Framework.Console.MainLog.Instance.Verbose( "Main.cs:setupDB() - creation"); | ||
169 | } | ||
170 | catch (Exception e) | ||
171 | { | ||
172 | db.Close(); | ||
173 | OpenSim.Framework.Console.MainLog.Instance.Warn("Main.cs:setupDB() - Exception occured"); | ||
174 | OpenSim.Framework.Console.MainLog.Instance.Warn(e.ToString()); | ||
175 | } | ||
176 | if (!yapfile) | ||
177 | { | ||
178 | this.LoadDB(); | ||
179 | } | ||
180 | } | ||
181 | |||
182 | public void LoadDB() | ||
183 | { | ||
184 | try | ||
185 | { | ||
186 | |||
187 | Console.WriteLine("setting up Asset database"); | ||
188 | |||
189 | AssetBase Image = new AssetBase(); | ||
190 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000001"); | ||
191 | Image.Name = "Bricks"; | ||
192 | this.LoadAsset(Image, true, "bricks.jp2"); | ||
193 | AssetStorage store = new AssetStorage(); | ||
194 | store.Data = Image.Data; | ||
195 | store.Name = Image.Name; | ||
196 | store.UUID = Image.FullID; | ||
197 | db.Set(store); | ||
198 | db.Commit(); | ||
199 | |||
200 | Image = new AssetBase(); | ||
201 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000002"); | ||
202 | Image.Name = "Plywood"; | ||
203 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
204 | store = new AssetStorage(); | ||
205 | store.Data = Image.Data; | ||
206 | store.Name = Image.Name; | ||
207 | store.UUID = Image.FullID; | ||
208 | db.Set(store); | ||
209 | db.Commit(); | ||
210 | |||
211 | Image = new AssetBase(); | ||
212 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000003"); | ||
213 | Image.Name = "Rocks"; | ||
214 | this.LoadAsset(Image, true, "rocks.jp2"); | ||
215 | store = new AssetStorage(); | ||
216 | store.Data = Image.Data; | ||
217 | store.Name = Image.Name; | ||
218 | store.UUID = Image.FullID; | ||
219 | db.Set(store); | ||
220 | db.Commit(); | ||
221 | |||
222 | Image = new AssetBase(); | ||
223 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000004"); | ||
224 | Image.Name = "Granite"; | ||
225 | this.LoadAsset(Image, true, "granite.jp2"); | ||
226 | store = new AssetStorage(); | ||
227 | store.Data = Image.Data; | ||
228 | store.Name = Image.Name; | ||
229 | store.UUID = Image.FullID; | ||
230 | db.Set(store); | ||
231 | db.Commit(); | ||
232 | |||
233 | Image = new AssetBase(); | ||
234 | Image.FullID = new LLUUID("00000000-0000-0000-9999-000000000005"); | ||
235 | Image.Name = "Hardwood"; | ||
236 | this.LoadAsset(Image, true, "hardwood.jp2"); | ||
237 | store = new AssetStorage(); | ||
238 | store.Data = Image.Data; | ||
239 | store.Name = Image.Name; | ||
240 | store.UUID = Image.FullID; | ||
241 | db.Set(store); | ||
242 | db.Commit(); | ||
243 | |||
244 | Image = new AssetBase(); | ||
245 | Image.FullID = new LLUUID("00000000-0000-0000-5005-000000000005"); | ||
246 | Image.Name = "Prim Base Texture"; | ||
247 | this.LoadAsset(Image, true, "plywood.jp2"); | ||
248 | store = new AssetStorage(); | ||
249 | store.Data = Image.Data; | ||
250 | store.Name = Image.Name; | ||
251 | store.UUID = Image.FullID; | ||
252 | db.Set(store); | ||
253 | db.Commit(); | ||
254 | |||
255 | Image = new AssetBase(); | ||
256 | Image.FullID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73"); | ||
257 | Image.Name = "Shape"; | ||
258 | this.LoadAsset(Image, false, "base_shape.dat"); | ||
259 | store = new AssetStorage(); | ||
260 | store.Data = Image.Data; | ||
261 | store.Name = Image.Name; | ||
262 | store.UUID = Image.FullID; | ||
263 | db.Set(store); | ||
264 | db.Commit(); | ||
265 | } | ||
266 | catch (Exception e) | ||
267 | { | ||
268 | Console.WriteLine(e.Message); | ||
269 | } | ||
270 | } | ||
271 | |||
272 | private void LoadAsset(AssetBase info, bool image, string filename) | ||
273 | { | ||
274 | |||
275 | |||
276 | string dataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "assets"); //+ folder; | ||
277 | string fileName = Path.Combine(dataPath, filename); | ||
278 | FileInfo fInfo = new FileInfo(fileName); | ||
279 | long numBytes = fInfo.Length; | ||
280 | FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); | ||
281 | byte[] idata = new byte[numBytes]; | ||
282 | BinaryReader br = new BinaryReader(fStream); | ||
283 | idata = br.ReadBytes((int)numBytes); | ||
284 | br.Close(); | ||
285 | fStream.Close(); | ||
286 | info.Data = idata; | ||
287 | //info.loaded=true; | ||
288 | } | ||
289 | |||
290 | /*private GridConfig LoadConfigDll(string dllName) | ||
291 | { | ||
292 | Assembly pluginAssembly = Assembly.LoadFrom(dllName); | ||
293 | GridConfig config = null; | ||
294 | |||
295 | foreach (Type pluginType in pluginAssembly.GetTypes()) | ||
296 | { | ||
297 | if (pluginType.IsPublic) | ||
298 | { | ||
299 | if (!pluginType.IsAbstract) | ||
300 | { | ||
301 | Type typeInterface = pluginType.GetInterface("IGridConfig", true); | ||
302 | |||
303 | if (typeInterface != null) | ||
304 | { | ||
305 | IGridConfig plug = (IGridConfig)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString())); | ||
306 | config = plug.GetConfigObject(); | ||
307 | break; | ||
308 | } | ||
309 | |||
310 | typeInterface = null; | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | pluginAssembly = null; | ||
315 | return config; | ||
316 | }*/ | ||
317 | |||
318 | public void RunCmd(string cmd, string[] cmdparams) | ||
319 | { | ||
320 | switch (cmd) | ||
321 | { | ||
322 | case "help": | ||
323 | m_console.Notice("shutdown - shutdown this asset server (USE CAUTION!)"); | ||
324 | break; | ||
325 | |||
326 | case "shutdown": | ||
327 | m_console.Close(); | ||
328 | Environment.Exit(0); | ||
329 | break; | ||
330 | } | ||
331 | } | ||
332 | |||
333 | public void Show(string ShowWhat) | ||
334 | { | ||
335 | } | ||
336 | } | ||
337 | } | ||