EventQueueGetModule.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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.Net;
  31. using System.Reflection;
  32. using System.Threading;
  33. using log4net;
  34. using Nini.Config;
  35. using Mono.Addins;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Messages.Linden;
  38. using OpenMetaverse.Packets;
  39. using OpenMetaverse.StructuredData;
  40. using OpenSim.Framework;
  41. using OpenSim.Framework.Console;
  42. using OpenSim.Framework.Servers;
  43. using OpenSim.Framework.Servers.HttpServer;
  44. using OpenSim.Region.Framework.Interfaces;
  45. using OpenSim.Region.Framework.Scenes;
  46. using BlockingLLSDQueue = OpenSim.Framework.BlockingQueue<OpenMetaverse.StructuredData.OSD>;
  47. using Caps=OpenSim.Framework.Capabilities.Caps;
  48. namespace OpenSim.Region.ClientStack.Linden
  49. {
  50. public struct QueueItem
  51. {
  52. public int id;
  53. public OSDMap body;
  54. }
  55. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "EventQueueGetModule")]
  56. public class EventQueueGetModule : IEventQueue, INonSharedRegionModule
  57. {
  58. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  59. private static string LogHeader = "[EVENT QUEUE GET MODULE]";
  60. /// <value>
  61. /// Debug level.
  62. /// </value>
  63. public int DebugLevel { get; set; }
  64. // Viewer post requests timeout in 60 secs
  65. // https://bitbucket.org/lindenlab/viewer-release/src/421c20423df93d650cc305dc115922bb30040999/indra/llmessage/llhttpclient.cpp?at=default#cl-44
  66. //
  67. private const int VIEWER_TIMEOUT = 60 * 1000;
  68. // Just to be safe, we work on a 10 sec shorter cycle
  69. private const int SERVER_EQ_TIME_NO_EVENTS = VIEWER_TIMEOUT - (10 * 1000);
  70. protected Scene m_scene;
  71. private Dictionary<UUID, int> m_ids = new Dictionary<UUID, int>();
  72. private Dictionary<UUID, Queue<OSD>> queues = new Dictionary<UUID, Queue<OSD>>();
  73. private Dictionary<UUID, UUID> m_QueueUUIDAvatarMapping = new Dictionary<UUID, UUID>();
  74. private Dictionary<UUID, UUID> m_AvatarQueueUUIDMapping = new Dictionary<UUID, UUID>();
  75. #region INonSharedRegionModule methods
  76. public virtual void Initialise(IConfigSource config)
  77. {
  78. }
  79. public void AddRegion(Scene scene)
  80. {
  81. m_scene = scene;
  82. scene.RegisterModuleInterface<IEventQueue>(this);
  83. scene.EventManager.OnClientClosed += ClientClosed;
  84. scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  85. MainConsole.Instance.Commands.AddCommand(
  86. "Debug",
  87. false,
  88. "debug eq",
  89. "debug eq [0|1|2]",
  90. "Turn on event queue debugging\n"
  91. + " <= 0 - turns off all event queue logging\n"
  92. + " >= 1 - turns on event queue setup and outgoing event logging\n"
  93. + " >= 2 - turns on poll notification",
  94. HandleDebugEq);
  95. MainConsole.Instance.Commands.AddCommand(
  96. "Debug",
  97. false,
  98. "show eq",
  99. "show eq",
  100. "Show contents of event queues for logged in avatars. Used for debugging.",
  101. HandleShowEq);
  102. }
  103. public void RemoveRegion(Scene scene)
  104. {
  105. if (m_scene != scene)
  106. return;
  107. scene.EventManager.OnClientClosed -= ClientClosed;
  108. scene.EventManager.OnRegisterCaps -= OnRegisterCaps;
  109. scene.UnregisterModuleInterface<IEventQueue>(this);
  110. m_scene = null;
  111. }
  112. public void RegionLoaded(Scene scene)
  113. {
  114. }
  115. public virtual void Close()
  116. {
  117. }
  118. public virtual string Name
  119. {
  120. get { return "EventQueueGetModule"; }
  121. }
  122. public Type ReplaceableInterface
  123. {
  124. get { return null; }
  125. }
  126. #endregion
  127. protected void HandleDebugEq(string module, string[] args)
  128. {
  129. int debugLevel;
  130. if (!(args.Length == 3 && int.TryParse(args[2], out debugLevel)))
  131. {
  132. MainConsole.Instance.OutputFormat("Usage: debug eq [0|1|2]");
  133. }
  134. else
  135. {
  136. DebugLevel = debugLevel;
  137. MainConsole.Instance.OutputFormat(
  138. "Set event queue debug level to {0} in {1}", DebugLevel, m_scene.RegionInfo.RegionName);
  139. }
  140. }
  141. protected void HandleShowEq(string module, string[] args)
  142. {
  143. MainConsole.Instance.OutputFormat("For scene {0}", m_scene.Name);
  144. lock (queues)
  145. {
  146. foreach (KeyValuePair<UUID, Queue<OSD>> kvp in queues)
  147. {
  148. MainConsole.Instance.OutputFormat(
  149. "For agent {0} there are {1} messages queued for send.",
  150. kvp.Key, kvp.Value.Count);
  151. }
  152. }
  153. }
  154. /// <summary>
  155. /// Always returns a valid queue
  156. /// </summary>
  157. /// <param name="agentId"></param>
  158. /// <returns></returns>
  159. private Queue<OSD> TryGetQueue(UUID agentId)
  160. {
  161. lock (queues)
  162. {
  163. if (!queues.ContainsKey(agentId))
  164. {
  165. if (DebugLevel > 0)
  166. m_log.DebugFormat(
  167. "[EVENTQUEUE]: Adding new queue for agent {0} in region {1}",
  168. agentId, m_scene.RegionInfo.RegionName);
  169. queues[agentId] = new Queue<OSD>();
  170. }
  171. return queues[agentId];
  172. }
  173. }
  174. /// <summary>
  175. /// May return a null queue
  176. /// </summary>
  177. /// <param name="agentId"></param>
  178. /// <returns></returns>
  179. private Queue<OSD> GetQueue(UUID agentId)
  180. {
  181. lock (queues)
  182. {
  183. if (queues.ContainsKey(agentId))
  184. {
  185. return queues[agentId];
  186. }
  187. else
  188. return null;
  189. }
  190. }
  191. #region IEventQueue Members
  192. public bool Enqueue(OSD ev, UUID avatarID)
  193. {
  194. //m_log.DebugFormat("[EVENTQUEUE]: Enqueuing event for {0} in region {1}", avatarID, m_scene.RegionInfo.RegionName);
  195. try
  196. {
  197. Queue<OSD> queue = GetQueue(avatarID);
  198. if (queue != null)
  199. {
  200. lock (queue)
  201. queue.Enqueue(ev);
  202. }
  203. else if (DebugLevel > 0)
  204. {
  205. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  206. // This assumes that an NPC should never have a queue.
  207. if (sp != null && sp.PresenceType != PresenceType.Npc)
  208. {
  209. OSDMap evMap = (OSDMap)ev;
  210. m_log.WarnFormat(
  211. "[EVENTQUEUE]: (Enqueue) No queue found for agent {0} {1} when placing message {2} in region {3}",
  212. sp.Name, sp.UUID, evMap["message"], m_scene.Name);
  213. }
  214. }
  215. }
  216. catch (NullReferenceException e)
  217. {
  218. m_log.Error("[EVENTQUEUE] Caught exception: " + e);
  219. return false;
  220. }
  221. return true;
  222. }
  223. #endregion
  224. private void ClientClosed(UUID agentID, Scene scene)
  225. {
  226. //m_log.DebugFormat("[EVENTQUEUE]: Closed client {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  227. lock (queues)
  228. queues.Remove(agentID);
  229. List<UUID> removeitems = new List<UUID>();
  230. lock (m_AvatarQueueUUIDMapping)
  231. m_AvatarQueueUUIDMapping.Remove(agentID);
  232. UUID searchval = UUID.Zero;
  233. removeitems.Clear();
  234. lock (m_QueueUUIDAvatarMapping)
  235. {
  236. foreach (UUID ky in m_QueueUUIDAvatarMapping.Keys)
  237. {
  238. searchval = m_QueueUUIDAvatarMapping[ky];
  239. if (searchval == agentID)
  240. {
  241. removeitems.Add(ky);
  242. }
  243. }
  244. foreach (UUID ky in removeitems)
  245. m_QueueUUIDAvatarMapping.Remove(ky);
  246. }
  247. // m_log.DebugFormat("[EVENTQUEUE]: Deleted queues for {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  248. }
  249. /// <summary>
  250. /// Generate an Event Queue Get handler path for the given eqg uuid.
  251. /// </summary>
  252. /// <param name='eqgUuid'></param>
  253. private string GenerateEqgCapPath(UUID eqgUuid)
  254. {
  255. return string.Format("/CAPS/EQG/{0}/", eqgUuid);
  256. }
  257. public void OnRegisterCaps(UUID agentID, Caps caps)
  258. {
  259. // Register an event queue for the client
  260. if (DebugLevel > 0)
  261. m_log.DebugFormat(
  262. "[EVENTQUEUE]: OnRegisterCaps: agentID {0} caps {1} region {2}",
  263. agentID, caps, m_scene.RegionInfo.RegionName);
  264. // Let's instantiate a Queue for this agent right now
  265. TryGetQueue(agentID);
  266. UUID eventQueueGetUUID;
  267. lock (m_AvatarQueueUUIDMapping)
  268. {
  269. // Reuse open queues. The client does!
  270. if (m_AvatarQueueUUIDMapping.ContainsKey(agentID))
  271. {
  272. //m_log.DebugFormat("[EVENTQUEUE]: Found Existing UUID!");
  273. eventQueueGetUUID = m_AvatarQueueUUIDMapping[agentID];
  274. }
  275. else
  276. {
  277. eventQueueGetUUID = UUID.Random();
  278. //m_log.DebugFormat("[EVENTQUEUE]: Using random UUID!");
  279. }
  280. }
  281. lock (m_QueueUUIDAvatarMapping)
  282. {
  283. if (!m_QueueUUIDAvatarMapping.ContainsKey(eventQueueGetUUID))
  284. m_QueueUUIDAvatarMapping.Add(eventQueueGetUUID, agentID);
  285. }
  286. lock (m_AvatarQueueUUIDMapping)
  287. {
  288. if (!m_AvatarQueueUUIDMapping.ContainsKey(agentID))
  289. m_AvatarQueueUUIDMapping.Add(agentID, eventQueueGetUUID);
  290. }
  291. caps.RegisterPollHandler(
  292. "EventQueueGet",
  293. new PollServiceEventArgs(null, GenerateEqgCapPath(eventQueueGetUUID), HasEvents, GetEvents, NoEvents, agentID, SERVER_EQ_TIME_NO_EVENTS));
  294. Random rnd = new Random(Environment.TickCount);
  295. lock (m_ids)
  296. {
  297. if (!m_ids.ContainsKey(agentID))
  298. m_ids.Add(agentID, rnd.Next(30000000));
  299. }
  300. }
  301. public bool HasEvents(UUID requestID, UUID agentID)
  302. {
  303. // Don't use this, because of race conditions at agent closing time
  304. //Queue<OSD> queue = TryGetQueue(agentID);
  305. Queue<OSD> queue = GetQueue(agentID);
  306. if (queue != null)
  307. lock (queue)
  308. {
  309. //m_log.WarnFormat("POLLED FOR EVENTS BY {0} in {1} -- {2}", agentID, m_scene.RegionInfo.RegionName, queue.Count);
  310. return queue.Count > 0;
  311. }
  312. return false;
  313. }
  314. /// <summary>
  315. /// Logs a debug line for an outbound event queue message if appropriate.
  316. /// </summary>
  317. /// <param name='element'>Element containing message</param>
  318. private void LogOutboundDebugMessage(OSD element, UUID agentId)
  319. {
  320. if (element is OSDMap)
  321. {
  322. OSDMap ev = (OSDMap)element;
  323. m_log.DebugFormat(
  324. "Eq OUT {0,-30} to {1,-20} {2,-20}",
  325. ev["message"], m_scene.GetScenePresence(agentId).Name, m_scene.Name);
  326. }
  327. }
  328. public Hashtable GetEvents(UUID requestID, UUID pAgentId)
  329. {
  330. if (DebugLevel >= 2)
  331. m_log.WarnFormat("POLLED FOR EQ MESSAGES BY {0} in {1}", pAgentId, m_scene.Name);
  332. Queue<OSD> queue = GetQueue(pAgentId);
  333. if (queue == null)
  334. {
  335. return NoEvents(requestID, pAgentId);
  336. }
  337. OSD element;
  338. lock (queue)
  339. {
  340. if (queue.Count == 0)
  341. return NoEvents(requestID, pAgentId);
  342. element = queue.Dequeue(); // 15s timeout
  343. }
  344. int thisID = 0;
  345. lock (m_ids)
  346. thisID = m_ids[pAgentId];
  347. OSDArray array = new OSDArray();
  348. if (element == null) // didn't have an event in 15s
  349. {
  350. // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  351. array.Add(EventQueueHelper.KeepAliveEvent());
  352. //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", pAgentId, m_scene.RegionInfo.RegionName);
  353. }
  354. else
  355. {
  356. if (DebugLevel > 0)
  357. LogOutboundDebugMessage(element, pAgentId);
  358. array.Add(element);
  359. lock (queue)
  360. {
  361. while (queue.Count > 0)
  362. {
  363. element = queue.Dequeue();
  364. if (DebugLevel > 0)
  365. LogOutboundDebugMessage(element, pAgentId);
  366. array.Add(element);
  367. thisID++;
  368. }
  369. }
  370. }
  371. OSDMap events = new OSDMap();
  372. events.Add("events", array);
  373. events.Add("id", new OSDInteger(thisID));
  374. lock (m_ids)
  375. {
  376. m_ids[pAgentId] = thisID + 1;
  377. }
  378. Hashtable responsedata = new Hashtable();
  379. responsedata["int_response_code"] = 200;
  380. responsedata["content_type"] = "application/xml";
  381. responsedata["keepalive"] = false;
  382. responsedata["reusecontext"] = false;
  383. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  384. //m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", pAgentId, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  385. return responsedata;
  386. }
  387. public Hashtable NoEvents(UUID requestID, UUID agentID)
  388. {
  389. Hashtable responsedata = new Hashtable();
  390. responsedata["int_response_code"] = 502;
  391. responsedata["content_type"] = "text/plain";
  392. responsedata["keepalive"] = false;
  393. responsedata["reusecontext"] = false;
  394. responsedata["str_response_string"] = "Upstream error: ";
  395. responsedata["error_status_text"] = "Upstream error:";
  396. responsedata["http_protocol_version"] = "HTTP/1.0";
  397. return responsedata;
  398. }
  399. // public Hashtable ProcessQueue(Hashtable request, UUID agentID, Caps caps)
  400. // {
  401. // // TODO: this has to be redone to not busy-wait (and block the thread),
  402. // // TODO: as soon as we have a non-blocking way to handle HTTP-requests.
  403. //
  404. //// if (m_log.IsDebugEnabled)
  405. //// {
  406. //// String debug = "[EVENTQUEUE]: Got request for agent {0} in region {1} from thread {2}: [ ";
  407. //// foreach (object key in request.Keys)
  408. //// {
  409. //// debug += key.ToString() + "=" + request[key].ToString() + " ";
  410. //// }
  411. //// m_log.DebugFormat(debug + " ]", agentID, m_scene.RegionInfo.RegionName, System.Threading.Thread.CurrentThread.Name);
  412. //// }
  413. //
  414. // Queue<OSD> queue = TryGetQueue(agentID);
  415. // OSD element;
  416. //
  417. // lock (queue)
  418. // element = queue.Dequeue(); // 15s timeout
  419. //
  420. // Hashtable responsedata = new Hashtable();
  421. //
  422. // int thisID = 0;
  423. // lock (m_ids)
  424. // thisID = m_ids[agentID];
  425. //
  426. // if (element == null)
  427. // {
  428. // //m_log.ErrorFormat("[EVENTQUEUE]: Nothing to process in " + m_scene.RegionInfo.RegionName);
  429. // if (thisID == -1) // close-request
  430. // {
  431. // m_log.ErrorFormat("[EVENTQUEUE]: 404 in " + m_scene.RegionInfo.RegionName);
  432. // responsedata["int_response_code"] = 404; //501; //410; //404;
  433. // responsedata["content_type"] = "text/plain";
  434. // responsedata["keepalive"] = false;
  435. // responsedata["str_response_string"] = "Closed EQG";
  436. // return responsedata;
  437. // }
  438. // responsedata["int_response_code"] = 502;
  439. // responsedata["content_type"] = "text/plain";
  440. // responsedata["keepalive"] = false;
  441. // responsedata["str_response_string"] = "Upstream error: ";
  442. // responsedata["error_status_text"] = "Upstream error:";
  443. // responsedata["http_protocol_version"] = "HTTP/1.0";
  444. // return responsedata;
  445. // }
  446. //
  447. // OSDArray array = new OSDArray();
  448. // if (element == null) // didn't have an event in 15s
  449. // {
  450. // // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  451. // array.Add(EventQueueHelper.KeepAliveEvent());
  452. // //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  453. // }
  454. // else
  455. // {
  456. // array.Add(element);
  457. //
  458. // if (element is OSDMap)
  459. // {
  460. // OSDMap ev = (OSDMap)element;
  461. // m_log.DebugFormat(
  462. // "[EVENT QUEUE GET MODULE]: Eq OUT {0} to {1}",
  463. // ev["message"], m_scene.GetScenePresence(agentID).Name);
  464. // }
  465. //
  466. // lock (queue)
  467. // {
  468. // while (queue.Count > 0)
  469. // {
  470. // element = queue.Dequeue();
  471. //
  472. // if (element is OSDMap)
  473. // {
  474. // OSDMap ev = (OSDMap)element;
  475. // m_log.DebugFormat(
  476. // "[EVENT QUEUE GET MODULE]: Eq OUT {0} to {1}",
  477. // ev["message"], m_scene.GetScenePresence(agentID).Name);
  478. // }
  479. //
  480. // array.Add(element);
  481. // thisID++;
  482. // }
  483. // }
  484. // }
  485. //
  486. // OSDMap events = new OSDMap();
  487. // events.Add("events", array);
  488. //
  489. // events.Add("id", new OSDInteger(thisID));
  490. // lock (m_ids)
  491. // {
  492. // m_ids[agentID] = thisID + 1;
  493. // }
  494. //
  495. // responsedata["int_response_code"] = 200;
  496. // responsedata["content_type"] = "application/xml";
  497. // responsedata["keepalive"] = false;
  498. // responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  499. //
  500. // m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", agentID, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  501. //
  502. // return responsedata;
  503. // }
  504. // public Hashtable EventQueuePath2(Hashtable request)
  505. // {
  506. // string capuuid = (string)request["uri"]; //path.Replace("/CAPS/EQG/","");
  507. // // pull off the last "/" in the path.
  508. // Hashtable responsedata = new Hashtable();
  509. // capuuid = capuuid.Substring(0, capuuid.Length - 1);
  510. // capuuid = capuuid.Replace("/CAPS/EQG/", "");
  511. // UUID AvatarID = UUID.Zero;
  512. // UUID capUUID = UUID.Zero;
  513. //
  514. // // parse the path and search for the avatar with it registered
  515. // if (UUID.TryParse(capuuid, out capUUID))
  516. // {
  517. // lock (m_QueueUUIDAvatarMapping)
  518. // {
  519. // if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  520. // {
  521. // AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  522. // }
  523. // }
  524. //
  525. // if (AvatarID != UUID.Zero)
  526. // {
  527. // return ProcessQueue(request, AvatarID, m_scene.CapsModule.GetCapsForUser(AvatarID));
  528. // }
  529. // else
  530. // {
  531. // responsedata["int_response_code"] = 404;
  532. // responsedata["content_type"] = "text/plain";
  533. // responsedata["keepalive"] = false;
  534. // responsedata["str_response_string"] = "Not Found";
  535. // responsedata["error_status_text"] = "Not Found";
  536. // responsedata["http_protocol_version"] = "HTTP/1.0";
  537. // return responsedata;
  538. // // return 404
  539. // }
  540. // }
  541. // else
  542. // {
  543. // responsedata["int_response_code"] = 404;
  544. // responsedata["content_type"] = "text/plain";
  545. // responsedata["keepalive"] = false;
  546. // responsedata["str_response_string"] = "Not Found";
  547. // responsedata["error_status_text"] = "Not Found";
  548. // responsedata["http_protocol_version"] = "HTTP/1.0";
  549. // return responsedata;
  550. // // return 404
  551. // }
  552. // }
  553. public OSD EventQueueFallBack(string path, OSD request, string endpoint)
  554. {
  555. // This is a fallback element to keep the client from loosing EventQueueGet
  556. // Why does CAPS fail sometimes!?
  557. m_log.Warn("[EVENTQUEUE]: In the Fallback handler! We lost the Queue in the rest handler!");
  558. string capuuid = path.Replace("/CAPS/EQG/","");
  559. capuuid = capuuid.Substring(0, capuuid.Length - 1);
  560. // UUID AvatarID = UUID.Zero;
  561. UUID capUUID = UUID.Zero;
  562. if (UUID.TryParse(capuuid, out capUUID))
  563. {
  564. /* Don't remove this yet code cleaners!
  565. * Still testing this!
  566. *
  567. lock (m_QueueUUIDAvatarMapping)
  568. {
  569. if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  570. {
  571. AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  572. }
  573. }
  574. if (AvatarID != UUID.Zero)
  575. {
  576. // Repair the CAP!
  577. //OpenSim.Framework.Capabilities.Caps caps = m_scene.GetCapsHandlerForUser(AvatarID);
  578. //string capsBase = "/CAPS/EQG/";
  579. //caps.RegisterHandler("EventQueueGet",
  580. //new RestHTTPHandler("POST", capsBase + capUUID.ToString() + "/",
  581. //delegate(Hashtable m_dhttpMethod)
  582. //{
  583. // return ProcessQueue(m_dhttpMethod, AvatarID, caps);
  584. //}));
  585. // start new ID sequence.
  586. Random rnd = new Random(System.Environment.TickCount);
  587. lock (m_ids)
  588. {
  589. if (!m_ids.ContainsKey(AvatarID))
  590. m_ids.Add(AvatarID, rnd.Next(30000000));
  591. }
  592. int thisID = 0;
  593. lock (m_ids)
  594. thisID = m_ids[AvatarID];
  595. BlockingLLSDQueue queue = GetQueue(AvatarID);
  596. OSDArray array = new OSDArray();
  597. LLSD element = queue.Dequeue(15000); // 15s timeout
  598. if (element == null)
  599. {
  600. array.Add(EventQueueHelper.KeepAliveEvent());
  601. }
  602. else
  603. {
  604. array.Add(element);
  605. while (queue.Count() > 0)
  606. {
  607. array.Add(queue.Dequeue(1));
  608. thisID++;
  609. }
  610. }
  611. OSDMap events = new OSDMap();
  612. events.Add("events", array);
  613. events.Add("id", new LLSDInteger(thisID));
  614. lock (m_ids)
  615. {
  616. m_ids[AvatarID] = thisID + 1;
  617. }
  618. return events;
  619. }
  620. else
  621. {
  622. return new LLSD();
  623. }
  624. *
  625. */
  626. }
  627. else
  628. {
  629. //return new LLSD();
  630. }
  631. return new OSDString("shutdown404!");
  632. }
  633. public void DisableSimulator(ulong handle, UUID avatarID)
  634. {
  635. OSD item = EventQueueHelper.DisableSimulator(handle);
  636. Enqueue(item, avatarID);
  637. }
  638. public virtual void EnableSimulator(ulong handle, IPEndPoint endPoint, UUID avatarID, int regionSizeX, int regionSizeY)
  639. {
  640. if (DebugLevel > 0)
  641. m_log.DebugFormat("{0} EnableSimulator. handle={1}, endPoint={2}, avatarID={3}",
  642. LogHeader, handle, endPoint, avatarID, regionSizeX, regionSizeY);
  643. OSD item = EventQueueHelper.EnableSimulator(handle, endPoint, regionSizeX, regionSizeY);
  644. Enqueue(item, avatarID);
  645. }
  646. public virtual void EstablishAgentCommunication(UUID avatarID, IPEndPoint endPoint, string capsPath,
  647. ulong regionHandle, int regionSizeX, int regionSizeY)
  648. {
  649. if (DebugLevel > 0)
  650. m_log.DebugFormat("{0} EstablishAgentCommunication. handle={1}, endPoint={2}, avatarID={3}",
  651. LogHeader, regionHandle, endPoint, avatarID, regionSizeX, regionSizeY);
  652. OSD item = EventQueueHelper.EstablishAgentCommunication(avatarID, endPoint.ToString(), capsPath, regionHandle, regionSizeX, regionSizeY);
  653. Enqueue(item, avatarID);
  654. }
  655. public virtual void TeleportFinishEvent(ulong regionHandle, byte simAccess,
  656. IPEndPoint regionExternalEndPoint,
  657. uint locationID, uint flags, string capsURL,
  658. UUID avatarID, int regionSizeX, int regionSizeY)
  659. {
  660. if (DebugLevel > 0)
  661. m_log.DebugFormat("{0} TeleportFinishEvent. handle={1}, endPoint={2}, avatarID={3}",
  662. LogHeader, regionHandle, regionExternalEndPoint, avatarID, regionSizeX, regionSizeY);
  663. OSD item = EventQueueHelper.TeleportFinishEvent(regionHandle, simAccess, regionExternalEndPoint,
  664. locationID, flags, capsURL, avatarID, regionSizeX, regionSizeY);
  665. Enqueue(item, avatarID);
  666. }
  667. public virtual void CrossRegion(ulong handle, Vector3 pos, Vector3 lookAt,
  668. IPEndPoint newRegionExternalEndPoint,
  669. string capsURL, UUID avatarID, UUID sessionID, int regionSizeX, int regionSizeY)
  670. {
  671. if (DebugLevel > 0)
  672. m_log.DebugFormat("{0} CrossRegion. handle={1}, avatarID={2}, regionSize={3},{4}>",
  673. LogHeader, handle, avatarID, regionSizeX, regionSizeY);
  674. OSD item = EventQueueHelper.CrossRegion(handle, pos, lookAt, newRegionExternalEndPoint,
  675. capsURL, avatarID, sessionID, regionSizeX, regionSizeY);
  676. Enqueue(item, avatarID);
  677. }
  678. public void ChatterboxInvitation(UUID sessionID, string sessionName,
  679. UUID fromAgent, string message, UUID toAgent, string fromName, byte dialog,
  680. uint timeStamp, bool offline, int parentEstateID, Vector3 position,
  681. uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  682. {
  683. OSD item = EventQueueHelper.ChatterboxInvitation(sessionID, sessionName, fromAgent, message, toAgent, fromName, dialog,
  684. timeStamp, offline, parentEstateID, position, ttl, transactionID,
  685. fromGroup, binaryBucket);
  686. Enqueue(item, toAgent);
  687. //m_log.InfoFormat("########### eq ChatterboxInvitation #############\n{0}", item);
  688. }
  689. public void ChatterBoxSessionAgentListUpdates(UUID sessionID, UUID fromAgent, UUID anotherAgent, bool canVoiceChat,
  690. bool isModerator, bool textMute)
  691. {
  692. OSD item = EventQueueHelper.ChatterBoxSessionAgentListUpdates(sessionID, fromAgent, canVoiceChat,
  693. isModerator, textMute);
  694. Enqueue(item, fromAgent);
  695. //m_log.InfoFormat("########### eq ChatterBoxSessionAgentListUpdates #############\n{0}", item);
  696. }
  697. public void ParcelProperties(ParcelPropertiesMessage parcelPropertiesMessage, UUID avatarID)
  698. {
  699. OSD item = EventQueueHelper.ParcelProperties(parcelPropertiesMessage);
  700. Enqueue(item, avatarID);
  701. }
  702. public void GroupMembership(AgentGroupDataUpdatePacket groupUpdate, UUID avatarID)
  703. {
  704. OSD item = EventQueueHelper.GroupMembership(groupUpdate);
  705. Enqueue(item, avatarID);
  706. }
  707. public void QueryReply(PlacesReplyPacket groupUpdate, UUID avatarID)
  708. {
  709. OSD item = EventQueueHelper.PlacesQuery(groupUpdate);
  710. Enqueue(item, avatarID);
  711. }
  712. public OSD ScriptRunningEvent(UUID objectID, UUID itemID, bool running, bool mono)
  713. {
  714. return EventQueueHelper.ScriptRunningReplyEvent(objectID, itemID, running, mono);
  715. }
  716. public OSD BuildEvent(string eventName, OSD eventBody)
  717. {
  718. return EventQueueHelper.BuildEvent(eventName, eventBody);
  719. }
  720. public void partPhysicsProperties(uint localID, byte physhapetype,
  721. float density, float friction, float bounce, float gravmod,UUID avatarID)
  722. {
  723. OSD item = EventQueueHelper.partPhysicsProperties(localID, physhapetype,
  724. density, friction, bounce, gravmod);
  725. Enqueue(item, avatarID);
  726. }
  727. }
  728. }