1
0

AvatarServicesConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. string uri = m_ServerURI + "/avatar";
  98. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  99. try
  100. {
  101. reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString);
  102. if (reply == null || (reply != null && reply == string.Empty))
  103. {
  104. m_log.DebugFormat("[AVATAR CONNECTOR]: GetAgent received null or empty reply");
  105. return null;
  106. }
  107. }
  108. catch (Exception e)
  109. {
  110. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  111. }
  112. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  113. AvatarData avatar = null;
  114. if ((replyData != null) && replyData.ContainsKey("result") && (replyData["result"] != null))
  115. {
  116. if (replyData["result"] is Dictionary<string, object>)
  117. {
  118. avatar = new AvatarData((Dictionary<string, object>)replyData["result"]);
  119. }
  120. }
  121. return avatar;
  122. }
  123. public bool SetAvatar(UUID userID, AvatarData avatar)
  124. {
  125. Dictionary<string, object> sendData = new Dictionary<string, object>();
  126. //sendData["SCOPEID"] = scopeID.ToString();
  127. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  128. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  129. sendData["METHOD"] = "setavatar";
  130. sendData["UserID"] = userID.ToString();
  131. Dictionary<string, object> structData = avatar.ToKeyValuePairs();
  132. foreach (KeyValuePair<string, object> kvp in structData)
  133. sendData[kvp.Key] = kvp.Value.ToString();
  134. string reqString = ServerUtils.BuildQueryString(sendData);
  135. string uri = m_ServerURI + "/avatar";
  136. //m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  137. try
  138. {
  139. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString);
  140. if (reply != string.Empty)
  141. {
  142. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  143. if (replyData.ContainsKey("result"))
  144. {
  145. if (replyData["result"].ToString().ToLower() == "success")
  146. return true;
  147. else
  148. return false;
  149. }
  150. else
  151. {
  152. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar reply data does not contain result field");
  153. }
  154. }
  155. else
  156. {
  157. m_log.DebugFormat("[AVATAR CONNECTOR]: SetAvatar received empty reply");
  158. }
  159. }
  160. catch (Exception e)
  161. {
  162. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  163. }
  164. return false;
  165. }
  166. public bool ResetAvatar(UUID userID)
  167. {
  168. Dictionary<string, object> sendData = new Dictionary<string, object>();
  169. //sendData["SCOPEID"] = scopeID.ToString();
  170. sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString();
  171. sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString();
  172. sendData["METHOD"] = "resetavatar";
  173. sendData["UserID"] = userID.ToString();
  174. string reqString = ServerUtils.BuildQueryString(sendData);
  175. string uri = m_ServerURI + "/avatar";
  176. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  177. try
  178. {
  179. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, 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 presence server at {0}: {1}", uri, 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. string uri = m_ServerURI + "/avatar";
  213. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  214. try
  215. {
  216. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, 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 presence server at {0}: {1}", uri, 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. string uri = m_ServerURI + "/avatar";
  250. // m_log.DebugFormat("[AVATAR CONNECTOR]: queryString = {0}", reqString);
  251. try
  252. {
  253. string reply = SynchronousRestFormsRequester.MakeRequest("POST", uri, reqString);
  254. if (reply != string.Empty)
  255. {
  256. Dictionary<string, object> replyData = ServerUtils.ParseXmlResponse(reply);
  257. if (replyData.ContainsKey("result"))
  258. {
  259. if (replyData["result"].ToString().ToLower() == "success")
  260. return true;
  261. else
  262. return false;
  263. }
  264. else
  265. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems reply data does not contain result field");
  266. }
  267. else
  268. m_log.DebugFormat("[AVATAR CONNECTOR]: RemoveItems received empty reply");
  269. }
  270. catch (Exception e)
  271. {
  272. m_log.DebugFormat("[AVATAR CONNECTOR]: Exception when contacting presence server at {0}: {1}", uri, e.Message);
  273. }
  274. return false;
  275. }
  276. #endregion
  277. }
  278. }