1
0

EventQueueGetModule.cs 32 KB

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