1
0
mirror of https://github.com/haiwen/libsearpc.git synced 2025-08-02 13:18:04 +00:00

Clean project

* Remove unused code generating scripts
* Rename files to be more clear
* Improve dependency in Makefile.am
This commit is contained in:
plt 2011-05-18 17:24:34 +08:00
parent c2bc98f295
commit c5e4719c4f
14 changed files with 252 additions and 433 deletions

8
.gitignore vendored
View File

@ -30,3 +30,11 @@ libtool
ltmain.sh
missing
py-compile
cscope.files
cscope.out
lib/fcall-impr.h
lib/marshal.h
lib/searpc-dfun.h
lib/searpc-fcall.h
lib/searpc-signature.h
tests/test-searpc

View File

@ -1,7 +1,7 @@
AM_CPPFLAGS = -I$(top_srcdir)/lib
ACLOCAL_AMFLAGS = -I m4
GNU_STANDARD_FILES = README COPYING COPYING.LESSER AUTHORS ChangeLog NEWS
GNU_STANDARD_FILES = README.markdown COPYING AUTHORS ChangeLog NEWS
EXTRA_DIST = $(GNU_STANDARD_FILES)

View File

@ -20,7 +20,7 @@ contains three fields:
Example
======
=======
Client
------
@ -41,7 +41,7 @@ a SearpcClient object and supply a transport function. For example:
* define a RPC function which takes a string argument and returns
* an integer. The xxx_INT__STRING naming convention is used to
* indicate the types of the param(string) and the return value(int).
*/
*/
SEARPC_CLIENT_DEFUN_INT__STRING(searpc_strlen)
/* create an rpc_client and supply the transport function. */
@ -124,7 +124,7 @@ prototype of this function is:
* function in the fucntion hash table.
* signature: the identifier used to get the corresponding marshal.
* Returns: a gboolean value indicating success or failure
*/
*/
gboolean searpc_server_register_function (void *func, const gchar *fname, const gchar *signature)
Before calling `searpc_server_register_function`, you need to call
@ -150,7 +150,7 @@ The prototype of `searpc_server_call_function` is:
* len: The length of **`data`**.
* ret_len: Place to hold the length of the JSON data stream to be returned
* Returns: The JSON data containing the result of the RPC
*/
*/
gchar* searpc_server_call_function (gchar *data, gsize len, gsize *ret_len)
The value returned by `searpc_server_call_function()` is the JSON data
@ -183,7 +183,7 @@ See the demo program for a more detailed example.
Demos
======
=====
There are well-commented demos in both C and Python.
@ -220,5 +220,3 @@ The following packages are required to build libsearpc:
* gobject-2.0 >= 2.16.0
* json-glib-1.0 >= 0.10.2
* pygobject-2.0 >= 2.0 (if you choose to build pysearpc)

View File

@ -13,8 +13,6 @@ AM_INIT_AUTOMAKE([1.9 foreign])
AC_PROG_CC
AC_PROG_LIBTOOL
# Checks for header files.
AC_CHECK_HEADERS([arpa/inet.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h])
@ -43,7 +41,7 @@ AM_CONDITIONAL([COMPILE_DEMO], [test x${compile_demo} = xyes])
AC_ARG_ENABLE([compile-python],
[AS_HELP_STRING([--enable-compile-python],
[compile pysearpc @<:@default: no@:>@])],
[compile_python=${enableval}], [compile_python=no])
[compile_python=${enableval}], [compile_python=yes])
AM_CONDITIONAL([COMPILE_PYTHON], [test x${compile_python} = xyes])

View File

@ -27,7 +27,7 @@ static char *transport_callback(void *arg, const char *fcall_str,
char buf[BUFLEN];
packet *pac, *pac_ret;
fd = (int) arg;
fd = (int)(long) arg;
pac = (packet *)buf;
/* construct the packet */
@ -88,7 +88,7 @@ main(int argc, char *argv[])
/* create an rpc_client and supply the transport function. */
rpc_client = searpc_client_new();
rpc_client->transport = transport_callback;
rpc_client->arg = (void *)sockfd;
rpc_client->arg = (void *)(long)sockfd;
/* call the client-side funcion */
ret = searpc_strlen(rpc_client, "hello searpc", &error);

View File

@ -1,38 +1,54 @@
rpc_headers = fcall-impr.h searpc-fcall.h searpc-dfun.h searpc-signature.h searpc-marshal.h
# Five files are generated by a python script:
# Used in client:
#
# * fcall-impr.h: contains the implementation of functions like
# `searpc_client_fcall__string_string`. These function is used by the
# client to pack the RPC call to json data.
# * searpc-fcall.h: constains the declaration of the functions in fcall-impr.h
# * searpc-dfun.h: contains macros like `SEARPC_CLIENT_DEFUN_INT__STRING(funcname)`.
#
# Used in server:
#
# * searpc-signature.h: contains functions like `searpc_signature_int__string()`.
# * searpc-marshal.h: contains functions like `marshal_int__string()`
#
genrpc_files = gen-dfun-macro.py rpc_table.py
generated_sources = fcall-impr.h searpc-fcall.h searpc-dfun.h \
searpc-signature.h marshal.h
# rpc_headers = fcall-impr.h searpc-fcall.h searpc-dfun.h searpc-signature.h searpc-marshal.h
AM_CFLAGS = @GLIB2_CFLAGS@ @GOBJECT_CFLAGS@ @JSON_GLIB_CFLAGS@ -I${top_builddir}/lib -I${top_srcdir}/lib
# we need to generate the headers first
BUILT_SOURCES = genrpc
# we need to generate the first
BUILT_SOURCES = gensource
lib_LTLIBRARIES = libsearpc.la
include_HEADERS = searpc-client.h searpc-server.h searpc-utils.h
include_HEADERS = searpc-fcall.h searpc-client.h searpc-dfun.h searpc-server.h
libsearpc_la_SOURCES = searpc-client.c searpc-server.c
noinst_HEADERS = searpc-utils.h fcall-impr.h marshal.h
EXTRA_libsearpc_la_SOURCES = ${genrpc_files}
libsearpc_la_SOURCES = searpc-client.c searpc-server.c $(generated_sources)
libsearpc_la_LDFLAGS = -Wl,-z -Wl,defs -version-info 1:0:0
libsearpc_la_LDFLAGS = -Wl,-z -Wl,defs -version-info 1:1:0
libsearpc_la_LIBADD = @GLIB2_LIBS@ @GOBJECT_LIBS@ @JSON_GLIB_LIBS@
genrpc_files = gencode.py rpc_table.py
genrpc: ${rpc_headers} ${genrpc_files}
dist_data_DATA = ${genrpc_files}
gensource: ${generated_sources}
${rpc_headers}: ${genrpc_files}
${generated_sources}: gencode.py rpc_table.py
@echo "[libsearpc]: generating rpc header files"
python $< gen-fcall > fcall-impr.h
python $< gen-fcall-declare > searpc-fcall.h
python $< gen-dfun-macro > searpc-dfun.h
python $< gen-signature > searpc-signature.h
python $< gen-marshal > searpc-marshal.h
python gencode.py gen-fcall > fcall-impr.h
python gencode.py gen-fcall-declare > searpc-fcall.h
python gencode.py gen-dfun-macro > searpc-dfun.h
python gencode.py gen-signature > searpc-signature.h
python gencode.py gen-marshal > marshal.h
@echo "[libsearpc]: done"
CLEANFILES = ${rpc_headers}
DISTCLEANFILES = ${generated_sources}

View File

@ -342,3 +342,5 @@ if __name__ == "__main__":
gen_fcall_declare_list()
elif command == "gen-dfun-macro":
gen_dfun_macro_list()
else:
print "Unknown command %s" % (command)

View File

@ -13,6 +13,7 @@ typedef struct {
} SearpcClient;
SearpcClient *searpc_client_new ();
void searpc_client_free (SearpcClient *client);
char* searpc_client_transport_send (SearpcClient *client,
@ -22,24 +23,23 @@ char* searpc_client_transport_send (SearpcClient *client,
#include <searpc-fcall.h>
/**
* searpc_client_fret__string:
* @data: the return string of RPC call
* @len: then length of @data
* @error: return location for a #GError, or %NULL
*
* Extract return value of type `char*` from the return string of RPC
* call.
*/
char* searpc_client_fret__string (char *data, size_t len, GError **error);
int searpc_client_fret__int (char *data, size_t len, GError **error);
GObject* searpc_client_fret__object (GType gtype, char *data,
size_t len, GError **error);
GList* searpc_client_fret__objlist (GType gtype, char *data,
size_t len, GError **error);
char*
searpc_client_fret__string (char *data, size_t len, GError **error);
int
searpc_client_fret__int (char *data, size_t len, GError **error);
GObject*
searpc_client_fret__object (GType gtype, char *data,
size_t len, GError **error);
GList*
searpc_client_fret__objlist (GType gtype, char *data,
size_t len, GError **error);
/* in case of transport error, the following code and message will be
* set in GError */
#define TRANSPORT_ERROR "Transport Error"
#define TRANSPORT_ERROR_CODE 500

View File

@ -100,7 +100,7 @@ marshal_set_ret_common (JsonObject *object, gsize *len, GError *error)
}
/* include the generated marshal functions */
#include <searpc-marshal.h>
#include "marshal.h"
void
searpc_server_init ()

View File

@ -54,6 +54,6 @@ gchar *searpc_server_call_function (gchar *func, gsize len, gsize *ret_len);
char* searpc_compute_signature (gchar *ret_type, int pnum, ...);
/* Signatures */
#include "searpc-signature.h"
#include <searpc-signature.h>
#endif

View File

@ -1,14 +1,16 @@
EXTRA_DIST = pysearpc-demo-client.py
genrpc_files = pysearpc-gen-dfun-macro.py rpc_table.py
genrpc_files = pygencode.py rpc_table.py
AM_CFLAGS = -DPKGDATADIR=\"$(pkgdatadir)\" \
-DPACKAGE_DATA_DIR=\""$(pkgdatadir)"\" \
@GLIB2_CFLAGS@ @PYGOBJECT_CFLAGS@ \
-I/usr/include/python$(PYTHON_VERSION) \
-I${top_builddir}/lib -I${top_srcdir}/lib
-I${top_builddir}/lib \
-I${top_srcdir}/lib
pkgpyexec_PYTHON = __init__.py client.py pysearpc-gen-dfun-macro.py
pkgpyexec_PYTHON = __init__.py client.py
BUILT_SOURCES = fcallfret.h
# generating the fcallfret module
pkgpyexec_LTLIBRARIES = fcallfret.la
@ -17,11 +19,9 @@ fcallfret_la_LDFLAGS = -module -avoid-version -export-symbols-regex initfcallfre
fcallfret_la_SOURCES = fcallfret.c fcallfret.h
fcallfret_la_LIBADD = @GLIB2_LIBS@ @GOBJECT_LIBS@ $(top_builddir)/lib/libsearpc.la
EXTRA_fcallfret_la_SOURCES = ${genrpc_files}
dist_data_DATA = ${genrpc_files}
CLEANFILES = fcallfret.h
fcallfret.h: ${genrpc_files}
python pygencode.py > fcallfret.h
fcallfret.c: ${genrpc_files}
python $< gen-searpcclient > fcallfret.h
.PHONY: fcallfret.c
DISTCLEANFILES = fcallfret.h

173
pysearpc/pygencode.py Normal file
View File

@ -0,0 +1,173 @@
#!/usr/bin/python
import string
import sys
sys.path += ['../']
module_func_array_template = r"""
static PyMethodDef SearpcClientModule_Functions[] = {
${array}
{NULL, NULL, 0, NULL},
};
"""
func_item_template = r"""
{"${pyfuncname}", (PyCFunction)${cfuncname},
METH_VARARGS, "" },"""
def gen_fcall_funcs_array(arg_types):
fcall_array = ""
if len(arg_types) == 0:
pyfuncname = "fcall__void"
cfuncname = "SearpcClient_Fcall__Void"
else:
pyfuncname = "fcall__" + "_".join(arg_types)
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
cfuncname = "SearpcClient_Fcall__" + "_".join(tmplist)
return string.Template(func_item_template)\
.substitute(pyfuncname=pyfuncname,
cfuncname=cfuncname)
def gen_fret_funcs_array(ret_type):
fret_array = ""
if ret_type is None:
pyfuncname = "fret__void"
cfuncname = "SearpcClient_Fret__Void"
else:
pyfuncname = "fret__" + ret_type
cfuncname = "SearpcClient_Fret__" + string.capitalize(ret_type)
return string.Template(func_item_template)\
.substitute(pyfuncname=pyfuncname,
cfuncname=cfuncname)
def gen_module_funcs_array():
"""
Generate static PyMethodDef SearpcClientModule_Functions[]
"""
from rpc_table import func_table
# generate fcall methods array
fcall_array = ""
arg_types_list = []
for item in func_table:
if item[1] not in arg_types_list:
arg_types_list.append(item[1])
for item in arg_types_list:
fcall_array += gen_fcall_funcs_array(item)
# generate fret methods array
fret_array = ""
ret_types_list = []
for item in func_table:
if item[0] not in ret_types_list:
ret_types_list.append(item[0])
for item in ret_types_list:
fret_array += gen_fret_funcs_array(item)
array = fcall_array
array += fret_array
print string.Template(module_func_array_template)\
.substitute(array=array)
type_table = {
"string" : ("char *", "s"),
"int" : ("int", "i"),
"object" : ("GObject *", "O"),
}
fcall_template = r"""
static PyObject *
SearpcClient_Fcall__${Suffix}(PyObject *self,
PyObject *args)
{
char *fname;
${def_args}
char *fcall;
gsize len;
if (!PyArg_ParseTuple(args, "${fmt}", ${args_addr}))
return NULL;
fcall = searpc_client_fcall__${suffix}(${args});
return PyString_FromString(fcall);
}
"""
def gen_fcall_func(arg_types):
if len(arg_types) == 0:
Suffix = "Void"
suffix = "void"
def_args = ""
kwargs_str = "NULL"
fmt = "s"
args_addr = "&fname"
args = "fname, &len"
return string.Template(fcall_template)\
.substitute(Suffix=Suffix, suffix=suffix,
def_args=def_args, kwargs_str=kwargs_str,
fmt=fmt, args_addr=args_addr, args=args)
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
Suffix = "_".join(tmplist)
suffix = "_".join(arg_types)
def_args = ""
kwargs_str = ""
fmt = "s"
args_addr = "&fname"
args = "fname"
for i, arg_type in enumerate(arg_types):
def_args += " " + type_table[arg_type][0] + " param" + \
str(i + 1) + ";\n"
kwargs_str += '"param' + str(i + 1) + '", '
fmt += type_table[arg_type][1]
args_addr += ", &param" + str(i + 1)
args += ", param" + str(i + 1)
kwargs_str += "NULL"
args += ", &len"
return string.Template(fcall_template)\
.substitute(Suffix=Suffix, suffix=suffix,
def_args=def_args, kwargs_str=kwargs_str,
fmt=fmt, args_addr=args_addr, args=args)
def gen_fcall_list():
from rpc_table import func_table
arg_types_list = []
for item in func_table:
if item[1] not in arg_types_list:
arg_types_list.append(item[1])
for item in arg_types_list:
print gen_fcall_func(item)
if __name__ == "__main__":
gen_fcall_list()
gen_module_funcs_array()

View File

@ -1,357 +0,0 @@
#!/usr/bin/python
"""
Generate function define macros.
"""
import string
import sys
sys.path += ['../']
# XXX: generate searpc-client.h file
module_func_array_template = r"""
static PyMethodDef SearpcClientModule_Functions[] = {
${array}
{NULL, NULL, 0, NULL},
};
"""
func_item_template = r"""
{"${pyfuncname}", (PyCFunction)${cfuncname},
METH_VARARGS, "" },"""
def gen_fcall_funcs_array(arg_types):
fcall_array = ""
if len(arg_types) == 0:
pyfuncname = "fcall__void"
cfuncname = "SearpcClient_Fcall__Void"
else:
pyfuncname = "fcall__" + "_".join(arg_types)
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
cfuncname = "SearpcClient_Fcall__" + "_".join(tmplist)
return string.Template(func_item_template)\
.substitute(pyfuncname=pyfuncname,
cfuncname=cfuncname)
def gen_fret_funcs_array(ret_type):
fret_array = ""
if ret_type is None:
pyfuncname = "fret__void"
cfuncname = "SearpcClient_Fret__Void"
else:
pyfuncname = "fret__" + ret_type
cfuncname = "SearpcClient_Fret__" + string.capitalize(ret_type)
return string.Template(func_item_template)\
.substitute(pyfuncname=pyfuncname,
cfuncname=cfuncname)
def gen_module_funcs_array():
from rpc_table import func_table
# generate fcall methods array
fcall_array = ""
arg_types_list = []
for item in func_table:
if item[1] not in arg_types_list:
arg_types_list.append(item[1])
for item in arg_types_list:
fcall_array += gen_fcall_funcs_array(item)
# generate fret methods array
fret_array = ""
ret_types_list = []
for item in func_table:
if item[0] not in ret_types_list:
ret_types_list.append(item[0])
for item in ret_types_list:
fret_array += gen_fret_funcs_array(item)
array = fcall_array
array += fret_array
print string.Template(module_func_array_template)\
.substitute(array=array)
type_table = {
"string" : ("char *", "s"),
"int" : ("int", "i"),
"object" : ("GObject *", "O"),
}
fcall_template = r"""
static PyObject *
SearpcClient_Fcall__${Suffix}(PyObject *self,
PyObject *args)
{
char *fname;
${def_args}
char *fcall;
gsize len;
if (!PyArg_ParseTuple(args, "${fmt}", ${args_addr}))
return NULL;
fcall = searpc_client_fcall__${suffix}(${args});
return PyString_FromString(fcall);
}
"""
def gen_fcall_func(arg_types):
if len(arg_types) == 0:
Suffix = "Void"
suffix = "void"
def_args = ""
kwargs_str = "NULL"
fmt = "s"
args_addr = "&fname"
args = "fname, &len"
return string.Template(fcall_template)\
.substitute(Suffix=Suffix, suffix=suffix,
def_args=def_args, kwargs_str=kwargs_str,
fmt=fmt, args_addr=args_addr, args=args)
tmplist = []
for arg in arg_types:
tmplist.append(string.capitalize(arg))
Suffix = "_".join(tmplist)
suffix = "_".join(arg_types)
def_args = ""
kwargs_str = ""
fmt = "s"
args_addr = "&fname"
args = "fname"
for i, arg_type in enumerate(arg_types):
def_args += " " + type_table[arg_type][0] + " param" + \
str(i + 1) + ";\n"
kwargs_str += '"param' + str(i + 1) + '", '
fmt += type_table[arg_type][1]
args_addr += ", &param" + str(i + 1)
args += ", param" + str(i + 1)
kwargs_str += "NULL"
args += ", &len"
return string.Template(fcall_template)\
.substitute(Suffix=Suffix, suffix=suffix,
def_args=def_args, kwargs_str=kwargs_str,
fmt=fmt, args_addr=args_addr, args=args)
def gen_fcall_list():
from rpc_table import func_table
arg_types_list = []
for item in func_table:
if item[1] not in arg_types_list:
arg_types_list.append(item[1])
for item in arg_types_list:
print gen_fcall_func(item)
ret_table = {
"string" : "PyString_FromString",
"int" : "PyLong_FromLong",
"object" : "pygobject_new",
}
fret_template = r"""
static PyObject *
SearpcClient_Fret__${Suffix}(PyObject *self,
PyObject *args)
{
char *data;
${def_args}
GError *error = NULL;
if (!PyArg_ParseTuple(args, "${fmt}", ${args_addr}))
return NULL;
res = searpc_client_fret__${suffix}(${args}, strlen(data), &error);
return ${ret_func};
}
"""
fret_objlist_template = r"""
static PyObject *SearpcClient_Fret__Objlist(PyObject *self,
PyObject *args)
{
char *data;
GType type;
PyObject *type_obj, *res, *tmp;
GList *list, *p;
GError *error = NULL;
static char *kwlist[] = { "type", "data", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
kwlist, &type_obj, &data))
return NULL;
type = pyg_type_from_object(type_obj);
list = searpc_client_fret__objlist(type, data, strlen(data), &error);
res = PyList_New(0);
for (p = list; p; p = p->next) {
tmp = pygobject_new(p->data);
PyList_Append(res, tmp);
}
return res;
}
"""
def gen_fret_func(ret_type):
if ret_type == "objlist":
return fret_objlist_template
Suffix = string.capitalize(ret_type)
suffix = ret_type
def_args = ""
kwargs_str = ""
fmt = ""
args_addr = ""
args = ""
if ret_type == "object":
def_args += " GType type;\n"
def_args += " PyObject *type_obj;\n"
def_args += " GObject *res;"
kwargs_str = '"type", "data", NULL'
fmt = "Os"
args_addr = "&type_obj, &data"
args = "type, data"
else:
def_args += " " + type_table[ret_type][0] + " res;"
kwargs_str = '"data", NULL'
fmt = "s"
args_addr = "&data"
args = "data"
ret_func = ret_table[ret_type] + "(res);"
return string.Template(fret_template)\
.substitute(Suffix=Suffix, suffix=suffix,
def_args=def_args, kwargs_str=kwargs_str,
fmt=fmt, args_addr=args_addr, args=args,
ret_func=ret_func)
def gen_fret_list():
from rpc_table import func_table
ret_type_list = []
for item in func_table:
if item[0] not in ret_type_list:
ret_type_list.append(item[0])
for item in ret_type_list:
print gen_fret_func(item)
# XXX: generate searpc.py file
searpc_file_hdr_template = r"""
import os
import sys
sys.path += ['', '.libs']
import json
import new
import types
from searpc_client import SearpcClientBase
"""
searpc_client_template = r"""
class SearpcClient(SearpcClientBase):
pass
def searpc_client_transport_send(client, fcall):
return client.transport_send(fcall, client.priv)
"""
searpc_defun_template = r"""
def defun_${ret_type}__${args_type}(func):
def newfunc(${args1}):
fcall = self.fcall__${args_type}(${args2})
fret = searpc_client_transport_send(self, fcall)
if fret is None:
return None
return self.fret__${ret_type}(${obj_type}fret)
return newfunc
"""
def gen_decorator(ret_type, args_type_list):
if len(args_type_list) == 0:
args_type = "void"
args1 = "self"
args2 = "func.__name__"
return string.Template(searpc_defun_template)\
.substitute(ret_type=ret_type, args_type=args_type,
args1=args1, args2=args2, obj_type="")
args_type = "_".join(args_type_list)
args1 = "self"
args2 = "func.__name__"
obj_type = ""
if ret_type == "object" or ret_type == "objlist":
args1 += ", obj_type"
obj_type = "obj_type, "
for i, arg_type in enumerate(args_type_list):
args1 += ", param" + str(i + 1)
args2 += ", param" + str(i + 1)
return string.Template(searpc_defun_template)\
.substitute(ret_type=ret_type, args_type=args_type,
args1=args1, args2=args2, obj_type=obj_type)
def gen_func_decorator():
from rpc_table import func_table
for item in func_table:
print gen_decorator(item[0], item[1])
def gen_pysearpc():
print searpc_file_hdr_template
print searpc_client_template
gen_func_decorator()
if __name__ == "__main__":
cmd = sys.argv[1]
if cmd == "gen-searpcclient":
gen_fcall_list()
gen_module_funcs_array()
elif cmd == "gen-pysearpc":
gen_pysearpc()
else:
print "arg error"

View File

@ -1,20 +0,0 @@
"""
Define RPC functions needed to generate
"""
# [ <ret-type>, [<arg_types>] ]
func_table = [
[ "int", ["string"] ],
[ "int", ["string", "string"] ],
[ "string", [] ],
[ "string", ["string"] ],
[ "string", ["string", "int"] ],
[ "objlist", [] ],
[ "objlist", ["string"] ],
[ "objlist", ["int", "int"] ],
[ "objlist", ["string", "int"] ],
[ "objlist", ["string", "int", "int"] ],
[ "objlist", ["string", "string", "int"] ],
[ "object", [] ],
[ "object", ["string"] ],
]

1
pysearpc/rpc_table.py Symbolic link
View File

@ -0,0 +1 @@
../lib/rpc_table.py