clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 1 /*
2 * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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 using System;
29 using System.Drawing;
30 using System.IO;
31 using System.Net;
32 using Nini.Config;
33 using OpenMetaverse;
34 using OpenMetaverse.Imaging;
35 using OpenSim.Region.CoreModules.Scripting.DynamicTexture;
36 using OpenSim.Region.Framework.Interfaces;
37 using OpenSim.Region.Framework.Scenes;
38 using log4net;
39 using System.Reflection;
40 using Mono.Addins;
41  
42 namespace OpenSim.Region.CoreModules.Scripting.LoadImageURL
43 {
44 [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LoadImageURLModule")]
45 public class LoadImageURLModule : ISharedRegionModule, IDynamicTextureRender
46 {
47 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
48  
49 private string m_name = "LoadImageURL";
50 private Scene m_scene;
51 private IDynamicTextureManager m_textureManager;
52  
53 private string m_proxyurl = "";
54 private string m_proxyexcepts = "";
55  
56 #region IDynamicTextureRender Members
57  
58 public string GetName()
59 {
60 return m_name;
61 }
62  
63 public string GetContentType()
64 {
65 return ("image");
66 }
67  
68 public bool SupportsAsynchronous()
69 {
70 return true;
71 }
72  
73 // public bool AlwaysIdenticalConversion(string bodyData, string extraParams)
74 // {
75 // // We don't support conversion of body data.
76 // return false;
77 // }
78  
79 public IDynamicTexture ConvertUrl(string url, string extraParams)
80 {
81 return null;
82 }
83  
84 public IDynamicTexture ConvertData(string bodyData, string extraParams)
85 {
86 return null;
87 }
88  
89 public bool AsyncConvertUrl(UUID id, string url, string extraParams)
90 {
91 MakeHttpRequest(url, id);
92 return true;
93 }
94  
95 public bool AsyncConvertData(UUID id, string bodyData, string extraParams)
96 {
97 return false;
98 }
99  
100 public void GetDrawStringSize(string text, string fontName, int fontSize,
101 out double xSize, out double ySize)
102 {
103 xSize = 0;
104 ySize = 0;
105 }
106  
107 #endregion
108  
109 #region ISharedRegionModule Members
110  
111 public void Initialise(IConfigSource config)
112 {
113 m_proxyurl = config.Configs["Startup"].GetString("HttpProxy");
114 m_proxyexcepts = config.Configs["Startup"].GetString("HttpProxyExceptions");
115 }
116  
117 public void PostInitialise()
118 {
119 }
120  
121 public void AddRegion(Scene scene)
122 {
123 if (m_scene == null)
124 m_scene = scene;
125  
126 }
127  
128 public void RemoveRegion(Scene scene)
129 {
130 }
131  
132 public void RegionLoaded(Scene scene)
133 {
134 if (m_textureManager == null && m_scene == scene)
135 {
136 m_textureManager = m_scene.RequestModuleInterface<IDynamicTextureManager>();
137 if (m_textureManager != null)
138 {
139 m_textureManager.RegisterRender(GetContentType(), this);
140 }
141 }
142 }
143  
144 public void Close()
145 {
146 }
147  
148 public string Name
149 {
150 get { return m_name; }
151 }
152  
153 public Type ReplaceableInterface
154 {
155 get { return null; }
156 }
157  
158 #endregion
159  
160 private void MakeHttpRequest(string url, UUID requestID)
161 {
162 WebRequest request = HttpWebRequest.Create(url);
163  
164 if (m_proxyurl != null && m_proxyurl.Length > 0)
165 {
166 if (m_proxyexcepts != null && m_proxyexcepts.Length > 0)
167 {
168 string[] elist = m_proxyexcepts.Split(';');
169 request.Proxy = new WebProxy(m_proxyurl, true, elist);
170 }
171 else
172 {
173 request.Proxy = new WebProxy(m_proxyurl, true);
174 }
175 }
176  
177 RequestState state = new RequestState((HttpWebRequest) request, requestID);
178 // IAsyncResult result = request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state);
179 request.BeginGetResponse(new AsyncCallback(HttpRequestReturn), state);
180  
181 TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
182 state.TimeOfRequest = (int) t.TotalSeconds;
183 }
184  
185 private void HttpRequestReturn(IAsyncResult result)
186 {
187 if (m_textureManager == null)
188 {
189 m_log.WarnFormat("[LOADIMAGEURLMODULE]: No texture manager. Can't function.");
190 return;
191 }
192  
193 RequestState state = (RequestState) result.AsyncState;
194 WebRequest request = (WebRequest) state.Request;
195 Stream stream = null;
196 byte[] imageJ2000 = new byte[0];
197 Size newSize = new Size(0, 0);
198  
199 try
200 {
201 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
202 if (response != null && response.StatusCode == HttpStatusCode.OK)
203 {
204 stream = response.GetResponseStream();
205 if (stream != null)
206 {
207 try
208 {
209 Bitmap image = new Bitmap(stream);
210  
211 // TODO: make this a bit less hard coded
212 if ((image.Height < 64) && (image.Width < 64))
213 {
214 newSize.Width = 32;
215 newSize.Height = 32;
216 }
217 else if ((image.Height < 128) && (image.Width < 128))
218 {
219 newSize.Width = 64;
220 newSize.Height = 64;
221 }
222 else if ((image.Height < 256) && (image.Width < 256))
223 {
224 newSize.Width = 128;
225 newSize.Height = 128;
226 }
227 else if ((image.Height < 512 && image.Width < 512))
228 {
229 newSize.Width = 256;
230 newSize.Height = 256;
231 }
232 else if ((image.Height < 1024 && image.Width < 1024))
233 {
234 newSize.Width = 512;
235 newSize.Height = 512;
236 }
237 else
238 {
239 newSize.Width = 1024;
240 newSize.Height = 1024;
241 }
242  
243 using (Bitmap resize = new Bitmap(image, newSize))
244 {
245 imageJ2000 = OpenJPEG.EncodeFromImage(resize, true);
246 }
247 }
248 catch (Exception)
249 {
250 m_log.Error("[LOADIMAGEURLMODULE]: OpenJpeg Conversion Failed. Empty byte data returned!");
251 }
252 }
253 else
254 {
255 m_log.WarnFormat("[LOADIMAGEURLMODULE] No data returned");
256 }
257 }
258 }
259 catch (WebException)
260 {
261 }
262 finally
263 {
264 if (stream != null)
265 {
266 stream.Close();
267 }
268 }
269  
270 m_log.DebugFormat("[LOADIMAGEURLMODULE]: Returning {0} bytes of image data for request {1}",
271 imageJ2000.Length, state.RequestID);
272  
273 m_textureManager.ReturnData(
274 state.RequestID,
275 new OpenSim.Region.CoreModules.Scripting.DynamicTexture.DynamicTexture(
276 request.RequestUri, null, imageJ2000, newSize, false));
277 }
278  
279 #region Nested type: RequestState
280  
281 public class RequestState
282 {
283 public HttpWebRequest Request = null;
284 public UUID RequestID = UUID.Zero;
285 public int TimeOfRequest = 0;
286  
287 public RequestState(HttpWebRequest request, UUID requestID)
288 {
289 Request = request;
290 RequestID = requestID;
291 }
292 }
293  
294 #endregion
295 }
296 }