diff options
Diffstat (limited to '')
-rw-r--r-- | OpenGridServices/OpenGridServices.AssetServer/Main.cs | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/OpenGridServices/OpenGridServices.AssetServer/Main.cs b/OpenGridServices/OpenGridServices.AssetServer/Main.cs new file mode 100644 index 0000000..adbf18b --- /dev/null +++ b/OpenGridServices/OpenGridServices.AssetServer/Main.cs | |||
@@ -0,0 +1,337 @@ | |||
1 | /* | ||
2 | Copyright (c) OpenSim project, http://osgrid.org/ | ||
3 | |||
4 | |||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions are met: | ||
9 | * * Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * * Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * * Neither the name of the <organization> nor the | ||
15 | * names of its contributors may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY | ||
19 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY | ||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
26 | * (INCLUDING 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.IO; | ||
32 | using System.Text; | ||
33 | using System.Timers; | ||
34 | using System.Net; | ||
35 | using System.Reflection; | ||
36 | using System.Threading; | ||
37 | using libsecondlife; | ||
38 | using OpenSim.Framework; | ||
39 | using OpenSim.Framework.Sims; | ||
40 | using OpenSim.Framework.Console; | ||
41 | using OpenSim.Framework.Types; | ||
42 | using OpenSim.Framework.Interfaces; | ||
43 | using OpenSim.Framework.Utilities; | ||
44 | using OpenSim.Servers; | ||
45 | using Db4objects.Db4o; | ||
46 | using Db4objects.Db4o.Query; | ||
47 | |||
48 | namespace OpenGridServices.AssetServer | ||
49 | { | ||
50 | /// <summary> | ||
51 | /// </summary> | ||
52 | public class OpenAsset_Main : BaseServer, conscmd_callback | ||
53 | { | ||
54 | private IObjectContainer db; | ||
55 | |||
56 | public static OpenAsset_Main assetserver; | ||
57 | |||
58 | private ConsoleBase 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.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "\nEnter help for a list of commands\n"); | ||
74 | |||
75 | while (true) | ||
76 | { | ||
77 | m_console.MainConsolePrompt(); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | private OpenAsset_Main() | ||
82 | { | ||
83 | m_console = new ConsoleBase("opengrid-AssetServer-console.log", "OpenGrid", this, false); | ||
84 | MainConsole.Instance = m_console; | ||
85 | } | ||
86 | |||
87 | public void Startup() | ||
88 | { | ||
89 | m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:Startup() - Setting up asset DB"); | ||
90 | setupDB(); | ||
91 | |||
92 | m_console.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "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.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Main.cs:setupDB() - creation"); | ||
169 | } | ||
170 | catch (Exception e) | ||
171 | { | ||
172 | db.Close(); | ||
173 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, "Main.cs:setupDB() - Exception occured"); | ||
174 | OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.MEDIUM, 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.WriteLine(OpenSim.Framework.Console.LogPriority.HIGH, "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 | } | ||