Dataserver.cs 9.0 KB

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