From ec4fbdff32e7afc79c01fbd9ca7b3ac18301f92b Mon Sep 17 00:00:00 2001 From: "roberto@sirius" Date: Mon, 3 Jan 2011 20:52:23 +0100 Subject: [PATCH] added generic functions for rpc --- plugins/python/uwsgi_pymodule.c | 22 ++++++++++++++ rpc.c | 52 +++++++++++++++++++++++++++++++++ tests/rpc.py | 7 +++++ uwsgi.c | 3 ++ uwsgi.h | 14 +++++++-- uwsgiconfig.py | 2 +- 6 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 rpc.c create mode 100644 tests/rpc.py diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index b11b8c2b..549d392f 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -190,6 +190,26 @@ PyObject *py_uwsgi_register_file_monitor(PyObject * self, PyObject * args) { return Py_None; } +PyObject *py_uwsgi_register_rpc(PyObject * self, PyObject * args) { + + uint8_t argc = 0; + char *name; + PyObject *func; + + if (!PyArg_ParseTuple(args, "sO|B:register_signal", &name, &func, &argc)) { + return NULL; + } + + + if (uwsgi_register_rpc(name, 0, argc, func)) { + Py_INCREF(Py_None); + return Py_None; + } + + Py_INCREF(Py_True); + return Py_True; +} + PyObject *py_uwsgi_register_signal(PyObject * self, PyObject * args) { uint8_t uwsgi_signal; @@ -1947,6 +1967,8 @@ static PyMethodDef uwsgi_advanced_methods[] = { {"signal", py_uwsgi_signal, METH_VARARGS, ""}, {"register_file_monitor", py_uwsgi_register_file_monitor, METH_VARARGS, ""}, {"register_timer", py_uwsgi_register_timer, METH_VARARGS, ""}, + + {"register_rpc", py_uwsgi_register_rpc, METH_VARARGS, ""}, #ifdef UWSGI_SENDFILE {"sendfile", py_uwsgi_advanced_sendfile, METH_VARARGS, ""}, #endif diff --git a/rpc.c b/rpc.c new file mode 100644 index 00000000..f33d78f7 --- /dev/null +++ b/rpc.c @@ -0,0 +1,52 @@ +#include "uwsgi.h" + +extern struct uwsgi_server uwsgi; + +int uwsgi_register_rpc(char *name, uint8_t modifier1, uint8_t args, void *func) { + + struct uwsgi_rpc *urpc; + int ret = -1; + + uwsgi_lock(uwsgi.rpc_table_lock); + + if (uwsgi.shared->rpc_count < MAX_RPC) { + urpc = &uwsgi.shared->rpc_table[uwsgi.shared->rpc_count]; + + memcpy(urpc->name, name, strlen(name)); + urpc->modifier1 = modifier1; + urpc->args = args; + urpc->func = func; + + uwsgi.shared->rpc_count++; + + ret = 0; + } + + uwsgi_unlock(uwsgi.rpc_table_lock); + + return ret; +} + +uint16_t uwsgi_rpc(char *name, uint8_t argc, char *argv[], char *output) { + + struct uwsgi_rpc *urpc = NULL; + int i; + uint16_t ret = 0; + + for(i=0;irpc_count;i++) { + if (uwsgi.shared->rpc_table[i].name[0] != 0) { + if (!strcmp(uwsgi.shared->rpc_table[i].name, name)) { + urpc = &uwsgi.shared->rpc_table[i]; + break; + } + } + } + + if (urpc) { + if (uwsgi.p[urpc->modifier1]->rpc) { + ret = uwsgi.p[urpc->modifier1]->rpc(urpc->func, argc, argv, output); + } + } + + return ret; +} diff --git a/tests/rpc.py b/tests/rpc.py new file mode 100644 index 00000000..5d0f30f7 --- /dev/null +++ b/tests/rpc.py @@ -0,0 +1,7 @@ +import uwsgi + + +def hello(): + return "Hello World" + +print uwsgi.register_rpc("hello", hello) diff --git a/uwsgi.c b/uwsgi.c index 4c186654..ea8242b2 100644 --- a/uwsgi.c +++ b/uwsgi.c @@ -946,6 +946,9 @@ int uwsgi_start(void *v_argv) { uwsgi_lock_init(uwsgi.timer_table_lock); } + uwsgi.rpc_table_lock = uwsgi_mmap_shared_lock(); + uwsgi_lock_init(uwsgi.rpc_table_lock); + if (uwsgi.sharedareasize > 0) { uwsgi.sharedareamutex = uwsgi_mmap_shared_lock(); diff --git a/uwsgi.h b/uwsgi.h index 53f54f91..8341a53f 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -15,6 +15,7 @@ #define MAX_APPS 64 #define MAX_GENERIC_PLUGINS 64 +#define MAX_RPC 64 #ifndef UWSGI_LOAD_EMBEDDED_PLUGINS #define UWSGI_LOAD_EMBEDDED_PLUGINS @@ -399,6 +400,8 @@ struct uwsgi_plugin { char* (*decode_string)(void *); int (*signal_handler)(uint8_t, void *, char *, uint8_t); + uint16_t (*rpc)(void *, uint8_t argc, char **, char *); + }; @@ -872,6 +875,7 @@ struct uwsgi_server { void *signal_table_lock; void *fmon_table_lock; void *timer_table_lock; + void *rpc_table_lock; }; @@ -879,8 +883,8 @@ struct uwsgi_rpc { char name[0xff]; void *func; uint8_t args; - uint8_t modifier; -} + uint8_t modifier1; +}; struct uwsgi_lb_group { char name[101]; @@ -992,6 +996,9 @@ struct uwsgi_shared { struct uwsgi_timer timers[64]; int timers_cnt; + + struct uwsgi_rpc rpc_table[MAX_RPC]; + int rpc_count; }; struct uwsgi_core { @@ -1343,3 +1350,6 @@ int uwsgi_signal_handler(uint8_t); void uwsgi_route_signal(uint8_t); int uwsgi_start(void *); + +int uwsgi_register_rpc(char *, uint8_t, uint8_t, void *); +uint16_t uwsgi_rpc(char *, uint8_t, char **, char *); diff --git a/uwsgiconfig.py b/uwsgiconfig.py index 108a53ad..15fcc4c6 100644 --- a/uwsgiconfig.py +++ b/uwsgiconfig.py @@ -139,7 +139,7 @@ class uConf(object): self.config = ConfigParser.ConfigParser() print("using profile: %s" % filename) self.config.read(filename) - self.gcc_list = ['utils', 'protocol', 'socket', 'logging', 'master', 'plugins', 'lock', 'cache', 'event', 'signal', 'loop', 'uwsgi'] + self.gcc_list = ['utils', 'protocol', 'socket', 'logging', 'master', 'plugins', 'lock', 'cache', 'event', 'signal', 'rpc', 'loop', 'uwsgi'] self.cflags = ['-O2', '-Wall', '-Werror', '-D_LARGEFILE_SOURCE', '-D_FILE_OFFSET_BITS=64'] + os.environ.get("CFLAGS", "").split() try: gcc_version = str(spcall2("%s -v" % GCC)).split('\n')[-1].split()[2]