AvatarServiceConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications;
  35. using OpenSim.Framework.Servers.HttpServer;
  36. using OpenSim.Services.Interfaces;
  37. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  38. using IAvatarService = OpenSim.Services.Interfaces.IAvatarService;
  39. using OpenSim.Server.Base;
  40. using OpenMetaverse;
  41. namespace OpenSim.Services.Connectors
  42. {
  43. public class AvatarServicesConnector : IAvatarService
  44. {
  45. private static readonly ILog m_log =
  46. LogManager.GetLogger(
  47. MethodBase.GetCurrentMethod().DeclaringType);
  48. private string m_ServerURI = String.Empty;
  49. public AvatarServicesConnector()
  50. {
  51. }
  52. public AvatarServicesConnector(string serverURI)
  53. {
  54. m_ServerURI = serverURI.TrimEnd('/');
  55. }
  56. public AvatarServicesConnector(IConfigSource source)
  57. {
  58. Initialise(source);
  59. }
  60. public virtual void Initialise(IConfigSource source)
  61. {
  62. IConfig gridConfig = source.Configs["AvatarService"];
  63. if (gridConfig == null)
  64. {
  65. m_log.Error("[AVATAR CONNECTOR]: AvatarService missing from OpenSim.ini");
  66. throw new Exception("Avatar connector init error");
  67. }
  68. string serviceURI = gridConfig.GetString("AvatarServerURI",
  69. String.Empty);
  70. if (serviceURI == String.Empty)
  71. {
  72. m_log.Error("[AVATAR CONNECTOR]: No Server URI named in section AvatarService");
  73. throw new Exception("Avatar connector init error");
  74. }
  75. m_ServerURI = serviceURI;
  76. }
  77. #region IAvatarService
  78. public AvatarAppearance GetAppearance(UUID userID)
  79. {
  80. AvatarData avatar = GetAvatar(userID);
  81. return avatar.ToAvatarAppearance(userID);
  82. }
  83. public bool SetAppearance(UUID userID, AvatarAppearance appearance)
  84. {
  85. AvatarData avatar = new AvatarData(appearance);
  86. return SetAvatar(userID,avatar);
  87. }
  88. public AvatarData GetAvatar(UUID userID)
  89. {
  90. Dictionary<string, object> sendData = new Dictionary<string, object>();
  91. //sendData["SCOPEID"] = scopeID.ToString();
  92. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  93. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  94. sendData["METHOD"] = "getavatar";
  95. sendData["UserID"] = userID;
  96. string reply = string.Empty;
  97. string reqString = ServerUtils.BuildQueryString(sendData);
  98. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  99. try
  100. {
  101. reply = SynchronousRestFormsRequester.MakeRequest("POST",
  102. m_ServerURI + "/avatar",
  103. reqString);
  104. if (reply == null || (reply != null && reply == string.Empty))
  105. {
  106. m_log.DebugFormat("[AVATAR CONNECTOR]: GetAgent received null or empty reply");
  107. return null;
  108. }
  109. }
  110. catch (Exception e)
  111. {
  112. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server: {0}", e.Message);
  113. }
  114. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  115. AvatarData avatar = null;
  116. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  117. {
  118. if (replyData["result"] is Dictionary<string, object>)
  119. {
  120. avatar = new AvatarData((Dictionary<string, object>)replyData["result"]);
  121. }
  122. }
  123. return avatar;
  124. }
  125. public bool SetAvatar(UUID userID, AvatarData avatar)
  126. {
  127. Dictionary<string, object> sendData = new Dictionary<string, object>();
  128. //sendData["SCOPEID"] = scopeID.ToString();
  129. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  130. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  131. sendData["METHOD"] = "setavatar";
  132. sendData["UserID"] = userID.ToString();
  133. Dictionary<string, object> structData = avatar.ToKeyValuePairs();
  134. foreach (KeyValuePair<string, object> kvp in structData)
  135. sendData[kvp.Key] = kvp.Value.ToString();
  136. string reqString = ServerUtils.BuildQueryString(sendData);
  137. //m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  138. try
  139. {
  140. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  141. m_ServerURI + "/avatar",
  142. reqString);
  143. if (reply != string.Empty)
  144. {
  145. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  146. if (replyData.ContainsKey("result"))
  147. {
  148. if (replyData["result"].ToString().ToLower() == "success")
  149. return true;
  150. else
  151. return false;
  152. }
  153. else
  154. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar reply data does not contain result field");
  155. }
  156. else
  157. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar received empty reply");
  158. }
  159. catch (Exception e)
  160. {
  161. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  162. }
  163. return false;
  164. }
  165. public bool ResetAvatar(UUID userID)
  166. {
  167. Dictionary<string, object> sendData = new Dictionary<string, object>();
  168. //sendData["SCOPEID"] = scopeID.ToString();
  169. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  170. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  171. sendData["METHOD"] = "resetavatar";
  172. sendData["UserID"] = userID.ToString();
  173. string reqString = ServerUtils.BuildQueryString(sendData);
  174. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  175. try
  176. {
  177. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  178. m_ServerURI + "/avatar",
  179. reqString);
  180. if (reply != string.Empty)
  181. {
  182. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  183. if (replyData.ContainsKey("result"))
  184. {
  185. if (replyData["result"].ToString().ToLower() == "success")
  186. return true;
  187. else
  188. return false;
  189. }
  190. else
  191. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  192. }
  193. else
  194. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  195. }
  196. catch (Exception e)
  197. {
  198. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  199. }
  200. return false;
  201. }
  202. public bool SetItems(UUID userID, string[] names, string[] values)
  203. {
  204. Dictionary<string, object> sendData = new Dictionary<string, object>();
  205. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  206. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  207. sendData["METHOD"] = "setitems";
  208. sendData["UserID"] = userID.ToString();
  209. sendData["Names"] = new List<string>(names);
  210. sendData["Values"] = new List<string>(values);
  211. string reqString = ServerUtils.BuildQueryString(sendData);
  212. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  213. try
  214. {
  215. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  216. m_ServerURI + "/avatar",
  217. reqString);
  218. if (reply != string.Empty)
  219. {
  220. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  221. if (replyData.ContainsKey("result"))
  222. {
  223. if (replyData["result"].ToString().ToLower() == "success")
  224. return true;
  225. else
  226. return false;
  227. }
  228. else
  229. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems reply data does not contain result field");
  230. }
  231. else
  232. m_log.DebugFormat("[AVATAR CONNECTOR]: SetItems received empty reply");
  233. }
  234. catch (Exception e)
  235. {
  236. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  237. }
  238. return false;
  239. }
  240. public bool RemoveItems(UUID userID, string[] names)
  241. {
  242. Dictionary<string, object> sendData = new Dictionary<string, object>();
  243. //sendData["SCOPEID"] = scopeID.ToString();
  244. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  245. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  246. sendData["METHOD"] = "removeitems";
  247. sendData["UserID"] = userID.ToString();
  248. sendData["Names"] = new List<string>(names);
  249. string reqString = ServerUtils.BuildQueryString(sendData);
  250. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  251. try
  252. {
  253. string reply = SynchronousRestFormsRequester.MakeRequest("POST",
  254. m_ServerURI + "/avatar",
  255. reqString);
  256. if (reply != string.Empty)
  257. {
  258. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  259. if (replyData.ContainsKey("result"))
  260. {
  261. if (replyData["result"].ToString().ToLower() == "success")
  262. return true;
  263. else
  264. return false;
  265. }
  266. else
  267. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems reply data does not contain result field");
  268. }
  269. else
  270. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems received empty reply");
  271. }
  272. catch (Exception e)
  273. {
  274. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting avatar server: {0}", e.Message);
  275. }
  276. return false;
  277. }
  278. #endregion
  279. }
  280. }