AvatarServiceConnector.cs 13 KB

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