1
0

EventQueueGetModule.cs 31 KB

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