EventQueueGetModule.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. /// <value>
  60. /// Debug level.
  61. /// </value>
  62. public int DebugLevel { get; set; }
  63. protected Scene m_scene;
  64. private Dictionary<UUID, int> m_ids = new Dictionary<UUID, int>();
  65. private Dictionary<UUID, Queue<OSD>> queues = new Dictionary<UUID, Queue<OSD>>();
  66. private Dictionary<UUID, UUID> m_QueueUUIDAvatarMapping = new Dictionary<UUID, UUID>();
  67. private Dictionary<UUID, UUID> m_AvatarQueueUUIDMapping = new Dictionary<UUID, UUID>();
  68. #region INonSharedRegionModule methods
  69. public virtual void Initialise(IConfigSource config)
  70. {
  71. }
  72. public void AddRegion(Scene scene)
  73. {
  74. m_scene = scene;
  75. scene.RegisterModuleInterface<IEventQueue>(this);
  76. scene.EventManager.OnClientClosed += ClientClosed;
  77. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  78. scene.EventManager.OnRegisterCaps += OnRegisterCaps;
  79. MainConsole.Instance.Commands.AddCommand(
  80. "Debug",
  81. false,
  82. "debug eq",
  83. "debug eq [0|1|2]",
  84. "Turn on event queue debugging\n"
  85. + " <= 0 - turns off all event queue logging\n"
  86. + " >= 1 - turns on outgoing event logging\n"
  87. + " >= 2 - turns on poll notification",
  88. HandleDebugEq);
  89. }
  90. public void RemoveRegion(Scene scene)
  91. {
  92. if (m_scene != scene)
  93. return;
  94. scene.EventManager.OnClientClosed -= ClientClosed;
  95. scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
  96. scene.EventManager.OnRegisterCaps -= OnRegisterCaps;
  97. scene.UnregisterModuleInterface<IEventQueue>(this);
  98. m_scene = null;
  99. }
  100. public void RegionLoaded(Scene scene)
  101. {
  102. }
  103. public virtual void Close()
  104. {
  105. }
  106. public virtual string Name
  107. {
  108. get { return "EventQueueGetModule"; }
  109. }
  110. public Type ReplaceableInterface
  111. {
  112. get { return null; }
  113. }
  114. #endregion
  115. protected void HandleDebugEq(string module, string[] args)
  116. {
  117. int debugLevel;
  118. if (!(args.Length == 3 && int.TryParse(args[2], out debugLevel)))
  119. {
  120. MainConsole.Instance.OutputFormat("Usage: debug eq [0|1]");
  121. }
  122. else
  123. {
  124. DebugLevel = debugLevel;
  125. MainConsole.Instance.OutputFormat(
  126. "Set event queue debug level to {0} in {1}", DebugLevel, m_scene.RegionInfo.RegionName);
  127. }
  128. }
  129. /// <summary>
  130. /// Always returns a valid queue
  131. /// </summary>
  132. /// <param name="agentId"></param>
  133. /// <returns></returns>
  134. private Queue<OSD> TryGetQueue(UUID agentId)
  135. {
  136. lock (queues)
  137. {
  138. if (!queues.ContainsKey(agentId))
  139. {
  140. /*
  141. m_log.DebugFormat(
  142. "[EVENTQUEUE]: Adding new queue for agent {0} in region {1}",
  143. agentId, m_scene.RegionInfo.RegionName);
  144. */
  145. queues[agentId] = new Queue<OSD>();
  146. }
  147. return queues[agentId];
  148. }
  149. }
  150. /// <summary>
  151. /// May return a null queue
  152. /// </summary>
  153. /// <param name="agentId"></param>
  154. /// <returns></returns>
  155. private Queue<OSD> GetQueue(UUID agentId)
  156. {
  157. lock (queues)
  158. {
  159. if (queues.ContainsKey(agentId))
  160. {
  161. return queues[agentId];
  162. }
  163. else
  164. return null;
  165. }
  166. }
  167. #region IEventQueue Members
  168. public bool Enqueue(OSD ev, UUID avatarID)
  169. {
  170. //m_log.DebugFormat("[EVENTQUEUE]: Enqueuing event for {0} in region {1}", avatarID, m_scene.RegionInfo.RegionName);
  171. try
  172. {
  173. Queue<OSD> queue = GetQueue(avatarID);
  174. if (queue != null)
  175. lock (queue)
  176. queue.Enqueue(ev);
  177. }
  178. catch (NullReferenceException e)
  179. {
  180. m_log.Error("[EVENTQUEUE] Caught exception: " + e);
  181. return false;
  182. }
  183. return true;
  184. }
  185. #endregion
  186. private void ClientClosed(UUID agentID, Scene scene)
  187. {
  188. // m_log.DebugFormat("[EVENTQUEUE]: Closed client {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  189. int count = 0;
  190. while (queues.ContainsKey(agentID) && queues[agentID].Count > 0 && count++ < 5)
  191. {
  192. Thread.Sleep(1000);
  193. }
  194. lock (queues)
  195. {
  196. queues.Remove(agentID);
  197. }
  198. List<UUID> removeitems = new List<UUID>();
  199. lock (m_AvatarQueueUUIDMapping)
  200. {
  201. foreach (UUID ky in m_AvatarQueueUUIDMapping.Keys)
  202. {
  203. // m_log.DebugFormat("[EVENTQUEUE]: Found key {0} in m_AvatarQueueUUIDMapping while looking for {1}", ky, AgentID);
  204. if (ky == agentID)
  205. {
  206. removeitems.Add(ky);
  207. }
  208. }
  209. foreach (UUID ky in removeitems)
  210. {
  211. UUID eventQueueGetUuid = m_AvatarQueueUUIDMapping[ky];
  212. m_AvatarQueueUUIDMapping.Remove(ky);
  213. string eqgPath = GenerateEqgCapPath(eventQueueGetUuid);
  214. MainServer.Instance.RemovePollServiceHTTPHandler("", eqgPath);
  215. // m_log.DebugFormat(
  216. // "[EVENT QUEUE GET MODULE]: Removed EQG handler {0} for {1} in {2}",
  217. // eqgPath, agentID, m_scene.RegionInfo.RegionName);
  218. }
  219. }
  220. UUID searchval = UUID.Zero;
  221. removeitems.Clear();
  222. lock (m_QueueUUIDAvatarMapping)
  223. {
  224. foreach (UUID ky in m_QueueUUIDAvatarMapping.Keys)
  225. {
  226. searchval = m_QueueUUIDAvatarMapping[ky];
  227. if (searchval == agentID)
  228. {
  229. removeitems.Add(ky);
  230. }
  231. }
  232. foreach (UUID ky in removeitems)
  233. m_QueueUUIDAvatarMapping.Remove(ky);
  234. }
  235. }
  236. private void MakeChildAgent(ScenePresence avatar)
  237. {
  238. //m_log.DebugFormat("[EVENTQUEUE]: Make Child agent {0} in region {1}.", avatar.UUID, m_scene.RegionInfo.RegionName);
  239. //lock (m_ids)
  240. // {
  241. //if (m_ids.ContainsKey(avatar.UUID))
  242. //{
  243. // close the event queue.
  244. //m_ids[avatar.UUID] = -1;
  245. //}
  246. //}
  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. string eventQueueGetPath = GenerateEqgCapPath(eventQueueGetUUID);
  290. // Register this as a caps handler
  291. // FIXME: Confusingly, we need to register separate as a capability so that the client is told about
  292. // EventQueueGet when it receive capability information, but then we replace the rest handler immediately
  293. // afterwards with the poll service. So for now, we'll pass a null instead to simplify code reading, but
  294. // really it should be possible to directly register the poll handler as a capability.
  295. caps.RegisterHandler("EventQueueGet", new RestHTTPHandler("POST", eventQueueGetPath, null));
  296. // delegate(Hashtable m_dhttpMethod)
  297. // {
  298. // return ProcessQueue(m_dhttpMethod, agentID, caps);
  299. // }));
  300. // This will persist this beyond the expiry of the caps handlers
  301. // TODO: Add EventQueueGet name/description for diagnostics
  302. MainServer.Instance.AddPollServiceHTTPHandler(
  303. eventQueueGetPath,
  304. new PollServiceEventArgs(null, HasEvents, GetEvents, NoEvents, agentID));
  305. // m_log.DebugFormat(
  306. // "[EVENT QUEUE GET MODULE]: Registered EQG handler {0} for {1} in {2}",
  307. // eventQueueGetPath, agentID, m_scene.RegionInfo.RegionName);
  308. Random rnd = new Random(Environment.TickCount);
  309. lock (m_ids)
  310. {
  311. if (!m_ids.ContainsKey(agentID))
  312. m_ids.Add(agentID, rnd.Next(30000000));
  313. }
  314. }
  315. public bool HasEvents(UUID requestID, UUID agentID)
  316. {
  317. // Don't use this, because of race conditions at agent closing time
  318. //Queue<OSD> queue = TryGetQueue(agentID);
  319. Queue<OSD> queue = GetQueue(agentID);
  320. if (queue != null)
  321. lock (queue)
  322. return queue.Count > 0;
  323. return false;
  324. }
  325. /// <summary>
  326. /// Logs a debug line for an outbound event queue message if appropriate.
  327. /// </summary>
  328. /// <param name='element'>Element containing message</param>
  329. private void LogOutboundDebugMessage(OSD element, UUID agentId)
  330. {
  331. if (element is OSDMap)
  332. {
  333. OSDMap ev = (OSDMap)element;
  334. m_log.DebugFormat(
  335. "Eq OUT {0,-30} to {1,-20} {2,-20}",
  336. ev["message"], m_scene.GetScenePresence(agentId).Name, m_scene.RegionInfo.RegionName);
  337. }
  338. }
  339. public Hashtable GetEvents(UUID requestID, UUID pAgentId, string request)
  340. {
  341. if (DebugLevel >= 2)
  342. m_log.DebugFormat("POLLED FOR EQ MESSAGES BY {0} in {1}", pAgentId, m_scene.RegionInfo.RegionName);
  343. Queue<OSD> queue = TryGetQueue(pAgentId);
  344. OSD element;
  345. lock (queue)
  346. {
  347. if (queue.Count == 0)
  348. return NoEvents(requestID, pAgentId);
  349. element = queue.Dequeue(); // 15s timeout
  350. }
  351. int thisID = 0;
  352. lock (m_ids)
  353. thisID = m_ids[pAgentId];
  354. OSDArray array = new OSDArray();
  355. if (element == null) // didn't have an event in 15s
  356. {
  357. // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  358. array.Add(EventQueueHelper.KeepAliveEvent());
  359. //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", pAgentId, m_scene.RegionInfo.RegionName);
  360. }
  361. else
  362. {
  363. if (DebugLevel > 0)
  364. LogOutboundDebugMessage(element, pAgentId);
  365. array.Add(element);
  366. lock (queue)
  367. {
  368. while (queue.Count > 0)
  369. {
  370. element = queue.Dequeue();
  371. if (DebugLevel > 0)
  372. LogOutboundDebugMessage(element, pAgentId);
  373. array.Add(element);
  374. thisID++;
  375. }
  376. }
  377. }
  378. OSDMap events = new OSDMap();
  379. events.Add("events", array);
  380. events.Add("id", new OSDInteger(thisID));
  381. lock (m_ids)
  382. {
  383. m_ids[pAgentId] = thisID + 1;
  384. }
  385. Hashtable responsedata = new Hashtable();
  386. responsedata["int_response_code"] = 200;
  387. responsedata["content_type"] = "application/xml";
  388. responsedata["keepalive"] = false;
  389. responsedata["reusecontext"] = false;
  390. responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  391. //m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", pAgentId, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  392. return responsedata;
  393. }
  394. public Hashtable NoEvents(UUID requestID, UUID agentID)
  395. {
  396. Hashtable responsedata = new Hashtable();
  397. responsedata["int_response_code"] = 502;
  398. responsedata["content_type"] = "text/plain";
  399. responsedata["keepalive"] = false;
  400. responsedata["reusecontext"] = false;
  401. responsedata["str_response_string"] = "Upstream error: ";
  402. responsedata["error_status_text"] = "Upstream error:";
  403. responsedata["http_protocol_version"] = "HTTP/1.0";
  404. return responsedata;
  405. }
  406. // public Hashtable ProcessQueue(Hashtable request, UUID agentID, Caps caps)
  407. // {
  408. // // TODO: this has to be redone to not busy-wait (and block the thread),
  409. // // TODO: as soon as we have a non-blocking way to handle HTTP-requests.
  410. //
  411. //// if (m_log.IsDebugEnabled)
  412. //// {
  413. //// String debug = "[EVENTQUEUE]: Got request for agent {0} in region {1} from thread {2}: [ ";
  414. //// foreach (object key in request.Keys)
  415. //// {
  416. //// debug += key.ToString() + "=" + request[key].ToString() + " ";
  417. //// }
  418. //// m_log.DebugFormat(debug + " ]", agentID, m_scene.RegionInfo.RegionName, System.Threading.Thread.CurrentThread.Name);
  419. //// }
  420. //
  421. // Queue<OSD> queue = TryGetQueue(agentID);
  422. // OSD element;
  423. //
  424. // lock (queue)
  425. // element = queue.Dequeue(); // 15s timeout
  426. //
  427. // Hashtable responsedata = new Hashtable();
  428. //
  429. // int thisID = 0;
  430. // lock (m_ids)
  431. // thisID = m_ids[agentID];
  432. //
  433. // if (element == null)
  434. // {
  435. // //m_log.ErrorFormat("[EVENTQUEUE]: Nothing to process in " + m_scene.RegionInfo.RegionName);
  436. // if (thisID == -1) // close-request
  437. // {
  438. // m_log.ErrorFormat("[EVENTQUEUE]: 404 in " + m_scene.RegionInfo.RegionName);
  439. // responsedata["int_response_code"] = 404; //501; //410; //404;
  440. // responsedata["content_type"] = "text/plain";
  441. // responsedata["keepalive"] = false;
  442. // responsedata["str_response_string"] = "Closed EQG";
  443. // return responsedata;
  444. // }
  445. // responsedata["int_response_code"] = 502;
  446. // responsedata["content_type"] = "text/plain";
  447. // responsedata["keepalive"] = false;
  448. // responsedata["str_response_string"] = "Upstream error: ";
  449. // responsedata["error_status_text"] = "Upstream error:";
  450. // responsedata["http_protocol_version"] = "HTTP/1.0";
  451. // return responsedata;
  452. // }
  453. //
  454. // OSDArray array = new OSDArray();
  455. // if (element == null) // didn't have an event in 15s
  456. // {
  457. // // Send it a fake event to keep the client polling! It doesn't like 502s like the proxys say!
  458. // array.Add(EventQueueHelper.KeepAliveEvent());
  459. // //m_log.DebugFormat("[EVENTQUEUE]: adding fake event for {0} in region {1}", agentID, m_scene.RegionInfo.RegionName);
  460. // }
  461. // else
  462. // {
  463. // array.Add(element);
  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. // lock (queue)
  474. // {
  475. // while (queue.Count > 0)
  476. // {
  477. // element = queue.Dequeue();
  478. //
  479. // if (element is OSDMap)
  480. // {
  481. // OSDMap ev = (OSDMap)element;
  482. // m_log.DebugFormat(
  483. // "[EVENT QUEUE GET MODULE]: Eq OUT {0} to {1}",
  484. // ev["message"], m_scene.GetScenePresence(agentID).Name);
  485. // }
  486. //
  487. // array.Add(element);
  488. // thisID++;
  489. // }
  490. // }
  491. // }
  492. //
  493. // OSDMap events = new OSDMap();
  494. // events.Add("events", array);
  495. //
  496. // events.Add("id", new OSDInteger(thisID));
  497. // lock (m_ids)
  498. // {
  499. // m_ids[agentID] = thisID + 1;
  500. // }
  501. //
  502. // responsedata["int_response_code"] = 200;
  503. // responsedata["content_type"] = "application/xml";
  504. // responsedata["keepalive"] = false;
  505. // responsedata["str_response_string"] = OSDParser.SerializeLLSDXmlString(events);
  506. //
  507. // m_log.DebugFormat("[EVENTQUEUE]: sending response for {0} in region {1}: {2}", agentID, m_scene.RegionInfo.RegionName, responsedata["str_response_string"]);
  508. //
  509. // return responsedata;
  510. // }
  511. // public Hashtable EventQueuePath2(Hashtable request)
  512. // {
  513. // string capuuid = (string)request["uri"]; //path.Replace("/CAPS/EQG/","");
  514. // // pull off the last "/" in the path.
  515. // Hashtable responsedata = new Hashtable();
  516. // capuuid = capuuid.Substring(0, capuuid.Length - 1);
  517. // capuuid = capuuid.Replace("/CAPS/EQG/", "");
  518. // UUID AvatarID = UUID.Zero;
  519. // UUID capUUID = UUID.Zero;
  520. //
  521. // // parse the path and search for the avatar with it registered
  522. // if (UUID.TryParse(capuuid, out capUUID))
  523. // {
  524. // lock (m_QueueUUIDAvatarMapping)
  525. // {
  526. // if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  527. // {
  528. // AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  529. // }
  530. // }
  531. //
  532. // if (AvatarID != UUID.Zero)
  533. // {
  534. // return ProcessQueue(request, AvatarID, m_scene.CapsModule.GetCapsForUser(AvatarID));
  535. // }
  536. // else
  537. // {
  538. // responsedata["int_response_code"] = 404;
  539. // responsedata["content_type"] = "text/plain";
  540. // responsedata["keepalive"] = false;
  541. // responsedata["str_response_string"] = "Not Found";
  542. // responsedata["error_status_text"] = "Not Found";
  543. // responsedata["http_protocol_version"] = "HTTP/1.0";
  544. // return responsedata;
  545. // // return 404
  546. // }
  547. // }
  548. // else
  549. // {
  550. // responsedata["int_response_code"] = 404;
  551. // responsedata["content_type"] = "text/plain";
  552. // responsedata["keepalive"] = false;
  553. // responsedata["str_response_string"] = "Not Found";
  554. // responsedata["error_status_text"] = "Not Found";
  555. // responsedata["http_protocol_version"] = "HTTP/1.0";
  556. // return responsedata;
  557. // // return 404
  558. // }
  559. // }
  560. public OSD EventQueueFallBack(string path, OSD request, string endpoint)
  561. {
  562. // This is a fallback element to keep the client from loosing EventQueueGet
  563. // Why does CAPS fail sometimes!?
  564. m_log.Warn("[EVENTQUEUE]: In the Fallback handler! We lost the Queue in the rest handler!");
  565. string capuuid = path.Replace("/CAPS/EQG/","");
  566. capuuid = capuuid.Substring(0, capuuid.Length - 1);
  567. // UUID AvatarID = UUID.Zero;
  568. UUID capUUID = UUID.Zero;
  569. if (UUID.TryParse(capuuid, out capUUID))
  570. {
  571. /* Don't remove this yet code cleaners!
  572. * Still testing this!
  573. *
  574. lock (m_QueueUUIDAvatarMapping)
  575. {
  576. if (m_QueueUUIDAvatarMapping.ContainsKey(capUUID))
  577. {
  578. AvatarID = m_QueueUUIDAvatarMapping[capUUID];
  579. }
  580. }
  581. if (AvatarID != UUID.Zero)
  582. {
  583. // Repair the CAP!
  584. //OpenSim.Framework.Capabilities.Caps caps = m_scene.GetCapsHandlerForUser(AvatarID);
  585. //string capsBase = "/CAPS/EQG/";
  586. //caps.RegisterHandler("EventQueueGet",
  587. //new RestHTTPHandler("POST", capsBase + capUUID.ToString() + "/",
  588. //delegate(Hashtable m_dhttpMethod)
  589. //{
  590. // return ProcessQueue(m_dhttpMethod, AvatarID, caps);
  591. //}));
  592. // start new ID sequence.
  593. Random rnd = new Random(System.Environment.TickCount);
  594. lock (m_ids)
  595. {
  596. if (!m_ids.ContainsKey(AvatarID))
  597. m_ids.Add(AvatarID, rnd.Next(30000000));
  598. }
  599. int thisID = 0;
  600. lock (m_ids)
  601. thisID = m_ids[AvatarID];
  602. BlockingLLSDQueue queue = GetQueue(AvatarID);
  603. OSDArray array = new OSDArray();
  604. LLSD element = queue.Dequeue(15000); // 15s timeout
  605. if (element == null)
  606. {
  607. array.Add(EventQueueHelper.KeepAliveEvent());
  608. }
  609. else
  610. {
  611. array.Add(element);
  612. while (queue.Count() > 0)
  613. {
  614. array.Add(queue.Dequeue(1));
  615. thisID++;
  616. }
  617. }
  618. OSDMap events = new OSDMap();
  619. events.Add("events", array);
  620. events.Add("id", new LLSDInteger(thisID));
  621. lock (m_ids)
  622. {
  623. m_ids[AvatarID] = thisID + 1;
  624. }
  625. return events;
  626. }
  627. else
  628. {
  629. return new LLSD();
  630. }
  631. *
  632. */
  633. }
  634. else
  635. {
  636. //return new LLSD();
  637. }
  638. return new OSDString("shutdown404!");
  639. }
  640. public void DisableSimulator(ulong handle, UUID avatarID)
  641. {
  642. OSD item = EventQueueHelper.DisableSimulator(handle);
  643. Enqueue(item, avatarID);
  644. }
  645. public virtual void EnableSimulator(ulong handle, IPEndPoint endPoint, UUID avatarID)
  646. {
  647. OSD item = EventQueueHelper.EnableSimulator(handle, endPoint);
  648. Enqueue(item, avatarID);
  649. }
  650. public virtual void EstablishAgentCommunication(UUID avatarID, IPEndPoint endPoint, string capsPath)
  651. {
  652. OSD item = EventQueueHelper.EstablishAgentCommunication(avatarID, endPoint.ToString(), capsPath);
  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)
  659. {
  660. OSD item = EventQueueHelper.TeleportFinishEvent(regionHandle, simAccess, regionExternalEndPoint,
  661. locationID, flags, capsURL, avatarID);
  662. Enqueue(item, avatarID);
  663. }
  664. public virtual void CrossRegion(ulong handle, Vector3 pos, Vector3 lookAt,
  665. IPEndPoint newRegionExternalEndPoint,
  666. string capsURL, UUID avatarID, UUID sessionID)
  667. {
  668. OSD item = EventQueueHelper.CrossRegion(handle, pos, lookAt, newRegionExternalEndPoint,
  669. capsURL, avatarID, sessionID);
  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 toAgent, bool canVoiceChat,
  684. bool isModerator, bool textMute)
  685. {
  686. OSD item = EventQueueHelper.ChatterBoxSessionAgentListUpdates(sessionID, fromAgent, canVoiceChat,
  687. isModerator, textMute);
  688. Enqueue(item, toAgent);
  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. }
  715. }