EventQueueGetModule.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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 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. m_log.DebugFormat(
  166. "[EVENTQUEUE]: Adding new queue for agent {0} in region {1}",
  167. agentId, m_scene.RegionInfo.RegionName);
  168. queues[agentId] = new Queue<OSD>();
  169. }
  170. return queues[agentId];
  171. }
  172. }
  173. /// <summary>
  174. /// May return a null queue
  175. /// </summary>
  176. /// <param name="agentId"></param>
  177. /// <returns></returns>
  178. private Queue<OSD> GetQueue(UUID agentId)
  179. {
  180. lock (queues)
  181. {
  182. if (queues.ContainsKey(agentId))
  183. {
  184. return queues[agentId];
  185. }
  186. else
  187. return null;
  188. }
  189. }
  190. #region IEventQueue Members
  191. public bool Enqueue(OSD ev, UUID avatarID)
  192. {
  193. //m_log.DebugFormat("[EVENTQUEUE]: Enqueuing event for {0} in region {1}", avatarID, m_scene.RegionInfo.RegionName);
  194. try
  195. {
  196. Queue<OSD> queue = GetQueue(avatarID);
  197. if (queue != null)
  198. {
  199. lock (queue)
  200. queue.Enqueue(ev);
  201. }
  202. else if (DebugLevel > 0)
  203. {
  204. ScenePresence sp = m_scene.GetScenePresence(avatarID);
  205. // This assumes that an NPC should never have a queue.
  206. if (sp != null && sp.PresenceType != PresenceType.Npc)
  207. {
  208. OSDMap evMap = (OSDMap)ev;
  209. m_log.WarnFormat(
  210. "[EVENTQUEUE]: (Enqueue) No queue found for agent {0} {1} when placing message {2} in region {3}",
  211. sp.Name, sp.UUID, evMap["message"], m_scene.Name);
  212. }
  213. }
  214. }
  215. catch (NullReferenceException e)
  216. {
  217. m_log.Error("[EVENTQUEUE] Caught exception: " + e);
  218. return false;
  219. }
  220. return true;
  221. }
  222. #endregion
  223. private void ClientClosed(UUID agentID, Scene scene)
  224. {
  225. //m_log.DebugFormat("[EVENTQUEUE]: Closed client {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  226. lock (queues)
  227. queues.Remove(agentID);
  228. List<UUID> removeitems = new List<UUID>();
  229. lock (m_AvatarQueueUUIDMapping)
  230. m_AvatarQueueUUIDMapping.Remove(agentID);
  231. UUID searchval = UUID.Zero;
  232. removeitems.Clear();
  233. lock (m_QueueUUIDAvatarMapping)
  234. {
  235. foreach (UUID ky in m_QueueUUIDAvatarMapping.Keys)
  236. {
  237. searchval = m_QueueUUIDAvatarMapping[ky];
  238. if (searchval == agentID)
  239. {
  240. removeitems.Add(ky);
  241. }
  242. }
  243. foreach (UUID ky in removeitems)
  244. m_QueueUUIDAvatarMapping.Remove(ky);
  245. }
  246. // m_log.DebugFormat("[EVENTQUEUE]: Deleted queues for {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  247. }
  248. /// <summary>
  249. /// Generate an Event Queue Get handler path for the given eqg uuid.
  250. /// </summary>
  251. /// <param name='eqgUuid'></param>
  252. private string GenerateEqgCapPath(UUID eqgUuid)
  253. {
  254. return string.Format("/CAPS/EQG/{0}/", eqgUuid);
  255. }
  256. public void OnRegisterCaps(UUID agentID, Caps caps)
  257. {
  258. // Register an event queue for the client
  259. m_log.DebugFormat(
  260. "[EVENTQUEUE]: OnRegisterCaps: agentID {0} caps {1} region {2}",
  261. agentID, caps, m_scene.RegionInfo.RegionName);
  262. // Let's instantiate a Queue for this agent right now
  263. TryGetQueue(agentID);
  264. UUID eventQueueGetUUID;
  265. lock (m_AvatarQueueUUIDMapping)
  266. {
  267. // Reuse open queues. The client does!
  268. if (m_AvatarQueueUUIDMapping.ContainsKey(agentID))
  269. {
  270. //m_log.DebugFormat("[EVENTQUEUE]: Found Existing UUID!");
  271. eventQueueGetUUID = m_AvatarQueueUUIDMapping[agentID];
  272. }
  273. else
  274. {
  275. eventQueueGetUUID = UUID.Random();
  276. //m_log.DebugFormat("[EVENTQUEUE]: Using random UUID!");
  277. }
  278. }
  279. lock (m_QueueUUIDAvatarMapping)
  280. {
  281. if (!m_QueueUUIDAvatarMapping.ContainsKey(eventQueueGetUUID))
  282. m_QueueUUIDAvatarMapping.Add(eventQueueGetUUID, agentID);
  283. }
  284. lock (m_AvatarQueueUUIDMapping)
  285. {
  286. if (!m_AvatarQueueUUIDMapping.ContainsKey(agentID))
  287. m_AvatarQueueUUIDMapping.Add(agentID, eventQueueGetUUID);
  288. }
  289. caps.RegisterPollHandler(
  290. "EventQueueGet",
  291. new PollServiceEventArgs(null, GenerateEqgCapPath(eventQueueGetUUID), HasEvents, GetEvents, NoEvents, agentID, SERVER_EQ_TIME_NO_EVENTS));
  292. Random rnd = new Random(Environment.TickCount);
  293. lock (m_ids)
  294. {
  295. if (!m_ids.ContainsKey(agentID))
  296. m_ids.Add(agentID, rnd.Next(30000000));
  297. }
  298. }
  299. public bool HasEvents(UUID requestID, UUID agentID)
  300. {
  301. // Don't use this, because of race conditions at agent closing time
  302. //Queue<OSD> queue = TryGetQueue(agentID);
  303. Queue<OSD> queue = GetQueue(agentID);
  304. if (queue != null)
  305. lock (queue)
  306. {
  307. //m_log.WarnFormat("POLLED FOR EVENTS BY {0} in {1} -- {2}", agentID, m_scene.RegionInfo.RegionName, queue.Count);
  308. return queue.Count > 0;
  309. }
  310. return false;
  311. }
  312. /// <summary>
  313. /// Logs a debug line for an outbound event queue message if appropriate.
  314. /// </summary>
  315. /// <param name='element'>Element containing message</param>
  316. private void LogOutboundDebugMessage(OSD element, UUID agentId)
  317. {
  318. if (element is OSDMap)
  319. {
  320. OSDMap ev = (OSDMap)element;
  321. m_log.DebugFormat(
  322. "Eq OUT {0,-30} to {1,-20} {2,-20}",
  323. ev["message"], m_scene.GetScenePresence(agentId).Name, m_scene.Name);
  324. }
  325. }
  326. public Hashtable GetEvents(UUID requestID, UUID pAgentId)
  327. {
  328. if (DebugLevel >= 2)
  329. m_log.WarnFormat("POLLED FOR EQ MESSAGES BY {0} in {1}", pAgentId, m_scene.Name);
  330. Queue<OSD> queue = GetQueue(pAgentId);
  331. if (queue == null)
  332. {
  333. return NoEvents(requestID, pAgentId);
  334. }
  335. OSD element;
  336. lock (queue)
  337. {
  338. if (queue.Count == 0)
  339. return NoEvents(requestID, pAgentId);
  340. element = queue.Dequeue(); // 15s timeout
  341. }
  342. int thisID = 0;
  343. lock (m_ids)
  344. thisID = m_ids[pAgentId];
  345. OSDArray array = new OSDArray();
  346. if (element == null) // didn't have an event in 15s
  347. {
  348. // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  349. array.Add(EventQueueHelper.KeepAliveEvent());
  350. //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", pAgentId, m_scene.RegionInfo.RegionName);
  351. }
  352. else
  353. {
  354. if (DebugLevel > 0)
  355. LogOutboundDebugMessage(element, pAgentId);
  356. array.Add(element);
  357. lock (queue)
  358. {
  359. while (queue.Count > 0)
  360. {
  361. element = queue.Dequeue();
  362. if (DebugLevel > 0)
  363. LogOutboundDebugMessage(element, pAgentId);
  364. array.Add(element);
  365. thisID++;
  366. }
  367. }
  368. }
  369. OSDMap events = new OSDMap();
  370. events.Add("events", array);
  371. events.Add("id", new OSDInteger(thisID));
  372. lock (m_ids)
  373. {
  374. m_ids[pAgentId] = thisID + 1;
  375. }
  376. Hashtable responsedata = new Hashtable();
  377. responsedata["int_response_code"] = 200;
  378. responsedata["content_type"] = "application/xml";
  379. responsedata["keepalive"] = false;
  380. responsedata["reusecontext"] = false;
  381. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  382. //m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", pAgentId, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  383. return responsedata;
  384. }
  385. public Hashtable NoEvents(UUID requestID, UUID agentID)
  386. {
  387. Hashtable responsedata = new Hashtable();
  388. responsedata["int_response_code"] = 502;
  389. responsedata["content_type"] = "text/plain";
  390. responsedata["keepalive"] = false;
  391. responsedata["reusecontext"] = false;
  392. responsedata["str_response_string"] = "Upstream error: ";
  393. responsedata["error_status_text"] = "Upstream error:";
  394. responsedata["http_protocol_version"] = "HTTP/1.0";
  395. return responsedata;
  396. }
  397. // public Hashtable ProcessQueue(Hashtable request, UUID agentID, Caps caps)
  398. // {
  399. // // TODO: this has to be redone to not busy-wait (and block the thread),
  400. // // TODO: as soon as we have a non-blocking way to handle HTTP-requests.
  401. //
  402. //// if (m_log.IsDebugEnabled)
  403. //// {
  404. //// String debug = "[EVENTQUEUE]: Got request for agent {0} in region {1} from thread {2}: [ ";
  405. //// foreach (object key in request.Keys)
  406. //// {
  407. //// debug += key.ToString() + "=" + request[key].ToString() + " ";
  408. //// }
  409. //// m_log.DebugFormat(debug + " ]", agentID, m_scene.RegionInfo.RegionName, System.Threading.Thread.CurrentThread.Name);
  410. //// }
  411. //
  412. // Queue<OSD> queue = TryGetQueue(agentID);
  413. // OSD element;
  414. //
  415. // lock (queue)
  416. // element = queue.Dequeue(); // 15s timeout
  417. //
  418. // Hashtable responsedata = new Hashtable();
  419. //
  420. // int thisID = 0;
  421. // lock (m_ids)
  422. // thisID = m_ids[agentID];
  423. //
  424. // if (element == null)
  425. // {
  426. // //m_log.ErrorFormat("[EVENTQUEUE]: Nothing to process in " + m_scene.RegionInfo.RegionName);
  427. // if (thisID == -1) // close-request
  428. // {
  429. // m_log.ErrorFormat("[EVENTQUEUE]: 404 in " + m_scene.RegionInfo.RegionName);
  430. // responsedata["int_response_code"] = 404; //501; //410; //404;
  431. // responsedata["content_type"] = "text/plain";
  432. // responsedata["keepalive"] = false;
  433. // responsedata["str_response_string"] = "Closed EQG";
  434. // return responsedata;
  435. // }
  436. // responsedata["int_response_code"] = 502;
  437. // responsedata["content_type"] = "text/plain";
  438. // responsedata["keepalive"] = false;
  439. // responsedata["str_response_string"] = "Upstream error: ";
  440. // responsedata["error_status_text"] = "Upstream error:";
  441. // responsedata["http_protocol_version"] = "HTTP/1.0";
  442. // return responsedata;
  443. // }
  444. //
  445. // OSDArray array = new OSDArray();
  446. // if (element == null) // didn't have an event in 15s
  447. // {
  448. // // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  449. // array.Add(EventQueueHelper.KeepAliveEvent());
  450. // //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  451. // }
  452. // else
  453. // {
  454. // array.Add(element);
  455. //
  456. // if (element is OSDMap)
  457. // {
  458. // OSDMap ev = (OSDMap)element;
  459. // m_log.DebugFormat(
  460. // "[EVENT QUEUE GET MODULE]: Eq OUT {0} to {1}",
  461. // ev["message"], m_scene.GetScenePresence(agentID).Name);
  462. // }
  463. //
  464. // lock (queue)
  465. // {
  466. // while (queue.Count > 0)
  467. // {
  468. // element = queue.Dequeue();
  469. //
  470. // if (element is OSDMap)
  471. // {
  472. // OSDMap ev = (OSDMap)element;
  473. // m_log.DebugFormat(
  474. // "[EVENT QUEUE GET MODULE]: Eq OUT {0} to {1}",
  475. // ev["message"], m_scene.GetScenePresence(agentID).Name);
  476. // }
  477. //
  478. // array.Add(element);
  479. // thisID++;
  480. // }
  481. // }
  482. // }
  483. //
  484. // OSDMap events = new OSDMap();
  485. // events.Add("events", array);
  486. //
  487. // events.Add("id", new OSDInteger(thisID));
  488. // lock (m_ids)
  489. // {
  490. // m_ids[agentID] = thisID + 1;
  491. // }
  492. //
  493. // responsedata["int_response_code"] = 200;
  494. // responsedata["content_type"] = "application/xml";
  495. // responsedata["keepalive"] = false;
  496. // responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  497. //
  498. // m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", agentID, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  499. //
  500. // return responsedata;
  501. // }
  502. // public Hashtable EventQueuePath2(Hashtable request)
  503. // {
  504. // string capuuid = (string)request["uri"]; //path.Replace("/CAPS/EQG/","");
  505. // // pull off the last "/" in the path.
  506. // Hashtable responsedata = new Hashtable();
  507. // capuuid = capuuid.Substring(0, capuuid.Length - 1);
  508. // capuuid = capuuid.Replace("/CAPS/EQG/", "");
  509. // UUID AvatarID = UUID.Zero;
  510. // UUID capUUID = UUID.Zero;
  511. //
  512. // // parse the path and search for the avatar with it registered
  513. // if (UUID.TryParse(capuuid, out capUUID))
  514. // {
  515. // lock (m_QueueUUIDAvatarMapping)
  516. // {
  517. // if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  518. // {
  519. // AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  520. // }
  521. // }
  522. //
  523. // if (AvatarID != UUID.Zero)
  524. // {
  525. // return ProcessQueue(request, AvatarID, m_scene.CapsModule.GetCapsForUser(AvatarID));
  526. // }
  527. // else
  528. // {
  529. // responsedata["int_response_code"] = 404;
  530. // responsedata["content_type"] = "text/plain";
  531. // responsedata["keepalive"] = false;
  532. // responsedata["str_response_string"] = "Not Found";
  533. // responsedata["error_status_text"] = "Not Found";
  534. // responsedata["http_protocol_version"] = "HTTP/1.0";
  535. // return responsedata;
  536. // // return 404
  537. // }
  538. // }
  539. // else
  540. // {
  541. // responsedata["int_response_code"] = 404;
  542. // responsedata["content_type"] = "text/plain";
  543. // responsedata["keepalive"] = false;
  544. // responsedata["str_response_string"] = "Not Found";
  545. // responsedata["error_status_text"] = "Not Found";
  546. // responsedata["http_protocol_version"] = "HTTP/1.0";
  547. // return responsedata;
  548. // // return 404
  549. // }
  550. // }
  551. public OSD EventQueueFallBack(string path, OSD request, string endpoint)
  552. {
  553. // This is a fallback element to keep the client from loosing EventQueueGet
  554. // Why does CAPS fail sometimes!?
  555. m_log.Warn("[EVENTQUEUE]: In the Fallback handler! We lost the Queue in the rest handler!");
  556. string capuuid = path.Replace("/CAPS/EQG/","");
  557. capuuid = capuuid.Substring(0, capuuid.Length - 1);
  558. // UUID AvatarID = UUID.Zero;
  559. UUID capUUID = UUID.Zero;
  560. if (UUID.TryParse(capuuid, out capUUID))
  561. {
  562. /* Don't remove this yet code cleaners!
  563. * Still testing this!
  564. *
  565. lock (m_QueueUUIDAvatarMapping)
  566. {
  567. if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  568. {
  569. AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  570. }
  571. }
  572. if (AvatarID != UUID.Zero)
  573. {
  574. // Repair the CAP!
  575. //OpenSim.Framework.Capabilities.Caps caps = m_scene.GetCapsHandlerForUser(AvatarID);
  576. //string capsBase = "/CAPS/EQG/";
  577. //caps.RegisterHandler("EventQueueGet",
  578. //new RestHTTPHandler("POST", capsBase + capUUID.ToString() + "/",
  579. //delegate(Hashtable m_dhttpMethod)
  580. //{
  581. // return ProcessQueue(m_dhttpMethod, AvatarID, caps);
  582. //}));
  583. // start new ID sequence.
  584. Random rnd = new Random(System.Environment.TickCount);
  585. lock (m_ids)
  586. {
  587. if (!m_ids.ContainsKey(AvatarID))
  588. m_ids.Add(AvatarID, rnd.Next(30000000));
  589. }
  590. int thisID = 0;
  591. lock (m_ids)
  592. thisID = m_ids[AvatarID];
  593. BlockingLLSDQueue queue = GetQueue(AvatarID);
  594. OSDArray array = new OSDArray();
  595. LLSD element = queue.Dequeue(15000); // 15s timeout
  596. if (element == null)
  597. {
  598. array.Add(EventQueueHelper.KeepAliveEvent());
  599. }
  600. else
  601. {
  602. array.Add(element);
  603. while (queue.Count() > 0)
  604. {
  605. array.Add(queue.Dequeue(1));
  606. thisID++;
  607. }
  608. }
  609. OSDMap events = new OSDMap();
  610. events.Add("events", array);
  611. events.Add("id", new LLSDInteger(thisID));
  612. lock (m_ids)
  613. {
  614. m_ids[AvatarID] = thisID + 1;
  615. }
  616. return events;
  617. }
  618. else
  619. {
  620. return new LLSD();
  621. }
  622. *
  623. */
  624. }
  625. else
  626. {
  627. //return new LLSD();
  628. }
  629. return new OSDString("shutdown404!");
  630. }
  631. public void DisableSimulator(ulong handle, UUID avatarID)
  632. {
  633. OSD item = EventQueueHelper.DisableSimulator(handle);
  634. Enqueue(item, avatarID);
  635. }
  636. public virtual void EnableSimulator(ulong handle, IPEndPoint endPoint, UUID avatarID, int regionSizeX, int regionSizeY)
  637. {
  638. m_log.DebugFormat("{0} EnableSimulator. handle={1}, avatarID={2}, regionSize={3},{4}>",
  639. LogHeader, handle, avatarID, regionSizeX, regionSizeY);
  640. OSD item = EventQueueHelper.EnableSimulator(handle, endPoint, regionSizeX, regionSizeY);
  641. Enqueue(item, avatarID);
  642. }
  643. public virtual void EstablishAgentCommunication(UUID avatarID, IPEndPoint endPoint, string capsPath,
  644. ulong regionHandle, int regionSizeX, int regionSizeY)
  645. {
  646. m_log.DebugFormat("{0} EstablishAgentCommunication. handle={1}, avatarID={2}, regionSize={3},{4}>",
  647. LogHeader, regionHandle, avatarID, regionSizeX, regionSizeY);
  648. OSD item = EventQueueHelper.EstablishAgentCommunication(avatarID, endPoint.ToString(), capsPath, regionHandle, regionSizeX, regionSizeY);
  649. Enqueue(item, avatarID);
  650. }
  651. public virtual void TeleportFinishEvent(ulong regionHandle, byte simAccess,
  652. IPEndPoint regionExternalEndPoint,
  653. uint locationID, uint flags, string capsURL,
  654. UUID avatarID, int regionSizeX, int regionSizeY)
  655. {
  656. m_log.DebugFormat("{0} TeleportFinishEvent. handle={1}, avatarID={2}, regionSize=<{3},{4}>",
  657. LogHeader, regionHandle, avatarID, regionSizeX, regionSizeY);
  658. OSD item = EventQueueHelper.TeleportFinishEvent(regionHandle, simAccess, regionExternalEndPoint,
  659. locationID, flags, capsURL, avatarID, regionSizeX, regionSizeY);
  660. Enqueue(item, avatarID);
  661. }
  662. public virtual void CrossRegion(ulong handle, Vector3 pos, Vector3 lookAt,
  663. IPEndPoint newRegionExternalEndPoint,
  664. string capsURL, UUID avatarID, UUID sessionID, int regionSizeX, int regionSizeY)
  665. {
  666. m_log.DebugFormat("{0} CrossRegion. handle={1}, avatarID={2}, regionSize={3},{4}>",
  667. LogHeader, handle, avatarID, regionSizeX, regionSizeY);
  668. OSD item = EventQueueHelper.CrossRegion(handle, pos, lookAt, newRegionExternalEndPoint,
  669. capsURL, avatarID, sessionID, regionSizeX, regionSizeY);
  670. Enqueue(item, avatarID);
  671. }
  672. public void ChatterboxInvitation(UUID sessionID, string sessionName,
  673. UUID fromAgent, string message, UUID toAgent, string fromName, byte dialog,
  674. uint timeStamp, bool offline, int parentEstateID, Vector3 position,
  675. uint ttl, UUID transactionID, bool fromGroup, byte[] binaryBucket)
  676. {
  677. OSD item = EventQueueHelper.ChatterboxInvitation(sessionID, sessionName, fromAgent, message, toAgent, fromName, dialog,
  678. timeStamp, offline, parentEstateID, position, ttl, transactionID,
  679. fromGroup, binaryBucket);
  680. Enqueue(item, toAgent);
  681. //m_log.InfoFormat("########### eq ChatterboxInvitation #############\n{0}", item);
  682. }
  683. public void ChatterBoxSessionAgentListUpdates(UUID sessionID, UUID fromAgent, UUID anotherAgent, bool canVoiceChat,
  684. bool isModerator, bool textMute)
  685. {
  686. OSD item = EventQueueHelper.ChatterBoxSessionAgentListUpdates(sessionID, fromAgent, canVoiceChat,
  687. isModerator, textMute);
  688. Enqueue(item, fromAgent);
  689. //m_log.InfoFormat("########### eq ChatterBoxSessionAgentListUpdates #############\n{0}", item);
  690. }
  691. public void ParcelProperties(ParcelPropertiesMessage parcelPropertiesMessage, UUID avatarID)
  692. {
  693. OSD item = EventQueueHelper.ParcelProperties(parcelPropertiesMessage);
  694. Enqueue(item, avatarID);
  695. }
  696. public void GroupMembership(AgentGroupDataUpdatePacket groupUpdate, UUID avatarID)
  697. {
  698. OSD item = EventQueueHelper.GroupMembership(groupUpdate);
  699. Enqueue(item, avatarID);
  700. }
  701. public void QueryReply(PlacesReplyPacket groupUpdate, UUID avatarID)
  702. {
  703. OSD item = EventQueueHelper.PlacesQuery(groupUpdate);
  704. Enqueue(item, avatarID);
  705. }
  706. public OSD ScriptRunningEvent(UUID objectID, UUID itemID, bool running, bool mono)
  707. {
  708. return EventQueueHelper.ScriptRunningReplyEvent(objectID, itemID, running, mono);
  709. }
  710. public OSD BuildEvent(string eventName, OSD eventBody)
  711. {
  712. return EventQueueHelper.BuildEvent(eventName, eventBody);
  713. }
  714. public void partPhysicsProperties(uint localID, byte physhapetype,
  715. float density, float friction, float bounce, float gravmod,UUID avatarID)
  716. {
  717. OSD item = EventQueueHelper.partPhysicsProperties(localID, physhapetype,
  718. density, friction, bounce, gravmod);
  719. Enqueue(item, avatarID);
  720. }
  721. }
  722. }