XMLRPCModule.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 OpenSim 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. */
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.Threading;
  32. using libsecondlife;
  33. using Nini.Config;
  34. using Nwc.XmlRpc;
  35. using OpenSim.Framework.Console;
  36. using OpenSim.Framework.Servers;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes;
  39. /*****************************************************
  40. *
  41. * XMLRPCModule
  42. *
  43. * Module for accepting incoming communications from
  44. * external XMLRPC client and calling a remote data
  45. * procedure for a registered data channel/prim.
  46. *
  47. *
  48. * 1. On module load, open a listener port
  49. * 2. Attach an XMLRPC handler
  50. * 3. When a request is received:
  51. * 3.1 Parse into components: channel key, int, string
  52. * 3.2 Look up registered channel listeners
  53. * 3.3 Call the channel (prim) remote data method
  54. * 3.4 Capture the response (llRemoteDataReply)
  55. * 3.5 Return response to client caller
  56. * 3.6 If no response from llRemoteDataReply within
  57. * RemoteReplyScriptTimeout, generate script timeout fault
  58. *
  59. * Prims in script must:
  60. * 1. Open a remote data channel
  61. * 1.1 Generate a channel ID
  62. * 1.2 Register primid,channelid pair with module
  63. * 2. Implement the remote data procedure handler
  64. *
  65. * llOpenRemoteDataChannel
  66. * llRemoteDataReply
  67. * remote_data(integer type, key channel, key messageid, string sender, integer ival, string sval)
  68. * llCloseRemoteDataChannel
  69. *
  70. * **************************************************/
  71. namespace OpenSim.Region.Environment.Modules
  72. {
  73. public class XMLRPCModule : IRegionModule, IXMLRPC
  74. {
  75. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  76. private Scene m_scene;
  77. private Queue<RPCRequestInfo> rpcQueue = new Queue<RPCRequestInfo>();
  78. private object XMLRPCListLock = new object();
  79. private string m_name = "XMLRPCModule";
  80. private int RemoteReplyScriptWait = 300;
  81. private int RemoteReplyScriptTimeout = 900;
  82. private int m_remoteDataPort = 0;
  83. private List<Scene> m_scenes = new List<Scene>();
  84. // <channel id, RPCChannelInfo>
  85. private Dictionary<LLUUID, RPCChannelInfo> m_openChannels;
  86. // <channel id, RPCRequestInfo>
  87. private Dictionary<LLUUID, RPCRequestInfo> m_pendingResponse;
  88. public void Initialise(Scene scene, IConfigSource config)
  89. {
  90. try
  91. {
  92. m_remoteDataPort = config.Configs["Network"].GetInt("remoteDataPort", m_remoteDataPort);
  93. }
  94. catch (Exception)
  95. {
  96. }
  97. if (!m_scenes.Contains(scene))
  98. {
  99. m_scenes.Add(scene);
  100. scene.RegisterModuleInterface<IXMLRPC>(this);
  101. }
  102. }
  103. public void PostInitialise()
  104. {
  105. if (IsEnabled())
  106. {
  107. m_openChannels = new Dictionary<LLUUID, RPCChannelInfo>();
  108. m_pendingResponse = new Dictionary<LLUUID, RPCRequestInfo>();
  109. // Start http server
  110. // Attach xmlrpc handlers
  111. m_log.Info("[REMOTE_DATA]: " +
  112. "Starting XMLRPC Server on port " + m_remoteDataPort + " for llRemoteData commands.");
  113. BaseHttpServer httpServer = new BaseHttpServer((uint) m_remoteDataPort);
  114. httpServer.AddXmlRPCHandler("llRemoteData", XmlRpcRemoteData);
  115. httpServer.Start();
  116. }
  117. }
  118. public void Close()
  119. {
  120. }
  121. public string Name
  122. {
  123. get { return m_name; }
  124. }
  125. public bool IsSharedModule
  126. {
  127. get { return true; }
  128. }
  129. public bool IsEnabled()
  130. {
  131. return (m_remoteDataPort > 0);
  132. }
  133. /**********************************************
  134. * OpenXMLRPCChannel
  135. *
  136. * Generate a LLUUID channel key and add it and
  137. * the prim id to dictionary <channelUUID, primUUID>
  138. *
  139. * First check if there is a channel assigned for
  140. * this itemID. If there is, then someone called
  141. * llOpenRemoteDataChannel twice. Just return the
  142. * original channel. Other option is to delete the
  143. * current channel and assign a new one.
  144. *
  145. * ********************************************/
  146. public LLUUID OpenXMLRPCChannel(uint localID, LLUUID itemID)
  147. {
  148. LLUUID channel = null;
  149. //Is a dupe?
  150. foreach (RPCChannelInfo ci in m_openChannels.Values)
  151. {
  152. if (ci.GetItemID().Equals(itemID))
  153. {
  154. // return the original channel ID for this item
  155. channel = ci.GetChannelID();
  156. break;
  157. }
  158. }
  159. if ((channel.Equals(null)) || (channel.Equals(LLUUID.Zero)))
  160. {
  161. channel = LLUUID.Random();
  162. RPCChannelInfo rpcChanInfo = new RPCChannelInfo(localID, itemID, channel);
  163. lock (XMLRPCListLock)
  164. {
  165. m_openChannels.Add(channel, rpcChanInfo);
  166. }
  167. }
  168. return channel;
  169. }
  170. public void DeleteChannel(LLUUID itemID)
  171. {
  172. foreach (RPCChannelInfo li in m_openChannels.Values)
  173. {
  174. if (li.GetItemID().Equals(itemID))
  175. {
  176. m_openChannels.Remove(li.GetChannelID());
  177. return;
  178. }
  179. }
  180. }
  181. /**********************************************
  182. * Remote Data Reply
  183. *
  184. * Response to RPC message
  185. *
  186. *********************************************/
  187. public void RemoteDataReply(string channel, string message_id, string sdata, int idata)
  188. {
  189. RPCRequestInfo rpcInfo;
  190. LLUUID message_key = new LLUUID(message_id);
  191. if (m_pendingResponse.TryGetValue(message_key, out rpcInfo))
  192. {
  193. rpcInfo.SetRetval(sdata);
  194. rpcInfo.SetProcessed(true);
  195. lock (XMLRPCListLock)
  196. {
  197. m_pendingResponse.Remove(message_key);
  198. }
  199. }
  200. }
  201. /**********************************************
  202. * CloseXMLRPCChannel
  203. *
  204. * Remove channel from dictionary
  205. *
  206. *********************************************/
  207. public void CloseXMLRPCChannel(LLUUID channelKey)
  208. {
  209. if (m_openChannels.ContainsKey(channelKey))
  210. m_openChannels.Remove(channelKey);
  211. }
  212. public XmlRpcResponse XmlRpcRemoteData(XmlRpcRequest request)
  213. {
  214. XmlRpcResponse response = new XmlRpcResponse();
  215. Hashtable requestData = (Hashtable) request.Params[0];
  216. bool GoodXML = (requestData.Contains("Channel") && requestData.Contains("IntValue") &&
  217. requestData.Contains("StringValue"));
  218. if (GoodXML)
  219. {
  220. LLUUID channel = new LLUUID((string) requestData["Channel"]);
  221. RPCChannelInfo rpcChanInfo;
  222. if (m_openChannels.TryGetValue(channel, out rpcChanInfo))
  223. {
  224. string intVal = (string) requestData["IntValue"];
  225. string strVal = (string) requestData["StringValue"];
  226. RPCRequestInfo rpcInfo;
  227. lock (XMLRPCListLock)
  228. {
  229. rpcInfo =
  230. new RPCRequestInfo(rpcChanInfo.GetLocalID(), rpcChanInfo.GetItemID(), channel, strVal,
  231. intVal);
  232. rpcQueue.Enqueue(rpcInfo);
  233. }
  234. int timeoutCtr = 0;
  235. while (!rpcInfo.IsProcessed() && (timeoutCtr < RemoteReplyScriptTimeout))
  236. {
  237. Thread.Sleep(RemoteReplyScriptWait);
  238. timeoutCtr += RemoteReplyScriptWait;
  239. }
  240. if (rpcInfo.IsProcessed())
  241. {
  242. response.Value = rpcInfo.GetRetval();
  243. rpcInfo = null;
  244. }
  245. else
  246. {
  247. response.SetFault(-1, "Script timeout");
  248. lock (XMLRPCListLock)
  249. {
  250. m_pendingResponse.Remove(rpcInfo.GetMessageID());
  251. }
  252. }
  253. }
  254. else
  255. {
  256. response.SetFault(-1, "Invalid channel");
  257. }
  258. }
  259. return response;
  260. }
  261. public bool hasRequests()
  262. {
  263. return (rpcQueue.Count > 0);
  264. }
  265. public RPCRequestInfo GetNextRequest()
  266. {
  267. lock (XMLRPCListLock)
  268. {
  269. RPCRequestInfo rpcInfo = rpcQueue.Dequeue();
  270. m_pendingResponse.Add(rpcInfo.GetMessageID(), rpcInfo);
  271. return rpcInfo;
  272. }
  273. }
  274. }
  275. /**************************************************************
  276. *
  277. * Class RPCRequestInfo
  278. *
  279. * Holds details about incoming requests until they are picked
  280. * from the queue by LSLLongCmdHandler
  281. * ***********************************************************/
  282. public class RPCRequestInfo
  283. {
  284. private string m_StrVal;
  285. private string m_IntVal;
  286. private bool m_processed;
  287. private string m_resp;
  288. private uint m_localID;
  289. private LLUUID m_ItemID;
  290. private LLUUID m_MessageID;
  291. private LLUUID m_ChannelKey;
  292. public RPCRequestInfo(uint localID, LLUUID itemID, LLUUID channelKey, string strVal, string intVal)
  293. {
  294. m_localID = localID;
  295. m_StrVal = strVal;
  296. m_IntVal = intVal;
  297. m_ItemID = itemID;
  298. m_ChannelKey = channelKey;
  299. m_MessageID = LLUUID.Random();
  300. m_processed = false;
  301. m_resp = String.Empty;
  302. }
  303. public bool IsProcessed()
  304. {
  305. return m_processed;
  306. }
  307. public LLUUID GetChannelKey()
  308. {
  309. return m_ChannelKey;
  310. }
  311. public void SetProcessed(bool processed)
  312. {
  313. m_processed = processed;
  314. }
  315. public void SetRetval(string resp)
  316. {
  317. m_resp = resp;
  318. }
  319. public string GetRetval()
  320. {
  321. return m_resp;
  322. }
  323. public uint GetLocalID()
  324. {
  325. return m_localID;
  326. }
  327. public LLUUID GetItemID()
  328. {
  329. return m_ItemID;
  330. }
  331. public string GetStrVal()
  332. {
  333. return m_StrVal;
  334. }
  335. public int GetIntValue()
  336. {
  337. return int.Parse(m_IntVal);
  338. }
  339. public LLUUID GetMessageID()
  340. {
  341. return m_MessageID;
  342. }
  343. }
  344. public class RPCChannelInfo
  345. {
  346. private LLUUID m_itemID;
  347. private uint m_localID;
  348. private LLUUID m_ChannelKey;
  349. public RPCChannelInfo(uint localID, LLUUID itemID, LLUUID channelID)
  350. {
  351. m_ChannelKey = channelID;
  352. m_localID = localID;
  353. m_itemID = itemID;
  354. }
  355. public LLUUID GetItemID()
  356. {
  357. return m_itemID;
  358. }
  359. public LLUUID GetChannelID()
  360. {
  361. return m_ChannelKey;
  362. }
  363. public uint GetLocalID()
  364. {
  365. return m_localID;
  366. }
  367. }
  368. }