1
0

CapabilitiesModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. Utils.LongToUInts(kvp.Key, out x, out y);
  228. x = x / Constants.RegionSize;
  229. y = y / Constants.RegionSize;
  230. m_log.Info(" >> "+x+", "+y+": "+kvp.Value);
  231. }
  232. }
  233. }
  234. private void HandleShowCapsListCommand(string module, string[] cmdParams)
  235. {
  236. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  237. return;
  238. StringBuilder capsReport = new StringBuilder();
  239. capsReport.AppendFormat("Region {0}:\n", m_scene.RegionInfo.RegionName);
  240. lock (m_capsObjects)
  241. {
  242. foreach (KeyValuePair<UUID, Caps> kvp in m_capsObjects)
  243. {
  244. capsReport.AppendFormat("** User {0}:\n", kvp.Key);
  245. Caps caps = kvp.Value;
  246. for (IDictionaryEnumerator kvp2 = caps.CapsHandlers.GetCapsDetails(false, null).GetEnumerator(); kvp2.MoveNext(); )
  247. {
  248. Uri uri = new Uri(kvp2.Value.ToString());
  249. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, uri.PathAndQuery);
  250. }
  251. foreach (KeyValuePair<string, PollServiceEventArgs> kvp2 in caps.GetPollHandlers())
  252. capsReport.AppendFormat(m_showCapsCommandFormat, kvp2.Key, kvp2.Value.Url);
  253. foreach (KeyValuePair<string, string> kvp3 in caps.ExternalCapsHandlers)
  254. capsReport.AppendFormat(m_showCapsCommandFormat, kvp3.Key, kvp3.Value);
  255. }
  256. }
  257. MainConsole.Instance.Output(capsReport.ToString());
  258. }
  259. private void HandleShowCapsStatsByCapCommand(string module, string[] cmdParams)
  260. {
  261. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  262. return;
  263. if (cmdParams.Length != 5 && cmdParams.Length != 6)
  264. {
  265. MainConsole.Instance.Output("Usage: show caps stats by cap [<cap-name>]");
  266. return;
  267. }
  268. StringBuilder sb = new StringBuilder();
  269. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  270. if (cmdParams.Length == 5)
  271. {
  272. BuildSummaryStatsByCapReport(sb);
  273. }
  274. else if (cmdParams.Length == 6)
  275. {
  276. BuildDetailedStatsByCapReport(sb, cmdParams[5]);
  277. }
  278. MainConsole.Instance.Output(sb.ToString());
  279. }
  280. private void BuildDetailedStatsByCapReport(StringBuilder sb, string capName)
  281. {
  282. sb.AppendFormat("Capability name {0}\n", capName);
  283. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  284. cdt.AddColumn("User Name", 34);
  285. cdt.AddColumn("Req Received", 12);
  286. cdt.AddColumn("Req Handled", 12);
  287. cdt.Indent = 2;
  288. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  289. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  290. m_scene.ForEachScenePresence(
  291. sp =>
  292. {
  293. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  294. if (caps == null)
  295. return;
  296. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  297. IRequestHandler reqHandler;
  298. if (capsHandlers.TryGetValue(capName, out reqHandler))
  299. {
  300. receivedStats[sp.Name] = reqHandler.RequestsReceived;
  301. handledStats[sp.Name] = reqHandler.RequestsHandled;
  302. }
  303. else
  304. {
  305. PollServiceEventArgs pollHandler = null;
  306. if (caps.TryGetPollHandler(capName, out pollHandler))
  307. {
  308. receivedStats[sp.Name] = pollHandler.RequestsReceived;
  309. handledStats[sp.Name] = pollHandler.RequestsHandled;
  310. }
  311. }
  312. }
  313. );
  314. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  315. {
  316. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  317. }
  318. sb.Append(cdt.ToString());
  319. }
  320. private void BuildSummaryStatsByCapReport(StringBuilder sb)
  321. {
  322. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  323. cdt.AddColumn("Name", 34);
  324. cdt.AddColumn("Req Received", 12);
  325. cdt.AddColumn("Req Handled", 12);
  326. cdt.Indent = 2;
  327. Dictionary<string, int> receivedStats = new Dictionary<string, int>();
  328. Dictionary<string, int> handledStats = new Dictionary<string, int>();
  329. m_scene.ForEachScenePresence(
  330. sp =>
  331. {
  332. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  333. if (caps == null)
  334. return;
  335. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  336. {
  337. string reqName = reqHandler.Name ?? "";
  338. if (!receivedStats.ContainsKey(reqName))
  339. {
  340. receivedStats[reqName] = reqHandler.RequestsReceived;
  341. handledStats[reqName] = reqHandler.RequestsHandled;
  342. }
  343. else
  344. {
  345. receivedStats[reqName] += reqHandler.RequestsReceived;
  346. handledStats[reqName] += reqHandler.RequestsHandled;
  347. }
  348. }
  349. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  350. {
  351. string name = kvp.Key;
  352. PollServiceEventArgs pollHandler = kvp.Value;
  353. if (!receivedStats.ContainsKey(name))
  354. {
  355. receivedStats[name] = pollHandler.RequestsReceived;
  356. handledStats[name] = pollHandler.RequestsHandled;
  357. }
  358. else
  359. {
  360. receivedStats[name] += pollHandler.RequestsReceived;
  361. handledStats[name] += pollHandler.RequestsHandled;
  362. }
  363. }
  364. }
  365. );
  366. foreach (KeyValuePair<string, int> kvp in receivedStats.OrderByDescending(kp => kp.Value))
  367. cdt.AddRow(kvp.Key, kvp.Value, handledStats[kvp.Key]);
  368. sb.Append(cdt.ToString());
  369. }
  370. private void HandleShowCapsStatsByUserCommand(string module, string[] cmdParams)
  371. {
  372. if (SceneManager.Instance.CurrentScene != null && SceneManager.Instance.CurrentScene != m_scene)
  373. return;
  374. if (cmdParams.Length != 5 && cmdParams.Length != 7)
  375. {
  376. MainConsole.Instance.Output("Usage: show caps stats by user [<first-name> <last-name>]");
  377. return;
  378. }
  379. StringBuilder sb = new StringBuilder();
  380. sb.AppendFormat("Region {0}:\n", m_scene.Name);
  381. if (cmdParams.Length == 5)
  382. {
  383. BuildSummaryStatsByUserReport(sb);
  384. }
  385. else if (cmdParams.Length == 7)
  386. {
  387. string firstName = cmdParams[5];
  388. string lastName = cmdParams[6];
  389. ScenePresence sp = m_scene.GetScenePresence(firstName, lastName);
  390. if (sp == null)
  391. return;
  392. BuildDetailedStatsByUserReport(sb, sp);
  393. }
  394. MainConsole.Instance.Output(sb.ToString());
  395. }
  396. private void BuildDetailedStatsByUserReport(StringBuilder sb, ScenePresence sp)
  397. {
  398. sb.AppendFormat("Avatar name {0}, type {1}\n", sp.Name, sp.IsChildAgent ? "child" : "root");
  399. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  400. cdt.AddColumn("Cap Name", 34);
  401. cdt.AddColumn("Req Received", 12);
  402. cdt.AddColumn("Req Handled", 12);
  403. cdt.Indent = 2;
  404. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  405. if (caps == null)
  406. return;
  407. List<CapTableRow> capRows = new List<CapTableRow>();
  408. foreach (IRequestHandler reqHandler in caps.CapsHandlers.GetCapsHandlers().Values)
  409. capRows.Add(new CapTableRow(reqHandler.Name, reqHandler.RequestsReceived, reqHandler.RequestsHandled));
  410. foreach (KeyValuePair<string, PollServiceEventArgs> kvp in caps.GetPollHandlers())
  411. capRows.Add(new CapTableRow(kvp.Key, kvp.Value.RequestsReceived, kvp.Value.RequestsHandled));
  412. foreach (CapTableRow ctr in capRows.OrderByDescending(ctr => ctr.RequestsReceived))
  413. cdt.AddRow(ctr.Name, ctr.RequestsReceived, ctr.RequestsHandled);
  414. sb.Append(cdt.ToString());
  415. }
  416. private void BuildSummaryStatsByUserReport(StringBuilder sb)
  417. {
  418. ConsoleDisplayTable cdt = new ConsoleDisplayTable();
  419. cdt.AddColumn("Name", 32);
  420. cdt.AddColumn("Type", 5);
  421. cdt.AddColumn("Req Received", 12);
  422. cdt.AddColumn("Req Handled", 12);
  423. cdt.Indent = 2;
  424. m_scene.ForEachScenePresence(
  425. sp =>
  426. {
  427. Caps caps = m_scene.CapsModule.GetCapsForUser(sp.UUID);
  428. if (caps == null)
  429. return;
  430. Dictionary<string, IRequestHandler> capsHandlers = caps.CapsHandlers.GetCapsHandlers();
  431. int totalRequestsReceived = 0;
  432. int totalRequestsHandled = 0;
  433. foreach (IRequestHandler reqHandler in capsHandlers.Values)
  434. {
  435. totalRequestsReceived += reqHandler.RequestsReceived;
  436. totalRequestsHandled += reqHandler.RequestsHandled;
  437. }
  438. Dictionary<string, PollServiceEventArgs> capsPollHandlers = caps.GetPollHandlers();
  439. foreach (PollServiceEventArgs handler in capsPollHandlers.Values)
  440. {
  441. totalRequestsReceived += handler.RequestsReceived;
  442. totalRequestsHandled += handler.RequestsHandled;
  443. }
  444. cdt.AddRow(sp.Name, sp.IsChildAgent ? "child" : "root", totalRequestsReceived, totalRequestsHandled);
  445. }
  446. );
  447. sb.Append(cdt.ToString());
  448. }
  449. private class CapTableRow
  450. {
  451. public string Name { get; set; }
  452. public int RequestsReceived { get; set; }
  453. public int RequestsHandled { get; set; }
  454. public CapTableRow(string name, int requestsReceived, int requestsHandled)
  455. {
  456. Name = name;
  457. RequestsReceived = requestsReceived;
  458. RequestsHandled = requestsHandled;
  459. }
  460. }
  461. }
  462. }