1
0

MapImageServicesConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 == String.Empty)
  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, out string reason)
  80. {
  81. reason = string.Empty;
  82. int tickstart = Util.EnvironmentTickCount();
  83. Dictionary<string, object> sendData = new Dictionary<string, object>();
  84. sendData["X"] = x.ToString();
  85. sendData["Y"] = y.ToString();
  86. string reqString = ServerUtils.BuildQueryString(sendData);
  87. string uri = m_ServerURI + "/removemap";
  88. try
  89. {
  90. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  91. uri,
  92. reqString,
  93. m_Auth);
  94. if (reply != string.Empty)
  95. {
  96. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  97. if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
  98. {
  99. return true;
  100. }
  101. else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
  102. {
  103. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
  104. reason = replyData["Message"].ToString();
  105. return false;
  106. }
  107. else if (!replyData.ContainsKey("Result"))
  108. {
  109. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
  110. }
  111. else
  112. {
  113. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
  114. reason = "Unexpected result " + replyData["Result"].ToString();
  115. }
  116. }
  117. else
  118. {
  119. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
  120. }
  121. }
  122. catch (Exception e)
  123. {
  124. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
  125. }
  126. finally
  127. {
  128. // This just dumps a warning for any operation that takes more than 100 ms
  129. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  130. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
  131. }
  132. return false;
  133. }
  134. public bool RemoveMapTile(int x, int y, UUID scopeID, out string reason)
  135. {
  136. reason = string.Empty;
  137. int tickstart = Util.EnvironmentTickCount();
  138. Dictionary<string, object> sendData = new Dictionary<string, object>();
  139. sendData["X"] = x.ToString();
  140. sendData["Y"] = y.ToString();
  141. sendData["SCOPE"] = scopeID.ToString();
  142. string reqString = ServerUtils.BuildQueryString(sendData);
  143. string uri = m_ServerURI + "/removemap";
  144. try
  145. {
  146. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  147. uri,
  148. reqString);
  149. if (reply != string.Empty)
  150. {
  151. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  152. if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
  153. {
  154. return true;
  155. }
  156. else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
  157. {
  158. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Delete failed: {0}", replyData["Message"].ToString());
  159. reason = replyData["Message"].ToString();
  160. return false;
  161. }
  162. else if (!replyData.ContainsKey("Result"))
  163. {
  164. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: reply data does not contain result field");
  165. }
  166. else
  167. {
  168. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: unexpected result {0}", replyData["Result"].ToString());
  169. reason = "Unexpected result " + replyData["Result"].ToString();
  170. }
  171. }
  172. else
  173. {
  174. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Map post received null reply");
  175. }
  176. }
  177. catch (Exception e)
  178. {
  179. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: Exception when contacting map server at {0}: {1}", uri, e.Message);
  180. }
  181. finally
  182. {
  183. // This just dumps a warning for any operation that takes more than 100 ms
  184. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  185. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile deleted in {0}ms", tickdiff);
  186. }
  187. return false;
  188. }
  189. public bool AddMapTile(int x, int y, byte[] jpgData, UUID scopeID, out string reason)
  190. {
  191. reason = string.Empty;
  192. int tickstart = Util.EnvironmentTickCount();
  193. Dictionary<string, object> sendData = new Dictionary<string, object>();
  194. sendData["X"] = x.ToString();
  195. sendData["Y"] = y.ToString();
  196. sendData["SCOPE"] = scopeID.ToString();
  197. sendData["TYPE"] = "image/jpeg";
  198. sendData["DATA"] = Convert.ToBase64String(jpgData);
  199. string reqString = ServerUtils.BuildQueryString(sendData);
  200. string uri = m_ServerURI + "/map";
  201. try
  202. {
  203. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  204. uri,
  205. reqString,
  206. 30,
  207. m_Auth);
  208. if (reply != string.Empty)
  209. {
  210. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  211. if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "success"))
  212. {
  213. return true;
  214. }
  215. else if (replyData.ContainsKey("Result") && (replyData["Result"].ToString().ToLower() == "failure"))
  216. {
  217. reason = string.Format("Map post to {0} failed: {1}", uri, replyData["Message"].ToString());
  218. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  219. return false;
  220. }
  221. else if (!replyData.ContainsKey("Result"))
  222. {
  223. reason = string.Format("Reply data from {0} does not contain result field", uri);
  224. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  225. }
  226. else
  227. {
  228. reason = string.Format("Unexpected result {0} from {1}" + replyData["Result"].ToString(), uri);
  229. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  230. }
  231. }
  232. else
  233. {
  234. reason = string.Format("Map post received null reply from {0}", uri);
  235. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  236. }
  237. }
  238. catch (Exception e)
  239. {
  240. reason = string.Format("Exception when posting to map server at {0}: {1}", uri, e.Message);
  241. m_log.WarnFormat("[MAP IMAGE CONNECTOR]: {0}", reason);
  242. }
  243. finally
  244. {
  245. // This just dumps a warning for any operation that takes more than 100 ms
  246. int tickdiff = Util.EnvironmentTickCountSubtract(tickstart);
  247. m_log.DebugFormat("[MAP IMAGE CONNECTOR]: map tile upload time {0}ms", tickdiff);
  248. }
  249. return false;
  250. }
  251. public byte[] GetMapTile(string fileName, UUID scopeID, out string format)
  252. {
  253. format = string.Empty;
  254. new Exception("GetMapTile method not Implemented");
  255. return null;
  256. }
  257. }
  258. }