EventQueueGetModule.cs 31 KB

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