CapabilitiesModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. // m_log.DebugFormat(
  118. // "[CAPS]: Adding capabilities for agent {0} in {1} with path {2}",
  119. // agentId, m_scene.RegionInfo.RegionName, capsObjectPath);
  120. caps = new Caps(MainServer.Instance, m_scene.RegionInfo.ExternalHostName,
  121. (MainServer.Instance == null) ? 0: MainServer.Instance.Port,
  122. capsObjectPath, agentId, m_scene.RegionInfo.RegionName);
  123. m_capsObjects[agentId] = caps;
  124. }
  125. m_scene.EventManager.TriggerOnRegisterCaps(agentId, caps);
  126. }
  127. public void RemoveCaps(UUID agentId)
  128. {
  129. m_log.DebugFormat("[CAPS]: Remove caps for agent {0} in region {1}", agentId, m_scene.RegionInfo.RegionName);
  130. lock (m_childrenSeeds)
  131. {
  132. if (m_childrenSeeds.ContainsKey(agentId))
  133. {
  134. m_childrenSeeds.Remove(agentId);
  135. }
  136. }
  137. lock (m_capsObjects)
  138. {
  139. if (m_capsObjects.ContainsKey(agentId))
  140. {
  141. m_capsObjects[agentId].DeregisterHandlers();
  142. m_scene.EventManager.TriggerOnDeregisterCaps(agentId, m_capsObjects[agentId]);
  143. m_capsObjects.Remove(agentId);
  144. }
  145. else
  146. {
  147. m_log.WarnFormat(
  148. "[CAPS]: Received request to remove CAPS handler for root agent {0} in {1}, but no such CAPS handler found!",
  149. agentId, m_scene.RegionInfo.RegionName);
  150. }
  151. }
  152. }
  153. public Caps GetCapsForUser(UUID agentId)
  154. {
  155. lock (m_capsObjects)
  156. {
  157. if (m_capsObjects.ContainsKey(agentId))
  158. {
  159. return m_capsObjects[agentId];
  160. }
  161. }
  162. return null;
  163. }
  164. public void SetAgentCapsSeeds(AgentCircuitData agent)
  165. {
  166. lock (m_capsPaths)
  167. m_capsPaths[agent.AgentID] = agent.CapsPath;
  168. lock (m_childrenSeeds)
  169. m_childrenSeeds[agent.AgentID]
  170. = ((agent.ChildrenCapSeeds == null) ? new Dictionary<ulong, string>() : agent.ChildrenCapSeeds);
  171. }
  172. public string GetCapsPath(UUID agentId)
  173. {
  174. lock (m_capsPaths)
  175. {
  176. if (m_capsPaths.ContainsKey(agentId))
  177. {
  178. return m_capsPaths[agentId];
  179. }
  180. }
  181. return null;
  182. }
  183. public Dictionary<ulong, string> GetChildrenSeeds(UUID agentID)
  184. {
  185. Dictionary<ulong, string> seeds = null;
  186. lock (m_childrenSeeds)
  187. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  188. return seeds;
  189. return new Dictionary<ulong, string>();
  190. }
  191. public void DropChildSeed(UUID agentID, ulong handle)
  192. {
  193. Dictionary<ulong, string> seeds;
  194. lock (m_childrenSeeds)
  195. {
  196. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  197. {
  198. seeds.Remove(handle);
  199. }
  200. }
  201. }
  202. public string GetChildSeed(UUID agentID, ulong handle)
  203. {
  204. Dictionary<ulong, string> seeds;
  205. string returnval;
  206. lock (m_childrenSeeds)
  207. {
  208. if (m_childrenSeeds.TryGetValue(agentID, out seeds))
  209. {
  210. if (seeds.TryGetValue(handle, out returnval))
  211. return returnval;
  212. }
  213. }
  214. return null;
  215. }
  216. public void SetChildrenSeed(UUID agentID, Dictionary<ulong, string> seeds)
  217. {
  218. //m_log.DebugFormat(" !!! Setting child seeds in {0} to {1}", m_scene.RegionInfo.RegionName, seeds.Count);
  219. lock (m_childrenSeeds)
  220. m_childrenSeeds[agentID] = seeds;
  221. }
  222. public void DumpChildrenSeeds(UUID agentID)
  223. {
  224. m_log.Info("================ ChildrenSeed "+m_scene.RegionInfo.RegionName+" ================");
  225. lock (m_childrenSeeds)
  226. {
  227. foreach (KeyValuePair<ulong, string> kvp in m_childrenSeeds[agentID])
  228. {
  229. uint x, y;
  230. Util.RegionHandleToRegionLoc(kvp.Key, out x, out y);
  231. m_log.Info(" >> "+x+", "+y+": "+kvp.Value);
  232. }
  233. }
  234. }
  235. private void HandleShowCapsListCommand(string module, string[] cmdParams)
  236. {
  237. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  238. return;
  239. StringBuilder capsReport = new StringBuilder();
  240. capsReport.AppendFormat("Region {0}:\n", m_scene.RegionInfo.RegionName);
  241. lock (m_capsObjects)
  242. {
  243. foreach (KeyValuePair<UUID, Caps> kvp in m_capsObjects)
  244. {
  245. capsReport.AppendFormat("** User {0}:\n", kvp.Key);
  246. Caps caps = kvp.Value;
  247. for (IDictionaryEnumerator kvp2 = caps.CapsHandlers.GetCapsDetails(false, null).GetEnumerator(); kvp2.MoveNext(); )
  248. {
  249. Uri uri = new Uri(kvp2.Value.ToString());
  250. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, uri.PathAndQuery);
  251. }
  252. foreach (KeyValuePair<string, PollServiceEventArgs> kvp2 in caps.GetPollHandlers())
  253. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, kvp2.Value.Url);
  254. foreach (KeyValuePair<string, string> kvp3 in caps.ExternalCapsHandlers)
  255. capsReport.AppendFormat(m_showCapsCommandFormat, kvp3.Key, kvp3.Value);
  256. }
  257. }
  258. MainConsole.Instance.Output(capsReport.ToString());
  259. }
  260. private void HandleShowCapsStatsByCapCommand(string module, string[] cmdParams)
  261. {
  262. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  263. return;
  264. if (cmdParams.Length != 5 && cmdParams.Length != 6)
  265. {
  266. MainConsole.Instance.Output("Usage: show caps stats by cap [<cap-name>]");
  267. return;
  268. }
  269. StringBuilder sb = new StringBuilder();
  270. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  271. if (cmdParams.Length == 5)
  272. {
  273. BuildSummaryStatsByCapReport(sb);
  274. }
  275. else if (cmdParams.Length == 6)
  276. {
  277. BuildDetailedStatsByCapReport(sb, cmdParams[5]);
  278. }
  279. MainConsole.Instance.Output(sb.ToString());
  280. }
  281. private void BuildDetailedStatsByCapReport(StringBuilder sb, string capName)
  282. {
  283. sb.AppendFormat("Capability name {0}\n", capName);
  284. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  285. cdt.AddColumn("User Name", 34);
  286. cdt.AddColumn("Req Received", 12);
  287. cdt.AddColumn("Req Handled", 12);
  288. cdt.Indent = 2;
  289. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  290. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  291. m_scene.ForEachScenePresence(
  292. sp =>
  293. {
  294. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  295. if (caps == null)
  296. return;
  297. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  298. IRequestHandler reqHandler;
  299. if (capsHandlers.TryGetValue(capName, out reqHandler))
  300. {
  301. receivedStats[sp.Name] = reqHandler.RequestsReceived;
  302. handledStats[sp.Name] = reqHandler.RequestsHandled;
  303. }
  304. else
  305. {
  306. PollServiceEventArgs pollHandler = null;
  307. if (caps.TryGetPollHandler(capName, out pollHandler))
  308. {
  309. receivedStats[sp.Name] = pollHandler.RequestsReceived;
  310. handledStats[sp.Name] = pollHandler.RequestsHandled;
  311. }
  312. }
  313. }
  314. );
  315. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  316. {
  317. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  318. }
  319. sb.Append(cdt.ToString());
  320. }
  321. private void BuildSummaryStatsByCapReport(StringBuilder sb)
  322. {
  323. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  324. cdt.AddColumn("Name", 34);
  325. cdt.AddColumn("Req Received", 12);
  326. cdt.AddColumn("Req Handled", 12);
  327. cdt.Indent = 2;
  328. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  329. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  330. m_scene.ForEachScenePresence(
  331. sp =>
  332. {
  333. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  334. if (caps == null)
  335. return;
  336. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  337. {
  338. string reqName = reqHandler.Name ?? "";
  339. if (!receivedStats.ContainsKey(reqName))
  340. {
  341. receivedStats[reqName] = reqHandler.RequestsReceived;
  342. handledStats[reqName] = reqHandler.RequestsHandled;
  343. }
  344. else
  345. {
  346. receivedStats[reqName] += reqHandler.RequestsReceived;
  347. handledStats[reqName] += reqHandler.RequestsHandled;
  348. }
  349. }
  350. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  351. {
  352. string name = kvp.Key;
  353. PollServiceEventArgs pollHandler = kvp.Value;
  354. if (!receivedStats.ContainsKey(name))
  355. {
  356. receivedStats[name] = pollHandler.RequestsReceived;
  357. handledStats[name] = pollHandler.RequestsHandled;
  358. }
  359. else
  360. {
  361. receivedStats[name] += pollHandler.RequestsReceived;
  362. handledStats[name] += pollHandler.RequestsHandled;
  363. }
  364. }
  365. }
  366. );
  367. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  368. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  369. sb.Append(cdt.ToString());
  370. }
  371. private void HandleShowCapsStatsByUserCommand(string module, string[] cmdParams)
  372. {
  373. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  374. return;
  375. if (cmdParams.Length != 5 && cmdParams.Length != 7)
  376. {
  377. MainConsole.Instance.Output("Usage: show caps stats by user [<first-name> <last-name>]");
  378. return;
  379. }
  380. StringBuilder sb = new StringBuilder();
  381. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  382. if (cmdParams.Length == 5)
  383. {
  384. BuildSummaryStatsByUserReport(sb);
  385. }
  386. else if (cmdParams.Length == 7)
  387. {
  388. string firstName = cmdParams[5];
  389. string lastName = cmdParams[6];
  390. ScenePresence sp = m_scene.GetScenePresence(firstName, lastName);
  391. if (sp == null)
  392. return;
  393. BuildDetailedStatsByUserReport(sb, sp);
  394. }
  395. MainConsole.Instance.Output(sb.ToString());
  396. }
  397. private void BuildDetailedStatsByUserReport(StringBuilder sb, ScenePresence sp)
  398. {
  399. sb.AppendFormat("Avatar name {0}, type {1}\n", sp.Name, sp.IsChildAgent ? "child" : "root");
  400. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  401. cdt.AddColumn("Cap Name", 34);
  402. cdt.AddColumn("Req Received", 12);
  403. cdt.AddColumn("Req Handled", 12);
  404. cdt.Indent = 2;
  405. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  406. if (caps == null)
  407. return;
  408. List<CapTableRow> capRows = new List<CapTableRow>();
  409. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  410. capRows.Add(new CapTableRow(reqHandler.Name, reqHandler.RequestsReceived, reqHandler.RequestsHandled));
  411. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  412. capRows.Add(new CapTableRow(kvp.Key, kvp.Value.RequestsReceived, kvp.Value.RequestsHandled));
  413. foreach (CapTableRow ctr in capRows.OrderByDescending(ctr => ctr.RequestsReceived))
  414. cdt.AddRow(ctr.Name, ctr.RequestsReceived, ctr.RequestsHandled);
  415. sb.Append(cdt.ToString());
  416. }
  417. private void BuildSummaryStatsByUserReport(StringBuilder sb)
  418. {
  419. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  420. cdt.AddColumn("Name", 32);
  421. cdt.AddColumn("Type", 5);
  422. cdt.AddColumn("Req Received", 12);
  423. cdt.AddColumn("Req Handled", 12);
  424. cdt.Indent = 2;
  425. m_scene.ForEachScenePresence(
  426. sp =>
  427. {
  428. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  429. if (caps == null)
  430. return;
  431. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  432. int totalRequestsReceived = 0;
  433. int totalRequestsHandled = 0;
  434. foreach (IRequestHandler reqHandler in capsHandlers.Values)
  435. {
  436. totalRequestsReceived += reqHandler.RequestsReceived;
  437. totalRequestsHandled += reqHandler.RequestsHandled;
  438. }
  439. Dictionary<string, PollServiceEventArgs> capsPollHandlers = caps.GetPollHandlers();
  440. foreach (PollServiceEventArgs handler in capsPollHandlers.Values)
  441. {
  442. totalRequestsReceived += handler.RequestsReceived;
  443. totalRequestsHandled += handler.RequestsHandled;
  444. }
  445. cdt.AddRow(sp.Name, sp.IsChildAgent ? "child" : "root", totalRequestsReceived, totalRequestsHandled);
  446. }
  447. );
  448. sb.Append(cdt.ToString());
  449. }
  450. private class CapTableRow
  451. {
  452. public string Name { get; set; }
  453. public int RequestsReceived { get; set; }
  454. public int RequestsHandled { get; set; }
  455. public CapTableRow(string name, int requestsReceived, int requestsHandled)
  456. {
  457. Name = name;
  458. RequestsReceived = requestsReceived;
  459. RequestsHandled = requestsHandled;
  460. }
  461. }
  462. }
  463. }