/**
* @author Ma Bingyao(andot@ujn.edu.cn)
* @copyright CoolCode.CN
* @package PHPRPC Agent Generator for .NET
* @version 2.1
* @last_update 2006-08-10
* @link http://www.coolcode.cn/?p=199
*/
using System;
using System.Text;
using System.IO;
using System.Net;
namespace pag
{
class Program
{
static void ShowVersion()
{
Console.WriteLine("PHPRPC Agent Generator for .NET 2.1.2006.0810");
Console.WriteLine("CopyRight (C) CoolCode.CN 2006");
Console.WriteLine();
}
static void ShowUsage()
{
Console.WriteLine(" Usage: pag <service url> <class name> [language]");
Console.WriteLine();
Console.WriteLine("Example: pag http://example.hostname.com/server.php MyClient c#");
}
static void ShowError(string error)
{
Console.WriteLine();
Console.WriteLine(error);
}
static Encoding GetEncoding(HttpWebResponse response)
{
string charset = response.GetResponseHeader("Content-Type");
if (charset.StartsWith("text/plain; charset="))
{
charset = charset.Substring(20, charset.Length - 20);
return Encoding.GetEncoding(charset);
}
return Encoding.UTF8;
}
static void genCSharp(string ClassName, string[] functions) {
StreamWriter sw = new StreamWriter(ClassName + ".cs");
sw.WriteLine("using System;");
sw.Write("public class ");
sw.Write(ClassName);
sw.WriteLine(": PHPRPC.Client");
sw.WriteLine("{");
for (int i = 0; i < functions.Length; i++)
{
sw.Write(" public object ");
sw.Write(functions[i]);
sw.WriteLine("(params object[] args)");
sw.WriteLine(" {");
sw.Write(" return this.Invoke(\"");
sw.Write(functions[i]);
sw.WriteLine("\", args);");
sw.WriteLine(" }");
sw.Write(" public object ");
sw.Write(functions[i]);
sw.WriteLine("(ref object[] args)");
sw.WriteLine(" {");
sw.Write(" return this.Invoke(\"");
sw.Write(functions[i]);
sw.WriteLine("\", ref args);");
sw.WriteLine(" }");
}
sw.WriteLine("}");
sw.Close();
}
static void Main(string[] args)
{
ShowVersion();
if (args.Length < 2)
{
ShowUsage();
return;
}
HttpWebRequest request;
HttpWebResponse response;
try
{
request = HttpWebRequest.Create(args[0]) as HttpWebRequest;
response = request.GetResponse() as HttpWebResponse;
}
catch
{
ShowUsage();
ShowError("Wrong URL!");
return;
}
Stream s = response.GetResponseStream();
Encoding encoding = GetEncoding(response);
StreamReader sr = new StreamReader(s, encoding);
string body = sr.ReadLine();
if (body.StartsWith("phprpc_functions"))
{
Console.Write(body.Substring(18, body.Length - 20));
string[] functions = (string[])PHPSerializer.UnSerialize(Convert.FromBase64String(body.Substring(18, body.Length - 20)), typeof(string[]), encoding);
if (args.Length == 2)
{
genCSharp(args[1], functions);
}
else
{
switch (args[2].ToLower())
{
case "cs":
case "c#":
case "csharp":
genCSharp(args[1], functions);
break;
/*
case "vb":
case "vb.net":
case "visualbasic":
genVisualBasic(args[1], functions);
break;
case "j#":
case "jsl":
case "jsharp":
genJSharp(args[1], functions);
break;
*/
default:
ShowError("The language " + args[2] + " isn't supported!");
break;
}
}
}
else
{
ShowUsage();
ShowError("The URL is not a PHPRPC Server!");
}
response.Close();
}
}
}