CapabilitiesModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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 copyrightD
  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.Linq;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Nini.Config;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Console;
  39. using OpenSim.Framework.Servers;
  40. using OpenSim.Framework.Servers.HttpServer;
  41. using OpenSim.Region.Framework.Interfaces;
  42. using OpenSim.Region.Framework.Scenes;
  43. using Caps=OpenSim.Framework.Capabilities.Caps;
  44. namespace OpenSim.Region.CoreModules.Framework
  45. {
  46. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "CapabilitiesModule")]
  47. public class CapabilitiesModule : INonSharedRegionModule, ICapabilitiesModule
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. private string m_showCapsCommandFormat = " {0,-38} {1,-60}\n";
  51. protected Scene m_scene;
  52. /// <summary>
  53. /// Each agent has its own capabilities handler.
  54. /// </summary>
  55. protected Dictionary<UUID, Caps> m_capsObjects = new Dictionary<UUID, Caps>();
  56. protected Dictionary<UUID, string> m_capsPaths = new Dictionary<UUID, string>();
  57. protected Dictionary<UUID, Dictionary<ulong, string>> m_childrenSeeds
  58. = new Dictionary<UUID, Dictionary<ulong, string>>();
  59. public void Initialise(IConfigSource source)
  60. {
  61. }
  62. public void AddRegion(Scene scene)
  63. {
  64. m_scene = scene;
  65. m_scene.RegisterModuleInterface<ICapabilitiesModule>(this);
  66. MainConsole.Instance.Commands.AddCommand(
  67. "Comms", false, "show caps list",
  68. "show caps list",
  69. "Shows list of registered capabilities for users.", HandleShowCapsListCommand);
  70. MainConsole.Instance.Commands.AddCommand(
  71. "Comms", false, "show caps stats by user",
  72. "show caps stats by user [<first-name> <last-name>]",
  73. "Shows statistics on capabilities use by user.",
  74. "If a user name is given, then prints a detailed breakdown of caps use ordered by number of requests received.",
  75. HandleShowCapsStatsByUserCommand);
  76. MainConsole.Instance.Commands.AddCommand(
  77. "Comms", false, "show caps stats by cap",
  78. "show caps stats by cap [<cap-name>]",
  79. "Shows statistics on capabilities use by capability.",
  80. "If a capability name is given, then prints a detailed breakdown of use by each user.",
  81. HandleShowCapsStatsByCapCommand);
  82. }
  83. public void RegionLoaded(Scene scene)
  84. {
  85. }
  86. public void RemoveRegion(Scene scene)
  87. {
  88. m_scene.UnregisterModuleInterface<ICapabilitiesModule>(this);
  89. }
  90. public void PostInitialise()
  91. {
  92. }
  93. public void Close() {}
  94. public string Name
  95. {
  96. get { return "Capabilities Module"; }
  97. }
  98. public Type ReplaceableInterface
  99. {
  100. get { return null; }
  101. }
  102. public void CreateCaps(UUID agentId)
  103. {
  104. if (m_scene.RegionInfo.EstateSettings.IsBanned(agentId))
  105. return;
  106. Caps caps;
  107. String capsObjectPath = GetCapsPath(agentId);
  108. lock (m_capsObjects)
  109. {
  110. if (m_capsObjects.ContainsKey(agentId))
  111. {
  112. Caps oldCaps = m_capsObjects[agentId];
  113. //m_log.WarnFormat(
  114. // "[CAPS]: Recreating caps for agent {0} in region {1}. Old caps path {2}, new caps path {3}. ",
  115. // agentId, m_scene.RegionInfo.RegionName, oldCaps.CapsObjectPath, capsObjectPath);
  116. }
  117. caps = new Caps(MainServer.Instance, m_scene.RegionInfo.ExternalHostName,
  118. (MainServer.Instance == null) ? 0: MainServer.Instance.Port,
  119. capsObjectPath, agentId, m_scene.RegionInfo.RegionName);
  120. m_capsObjects[agentId] = caps;
  121. }
  122. m_scene.EventManager.TriggerOnRegisterCaps(agentId, caps);
  123. }
  124. public void RemoveCaps(UUID agentId)
  125. {
  126. m_log.DebugFormat("[CAPS]: Remove caps for agent {0} in region {1}", agentId, m_scene.RegionInfo.RegionName);
  127. lock (m_childrenSeeds)
  128. {
  129. if (m_childrenSeeds.ContainsKey(agentId))
  130. {
  131. m_childrenSeeds.Remove(agentId);
  132. }
  133. }
  134. lock (m_capsObjects)
  135. {
  136. if (m_capsObjects.ContainsKey(agentId))
  137. {
  138. m_capsObjects[agentId].DeregisterHandlers();
  139. m_scene.EventManager.TriggerOnDeregisterCaps(agentId, m_capsObjects[agentId]);
  140. m_capsObjects.Remove(agentId);
  141. }
  142. else
  143. {
  144. m_log.WarnFormat(
  145. "[CAPS]: Received request to remove CAPS handler for root agent {0} in {1}, but no such CAPS handler found!",
  146. agentId, m_scene.RegionInfo.RegionName);
  147. }
  148. }
  149. }
  150. public Caps GetCapsForUser(UUID agentId)
  151. {
  152. lock (m_capsObjects)
  153. {
  154. if (m_capsObjects.ContainsKey(agentId))
  155. {
  156. return m_capsObjects[agentId];
  157. }
  158. }
  159. return null;
  160. }
  161. public void SetAgentCapsSeeds(AgentCircuitData agent)
  162. {
  163. lock (m_capsPaths)
  164. m_capsPaths[agent.AgentID] = agent.CapsPath;
  165. lock (m_childrenSeeds)
  166. m_childrenSeeds[agent.AgentID]
  167. = ((agent.ChildrenCapSeeds == null) ? new Dictionary<ulong, string>() : agent.ChildrenCapSeeds);
  168. }
  169. public string GetCapsPath(UUID agentId)
  170. {
  171. lock (m_capsPaths)
  172. {
  173. if (m_capsPaths.ContainsKey(agentId))
  174. {
  175. return m_capsPaths[agentId];
  176. }
  177. }
  178. return null;
  179. }
  180. public Dictionary<ulong, string> GetChildrenSeeds(UUID agentID)
  181. {
  182. Dictionary<ulong, string> seeds = null;
  183. lock (m_childrenSeeds)
  184. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  185. return seeds;
  186. return new Dictionary<ulong, string>();
  187. }
  188. public void DropChildSeed(UUID agentID, ulong handle)
  189. {
  190. Dictionary<ulong, string> seeds;
  191. lock (m_childrenSeeds)
  192. {
  193. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  194. {
  195. seeds.Remove(handle);
  196. }
  197. }
  198. }
  199. public string GetChildSeed(UUID agentID, ulong handle)
  200. {
  201. Dictionary<ulong, string> seeds;
  202. string returnval;
  203. lock (m_childrenSeeds)
  204. {
  205. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  206. {
  207. if (seeds.TryGetValue(handle, out returnval))
  208. return returnval;
  209. }
  210. }
  211. return null;
  212. }
  213. public void SetChildrenSeed(UUID agentID, Dictionary<ulong, string> seeds)
  214. {
  215. //m_log.DebugFormat(" !!! Setting child seeds in {0} to {1}", m_scene.RegionInfo.RegionName, seeds.Count);
  216. lock (m_childrenSeeds)
  217. m_childrenSeeds[agentID] = seeds;
  218. }
  219. public void DumpChildrenSeeds(UUID agentID)
  220. {
  221. m_log.Info("================ ChildrenSeed "+m_scene.RegionInfo.RegionName+" ================");
  222. lock (m_childrenSeeds)
  223. {
  224. foreach (KeyValuePair<ulong, string> kvp in m_childrenSeeds[agentID])
  225. {
  226. uint x, y;
  227. Util.RegionHandleToRegionLoc(kvp.Key, out x, out y);
  228. m_log.Info(" >> "+x+", "+y+": "+kvp.Value);
  229. }
  230. }
  231. }
  232. private void HandleShowCapsListCommand(string module, string[] cmdParams)
  233. {
  234. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  235. return;
  236. StringBuilder capsReport = new StringBuilder();
  237. capsReport.AppendFormat("Region {0}:\n", m_scene.RegionInfo.RegionName);
  238. lock (m_capsObjects)
  239. {
  240. foreach (KeyValuePair<UUID, Caps> kvp in m_capsObjects)
  241. {
  242. capsReport.AppendFormat("** User {0}:\n", kvp.Key);
  243. Caps caps = kvp.Value;
  244. for (IDictionaryEnumerator kvp2 = caps.CapsHandlers.GetCapsDetails(false, null).GetEnumerator(); kvp2.MoveNext(); )
  245. {
  246. Uri uri = new Uri(kvp2.Value.ToString());
  247. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, uri.PathAndQuery);
  248. }
  249. foreach (KeyValuePair<string, PollServiceEventArgs> kvp2 in caps.GetPollHandlers())
  250. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, kvp2.Value.Url);
  251. foreach (KeyValuePair<string, string> kvp3 in caps.ExternalCapsHandlers)
  252. capsReport.AppendFormat(m_showCapsCommandFormat, kvp3.Key, kvp3.Value);
  253. }
  254. }
  255. MainConsole.Instance.Output(capsReport.ToString());
  256. }
  257. private void HandleShowCapsStatsByCapCommand(string module, string[] cmdParams)
  258. {
  259. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  260. return;
  261. if (cmdParams.Length != 5 && cmdParams.Length != 6)
  262. {
  263. MainConsole.Instance.Output("Usage: show caps stats by cap [<cap-name>]");
  264. return;
  265. }
  266. StringBuilder sb = new StringBuilder();
  267. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  268. if (cmdParams.Length == 5)
  269. {
  270. BuildSummaryStatsByCapReport(sb);
  271. }
  272. else if (cmdParams.Length == 6)
  273. {
  274. BuildDetailedStatsByCapReport(sb, cmdParams[5]);
  275. }
  276. MainConsole.Instance.Output(sb.ToString());
  277. }
  278. private void BuildDetailedStatsByCapReport(StringBuilder sb, string capName)
  279. {
  280. sb.AppendFormat("Capability name {0}\n", capName);
  281. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  282. cdt.AddColumn("User Name", 34);
  283. cdt.AddColumn("Req Received", 12);
  284. cdt.AddColumn("Req Handled", 12);
  285. cdt.Indent = 2;
  286. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  287. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  288. m_scene.ForEachScenePresence(
  289. sp =>
  290. {
  291. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  292. if (caps == null)
  293. return;
  294. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  295. IRequestHandler reqHandler;
  296. if (capsHandlers.TryGetValue(capName, out reqHandler))
  297. {
  298. receivedStats[sp.Name] = reqHandler.RequestsReceived;
  299. handledStats[sp.Name] = reqHandler.RequestsHandled;
  300. }
  301. else
  302. {
  303. PollServiceEventArgs pollHandler = null;
  304. if (caps.TryGetPollHandler(capName, out pollHandler))
  305. {
  306. receivedStats[sp.Name] = pollHandler.RequestsReceived;
  307. handledStats[sp.Name] = pollHandler.RequestsHandled;
  308. }
  309. }
  310. }
  311. );
  312. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  313. {
  314. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  315. }
  316. sb.Append(cdt.ToString());
  317. }
  318. private void BuildSummaryStatsByCapReport(StringBuilder sb)
  319. {
  320. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  321. cdt.AddColumn("Name", 34);
  322. cdt.AddColumn("Req Received", 12);
  323. cdt.AddColumn("Req Handled", 12);
  324. cdt.Indent = 2;
  325. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  326. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  327. m_scene.ForEachScenePresence(
  328. sp =>
  329. {
  330. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  331. if (caps == null)
  332. return;
  333. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  334. {
  335. string reqName = reqHandler.Name ?? "";
  336. if (!receivedStats.ContainsKey(reqName))
  337. {
  338. receivedStats[reqName] = reqHandler.RequestsReceived;
  339. handledStats[reqName] = reqHandler.RequestsHandled;
  340. }
  341. else
  342. {
  343. receivedStats[reqName] += reqHandler.RequestsReceived;
  344. handledStats[reqName] += reqHandler.RequestsHandled;
  345. }
  346. }
  347. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  348. {
  349. string name = kvp.Key;
  350. PollServiceEventArgs pollHandler = kvp.Value;
  351. if (!receivedStats.ContainsKey(name))
  352. {
  353. receivedStats[name] = pollHandler.RequestsReceived;
  354. handledStats[name] = pollHandler.RequestsHandled;
  355. }
  356. else
  357. {
  358. receivedStats[name] += pollHandler.RequestsReceived;
  359. handledStats[name] += pollHandler.RequestsHandled;
  360. }
  361. }
  362. }
  363. );
  364. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  365. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  366. sb.Append(cdt.ToString());
  367. }
  368. private void HandleShowCapsStatsByUserCommand(string module, string[] cmdParams)
  369. {
  370. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  371. return;
  372. if (cmdParams.Length != 5 && cmdParams.Length != 7)
  373. {
  374. MainConsole.Instance.Output("Usage: show caps stats by user [<first-name> <last-name>]");
  375. return;
  376. }
  377. StringBuilder sb = new StringBuilder();
  378. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  379. if (cmdParams.Length == 5)
  380. {
  381. BuildSummaryStatsByUserReport(sb);
  382. }
  383. else if (cmdParams.Length == 7)
  384. {
  385. string firstName = cmdParams[5];
  386. string lastName = cmdParams[6];
  387. ScenePresence sp = m_scene.GetScenePresence(firstName, lastName);
  388. if (sp == null)
  389. return;
  390. BuildDetailedStatsByUserReport(sb, sp);
  391. }
  392. MainConsole.Instance.Output(sb.ToString());
  393. }
  394. private void BuildDetailedStatsByUserReport(StringBuilder sb, ScenePresence sp)
  395. {
  396. sb.AppendFormat("Avatar name {0}, type {1}\n", sp.Name, sp.IsChildAgent ? "child" : "root");
  397. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  398. cdt.AddColumn("Cap Name", 34);
  399. cdt.AddColumn("Req Received", 12);
  400. cdt.AddColumn("Req Handled", 12);
  401. cdt.Indent = 2;
  402. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  403. if (caps == null)
  404. return;
  405. List<CapTableRow> capRows = new List<CapTableRow>();
  406. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  407. capRows.Add(new CapTableRow(reqHandler.Name, reqHandler.RequestsReceived, reqHandler.RequestsHandled));
  408. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  409. capRows.Add(new CapTableRow(kvp.Key, kvp.Value.RequestsReceived, kvp.Value.RequestsHandled));
  410. foreach (CapTableRow ctr in capRows.OrderByDescending(ctr => ctr.RequestsReceived))
  411. cdt.AddRow(ctr.Name, ctr.RequestsReceived, ctr.RequestsHandled);
  412. sb.Append(cdt.ToString());
  413. }
  414. private void BuildSummaryStatsByUserReport(StringBuilder sb)
  415. {
  416. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  417. cdt.AddColumn("Name", 32);
  418. cdt.AddColumn("Type", 5);
  419. cdt.AddColumn("Req Received", 12);
  420. cdt.AddColumn("Req Handled", 12);
  421. cdt.Indent = 2;
  422. m_scene.ForEachScenePresence(
  423. sp =>
  424. {
  425. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  426. if (caps == null)
  427. return;
  428. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  429. int totalRequestsReceived = 0;
  430. int totalRequestsHandled = 0;
  431. foreach (IRequestHandler reqHandler in capsHandlers.Values)
  432. {
  433. totalRequestsReceived += reqHandler.RequestsReceived;
  434. totalRequestsHandled += reqHandler.RequestsHandled;
  435. }
  436. Dictionary<string, PollServiceEventArgs> capsPollHandlers = caps.GetPollHandlers();
  437. foreach (PollServiceEventArgs handler in capsPollHandlers.Values)
  438. {
  439. totalRequestsReceived += handler.RequestsReceived;
  440. totalRequestsHandled += handler.RequestsHandled;
  441. }
  442. cdt.AddRow(sp.Name, sp.IsChildAgent ? "child" : "root", totalRequestsReceived, totalRequestsHandled);
  443. }
  444. );
  445. sb.Append(cdt.ToString());
  446. }
  447. private class CapTableRow
  448. {
  449. public string Name { get; set; }
  450. public int RequestsReceived { get; set; }
  451. public int RequestsHandled { get; set; }
  452. public CapTableRow(string name, int requestsReceived, int requestsHandled)
  453. {
  454. Name = name;
  455. RequestsReceived = requestsReceived;
  456. RequestsHandled = requestsHandled;
  457. }
  458. }
  459. }
  460. }