1
0

MapImageServicesConnector.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Net;
  32. using System.Reflection;
  33. using Nini.Config;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.ServiceAuth;
  37. using OpenSim.Server.Base;
  38. using OpenSim.Services.Interfaces;
  39. using OpenMetaverse;
  40. using OpenMetaverse.StructuredData;
  41. namespace OpenSim.Services.Connectors
  42. {
  43. public class MapImageServicesConnector : BaseServiceConnector, IMapImageService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. private string m_ServerURI = String.Empty;
  49. public MapImageServicesConnector()
  50. {
  51. }
  52. public MapImageServicesConnector(string serverURI)
  53. {
  54. m_ServerURI = serverURI.TrimEnd('/');
  55. }
  56. public MapImageServicesConnector(IConfigSource source)
  57. {
  58. Initialise(source);
  59. }
  60. public virtual void Initialise(IConfigSource source)
  61. {
  62. IConfig config = source.Configs["MapImageService"];
  63. if (config == null)
  64. {
  65. m_log.Error("[MAP IMAGE CONNECTOR]: MapImageService missing");
  66. throw new Exception("MapImage connector init error");
  67. }
  68. string serviceURI = config.GetString("MapImageServerURI",
  69. String.Empty);
  70. if (serviceURI.Length == 0)
  71. {
  72. m_log.Error("[MAP IMAGE CONNECTOR]: No Server URI named in section MapImageService");
  73. throw new Exception("MapImage connector init error");
  74. }
  75. m_ServerURI = serviceURI;
  76. m_ServerURI = serviceURI.TrimEnd('/');
  77. base.Initialise(source, "MapImageService");
  78. }
  79. public bool RemoveMapTile(int x, int y, UUID scopeID, out string reason)
  80. {
  81. reason = string.Empty;
  82. string reqString;
  83. if (scopeID.IsNotZero())
  84. {
  85. reqString = ServerUtils.BuildQueryString(
  86. new Dictionary<string, object>()
  87. {
  88. {"X" , x.ToString() },
  89. {"Y" , y.ToString() },
  90. { "SCOPE" , scopeID.ToString() },
  91. }
  92. );
  93. }
  94. else
  95. {
  96. reqString = ServerUtils.BuildQueryString(
  97. new Dictionary<string, object>()
  98. {
  99. {"X" , x.ToString() },
  100. {"Y" , y.ToString() },
  101. { "SCOPE" , scopeID.ToString() },
  102. }
  103. );
  104. }
  105. try
  106. {
  107. string reply = SynchronousRestFormsRequester.MakeRequest("POST", m_ServerURI + "/map", reqString, 10, null, false);
  108. if (reply.Length > 0)
  109. {
  110. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  111. if(replyData.TryGetValue("Result", out object resultobj))
  112. {
  113. string res = resultobj as string;
  114. if(string.IsNullOrEmpty(res))
  115. {
  116. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unknown result field");
  117. return false;
  118. }
  119. else if (res.Equals("success", StringComparison.InvariantCultureIgnoreCase))
  120. return true;
  121. else if (res.Equals("failure", StringComparison.InvariantCultureIgnoreCase))
  122. {
  123. reason = replyData["Message"].ToString();
  124. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: RemoveMapTile failed: {0}", reason);
  125. return false;
  126. }
  127. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: RemoveMapTile unknown result field contents");
  128. return false;
  129. }
  130. }
  131. else
  132. {
  133. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: RemoveMapTile reply data does not contain result field");
  134. }
  135. }
  136. catch (Exception e)
  137. {
  138. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: RemoveMapTile Exception at {0}/map: {1}", m_ServerURI, e.Message);
  139. }
  140. return false;
  141. }
  142. public bool AddMapTile(int x, int y, byte[] jpgData, UUID scopeID, out string reason)
  143. {
  144. reason = string.Empty;
  145. int tickstart = Util.EnvironmentTickCount();
  146. string reqString;
  147. if (scopeID.IsNotZero())
  148. {
  149. reqString = ServerUtils.BuildQueryString(
  150. new Dictionary<string, object>()
  151. {
  152. {"X" , x.ToString() },
  153. {"Y" , y.ToString() },
  154. { "SCOPE" , scopeID.ToString() },
  155. { "TYPE" , "image/jpeg" },
  156. { "DATA" , Convert.ToBase64String(jpgData) }
  157. }
  158. );
  159. }
  160. else
  161. {
  162. reqString = ServerUtils.BuildQueryString(
  163. new Dictionary<string, object>()
  164. {
  165. {"X" , x.ToString() },
  166. {"Y" , y.ToString() },
  167. { "TYPE" , "image/jpeg" },
  168. { "DATA" , Convert.ToBase64String(jpgData) }
  169. }
  170. );
  171. }
  172. try
  173. {
  174. string reply = SynchronousRestFormsRequester.MakeRequest("POST", m_ServerURI + "/map", reqString, 10, m_Auth, false);
  175. if (reply.Length > 0)
  176. {
  177. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  178. if (replyData.TryGetValue("Result", out object resultobj))
  179. {
  180. string res = resultobj as string;
  181. if (string.IsNullOrEmpty(res))
  182. {
  183. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile unknown result field");
  184. return false;
  185. }
  186. else if (res.Equals("success", StringComparison.InvariantCultureIgnoreCase))
  187. return true;
  188. else if (res.Equals("failure", StringComparison.InvariantCultureIgnoreCase))
  189. {
  190. reason = replyData["Message"].ToString();
  191. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile failed: {0}", reason);
  192. return false;
  193. }
  194. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile unknown result field contents");
  195. return false;
  196. }
  197. }
  198. else
  199. {
  200. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile reply data does not contain result field");
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile Exception at {0}/map: {1}", m_ServerURI, e.Message);
  206. }
  207. finally
  208. {
  209. // This just dumps a warning for any operation that takes more than 100 ms
  210. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  211. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: AddMapTile {1} Bytes in {0}ms", tickdiff, jpgData.Length);
  212. }
  213. return false;
  214. }
  215. public byte[] GetMapTile(string fileName, UUID scopeID, out string format)
  216. {
  217. format = string.Empty;
  218. new Exception("GetMapTile method not Implemented");
  219. return null;
  220. }
  221. }
  222. }