CapabilitiesModule.cs 22 KB

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