1
0

XmlRpcClientProxy.cs 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Runtime.Remoting.Proxies;
  32. using System.Runtime.Remoting.Messaging;
  33. /// <summary>This class provides support for creating local proxies of XML-RPC remote objects</summary>
  34. /// <remarks>
  35. /// To create a local proxy you need to create a local C# interface and then, via <i>createProxy</i>
  36. /// associate that interface with a remote object at a given URL.
  37. /// </remarks>
  38. public class XmlRpcClientProxy : RealProxy
  39. {
  40. private String _remoteObjectName;
  41. private String _url;
  42. private XmlRpcRequest _client = new XmlRpcRequest();
  43. /// <summary>Factory method to create proxies.</summary>
  44. /// <remarks>
  45. /// To create a local proxy you need to create a local C# interface with methods that mirror those of the server object.
  46. /// Next, pass that interface into <c>createProxy</c> along with the object name and URL of the remote object and
  47. /// cast the resulting object to the specifice interface.
  48. /// </remarks>
  49. /// <param name="remoteObjectName"><c>String</c> The name of the remote object.</param>
  50. /// <param name="url"><c>String</c> The URL of the remote object.</param>
  51. /// <param name="anInterface"><c>Type</c> The typeof() of a C# interface.</param>
  52. /// <returns><c>Object</c> A proxy for your specified interface. Cast to appropriate type.</returns>
  53. public static Object createProxy(String remoteObjectName, String url, Type anInterface)
  54. {
  55. return new XmlRpcClientProxy(remoteObjectName, url, anInterface).GetTransparentProxy();
  56. }
  57. private XmlRpcClientProxy(String remoteObjectName, String url, Type t) : base(t)
  58. {
  59. _remoteObjectName = remoteObjectName;
  60. _url = url;
  61. }
  62. /// <summary>The local method dispatcher - do not invoke.</summary>
  63. override public IMessage Invoke(IMessage msg)
  64. {
  65. IMethodCallMessage methodMessage = (IMethodCallMessage)msg;
  66. _client.MethodName = _remoteObjectName + "." + methodMessage.MethodName;
  67. _client.Params.Clear();
  68. foreach (Object o in methodMessage.Args)
  69. _client.Params.Add(o);
  70. try
  71. {
  72. Object ret = _client.Invoke(_url);
  73. return new ReturnMessage(ret,null,0,
  74. methodMessage.LogicalCallContext, methodMessage);
  75. }
  76. catch (Exception e)
  77. {
  78. return new ReturnMessage(e, methodMessage);
  79. }
  80. }
  81. }
  82. }