Dataserver.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.Generic;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
  32. {
  33. public class Dataserver
  34. {
  35. private ObjectJobEngine m_WorkPool;
  36. public AsyncCommandManager m_CmdManager;
  37. public int DataserverRequestsCount
  38. {
  39. get
  40. {
  41. lock (DataserverRequests)
  42. return DataserverRequests.Count;
  43. }
  44. }
  45. private Dictionary<string, DataserverRequest> DataserverRequests = new Dictionary<string, DataserverRequest>();
  46. public Dataserver(AsyncCommandManager CmdManager)
  47. {
  48. m_CmdManager = CmdManager;
  49. m_WorkPool = new ObjectJobEngine(ProcessActions, "ScriptDataServer", 1000, 4);
  50. }
  51. private class DataserverRequest
  52. {
  53. public uint localID;
  54. public UUID itemID;
  55. public UUID ID;
  56. public string handle;
  57. public DateTime startTime;
  58. public Action<string> action;
  59. }
  60. public string RequestWithImediatePost(uint localID, UUID itemID, string reply)
  61. {
  62. string ID = UUID.Random().ToString();
  63. m_CmdManager.m_ScriptEngine.PostObjectEvent(localID,
  64. new EventParams("dataserver", new Object[]
  65. { new LSL_Types.LSLString(ID),
  66. new LSL_Types.LSLString(reply)},
  67. new DetectParams[0]));
  68. return ID;
  69. }
  70. //legacy
  71. public UUID RegisterRequest(uint localID, UUID itemID, string identifier)
  72. {
  73. lock (DataserverRequests)
  74. {
  75. if (DataserverRequests.ContainsKey(identifier))
  76. return UUID.Zero;
  77. DataserverRequest ds = new DataserverRequest()
  78. {
  79. localID = localID,
  80. itemID = itemID,
  81. ID = UUID.Random(),
  82. handle = identifier,
  83. startTime = DateTime.UtcNow,
  84. action = null
  85. };
  86. DataserverRequests[identifier] = ds;
  87. return ds.ID;
  88. }
  89. }
  90. // action, if provided, is executed async
  91. // its code pattern should be:
  92. //Action<string> act = eventID =>
  93. //{
  94. // need operations to get reply string
  95. // m_AsyncCommands.DataserverPlugin.DataserverReply(eventID, reply);
  96. //}
  97. // eventID is the event id, provided by this on Invoque
  98. // see ProcessActions below
  99. // temporary don't use
  100. public UUID RegisterRequest(uint localID, UUID itemID, string identifier, Action<string> action)
  101. {
  102. lock (DataserverRequests)
  103. {
  104. if (DataserverRequests.ContainsKey(identifier))
  105. return UUID.Zero;
  106. DataserverRequest ds = new DataserverRequest()
  107. {
  108. localID = localID,
  109. itemID = itemID,
  110. ID = UUID.Random(),
  111. handle = identifier,
  112. startTime = DateTime.UtcNow,
  113. action = action
  114. };
  115. DataserverRequests[identifier] = ds;
  116. if (action != null)
  117. m_WorkPool.Enqueue(identifier);
  118. return ds.ID;
  119. }
  120. }
  121. public UUID RegisterRequest(uint localID, UUID itemID, Action<string> action)
  122. {
  123. lock (DataserverRequests)
  124. {
  125. string identifier = UUID.Random().ToString();
  126. DataserverRequest ds = new DataserverRequest()
  127. {
  128. localID = localID,
  129. itemID = itemID,
  130. ID = UUID.Random(),
  131. handle = identifier,
  132. startTime = DateTime.MaxValue,
  133. action = action
  134. };
  135. DataserverRequests[identifier] = ds;
  136. if (action != null)
  137. m_WorkPool.Enqueue(identifier);
  138. return ds.ID;
  139. }
  140. }
  141. public void ProcessActions(object st)
  142. {
  143. string id = st as string;
  144. if(string.IsNullOrEmpty(id))
  145. return;
  146. DataserverRequest ds = null;
  147. lock (DataserverRequests)
  148. {
  149. if (!DataserverRequests.TryGetValue(id, out ds))
  150. return;
  151. }
  152. if (ds == null || ds.action == null)
  153. return;
  154. try
  155. {
  156. ds.action.Invoke(ds.handle);
  157. }
  158. catch { }
  159. ds.action = null;
  160. lock (DataserverRequests)
  161. {
  162. if (DataserverRequests.TryGetValue(id, out ds))
  163. DataserverRequests.Remove(id);
  164. }
  165. }
  166. //legacy ?
  167. public void DataserverReply(string identifier, string reply)
  168. {
  169. DataserverRequest ds;
  170. lock (DataserverRequests)
  171. {
  172. if (!DataserverRequests.ContainsKey(identifier))
  173. return;
  174. ds = DataserverRequests[identifier];
  175. DataserverRequests.Remove(identifier);
  176. }
  177. m_CmdManager.m_ScriptEngine.PostObjectEvent(ds.localID,
  178. new EventParams("dataserver", new Object[]
  179. { new LSL_Types.LSLString(ds.ID.ToString()),
  180. new LSL_Types.LSLString(reply)},
  181. new DetectParams[0]));
  182. }
  183. public void RemoveEvents(uint localID, UUID itemID)
  184. {
  185. lock (DataserverRequests)
  186. {
  187. List<string> toremove = new List<string>(DataserverRequests.Count);
  188. foreach (DataserverRequest ds in DataserverRequests.Values)
  189. {
  190. if (ds.itemID == itemID)
  191. toremove.Add(ds.handle);
  192. }
  193. foreach (string s in toremove)
  194. {
  195. DataserverRequests.Remove(s);
  196. }
  197. }
  198. }
  199. public void ExpireRequests()
  200. {
  201. lock (DataserverRequests)
  202. {
  203. List<string> toremove = new List<string>(DataserverRequests.Count);
  204. DateTime expirebase = DateTime.UtcNow.AddSeconds(-30);
  205. foreach (DataserverRequest ds in DataserverRequests.Values)
  206. {
  207. if (ds.action == null && ds.startTime < expirebase)
  208. toremove.Add(ds.handle);
  209. }
  210. foreach (string s in toremove)
  211. {
  212. DataserverRequests.Remove(s);
  213. }
  214. }
  215. }
  216. }
  217. }