MapImageServiceModule.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Net;
  31. using System.IO;
  32. using System.Timers;
  33. using System.Drawing;
  34. using System.Drawing.Imaging;
  35. using log4net;
  36. using Mono.Addins;
  37. using Nini.Config;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Services.Interfaces;
  42. using OpenSim.Server.Base;
  43. using OpenMetaverse;
  44. using OpenMetaverse.StructuredData;
  45. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.MapImage
  46. {
  47. /// <summary>
  48. /// </summary>
  49. /// <remarks>
  50. /// </remarks>
  51. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  52. public class MapImageServiceModule : ISharedRegionModule
  53. {
  54. private static readonly ILog m_log =
  55. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  56. private bool m_enabled = false;
  57. private IMapImageService m_MapService;
  58. private Dictionary<UUID, Scene> m_scenes = new Dictionary<UUID, Scene>();
  59. private int m_refreshtime = 0;
  60. private int m_lastrefresh = 0;
  61. private System.Timers.Timer m_refreshTimer = new System.Timers.Timer();
  62. #region ISharedRegionModule
  63. public Type ReplaceableInterface { get { return null; } }
  64. public string Name { get { return "MapImageServiceModule"; } }
  65. public void RegionLoaded(Scene scene) { }
  66. public void Close() { }
  67. public void PostInitialise() { }
  68. ///<summary>
  69. ///
  70. ///</summary>
  71. public void Initialise(IConfigSource source)
  72. {
  73. IConfig moduleConfig = source.Configs["Modules"];
  74. if (moduleConfig != null)
  75. {
  76. string name = moduleConfig.GetString("MapImageService", "");
  77. if (name != Name)
  78. return;
  79. }
  80. IConfig config = source.Configs["MapImageService"];
  81. if (config == null)
  82. return;
  83. int refreshminutes = Convert.ToInt32(config.GetString("RefreshTime"));
  84. if (refreshminutes <= 0)
  85. {
  86. m_log.WarnFormat("[MAP IMAGE SERVICE MODULE]: No refresh time given in config. Module disabled.");
  87. return;
  88. }
  89. m_refreshtime = refreshminutes * 60 * 1000; // convert from minutes to ms
  90. string service = config.GetString("LocalServiceModule", string.Empty);
  91. if (service == string.Empty)
  92. {
  93. m_log.WarnFormat("[MAP IMAGE SERVICE MODULE]: No service dll given in config. Unable to proceed.");
  94. return;
  95. }
  96. Object[] args = new Object[] { source };
  97. m_MapService = ServerUtils.LoadPlugin<IMapImageService>(service, args);
  98. if (m_MapService == null)
  99. {
  100. m_log.WarnFormat("[MAP IMAGE SERVICE MODULE]: Unable to load LocalServiceModule from {0}. MapService module disabled. Please fix the configuration.", service);
  101. return;
  102. }
  103. m_refreshTimer.Enabled = true;
  104. m_refreshTimer.AutoReset = true;
  105. m_refreshTimer.Interval = m_refreshtime;
  106. m_refreshTimer.Elapsed += new ElapsedEventHandler(HandleMaptileRefresh);
  107. m_log.InfoFormat("[MAP IMAGE SERVICE MODULE]: enabled with refresh time {0}min and service object {1}",
  108. refreshminutes, service);
  109. m_enabled = true;
  110. }
  111. ///<summary>
  112. ///
  113. ///</summary>
  114. ///<summary>
  115. ///
  116. ///</summary>
  117. public void AddRegion(Scene scene)
  118. {
  119. if (! m_enabled)
  120. return;
  121. // Every shared region module has to maintain an indepedent list of
  122. // currently running regions
  123. lock (m_scenes)
  124. m_scenes[scene.RegionInfo.RegionID] = scene;
  125. scene.EventManager.OnPrimsLoaded += new EventManager.PrimsLoaded(EventManager_OnPrimsLoaded);
  126. }
  127. ///<summary>
  128. ///
  129. ///</summary>
  130. public void RemoveRegion(Scene scene)
  131. {
  132. if (! m_enabled)
  133. return;
  134. lock (m_scenes)
  135. m_scenes.Remove(scene.RegionInfo.RegionID);
  136. }
  137. #endregion ISharedRegionModule
  138. void EventManager_OnPrimsLoaded(Scene s)
  139. {
  140. UploadMapTile(s);
  141. }
  142. ///<summary>
  143. ///
  144. ///</summary>
  145. private void HandleMaptileRefresh(object sender, EventArgs ea)
  146. {
  147. // this approach is a bit convoluted becase we want to wait for the
  148. // first upload to happen on startup but after all the objects are
  149. // loaded and initialized
  150. if (m_lastrefresh > 0 && Util.EnvironmentTickCountSubtract(m_lastrefresh) < m_refreshtime)
  151. return;
  152. m_log.DebugFormat("[MAP IMAGE SERVICE MODULE]: map refresh!");
  153. lock (m_scenes)
  154. {
  155. foreach (IScene scene in m_scenes.Values)
  156. {
  157. try
  158. {
  159. UploadMapTile(scene);
  160. }
  161. catch (Exception ex)
  162. {
  163. m_log.WarnFormat("[MAP IMAGE SERVICE MODULE]: something bad happened {0}", ex.Message);
  164. }
  165. }
  166. }
  167. m_lastrefresh = Util.EnvironmentTickCount();
  168. }
  169. ///<summary>
  170. ///
  171. ///</summary>
  172. private void UploadMapTile(IScene scene)
  173. {
  174. m_log.DebugFormat("[MAP IMAGE SERVICE MODULE]: upload maptile for {0}", scene.RegionInfo.RegionName);
  175. // Create a JPG map tile and upload it to the AddMapTile API
  176. byte[] jpgData = Utils.EmptyBytes;
  177. IMapImageGenerator tileGenerator = scene.RequestModuleInterface<IMapImageGenerator>();
  178. if (tileGenerator == null)
  179. {
  180. m_log.Warn("[MAP IMAGE SERVICE MODULE]: Cannot upload PNG map tile without an ImageGenerator");
  181. return;
  182. }
  183. using (Image mapTile = tileGenerator.CreateMapTile())
  184. {
  185. using (MemoryStream stream = new MemoryStream())
  186. {
  187. mapTile.Save(stream, ImageFormat.Jpeg);
  188. jpgData = stream.ToArray();
  189. }
  190. }
  191. if (jpgData == Utils.EmptyBytes)
  192. {
  193. m_log.WarnFormat("[MAP IMAGE SERVICE MODULE]: Tile image generation failed");
  194. return;
  195. }
  196. string reason = string.Empty;
  197. if (!m_MapService.AddMapTile((int)scene.RegionInfo.RegionLocX, (int)scene.RegionInfo.RegionLocY, jpgData, out reason))
  198. {
  199. m_log.DebugFormat("[MAP IMAGE SERVICE MODULE]: Unable to upload tile image for {0} at {1}-{2}: {3}",
  200. scene.RegionInfo.RegionName, scene.RegionInfo.RegionLocX, scene.RegionInfo.RegionLocY, reason);
  201. }
  202. }
  203. }
  204. }