Show code - CODE/C-sharp/interfaces_delegates_xmlrpc.cs


using System;
using System.Text;
using CookComputing.XmlRpc;
using Base64;
// An interface to the weblog api.
[XmlRpcUrl("http://starlon.lyrical.net/XMLRPC/")]
interface IStarlonSite
{
[XmlRpcMethod("uptime", Description="Check uptime.")]
string Uptime();
[XmlRpcMethod("greeting", Description="Greet the client.")]
string Greeting( string clientName );
[XmlRpcMethod("add_news", Description="Add news to the database")]
string AddNews( string header, string body, string date );
}
// Inheritance class for the IStarlonSite interface.
public class StarlonSite : IStarlonSite
{
// Any method with string return type and no parameters
// can be encapsulated by MyDelegate.
public delegate string MyDelegate();
public string BasicMethod( MyDelegate method )
{
string rs = "\0";
try
{
rs = method();
}
catch(Exception e)
{
Console.WriteLine("Caught exception:\n {0}", e );
}
return rs;
}
public string Uptime()
{
IStarlonSite proxy = (IStarlonSite)
XmlRpcProxyGen.Create( typeof( IStarlonSite ) );
MyDelegate rpcCall = new MyDelegate( proxy.Uptime );
return BasicMethod( rpcCall );
}
/*
public string Uptime()
{
IStarlonSite proxy = (IStarlonSite)
XmlRpcProxyGen.Create( typeof( IStarlonSite ) );
string rs = "\0";
try
{
rs = proxy.Uptime();
}
catch(Exception e)
{
Console.WriteLine("Caught exception:\n {0}", e );
}
return rs;
}
*/
public string Greeting( string clientName )
{
IStarlonSite proxy = (IStarlonSite)
XmlRpcProxyGen.Create( typeof(IStarlonSite) );
string rs = "\0";
try
{
rs = proxy.Greeting( "Starlon" );
}
catch (Exception e)
{
Console.WriteLine("Caught exception:\n {0}", e );
}
return rs;
}
public string AddNews( string header, string body, string date )
{
IStarlonSite proxy = (IStarlonSite)
XmlRpcProxyGen.Create( typeof(IStarlonSite) );
string rs = "\0";
try
{
rs = proxy.AddNews( header, body, date );
}
catch (Exception e)
{
Console.WriteLine("Caught exception:\n {0}", e );
}
return rs;
}
// A temporary class. This will vanish when the GUI is started.
public class MyRpcClient
{
public static void Main()
{
//string userName = "starlon";
//string userPass = "passwordasdf";
string rs = "\0";
StarlonSite starlonSite = new StarlonSite();
rs = starlonSite.Uptime();
Console.WriteLine( rs );
rs = starlonSite.Greeting( "Starlon" );
rs = starlonSite.Uptime();
byte[] byteArray = Convert.FromBase64String( rs );
long arrayLength = (long) ((4/3.0) * byteArray.Length);
if (arrayLength % 4 != 0)
{
arrayLength += 4 - arrayLength % 4;
}
char[] charArray = new char[arrayLength];
int worked = Convert.ToBase64CharArray(
byteArray, 0, byteArray.Length, charArray, 0 );
Base64Decoder decoder = new Base64Decoder( charArray );
byte[] decodedArray = decoder.GetDecoded();
StringBuilder sb = new StringBuilder();
foreach( byte piece in decodedArray )
{
sb.Append( Convert.ToChar( piece ) );
}
Console.WriteLine( sb );
rs = starlonSite.Greeting( "Starlon" );
Console.WriteLine( rs );
rs = starlonSite.AddNews( "Test Message", "Test...", "6-3-2004" );
Console.WriteLine( rs );
}
}
}