EventQueueGetModule.cs 32 KB

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