MapImageServicesConnector.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Communications;
  37. using OpenSim.Framework.ServiceAuth;
  38. using OpenSim.Server.Base;
  39. using OpenSim.Services.Interfaces;
  40. using OpenMetaverse;
  41. using OpenMetaverse.StructuredData;
  42. namespace OpenSim.Services.Connectors
  43. {
  44. public class MapImageServicesConnector : BaseServiceConnector, IMapImageService
  45. {
  46. private static readonly ILog m_log =
  47. LogManager.GetLogger(
  48. MethodBase.GetCurrentMethod().DeclaringType);
  49. private string m_ServerURI = String.Empty;
  50. public MapImageServicesConnector()
  51. {
  52. }
  53. public MapImageServicesConnector(string serverURI)
  54. {
  55. m_ServerURI = serverURI.TrimEnd('/');
  56. }
  57. public MapImageServicesConnector(IConfigSource source)
  58. {
  59. Initialise(source);
  60. }
  61. public virtual void Initialise(IConfigSource source)
  62. {
  63. IConfig config = source.Configs["MapImageService"];
  64. if (config == null)
  65. {
  66. m_log.Error("[MAP IMAGE CONNECTOR]: MapImageService missing");
  67. throw new Exception("MapImage connector init error");
  68. }
  69. string serviceURI = config.GetString("MapImageServerURI",
  70. String.Empty);
  71. if (serviceURI == String.Empty)
  72. {
  73. m_log.Error("[MAP IMAGE CONNECTOR]: No Server URI named in section MapImageService");
  74. throw new Exception("MapImage connector init error");
  75. }
  76. m_ServerURI = serviceURI;
  77. m_ServerURI = serviceURI.TrimEnd('/');
  78. base.Initialise(source, "MapImageService");
  79. }
  80. public bool RemoveMapTile(int x, int y, out string reason)
  81. {
  82. reason = string.Empty;
  83. int tickstart = Util.EnvironmentTickCount();
  84. Dictionary<string, object> sendData = new Dictionary<string, object>();
  85. sendData["X"] = x.ToString();
  86. sendData["Y"] = y.ToString();
  87. string reqString = ServerUtils.BuildQueryString(sendData);
  88. string uri = m_ServerURI + "/removemap";
  89. try
  90. {
  91. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  92. uri,
  93. reqString,
  94. m_Auth);
  95. if (reply != string.Empty)
  96. {
  97. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  98. if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
  99. {
  100. return true;
  101. }
  102. else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
  103. {
  104. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
  105. reason = replyData["Message"].ToString();
  106. return false;
  107. }
  108. else if (!replyData.ContainsKey("Result"))
  109. {
  110. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
  111. }
  112. else
  113. {
  114. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
  115. reason = "Unexpected result " + replyData["Result"].ToString();
  116. }
  117. }
  118. else
  119. {
  120. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
  121. }
  122. }
  123. catch (Exception e)
  124. {
  125. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
  126. }
  127. finally
  128. {
  129. // This just dumps a warning for any operation that takes more than 100 ms
  130. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  131. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
  132. }
  133. return false;
  134. }
  135. public bool AddMapTile(int x, int y, byte[] jpgData, out string reason)
  136. {
  137. reason = string.Empty;
  138. int tickstart = Util.EnvironmentTickCount();
  139. Dictionary<string, object> sendData = new Dictionary<string, object>();
  140. sendData["X"] = x.ToString();
  141. sendData["Y"] = y.ToString();
  142. sendData["TYPE"] = "image/jpeg";
  143. sendData["DATA"] = Convert.ToBase64String(jpgData);
  144. string reqString = ServerUtils.BuildQueryString(sendData);
  145. string uri = m_ServerURI + "/map";
  146. try
  147. {
  148. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  149. uri,
  150. reqString,
  151. m_Auth);
  152. if (reply != string.Empty)
  153. {
  154. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  155. if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
  156. {
  157. return true;
  158. }
  159. else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
  160. {
  161. reason = string.Format("Map post to {0} failed: {1}", uri, replyData["Message"].ToString());
  162. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  163. return false;
  164. }
  165. else if (!replyData.ContainsKey("Result"))
  166. {
  167. reason = string.Format("Reply data from {0} does not contain result field", uri);
  168. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  169. }
  170. else
  171. {
  172. reason = string.Format("Unexpected result {0} from {1}" + replyData["Result"].ToString(), uri);
  173. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  174. }
  175. }
  176. else
  177. {
  178. reason = string.Format("Map post received null reply from {0}", uri);
  179. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  180. }
  181. }
  182. catch (Exception e)
  183. {
  184. reason = string.Format("Exception when posting to map server at {0}: {1}", uri, e.Message);
  185. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  186. }
  187. finally
  188. {
  189. // This just dumps a warning for any operation that takes more than 100 ms
  190. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  191. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile uploaded in {0}ms", tickdiff);
  192. }
  193. return false;
  194. }
  195. public byte[] GetMapTile(string fileName, out string format)
  196. {
  197. format = string.Empty;
  198. new Exception("GetMapTile method not Implemented");
  199. return null;
  200. }
  201. }
  202. }