Skip to content

Commit

Permalink
Improve README and demo
Browse files Browse the repository at this point in the history
  • Loading branch information
plt committed May 18, 2011
1 parent 02eb074 commit c2bc98f
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 900 deletions.
827 changes: 159 additions & 668 deletions COPYING

Large diffs are not rendered by default.

165 changes: 0 additions & 165 deletions COPYING.LESSER

This file was deleted.

Empty file removed README
Empty file.
88 changes: 34 additions & 54 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
libsearpc 1.0

Last Modified: 2011-4-8

This file is free software; as a special exception the author gives
unlimited permission to copy and/or distribute it, with or without
modifications, as long as this notice is preserved.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


Introduction
======
============

Searpc is a simple C language RPC framework based on GObject system. Searpc
handles the serialization/deserialization part of RPC, the transport
Expand Down Expand Up @@ -40,33 +27,33 @@ Client

In the client side, you need to:

* Define your RPC function with the our marco.
* Define your RPC function with our marco.
* write code to send the request to server and get the resonpse from it.


### Define your RPC function ###

The client define RPC functions using macros defined in
**`searpc-client.h`**. To call the functions, the client needs to create
`searpc-client.h`. To call the functions, the client needs to create
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
* 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_demo_int__string)
SEARPC_CLIENT_DEFUN_INT__STRING(searpc_strlen)

/* create an rpc_client and supply the transport function. */
SearpcClient *rpc_client;
rpc_client = searpc_client_new();
rpc_client->transport = transport_callback;
rpc_client->arg = &sockfd;

The **`SEARPC_CLIENT_DEFUN_XXX__YYY`** macro defines a client side RPC function.
The `SEARPC_CLIENT_DEFUN_XXX__YYY` macro defines a client side RPC function.
**XXX** stands for the return type and **YYY** stands for the param type. If there
are multiple params, just append every param type there. For example,
**`SEARPC_CLIENT_DEFUN_INT__STRING_STRING`** defines a RPC function which takes two
`SEARPC_CLIENT_DEFUN_INT__STRING_STRING` defines a RPC function which takes two
string params and returns an integer value.


Expand All @@ -90,10 +77,10 @@ The prototype of the transport function is:
/*
* arg: rpc_client->arg. Normally a socket number.
* fcall_str: the JSON data stream generated by Searpc.
* fcall_len: the length of **`fcall_str`**.
* fcall_len: the length of `fcall_str`.
* ret_len: place to get the length of the returned json data stream.
* Returns: the JSON data stream ready for transporting.
*/
* Returns: A newly allocated string stores the JSON data stream.
*/
static char *transport_callback (void *arg, const char *fcall_str, size_t fcall_len, size_t *ret_len);


Expand All @@ -109,24 +96,25 @@ And Searpc handles the others for you.

### Concepts ###

* **`Marshal`**: The process of packing the result of the RPC function
into JSON data format stream is called marshalling, and the
function used to pack the result is called a **`marshal`**.
* **Marshal**: The process of unpacking the function arguments from
JSON data, call the RPC function and packing the result into JSON
data format is called marshalling. The function used to
pack the result is called a **marshal**.

* **`Signature`**: Signatures are used to identify different types of
* **Signature**: Signatures are used to identify different types of
marshals. After the execution of a RPC function on the server side,
different types of results need different marshals.

* **`Function table`**: The hash table used to store the (funcname, func)
* **Function table**: The hash table used to store the (funcname, func)
pairs.

* **`Marshal table`**: The hash table used to store the (signature,
* **Marshal table**: The hash table used to store the (signature,
marshal) pairs.


### Register your function ###

The **`seaprc_server_register_function`** routine registers a function as
The `seaprc_server_register_function` routine registers a function as
a RPC function. It inserts your function into the function table. The
prototype of this function is:

Expand All @@ -139,23 +127,23 @@ prototype of this function is:
*/
gboolean searpc_server_register_function (void *func, const gchar *fname, const gchar *signature)

Before calling **`searpc_server_register_function`**, you need to call
Before calling `searpc_server_register_function`, you need to call
**`searpc_server_init()`** to do the initialization work.


### Call the RPC fucntion ###

After the registration, you should listen to the socket and wait for the
incoming request data stream. Once you get a valid request, call the
**`searpc_server_call_function()`** routine, which will automatically do the
`searpc_server_call_function()` routine, which will automatically do the
following work for you:

* Parse the JSON data stream to resolve the function name and the data.
* Lookup the function in the function table according to the funcname.
* If a proper function is found, call the function with the given params.
* Packing the result into a JSON data string.

The prototype of **`searpc_server_call_function`** is:
The prototype of `searpc_server_call_function` is:

/*
* data: The incoming JSON data stream.
Expand All @@ -165,24 +153,20 @@ The prototype of **`searpc_server_call_function`** is:
*/
gchar* searpc_server_call_function (gchar *data, gsize len, gsize *ret_len)

The value returned by **`searpc_server_call_function()`** is the JSON data
ready for transportation, which you can send to the client directly.

In the client side, Searpc would unpack the data stream and return the value
contained within it as the return value of the client-side RPC function defined
in the macro **`SEARPC_CLIENT_DEFUN_XXX__YYY()`**.
The value returned by `searpc_server_call_function()` is the JSON data
ready to send back to the client.


Pysearpc
======
========

**Pysearpc** is the Python binding of **Searpc**. Only the client side function is
supported. To use it, simply define a class which inherits **SearpcClient**, and
provide a **`call_remote_func_sync`** method, which is equivalent to the
**`transport_callback`**.
provide a `call_remote_func_sync` method, which is equivalent to the
`transport_callback`.

To define your RPC funtion, use the **`@searpc_func`** decorator. It is
equivalent to **`SEARPC_CLIENT_DEFUN_XXX__YYY`** macro. To define a RPC
To define your RPC funtion, use the `@searpc_func` decorator. It is
equivalent to `SEARPC_CLIENT_DEFUN_XXX__YYY` macro. To define a RPC
function which accepts multiple params, here is an example:

class SampeSearpcClient(SearpcClient):
Expand All @@ -207,16 +191,17 @@ There are well-commented demos in both C and Python.
* **searpc-demo-client.c**: The client side demo in C
* **pysearpc-demo-client.py**: The client side demo in Python

To run the demo, run the server demo in a shell, and run the client demo(either
the C or Python demo) in another.
To run the demo, run the server demo in a shell, and run the client
demo in another. To run the python demo, you should first install the
package and setup the **PYTHONPATH** appropriately.


Thread Safety
======
=============


Extend Searpc
======
=============

To support new rpc function types,

Expand All @@ -227,7 +212,7 @@ To support new rpc function types,


Dependency
=====
==========

The following packages are required to build libsearpc:

Expand All @@ -237,8 +222,3 @@ The following packages are required to build libsearpc:
* pygobject-2.0 >= 2.0 (if you choose to build pysearpc)


License
======

You may redistribute copies of libsearpc under the terms of the GNU LGPL(Lesser General Public License). For
more information about these matters, see the files named 'LICENSE' and 'COPYING'.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])


AM_INIT_AUTOMAKE([1.9])
AM_INIT_AUTOMAKE([1.9 foreign])
# Checks for programs.
AC_PROG_CC
AC_PROG_LIBTOOL
Expand Down
Loading

0 comments on commit c2bc98f

Please sign in to comment.