1
0

XMLRPCModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. 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 libsecondlife;
  34. using log4net;
  35. using Nini.Config;
  36. using Nwc.XmlRpc;
  37. using OpenSim.Framework;
  38. using OpenSim.Framework.Servers;
  39. using OpenSim.Region.Environment.Interfaces;
  40. using OpenSim.Region.Environment.Scenes;
  41. /*****************************************************
  42. *
  43. * XMLRPCModule
  44. *
  45. * Module for accepting incoming communications from
  46. * external XMLRPC client and calling a remote data
  47. * procedure for a registered data channel/prim.
  48. *
  49. *
  50. * 1. On module load, open a listener port
  51. * 2. Attach an XMLRPC handler
  52. * 3. When a request is received:
  53. * 3.1 Parse into components: channel key, int, string
  54. * 3.2 Look up registered channel listeners
  55. * 3.3 Call the channel (prim) remote data method
  56. * 3.4 Capture the response (llRemoteDataReply)
  57. * 3.5 Return response to client caller
  58. * 3.6 If no response from llRemoteDataReply within
  59. * RemoteReplyScriptTimeout, generate script timeout fault
  60. *
  61. * Prims in script must:
  62. * 1. Open a remote data channel
  63. * 1.1 Generate a channel ID
  64. * 1.2 Register primid,channelid pair with module
  65. * 2. Implement the remote data procedure handler
  66. *
  67. * llOpenRemoteDataChannel
  68. * llRemoteDataReply
  69. * remote_data(integer type, key channel, key messageid, string sender, integer ival, string sval)
  70. * llCloseRemoteDataChannel
  71. *
  72. * **************************************************/
  73. namespace OpenSim.Region.Environment.Modules.Scripting.XMLRPC
  74. {
  75. public class XMLRPCModule : IRegionModule, IXMLRPC
  76. {
  77. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  78. private string m_name = "XMLRPCModule";
  79. // <channel id, RPCChannelInfo>
  80. private Dictionary<LLUUID, RPCChannelInfo> m_openChannels;
  81. private Dictionary<LLUUID, SendRemoteDataRequest> m_pendingSRDResponses;
  82. private int m_remoteDataPort = 0;
  83. private Dictionary<LLUUID, RPCRequestInfo> m_rpcPending;
  84. private Dictionary<LLUUID, RPCRequestInfo> m_rpcPendingResponses;
  85. private List<Scene> m_scenes = new List<Scene>();
  86. private int RemoteReplyScriptTimeout = 9000;
  87. private int RemoteReplyScriptWait = 300;
  88. private object XMLRPCListLock = new object();
  89. #region IRegionModule Members
  90. public void Initialise(Scene scene, IConfigSource config)
  91. {
  92. try
  93. {
  94. m_remoteDataPort = config.Configs["Network"].GetInt("remoteDataPort", m_remoteDataPort);
  95. }
  96. catch (Exception)
  97. {
  98. }
  99. if (!m_scenes.Contains(scene))
  100. {
  101. m_scenes.Add(scene);
  102. scene.RegisterModuleInterface<IXMLRPC>(this);
  103. }
  104. }
  105. public void PostInitialise()
  106. {
  107. if (IsEnabled())
  108. {
  109. m_openChannels = new Dictionary<LLUUID, RPCChannelInfo>();
  110. m_rpcPending = new Dictionary<LLUUID, RPCRequestInfo>();
  111. m_rpcPendingResponses = new Dictionary<LLUUID, RPCRequestInfo>();
  112. m_pendingSRDResponses = new Dictionary<LLUUID, SendRemoteDataRequest>();
  113. // Start http server
  114. // Attach xmlrpc handlers
  115. m_log.Info("[REMOTE_DATA]: " +
  116. "Starting XMLRPC Server on port " + m_remoteDataPort + " for llRemoteData commands.");
  117. BaseHttpServer httpServer = new BaseHttpServer((uint) m_remoteDataPort);
  118. httpServer.AddXmlRPCHandler("llRemoteData", XmlRpcRemoteData);
  119. httpServer.Start();
  120. }
  121. }
  122. public void Close()
  123. {
  124. }
  125. public string Name
  126. {
  127. get { return m_name; }
  128. }
  129. public bool IsSharedModule
  130. {
  131. get { return true; }
  132. }
  133. #endregion
  134. #region IXMLRPC Members
  135. public bool IsEnabled()
  136. {
  137. return (m_remoteDataPort > 0);
  138. }
  139. /**********************************************
  140. * OpenXMLRPCChannel
  141. *
  142. * Generate a LLUUID channel key and add it and
  143. * the prim id to dictionary <channelUUID, primUUID>
  144. *
  145. * First check if there is a channel assigned for
  146. * this itemID. If there is, then someone called
  147. * llOpenRemoteDataChannel twice. Just return the
  148. * original channel. Other option is to delete the
  149. * current channel and assign a new one.
  150. *
  151. * ********************************************/
  152. public LLUUID OpenXMLRPCChannel(uint localID, LLUUID itemID)
  153. {
  154. LLUUID channel = new LLUUID();
  155. //Is a dupe?
  156. foreach (RPCChannelInfo ci in m_openChannels.Values)
  157. {
  158. if (ci.GetItemID().Equals(itemID))
  159. {
  160. // return the original channel ID for this item
  161. channel = ci.GetChannelID();
  162. break;
  163. }
  164. }
  165. if (channel == LLUUID.Zero)
  166. {
  167. channel = LLUUID.Random();
  168. RPCChannelInfo rpcChanInfo = new RPCChannelInfo(localID, itemID, channel);
  169. lock (XMLRPCListLock)
  170. {
  171. m_openChannels.Add(channel, rpcChanInfo);
  172. }
  173. }
  174. return channel;
  175. }
  176. // Delete channels based on itemID
  177. // for when a script is deleted
  178. public void DeleteChannels(LLUUID itemID)
  179. {
  180. if (m_openChannels != null)
  181. {
  182. ArrayList tmp = new ArrayList();
  183. lock (XMLRPCListLock)
  184. {
  185. foreach (RPCChannelInfo li in m_openChannels.Values)
  186. {
  187. if (li.GetItemID().Equals(itemID))
  188. {
  189. tmp.Add(itemID);
  190. }
  191. }
  192. IEnumerator tmpEnumerator = tmp.GetEnumerator();
  193. while (tmpEnumerator.MoveNext())
  194. m_openChannels.Remove((LLUUID) tmpEnumerator.Current);
  195. }
  196. }
  197. }
  198. /**********************************************
  199. * Remote Data Reply
  200. *
  201. * Response to RPC message
  202. *
  203. *********************************************/
  204. public void RemoteDataReply(string channel, string message_id, string sdata, int idata)
  205. {
  206. RPCRequestInfo rpcInfo;
  207. LLUUID message_key = new LLUUID(message_id);
  208. if (m_rpcPendingResponses.TryGetValue(message_key, out rpcInfo))
  209. {
  210. rpcInfo.SetStrRetval(sdata);
  211. rpcInfo.SetIntRetval(idata);
  212. rpcInfo.SetProcessed(true);
  213. m_rpcPendingResponses.Remove(message_key);
  214. }
  215. }
  216. /**********************************************
  217. * CloseXMLRPCChannel
  218. *
  219. * Remove channel from dictionary
  220. *
  221. *********************************************/
  222. public void CloseXMLRPCChannel(LLUUID channelKey)
  223. {
  224. if (m_openChannels.ContainsKey(channelKey))
  225. m_openChannels.Remove(channelKey);
  226. }
  227. public bool hasRequests()
  228. {
  229. lock (XMLRPCListLock)
  230. {
  231. if (m_rpcPending != null)
  232. return (m_rpcPending.Count > 0);
  233. else
  234. return false;
  235. }
  236. }
  237. public RPCRequestInfo GetNextCompletedRequest()
  238. {
  239. if (m_rpcPending != null)
  240. {
  241. lock (XMLRPCListLock)
  242. {
  243. foreach (LLUUID luid in m_rpcPending.Keys)
  244. {
  245. RPCRequestInfo tmpReq;
  246. if (m_rpcPending.TryGetValue(luid, out tmpReq))
  247. {
  248. if (!tmpReq.IsProcessed()) return tmpReq;
  249. }
  250. }
  251. }
  252. }
  253. return null;
  254. }
  255. public void RemoveCompletedRequest(LLUUID id)
  256. {
  257. lock (XMLRPCListLock)
  258. {
  259. RPCRequestInfo tmp;
  260. if (m_rpcPending.TryGetValue(id, out tmp))
  261. {
  262. m_rpcPending.Remove(id);
  263. m_rpcPendingResponses.Add(id, tmp);
  264. }
  265. else
  266. {
  267. Console.WriteLine("UNABLE TO REMOVE COMPLETED REQUEST");
  268. }
  269. }
  270. }
  271. public LLUUID SendRemoteData(uint localID, LLUUID itemID, string channel, string dest, int idata, string sdata)
  272. {
  273. SendRemoteDataRequest req = new SendRemoteDataRequest(
  274. localID, itemID, channel, dest, idata, sdata
  275. );
  276. m_pendingSRDResponses.Add(req.GetReqID(), req);
  277. return req.process();
  278. }
  279. public SendRemoteDataRequest GetNextCompletedSRDRequest()
  280. {
  281. if (m_pendingSRDResponses != null)
  282. {
  283. lock (XMLRPCListLock)
  284. {
  285. foreach (LLUUID luid in m_pendingSRDResponses.Keys)
  286. {
  287. SendRemoteDataRequest tmpReq;
  288. if (m_pendingSRDResponses.TryGetValue(luid, out tmpReq))
  289. {
  290. if (tmpReq.finished)
  291. return tmpReq;
  292. }
  293. }
  294. }
  295. }
  296. return null;
  297. }
  298. public void RemoveCompletedSRDRequest(LLUUID id)
  299. {
  300. lock (XMLRPCListLock)
  301. {
  302. SendRemoteDataRequest tmpReq;
  303. if (m_pendingSRDResponses.TryGetValue(id, out tmpReq))
  304. {
  305. m_pendingSRDResponses.Remove(id);
  306. }
  307. }
  308. }
  309. public void CancelSRDRequests(LLUUID itemID)
  310. {
  311. if (m_pendingSRDResponses != null)
  312. {
  313. lock (XMLRPCListLock)
  314. {
  315. foreach (SendRemoteDataRequest li in m_pendingSRDResponses.Values)
  316. {
  317. if (li.m_itemID.Equals(itemID))
  318. m_pendingSRDResponses.Remove(li.GetReqID());
  319. }
  320. }
  321. }
  322. }
  323. #endregion
  324. public XmlRpcResponse XmlRpcRemoteData(XmlRpcRequest request)
  325. {
  326. XmlRpcResponse response = new XmlRpcResponse();
  327. Hashtable requestData = (Hashtable) request.Params[0];
  328. bool GoodXML = (requestData.Contains("Channel") && requestData.Contains("IntValue") &&
  329. requestData.Contains("StringValue"));
  330. if (GoodXML)
  331. {
  332. LLUUID channel = new LLUUID((string) requestData["Channel"]);
  333. RPCChannelInfo rpcChanInfo;
  334. if (m_openChannels.TryGetValue(channel, out rpcChanInfo))
  335. {
  336. string intVal = (string) requestData["IntValue"];
  337. string strVal = (string) requestData["StringValue"];
  338. RPCRequestInfo rpcInfo;
  339. lock (XMLRPCListLock)
  340. {
  341. rpcInfo =
  342. new RPCRequestInfo(rpcChanInfo.GetLocalID(), rpcChanInfo.GetItemID(), channel, strVal,
  343. intVal);
  344. m_rpcPending.Add(rpcInfo.GetMessageID(), rpcInfo);
  345. }
  346. int timeoutCtr = 0;
  347. while (!rpcInfo.IsProcessed() && (timeoutCtr < RemoteReplyScriptTimeout))
  348. {
  349. Thread.Sleep(RemoteReplyScriptWait);
  350. timeoutCtr += RemoteReplyScriptWait;
  351. }
  352. if (rpcInfo.IsProcessed())
  353. {
  354. Hashtable param = new Hashtable();
  355. param["StringValue"] = rpcInfo.GetStrRetval();
  356. param["IntValue"] = Convert.ToString(rpcInfo.GetIntRetval());
  357. ArrayList parameters = new ArrayList();
  358. parameters.Add(param);
  359. response.Value = parameters;
  360. rpcInfo = null;
  361. }
  362. else
  363. {
  364. response.SetFault(-1, "Script timeout");
  365. rpcInfo = null;
  366. }
  367. }
  368. else
  369. {
  370. response.SetFault(-1, "Invalid channel");
  371. }
  372. }
  373. return response;
  374. }
  375. }
  376. public class RPCRequestInfo
  377. {
  378. private LLUUID m_ChannelKey;
  379. private string m_IntVal;
  380. private LLUUID m_ItemID;
  381. private uint m_localID;
  382. private LLUUID m_MessageID;
  383. private bool m_processed;
  384. private int m_respInt;
  385. private string m_respStr;
  386. private string m_StrVal;
  387. public RPCRequestInfo(uint localID, LLUUID itemID, LLUUID channelKey, string strVal, string intVal)
  388. {
  389. m_localID = localID;
  390. m_StrVal = strVal;
  391. m_IntVal = intVal;
  392. m_ItemID = itemID;
  393. m_ChannelKey = channelKey;
  394. m_MessageID = LLUUID.Random();
  395. m_processed = false;
  396. m_respStr = String.Empty;
  397. m_respInt = 0;
  398. }
  399. public bool IsProcessed()
  400. {
  401. return m_processed;
  402. }
  403. public LLUUID GetChannelKey()
  404. {
  405. return m_ChannelKey;
  406. }
  407. public void SetProcessed(bool processed)
  408. {
  409. m_processed = processed;
  410. }
  411. public void SetStrRetval(string resp)
  412. {
  413. m_respStr = resp;
  414. }
  415. public string GetStrRetval()
  416. {
  417. return m_respStr;
  418. }
  419. public void SetIntRetval(int resp)
  420. {
  421. m_respInt = resp;
  422. }
  423. public int GetIntRetval()
  424. {
  425. return m_respInt;
  426. }
  427. public uint GetLocalID()
  428. {
  429. return m_localID;
  430. }
  431. public LLUUID GetItemID()
  432. {
  433. return m_ItemID;
  434. }
  435. public string GetStrVal()
  436. {
  437. return m_StrVal;
  438. }
  439. public int GetIntValue()
  440. {
  441. return int.Parse(m_IntVal);
  442. }
  443. public LLUUID GetMessageID()
  444. {
  445. return m_MessageID;
  446. }
  447. }
  448. public class RPCChannelInfo
  449. {
  450. private LLUUID m_ChannelKey;
  451. private LLUUID m_itemID;
  452. private uint m_localID;
  453. public RPCChannelInfo(uint localID, LLUUID itemID, LLUUID channelID)
  454. {
  455. m_ChannelKey = channelID;
  456. m_localID = localID;
  457. m_itemID = itemID;
  458. }
  459. public LLUUID GetItemID()
  460. {
  461. return m_itemID;
  462. }
  463. public LLUUID GetChannelID()
  464. {
  465. return m_ChannelKey;
  466. }
  467. public uint GetLocalID()
  468. {
  469. return m_localID;
  470. }
  471. }
  472. public class SendRemoteDataRequest
  473. {
  474. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  475. public string channel;
  476. public string destURL;
  477. public bool finished;
  478. private Thread httpThread;
  479. public int idata;
  480. public LLUUID m_itemID;
  481. public uint m_localID;
  482. public LLUUID reqID;
  483. public XmlRpcRequest request;
  484. public int response_idata;
  485. public string response_sdata;
  486. public string sdata;
  487. public SendRemoteDataRequest(uint localID, LLUUID itemID, string channel, string dest, int idata, string sdata)
  488. {
  489. this.channel = channel;
  490. destURL = dest;
  491. this.idata = idata;
  492. this.sdata = sdata;
  493. m_itemID = itemID;
  494. m_localID = localID;
  495. reqID = LLUUID.Random();
  496. }
  497. public LLUUID process()
  498. {
  499. httpThread = new Thread(SendRequest);
  500. httpThread.Name = "HttpRequestThread";
  501. httpThread.Priority = ThreadPriority.BelowNormal;
  502. httpThread.IsBackground = true;
  503. finished = false;
  504. httpThread.Start();
  505. ThreadTracker.Add(httpThread);
  506. return reqID;
  507. }
  508. /*
  509. * TODO: More work on the response codes. Right now
  510. * returning 200 for success or 499 for exception
  511. */
  512. public void SendRequest()
  513. {
  514. Hashtable param = new Hashtable();
  515. // Check if channel is an LLUUID
  516. // if not, use as method name
  517. LLUUID parseUID;
  518. string mName = "llRemoteData";
  519. if ((channel != null) && (channel != ""))
  520. if (!LLUUID.TryParse(channel, out parseUID))
  521. mName = channel;
  522. else
  523. param["Channel"] = channel;
  524. param["StringValue"] = sdata;
  525. param["IntValue"] = Convert.ToString(idata);
  526. ArrayList parameters = new ArrayList();
  527. parameters.Add(param);
  528. XmlRpcRequest req = new XmlRpcRequest(mName, parameters);
  529. try
  530. {
  531. XmlRpcResponse resp = req.Send(destURL, 30000);
  532. if (resp != null)
  533. {
  534. Hashtable respParms;
  535. if (resp.Value.GetType().Equals(Type.GetType("System.Collections.Hashtable")))
  536. {
  537. respParms = (Hashtable) resp.Value;
  538. }
  539. else
  540. {
  541. ArrayList respData = (ArrayList) resp.Value;
  542. respParms = (Hashtable) respData[0];
  543. }
  544. if (respParms != null)
  545. {
  546. if (respParms.Contains("StringValue"))
  547. {
  548. sdata = (string) respParms["StringValue"];
  549. }
  550. if (respParms.Contains("IntValue"))
  551. {
  552. idata = Convert.ToInt32((string) respParms["IntValue"]);
  553. }
  554. if (respParms.Contains("faultString"))
  555. {
  556. sdata = (string) respParms["faultString"];
  557. }
  558. if (respParms.Contains("faultCode"))
  559. {
  560. idata = Convert.ToInt32(respParms["faultCode"]);
  561. }
  562. }
  563. }
  564. }
  565. catch (WebException we)
  566. {
  567. sdata = we.Message;
  568. m_log.Warn("[SendRemoteDataRequest]: Request failed");
  569. m_log.Warn(we.StackTrace);
  570. }
  571. finished = true;
  572. }
  573. public void Stop()
  574. {
  575. try
  576. {
  577. httpThread.Abort();
  578. }
  579. catch (Exception)
  580. {
  581. }
  582. }
  583. public LLUUID GetReqID()
  584. {
  585. return reqID;
  586. }
  587. }
  588. }