XMLRPCModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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.Threading;
  31. using libsecondlife;
  32. using Nini.Config;
  33. using Nwc.XmlRpc;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Servers;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Environment.Scenes;
  38. /*****************************************************
  39. *
  40. * XMLRPCModule
  41. *
  42. * Module for accepting incoming communications from
  43. * external XMLRPC client and calling a remote data
  44. * procedure for a registered data channel/prim.
  45. *
  46. *
  47. * 1. On module load, open a listener port
  48. * 2. Attach an XMLRPC handler
  49. * 3. When a request is received:
  50. * 3.1 Parse into components: channel key, int, string
  51. * 3.2 Look up registered channel listeners
  52. * 3.3 Call the channel (prim) remote data method
  53. * 3.4 Capture the response (llRemoteDataReply)
  54. * 3.5 Return response to client caller
  55. * 3.6 If no response from llRemoteDataReply within
  56. * RemoteReplyScriptTimeout, generate script timeout fault
  57. *
  58. * Prims in script must:
  59. * 1. Open a remote data channel
  60. * 1.1 Generate a channel ID
  61. * 1.2 Register primid,channelid pair with module
  62. * 2. Implement the remote data procedure handler
  63. *
  64. * llOpenRemoteDataChannel
  65. * llRemoteDataReply
  66. * remote_data(integer type, key channel, key messageid, string sender, integer ival, string sval)
  67. * llCloseRemoteDataChannel
  68. *
  69. * **************************************************/
  70. namespace OpenSim.Region.Environment.Modules
  71. {
  72. public class XMLRPCModule : IRegionModule, IXMLRPC
  73. {
  74. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  75. private object XMLRPCListLock = new object();
  76. private string m_name = "XMLRPCModule";
  77. private int RemoteReplyScriptWait = 300;
  78. private int RemoteReplyScriptTimeout = 9000;
  79. private int m_remoteDataPort = 0;
  80. private List<Scene> m_scenes = new List<Scene>();
  81. // <channel id, RPCChannelInfo>
  82. private Dictionary<LLUUID, RPCChannelInfo> m_openChannels;
  83. private Dictionary<LLUUID, RPCRequestInfo> m_rpcPending;
  84. private Dictionary<LLUUID, RPCRequestInfo> m_rpcPendingResponses;
  85. private Dictionary<LLUUID, SendRemoteDataRequest> m_pendingSRDResponses;
  86. public void Initialise(Scene scene, IConfigSource config)
  87. {
  88. try
  89. {
  90. m_remoteDataPort = config.Configs["Network"].GetInt("remoteDataPort", m_remoteDataPort);
  91. }
  92. catch (Exception)
  93. {
  94. }
  95. if (!m_scenes.Contains(scene))
  96. {
  97. m_scenes.Add(scene);
  98. scene.RegisterModuleInterface<IXMLRPC>(this);
  99. }
  100. }
  101. public void PostInitialise()
  102. {
  103. if (IsEnabled())
  104. {
  105. m_openChannels = new Dictionary<LLUUID, RPCChannelInfo>();
  106. m_rpcPending = new Dictionary<LLUUID, RPCRequestInfo>();
  107. m_rpcPendingResponses = new Dictionary<LLUUID, RPCRequestInfo>();
  108. m_pendingSRDResponses = new Dictionary<LLUUID, SendRemoteDataRequest>();
  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 = new LLUUID();
  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 == 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. // Delete channels based on itemID
  171. // for when a script is deleted
  172. public void DeleteChannels(LLUUID itemID)
  173. {
  174. if (m_openChannels != null)
  175. {
  176. ArrayList tmp = new ArrayList();
  177. lock (XMLRPCListLock)
  178. {
  179. foreach (RPCChannelInfo li in m_openChannels.Values)
  180. {
  181. if (li.GetItemID().Equals(itemID))
  182. {
  183. tmp.Add(itemID);
  184. }
  185. }
  186. System.Collections.IEnumerator tmpEnumerator = tmp.GetEnumerator();
  187. while ( tmpEnumerator.MoveNext() )
  188. m_openChannels.Remove((LLUUID)tmpEnumerator.Current);
  189. }
  190. }
  191. }
  192. /**********************************************
  193. * Remote Data Reply
  194. *
  195. * Response to RPC message
  196. *
  197. *********************************************/
  198. public void RemoteDataReply(string channel, string message_id, string sdata, int idata)
  199. {
  200. RPCRequestInfo rpcInfo;
  201. LLUUID message_key = new LLUUID(message_id);
  202. if (m_rpcPendingResponses.TryGetValue(message_key, out rpcInfo))
  203. {
  204. rpcInfo.SetStrRetval(sdata);
  205. rpcInfo.SetIntRetval(idata);
  206. rpcInfo.SetProcessed(true);
  207. m_rpcPendingResponses.Remove(message_key);
  208. }
  209. }
  210. /**********************************************
  211. * CloseXMLRPCChannel
  212. *
  213. * Remove channel from dictionary
  214. *
  215. *********************************************/
  216. public void CloseXMLRPCChannel(LLUUID channelKey)
  217. {
  218. if (m_openChannels.ContainsKey(channelKey))
  219. m_openChannels.Remove(channelKey);
  220. }
  221. public XmlRpcResponse XmlRpcRemoteData(XmlRpcRequest request)
  222. {
  223. XmlRpcResponse response = new XmlRpcResponse();
  224. Hashtable requestData = (Hashtable)request.Params[0];
  225. bool GoodXML = (requestData.Contains("Channel") && requestData.Contains("IntValue") &&
  226. requestData.Contains("StringValue"));
  227. if (GoodXML)
  228. {
  229. LLUUID channel = new LLUUID((string)requestData["Channel"]);
  230. RPCChannelInfo rpcChanInfo;
  231. if (m_openChannels.TryGetValue(channel, out rpcChanInfo))
  232. {
  233. string intVal = (string)requestData["IntValue"];
  234. string strVal = (string)requestData["StringValue"];
  235. RPCRequestInfo rpcInfo;
  236. lock (XMLRPCListLock)
  237. {
  238. rpcInfo =
  239. new RPCRequestInfo(rpcChanInfo.GetLocalID(), rpcChanInfo.GetItemID(), channel, strVal,
  240. intVal);
  241. m_rpcPending.Add(rpcInfo.GetMessageID(), rpcInfo);
  242. }
  243. int timeoutCtr = 0;
  244. while (!rpcInfo.IsProcessed() && (timeoutCtr < RemoteReplyScriptTimeout))
  245. {
  246. Thread.Sleep(RemoteReplyScriptWait);
  247. timeoutCtr += RemoteReplyScriptWait;
  248. }
  249. if (rpcInfo.IsProcessed())
  250. {
  251. Hashtable param = new Hashtable();
  252. param["StringValue"] = rpcInfo.GetStrRetval();
  253. param["IntValue"] = Convert.ToString(rpcInfo.GetIntRetval());
  254. ArrayList parameters = new ArrayList();
  255. parameters.Add(param);
  256. response.Value = parameters;
  257. rpcInfo = null;
  258. }
  259. else
  260. {
  261. response.SetFault(-1, "Script timeout");
  262. rpcInfo = null;
  263. }
  264. }
  265. else
  266. {
  267. response.SetFault(-1, "Invalid channel");
  268. }
  269. }
  270. return response;
  271. }
  272. public bool hasRequests()
  273. {
  274. lock (XMLRPCListLock)
  275. {
  276. if (m_rpcPending != null)
  277. return (m_rpcPending.Count > 0);
  278. else
  279. return false;
  280. }
  281. }
  282. public RPCRequestInfo GetNextCompletedRequest()
  283. {
  284. if (m_rpcPending != null)
  285. {
  286. lock (XMLRPCListLock)
  287. {
  288. foreach (LLUUID luid in m_rpcPending.Keys)
  289. {
  290. RPCRequestInfo tmpReq;
  291. if (m_rpcPending.TryGetValue(luid, out tmpReq))
  292. {
  293. if (!tmpReq.IsProcessed()) return tmpReq;
  294. }
  295. }
  296. }
  297. }
  298. return null;
  299. }
  300. public void RemoveCompletedRequest(LLUUID id)
  301. {
  302. lock (XMLRPCListLock)
  303. {
  304. RPCRequestInfo tmp;
  305. if (m_rpcPending.TryGetValue(id, out tmp))
  306. {
  307. m_rpcPending.Remove(id);
  308. m_rpcPendingResponses.Add(id, tmp);
  309. }
  310. else
  311. {
  312. Console.WriteLine("UNABLE TO REMOVE COMPLETED REQUEST");
  313. }
  314. }
  315. }
  316. public LLUUID SendRemoteData(uint localID, LLUUID itemID, string channel, string dest, int idata, string sdata)
  317. {
  318. SendRemoteDataRequest req = new SendRemoteDataRequest(
  319. localID, itemID, channel, dest, idata, sdata
  320. );
  321. m_pendingSRDResponses.Add(req.GetReqID(), req);
  322. return req.process();
  323. }
  324. public SendRemoteDataRequest GetNextCompletedSRDRequest()
  325. {
  326. if (m_pendingSRDResponses != null)
  327. {
  328. lock (XMLRPCListLock)
  329. {
  330. foreach (LLUUID luid in m_pendingSRDResponses.Keys)
  331. {
  332. SendRemoteDataRequest tmpReq;
  333. if (m_pendingSRDResponses.TryGetValue(luid, out tmpReq))
  334. {
  335. if (tmpReq.finished)
  336. return tmpReq;
  337. }
  338. }
  339. }
  340. }
  341. return null;
  342. }
  343. public void RemoveCompletedSRDRequest(LLUUID id)
  344. {
  345. lock (XMLRPCListLock)
  346. {
  347. SendRemoteDataRequest tmpReq;
  348. if (m_pendingSRDResponses.TryGetValue(id, out tmpReq))
  349. {
  350. m_pendingSRDResponses.Remove(id);
  351. }
  352. }
  353. }
  354. public void CancelSRDRequests(LLUUID itemID)
  355. {
  356. if (m_pendingSRDResponses != null)
  357. {
  358. lock (XMLRPCListLock)
  359. {
  360. foreach (SendRemoteDataRequest li in m_pendingSRDResponses.Values)
  361. {
  362. if (li.m_itemID.Equals(itemID))
  363. m_pendingSRDResponses.Remove(li.GetReqID());
  364. }
  365. }
  366. }
  367. }
  368. }
  369. /**************************************************************
  370. *
  371. * Class RPCRequestInfo
  372. *
  373. * Holds details about incoming requests until they are picked
  374. * from the queue by LSLLongCmdHandler
  375. * ***********************************************************/
  376. public class RPCRequestInfo
  377. {
  378. private string m_StrVal;
  379. private string m_IntVal;
  380. private bool m_processed;
  381. private string m_respStr;
  382. private int m_respInt;
  383. private uint m_localID;
  384. private LLUUID m_ItemID;
  385. private LLUUID m_MessageID;
  386. private LLUUID m_ChannelKey;
  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_itemID;
  451. private uint m_localID;
  452. private LLUUID m_ChannelKey;
  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. public LLUUID reqID;
  475. public string destURL;
  476. public string channel;
  477. public string sdata;
  478. public int idata;
  479. public bool finished;
  480. public string response_sdata;
  481. public int response_idata;
  482. public XmlRpcRequest request;
  483. private Thread httpThread;
  484. public LLUUID m_itemID;
  485. public uint m_localID;
  486. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  487. public SendRemoteDataRequest(uint localID, LLUUID itemID, string channel, string dest, int idata, string sdata)
  488. {
  489. this.channel = channel;
  490. this.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. OpenSim.Framework.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. respParms = (Hashtable)resp.Value;
  537. }
  538. else {
  539. ArrayList respData = (ArrayList)resp.Value;
  540. respParms = (Hashtable)respData[0];
  541. }
  542. if (respParms != null)
  543. {
  544. if (respParms.Contains("StringValue"))
  545. {
  546. sdata = (string)respParms["StringValue"];
  547. }
  548. if (respParms.Contains("IntValue"))
  549. {
  550. idata = Convert.ToInt32((string)respParms["IntValue"]);
  551. }
  552. if (respParms.Contains("faultString"))
  553. {
  554. sdata = (string)respParms["faultString"];
  555. }
  556. if (respParms.Contains("faultCode"))
  557. {
  558. idata = Convert.ToInt32(respParms["faultCode"]);
  559. }
  560. }
  561. }
  562. }
  563. catch (System.Net.WebException we)
  564. {
  565. sdata = we.Message;
  566. m_log.Warn("[SendRemoteDataRequest]: Request failed");
  567. m_log.Warn(we.StackTrace);
  568. }
  569. finished = true;
  570. }
  571. public void Stop()
  572. {
  573. try
  574. {
  575. httpThread.Abort();
  576. }
  577. catch (Exception)
  578. {
  579. }
  580. }
  581. public LLUUID GetReqID()
  582. {
  583. return reqID;
  584. }
  585. }
  586. }