ActiveConnectionsAJAX.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Text;
  32. using Mono.Data.SqliteClient;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Framework.Monitoring;
  38. namespace OpenSim.Region.UserStatistics
  39. {
  40. public class ActiveConnectionsAJAX : IStatsController
  41. {
  42. private Vector3 DefaultNeighborPosition = new Vector3(((int)Constants.RegionSize * 0.5f), ((int)Constants.RegionSize * 0.5f), 70);
  43. #region IStatsController Members
  44. public string ReportName
  45. {
  46. get { return ""; }
  47. }
  48. public Hashtable ProcessModel(Hashtable pParams)
  49. {
  50. List<Scene> m_scene = (List<Scene>)pParams["Scenes"];
  51. Hashtable nh = new Hashtable();
  52. nh.Add("hdata", m_scene);
  53. return nh;
  54. }
  55. public string RenderView(Hashtable pModelResult)
  56. {
  57. List<Scene> all_scenes = (List<Scene>) pModelResult["hdata"];
  58. StringBuilder output = new StringBuilder();
  59. HTMLUtil.OL_O(ref output, "");
  60. foreach (Scene scene in all_scenes)
  61. {
  62. HTMLUtil.LI_O(ref output, String.Empty);
  63. output.Append(scene.RegionInfo.RegionName);
  64. HTMLUtil.OL_O(ref output, String.Empty);
  65. scene.ForEachScenePresence(delegate(ScenePresence av)
  66. {
  67. Dictionary<string, string> queues = new Dictionary<string, string>();
  68. if (av.ControllingClient is IStatsCollector)
  69. {
  70. IStatsCollector isClient = (IStatsCollector)av.ControllingClient;
  71. queues = decodeQueueReport(isClient.Report());
  72. }
  73. HTMLUtil.LI_O(ref output, String.Empty);
  74. output.Append(av.Name);
  75. output.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
  76. output.Append((av.IsChildAgent ? "Child" : "Root"));
  77. if (av.AbsolutePosition == DefaultNeighborPosition)
  78. {
  79. output.Append("<br />Position: ?");
  80. }
  81. else
  82. {
  83. output.Append(string.Format("<br /><NOBR>Position: <{0},{1},{2}></NOBR>", (int)av.AbsolutePosition.X,
  84. (int)av.AbsolutePosition.Y,
  85. (int)av.AbsolutePosition.Z));
  86. }
  87. Dictionary<string, int> throttles = DecodeClientThrottles(av.ControllingClient.GetThrottlesPacked(1));
  88. HTMLUtil.UL_O(ref output, String.Empty);
  89. foreach (string throttlename in throttles.Keys)
  90. {
  91. HTMLUtil.LI_O(ref output, String.Empty);
  92. output.Append(throttlename);
  93. output.Append(":");
  94. output.Append(throttles[throttlename].ToString());
  95. if (queues.ContainsKey(throttlename))
  96. {
  97. output.Append("/");
  98. output.Append(queues[throttlename]);
  99. }
  100. HTMLUtil.LI_C(ref output);
  101. }
  102. if (queues.ContainsKey("Incoming") && queues.ContainsKey("Outgoing"))
  103. {
  104. HTMLUtil.LI_O(ref output, "red");
  105. output.Append("SEND:");
  106. output.Append(queues["Outgoing"]);
  107. output.Append("/");
  108. output.Append(queues["Incoming"]);
  109. HTMLUtil.LI_C(ref output);
  110. }
  111. HTMLUtil.UL_C(ref output);
  112. HTMLUtil.LI_C(ref output);
  113. });
  114. HTMLUtil.OL_C(ref output);
  115. }
  116. HTMLUtil.OL_C(ref output);
  117. return output.ToString();
  118. }
  119. /// <summary>
  120. /// Convert active connections information to JSON string. Returns a structure:
  121. /// <pre>
  122. /// {"regionName": {
  123. /// "presenceName": {
  124. /// "name": "presenceName",
  125. /// "position": "<x,y,z>",
  126. /// "isRoot": "false",
  127. /// "throttle": {
  128. /// },
  129. /// "queue": {
  130. /// }
  131. /// },
  132. /// ... // multiple presences in the scene
  133. /// },
  134. /// ... // multiple regions in the sim
  135. /// }
  136. ///
  137. /// </pre>
  138. /// </summary>
  139. /// <param name="pModelResult"></param>
  140. /// <returns></returns>
  141. public string RenderJson(Hashtable pModelResult)
  142. {
  143. List<Scene> all_scenes = (List<Scene>) pModelResult["hdata"];
  144. OSDMap regionInfo = new OSDMap();
  145. foreach (Scene scene in all_scenes)
  146. {
  147. OSDMap sceneInfo = new OpenMetaverse.StructuredData.OSDMap();
  148. List<ScenePresence> avatarInScene = scene.GetScenePresences();
  149. foreach (ScenePresence av in avatarInScene)
  150. {
  151. OSDMap presenceInfo = new OSDMap();
  152. presenceInfo.Add("Name", new OSDString(av.Name));
  153. Dictionary<string,string> queues = new Dictionary<string, string>();
  154. if (av.ControllingClient is IStatsCollector)
  155. {
  156. IStatsCollector isClient = (IStatsCollector) av.ControllingClient;
  157. queues = decodeQueueReport(isClient.Report());
  158. }
  159. OSDMap queueInfo = new OpenMetaverse.StructuredData.OSDMap();
  160. foreach (KeyValuePair<string, string> kvp in queues) {
  161. queueInfo.Add(kvp.Key, new OSDString(kvp.Value));
  162. }
  163. sceneInfo.Add("queues", queueInfo);
  164. if (av.IsChildAgent)
  165. presenceInfo.Add("isRoot", new OSDString("false"));
  166. else
  167. presenceInfo.Add("isRoot", new OSDString("true"));
  168. if (av.AbsolutePosition == DefaultNeighborPosition)
  169. {
  170. presenceInfo.Add("position", new OSDString("<0, 0, 0>"));
  171. }
  172. else
  173. {
  174. presenceInfo.Add("position", new OSDString(string.Format("<{0},{1},{2}>",
  175. (int)av.AbsolutePosition.X,
  176. (int) av.AbsolutePosition.Y,
  177. (int) av.AbsolutePosition.Z)) );
  178. }
  179. Dictionary<string, int> throttles = DecodeClientThrottles(av.ControllingClient.GetThrottlesPacked(1));
  180. OSDMap throttleInfo = new OpenMetaverse.StructuredData.OSDMap();
  181. foreach (string throttlename in throttles.Keys)
  182. {
  183. throttleInfo.Add(throttlename, new OSDString(throttles[throttlename].ToString()));
  184. }
  185. presenceInfo.Add("throttle", throttleInfo);
  186. sceneInfo.Add(av.Name, presenceInfo);
  187. }
  188. regionInfo.Add(scene.RegionInfo.RegionName, sceneInfo);
  189. }
  190. return regionInfo.ToString();
  191. }
  192. public Dictionary<string, int> DecodeClientThrottles(byte[] throttle)
  193. {
  194. Dictionary<string, int> returndict = new Dictionary<string, int>();
  195. // From mantis http://opensimulator.org/mantis/view.php?id=1374
  196. // it appears that sometimes we are receiving empty throttle byte arrays.
  197. // TODO: Investigate this behaviour
  198. if (throttle.Length == 0)
  199. {
  200. return new Dictionary<string, int>();
  201. }
  202. int tResend = -1;
  203. int tLand = -1;
  204. int tWind = -1;
  205. int tCloud = -1;
  206. int tTask = -1;
  207. int tTexture = -1;
  208. int tAsset = -1;
  209. int tall = -1;
  210. const int singlefloat = 4;
  211. //Agent Throttle Block contains 7 single floatingpoint values.
  212. int j = 0;
  213. // Some Systems may be big endian...
  214. // it might be smart to do this check more often...
  215. if (!BitConverter.IsLittleEndian)
  216. for (int i = 0; i < 7; i++)
  217. Array.Reverse(throttle, j + i * singlefloat, singlefloat);
  218. // values gotten from OpenMetaverse.org/wiki/Throttle. Thanks MW_
  219. // bytes
  220. // Convert to integer, since.. the full fp space isn't used.
  221. tResend = (int)BitConverter.ToSingle(throttle, j);
  222. returndict.Add("Resend", tResend);
  223. j += singlefloat;
  224. tLand = (int)BitConverter.ToSingle(throttle, j);
  225. returndict.Add("Land", tLand);
  226. j += singlefloat;
  227. tWind = (int)BitConverter.ToSingle(throttle, j);
  228. returndict.Add("Wind", tWind);
  229. j += singlefloat;
  230. tCloud = (int)BitConverter.ToSingle(throttle, j);
  231. returndict.Add("Cloud", tCloud);
  232. j += singlefloat;
  233. tTask = (int)BitConverter.ToSingle(throttle, j);
  234. returndict.Add("Task", tTask);
  235. j += singlefloat;
  236. tTexture = (int)BitConverter.ToSingle(throttle, j);
  237. returndict.Add("Texture", tTexture);
  238. j += singlefloat;
  239. tAsset = (int)BitConverter.ToSingle(throttle, j);
  240. returndict.Add("Asset", tAsset);
  241. tall = tResend + tLand + tWind + tCloud + tTask + tTexture + tAsset;
  242. returndict.Add("All", tall);
  243. return returndict;
  244. }
  245. public Dictionary<string,string> decodeQueueReport(string rep)
  246. {
  247. Dictionary<string, string> returndic = new Dictionary<string, string>();
  248. if (rep.Length == 79)
  249. {
  250. int pos = 1;
  251. returndic.Add("All", rep.Substring((6 * pos), 8)); pos++;
  252. returndic.Add("Incoming", rep.Substring((7 * pos), 8)); pos++;
  253. returndic.Add("Outgoing", rep.Substring((7 * pos) , 8)); pos++;
  254. returndic.Add("Resend", rep.Substring((7 * pos) , 8)); pos++;
  255. returndic.Add("Land", rep.Substring((7 * pos) , 8)); pos++;
  256. returndic.Add("Wind", rep.Substring((7 * pos) , 8)); pos++;
  257. returndic.Add("Cloud", rep.Substring((7 * pos) , 8)); pos++;
  258. returndic.Add("Task", rep.Substring((7 * pos) , 8)); pos++;
  259. returndic.Add("Texture", rep.Substring((7 * pos), 8)); pos++;
  260. returndic.Add("Asset", rep.Substring((7 * pos), 8));
  261. /*
  262. * return string.Format("{0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7}",
  263. SendQueue.Count(),
  264. IncomingPacketQueue.Count,
  265. OutgoingPacketQueue.Count,
  266. ResendOutgoingPacketQueue.Count,
  267. LandOutgoingPacketQueue.Count,
  268. WindOutgoingPacketQueue.Count,
  269. CloudOutgoingPacketQueue.Count,
  270. TaskOutgoingPacketQueue.Count,
  271. TextureOutgoingPacketQueue.Count,
  272. AssetOutgoingPacketQueue.Count);
  273. */
  274. }
  275. return returndic;
  276. }
  277. #endregion
  278. }
  279. }