I'm working on the xmlrpc vapi, but i have some questions about what's the best
way to define such abstractions on C. Attached is the vapi file and a example
program.

Please, tell me which things will you change, and how can I avoid using cname 
for
each function, because looks like cprefix is ignored.

things like env.string_new() vs new XmlRpc.String(env)... are not really nice, 
but
I would like to be able to map all this stuff in the language itself, is this 
possible
with a vapi? like in dbus or soup does?

Maybe I should write vala code inside the vapi to provide this abstraction?

Thanks
using Bincrowd;

const string NAME = "XMLRPC Vala Auth Client";
const string SERVER = "http://invalidurl.com/RPC2";;
const string VERSION = "1.0";

private void fail_if_fault(Env env) {
	if (env.fault_occurred) {
		stderr.printf ("FAIL: %s (%d)\n", env.fault_string,
			env.fault_code);
		Thread.exit (null);
	}
}

void print_value(Env env, XmlRpc.Value v) {
	XmlRpc.Type type = v.type ();
	switch (type) {
	case ARRAY:
		var a = (XmlRpc.Array*) v;
		int arsz = a->size (env);
		print ("Array [%d] = {\n", arsz);
		for (int i=0; i< arsz; i++) {
			XmlRpc.Value item = a->get_item (env, i);
			print ("  array[%d] = ", i);
			print_value (env, item);
			//print_value (env, env.array_get);
		}
		print ("}\n");
		break;
	case INT:
		int num;
		env.read_int(v, out num);
		print ("int(%d)\n", num);
		break;
	case STRING:
		unowned string str;
		env.read_string (v, out str);
		print ("string(%s)\n", str);
		break;
	case STRUCT:
		var s = (Struct*) v;
		print ("Struct size: %d\n", s->size (env));
		//env.struct_size (v);
		break;
	default:
		print ("Unknown data type %d\n", type);
		break;
	}
}

XmlRpc.Value do_login(Env env, Client cli, ServerInfo si) {
	XmlRpc.Value ret;
	var stru = new XmlRpc.Struct(env);
	stru.set_value (env, "username", env.string_new ("myname"));
	stru.set_value (env, "password", env.string_new ("mypass"));
	stru.set_value (env, "version", env.string_new ("1"));
	var functions = new XmlRpc.Array (env);
	stru.set_value (env, "functions", functions);
	var query = new XmlRpc.Array(env);
	query.append_item (env, stru);
	cli.call2 (env, si, "download", query, out ret);
	return ret;
}

void main() {
	Env env; 
	Client cli;
	print ("xmlrpc test program\n");

	Env.init (out env);
	Client.init (0, NAME, VERSION);
	Client.create (env, 0, NAME, VERSION, null, 0, out cli);

	var si = new ServerInfo (env, SERVER);
	fail_if_fault (env);

	si.set_basic_auth (env, "user", "pass");
	fail_if_fault (env);

	var v = do_login(env, cli, si);
	if (env.fault_occurred) {
		print ("Oops.. fault happenz (%s)\n", env.fault_string);
	} else {
		print ("type = %d (struct = %d)\n", v.type(), XmlRpc.Type.STRUCT);
		print_value (env, v);
	}
	
	si = null;
}
[CCode (cheader_filename="xmlrpc-c/base.h,xmlrpc-c/client.h")]
namespace XmlRpc {
	[Compact]
	[CCode (cname="xmlrpc_client", free_function="xmlrpc_client_destroy", cprefix="xmlrpc_client_")]
	public class Client {
		public int version_major;
		public int version_minor;
		public int version_point;
		public static void init(int flags, string appname, string appversion);

		[CCode (cname="xmlrpc_client_create")]
		public static void create (Env env, int flags, string appname, string appversion, Value? parms, int nparms, out Client cli);

		[CCode (instance_pos=1.1)]
		public void call2(Env e, ServerInfo si, string method_name, Value param, out Value result);
		public static Value call_server(Env e, ServerInfo si, string method_name, string format, ...);
		public static Value call_server_params(Env e, ServerInfo si, string method_name, Value values);
		//TODO: static void init2()
		//static Value call(Env env, string url, string method_name, string format, ...);
		public void setup_global_const(Env e);
		public static void teardown_global_const();
	}

	public enum Flags {
		NO_FLAGS = 0,
		SKIP_LIBWWW_INIT = 1
	}

	[Compact]
	[CCode (cname ="xmlrpc_server_info", cprefix="xmlrpc_server_info_")]
	public class ServerInfo {
		public ServerInfo(Env env, string server_url);
		public ServerInfo copy(Env e);
		public void free();
		/* auth */
		[CCode (instance_pos=1.1)]
		public void set_basic_auth(Env e, string user, string pass);
		[CCode (instance_pos=1.1)]
		public void set_user(Env e, string user, string pass);
		[CCode (instance_pos=1.1)]
		public void allow_auth_basic(Env e);
		[CCode (instance_pos=1.1)]
		public void disallow_auth_basic(Env e);
		[CCode (instance_pos=1.1)]
		public void allow_auth_digest(Env e);
		[CCode (instance_pos=1.1)]
		public void disallow_auth_digest(Env e);
		[CCode (instance_pos=1.1)]
		public void allow_auth_negotiate(Env e);
		[CCode (instance_pos=1.1)]
		public void disallow_auth_negotiate(Env e);
		[CCode (instance_pos=1.1)]
		public void allow_auth_ntlm(Env e);
		[CCode (instance_pos=1.1)]
		public void disallow_auth_ntlm(Env e);
	}

	[Compact]
	[CCode (cname ="xmlrpc_value", cprefix="xmlrpc_value_", free_function="")]
	public class Value {
		public Type type();
	}

	[CCode (cname="xmlrpc_type", cprefix="XMLRPC_TYPE_")]
	public enum Type {
		INT,
		BOOL,
		DOUBLE,
		DATETIME,
		STRING,
		BASE64,
		ARRAY,
		STRUCT,
		C_PTR,
		NIL,
		I8,
		DEAD
	}

	[Compact]
	[CCode (cname ="xmlrpc_env", cprefix="xmlrpc_env_", free_function="", destroy_function="")]
	public struct Env {
		public string fault_string;
		public int fault_code;
		public bool fault_occurred;
		public static void init(out Env env);

		[CCode (cname="xmlrpc_parse_response")]
		public Value parse_response(string xmldata, int xmldata_len);

		/* create */
		public Value struct_new();
		public Value array_new();
		public Value bool_new(bool v);
		[CCode (cname="xmlrpc_string_new")]
		public Value string_new(string str);
		public Value int_new(int num);
		public Value double_new(double v);

		/* read */
		[CCode (cname="xmlrpc_read_bool")]
		public void read_bool(Value v, out bool num);
		[CCode (cname="xmlrpc_read_string")]
		public void read_string(Value v, out unowned string str);
		[CCode (cname="xmlrpc_read_int")]
		public void read_int(Value v, out int num);
		[CCode (cname="xmlrpc_read_double")]
		public void read_double(Value v, out double num);

		/* array */
		[CCode (cname="xmlrpc_array_size")]
		public int array_size(Value a);
		[CCode (cname="xmlrpc_array_size")]
		public Value array_get_item(Value a, int idx);
		[CCode (cprefix="xmlrpc_")]
		public int array_append_item(Value a, int idx, ref Value b);
		[CCode (cprefix="xmlrpc_")]
		public int array_read_item(Value a, int idx, out Value b);

		/* struct */
		// TODO: move into StructValue
		[CCode (cname="xmlrpc_struct_size")]
		public int struct_size(Value stru);
		[CCode (cname="xmlrpc_struct_has_key")]
		public bool struct_has_key(Value stru, string key);
		[CCode (cprefix="xmlrpc_")]
		public bool struct_has_key_n(Value stru, string key, int key_len);
		[CCode (cprefix="xmlrpc_")]
		public void struct_has_find_value(Value stru, string key, out Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_has_find_value_v(Value stru, string key, out Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_read_value(Value stru, string key, out Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_read_value_v(Value stru, Value key, out Value v);
		[CCode (cprefix="xmlrpc_")]
		public Value struct_get_value(Value stru, string key);
		[CCode (cprefix="xmlrpc_")]
		public Value struct_get_value_n(Value stru, string key, int key_len);
		[CCode (cprefix="xmlrpc_")]
		public void struct_set_value(Value stru, string key, Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_set_value_n(Value stru, string key, int key_len, Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_set_value_v(Value stru, Value keyval, Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_read_member(Value stru, int idx, out Value key, out Value v);
		[CCode (cprefix="xmlrpc_")]
		public void struct_get_key_and_value(Value stru, int idx, out Value key, out Value v);
	}

	// experimental //
	[CCode (cname="xmlrpc_value")]
	class Struct : Value {
		[CCode (cname="xmlrpc_struct_new", instance_pos=1.1)]
		public Struct (Env env);
		[CCode (cname="xmlrpc_struct_size", instance_pos=1.1)]
		public int size(Env env);
		[CCode (cname="xmlrpc_struct_set_value", instance_pos=1.1)]
		public void set_value(Env env, string key, Value v);
	}

	[CCode (cname="xmlrpc_value")]
	public class String : Value {
		[CCode (cname="xmlrpc_string_new")]
		public String(string str);
	}

	[CCode (cname="xmlrpc_value")]
	public class Array : Value {
		[CCode (cname="xmlrpc_array_new", instance_pos=1.1)]
		public Array (Env env);
		[CCode (cname="xmlrpc_array_size", instance_pos=1.1)]
		public int size(Env env);
		[CCode (cname="xmlrpc_array_get_item", instance_pos=1.1)]
		public Value get_item(Env env, int idx);
		[CCode (cname="xmlrpc_array_append_item", instance_pos=1.1)]
		public int append_item(Env env, Value b);
	}
}
_______________________________________________
vala-list mailing list
vala-list@gnome.org
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to