ONCRPC restricted us to remote procedures that accept a single parameter and return a single parameter. (Might not be true now but stick to this restriction)
Instead of starting with IDL (like Example 1 & 2), start wtih API.h and generate ONCRPC middle-layer automatically. Keep (or share) the same local implementation between RPC server and RPC client.
Similar to non-RPC application, the below files are manaully created. Create RPC middle-layer to integrate the local APIs defined in cl_calc.h
. Use the same API locally or remotely.
cl_calc.c/h
: functions declaration, definition.
cl_client.c
: client main program
cl_common.c/h
: RPC Client handler
cl_return_t CL_CALC_WRITE ( HANDLER *remote , cl_msg_t in, cl_bus_t *out )
{
if ( remote != NULL && remote->clnt != (CLIENT *)NULL ) {
...
res = cl_calc_write_func_1( &req, remote->clnt );
...
return ret;
} else {
return cl_calc_write( in,out );
}
}
CL is an imaginary product name for example 3.
- cpp cl_calc.h -o cl_calc.i
- pycparser to generate AST
- AST visitor extracts typdef & *** fucntion declaration *** out of AST
- generate IDL & RPC middle-layer Interface
The following files are generated from cl_calc.h
- CL.x (ONCRPC IDL)
- CL_proc.c (Client Middlelayer)
- CL_svc_proc.c (Server Middlelayer)
- CL_proc.h
CL.x
generates :
- CL.h
- CL_svc.c
- CL_clnt.c
- CL_xdr.c
pycparser installation. ( powered by PLY )
pip install --index-url=https://pypi.python.org/simple/ pycparser
from pycparser import c_parser, c_ast, parse_file
class FuncCallVisitor(c_ast.NodeVisitor):
def __init__(self):
...
def visit_FuncDecl (self, node):
...
if __name__ == "__main__":
ast = parse_file(args.filename, use_cpp=False)
v = FuncCallVisitor()
v.visit(ast)