34 using OpenMetaverse.Imaging;
35 using OpenSim.Framework;
36 using OpenSim.Region.CoreModules.Scripting.DynamicTexture;
37 using OpenSim.Region.Framework.Interfaces;
38 using OpenSim.Region.Framework.Scenes;
40 using System.Reflection;
45 [Extension(Path =
"/OpenSim/RegionModules", NodeName =
"RegionModule", Id =
"LoadImageURLModule")]
48 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
50 private string m_name =
"LoadImageURL";
51 private Scene m_scene;
55 private string m_proxyurl =
"";
56 private string m_proxyexcepts =
"";
58 #region IDynamicTextureRender Members
93 return MakeHttpRequest(url,
id);
102 out
double xSize, out
double ySize)
110 #region ISharedRegionModule Members
114 m_outboundUrlFilter =
new OutboundUrlFilter(
"Script dynamic texture image module", config);
115 m_proxyurl = config.Configs[
"Startup"].GetString(
"HttpProxy");
116 m_proxyexcepts = config.Configs[
"Startup"].GetString(
"HttpProxyExceptions");
136 if (m_textureManager == null && m_scene == scene)
139 if (m_textureManager != null)
141 m_textureManager.RegisterRender(GetContentType(),
this);
152 get {
return m_name; }
155 public Type ReplaceableInterface
162 private bool MakeHttpRequest(
string url, UUID requestID)
164 if (!m_outboundUrlFilter.CheckAllowed(
new Uri(url)))
167 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
168 request.AllowAutoRedirect =
false;
170 if (!
string.IsNullOrEmpty(m_proxyurl))
172 if (!
string.IsNullOrEmpty(m_proxyexcepts))
174 string[] elist = m_proxyexcepts.Split(
';');
175 request.Proxy =
new WebProxy(m_proxyurl,
true, elist);
179 request.Proxy =
new WebProxy(m_proxyurl,
true);
183 RequestState state =
new RequestState(request, requestID);
185 request.BeginGetResponse(
new AsyncCallback(HttpRequestReturn), state);
187 TimeSpan t = (DateTime.UtcNow -
new DateTime(1970, 1, 1));
188 state.TimeOfRequest = (int) t.TotalSeconds;
193 private void HttpRequestReturn(IAsyncResult result)
195 if (m_textureManager == null)
197 m_log.WarnFormat(
"[LOADIMAGEURLMODULE]: No texture manager. Can't function.");
201 RequestState state = (RequestState) result.AsyncState;
202 WebRequest request = (WebRequest) state.Request;
203 Stream stream = null;
204 byte[] imageJ2000 =
new byte[0];
206 HttpWebResponse response = null;
210 response = (HttpWebResponse)request.EndGetResponse(result);
211 if (response != null && response.StatusCode == HttpStatusCode.OK)
213 stream = response.GetResponseStream();
218 Bitmap image =
new Bitmap(stream);
221 if ((image.Height < 64) && (image.Width < 64))
226 else if ((image.Height < 128) && (image.Width < 128))
231 else if ((image.Height < 256) && (image.Width < 256))
234 newSize.Height = 128;
236 else if ((image.Height < 512 && image.Width < 512))
239 newSize.Height = 256;
241 else if ((image.Height < 1024 && image.Width < 1024))
244 newSize.Height = 512;
248 newSize.Width = 1024;
249 newSize.Height = 1024;
252 using (Bitmap resize =
new Bitmap(image, newSize))
254 imageJ2000 = OpenJPEG.EncodeFromImage(resize,
true);
259 m_log.Error(
"[LOADIMAGEURLMODULE]: OpenJpeg Conversion Failed. Empty byte data returned!");
264 m_log.WarnFormat(
"[LOADIMAGEURLMODULE] No data returned");
276 if (response != null)
278 if (response.StatusCode == HttpStatusCode.MovedPermanently
279 || response.StatusCode == HttpStatusCode.Found
280 || response.StatusCode == HttpStatusCode.SeeOther
281 || response.StatusCode == HttpStatusCode.TemporaryRedirect)
283 string redirectedUrl = response.Headers[
"Location"];
285 MakeHttpRequest(redirectedUrl, state.RequestID);
289 m_log.DebugFormat(
"[LOADIMAGEURLMODULE]: Returning {0} bytes of image data for request {1}",
290 imageJ2000.Length, state.RequestID);
292 m_textureManager.ReturnData(
295 request.RequestUri, null, imageJ2000, newSize,
false));
302 #region Nested type: RequestState
306 public HttpWebRequest Request = null;
307 public UUID RequestID = UUID.Zero;
308 public int TimeOfRequest = 0;
313 RequestID = requestID;
bool SupportsAsynchronous()
IDynamicTexture ConvertData(string bodyData, string extraParams)
void Initialise(IConfigSource config)
This is called to initialize the region module. For shared modules, this is called exactly once...
void AddRegion(Scene scene)
This is called whenever a Scene is added. For shared modules, this can happen several times...
void GetDrawStringSize(string text, string fontName, int fontSize, out double xSize, out double ySize)
IDynamicTexture ConvertUrl(string url, string extraParams)
void RegionLoaded(Scene scene)
This will be called once for every scene loaded. In a shared module this will be multiple times in on...
Interactive OpenSim region server
void PostInitialise()
This is called exactly once after all the shared region-modules have been instanciated and IRegionMod...
void RemoveRegion(Scene scene)
This is called whenever a Scene is removed. For shared modules, this can happen several times...
bool AsyncConvertData(UUID id, string bodyData, string extraParams)
bool AsyncConvertUrl(UUID id, string url, string extraParams)
void Close()
This is the inverse to Initialise. After a Close(), this instance won't be usable anymore...
RequestState(HttpWebRequest request, UUID requestID)