Skip to content

Commit

Permalink
added uwsgi.erlang_register_process
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto@natty32 committed Mar 4, 2011
1 parent 5f207e6 commit c478b0a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
21 changes: 20 additions & 1 deletion plugins/erlang/erlang.c
Expand Up @@ -247,6 +247,7 @@ void erlang_loop() {
int fd;

int eversion;
int i;

ei_x_buff x, xr;

Expand Down Expand Up @@ -291,13 +292,31 @@ void erlang_loop() {

x.index = 0;
ei_decode_version(x.buff, &x.index, &eversion);
#ifdef UWSGI_DEBUG
uwsgi_log("eversion: %d\n", eversion);
#endif

if (!strcmp(em.toname, "rex")) {
uwsgi_erlang_rpc(fd, &em.from, &x);
}
else {
dump_eterm(&x);
int uep = -1;
for(i=0;i<uerl.uep_cnt;i++) {
if (!strcmp(uerl.uep[i].name, em.toname)) {
uep = i;
break;
}
}

if (uep > -1) {
if (uerl.uep[uep].plugin) {
uerl.uep[uep].plugin( uerl.uep[uep].func, &x );
}
}
else {
uwsgi_log("!!! unregistered erlang process requested, dumping it !!!\n");
dump_eterm(&x);
}
}


Expand Down
14 changes: 13 additions & 1 deletion plugins/erlang/erlang.h
@@ -1,9 +1,19 @@
#include <ei.h>

#define MAX_UWSGI_ERLANG_PROCESSES 64

#define LONG_ARGS_ERLANG 17012
#define LONG_ARGS_ERLANG_COOKIE 17013



struct uwsgi_erlang_process {

char name[0xff];
void (*plugin)(void *, ei_x_buff *);
void *func;
};

struct uwsgi_erlang {

ei_cnode cnode;
Expand All @@ -13,6 +23,8 @@ struct uwsgi_erlang {
int fd;

void *lock;
};

struct uwsgi_erlang_process uep[MAX_UWSGI_ERLANG_PROCESSES];
int uep_cnt;
};

37 changes: 37 additions & 0 deletions plugins/pyerl/pyerl.c
Expand Up @@ -298,6 +298,42 @@ PyObject *pyerl_sr(PyObject * self, PyObject * args) {
return Py_None;
}

void pyerl_call_registered(void *func, ei_x_buff *x) {

PyObject *pyargs = PyTuple_New(1);
PyObject *ret;

PyTuple_SetItem(pyargs, 0, erl_to_py(x));

ret = python_call((PyObject *) func, pyargs, 0);
}

PyObject *pyerl_register_process(PyObject * self, PyObject * args) {

char *name;
PyObject *callable;

if (!PyArg_ParseTuple(args, "sO:erlang_register_process", &name, &callable)) {
return NULL;
}

if (uerl.uep_cnt >= MAX_UWSGI_ERLANG_PROCESSES)
return PyErr_Format(PyExc_ValueError, "You can define max %d erlang registered processes", MAX_UWSGI_ERLANG_PROCESSES);

if (strlen(name) > 0xff-1)
return PyErr_Format(PyExc_ValueError, "Invalid erlang process name");

strcpy(uerl.uep[uerl.uep_cnt].name, name);
uerl.uep[uerl.uep_cnt].plugin = pyerl_call_registered;
uerl.uep[uerl.uep_cnt].func = callable;

uerl.uep_cnt++;

Py_INCREF(Py_None);
return Py_None;

}

PyObject *pyerl_recv(PyObject * self, PyObject * args) {

ei_x_buff x;
Expand Down Expand Up @@ -407,6 +443,7 @@ static PyMethodDef uwsgi_pyerl_methods[] = {
{"erlang_rpc", pyerl_rpc, METH_VARARGS, ""},
{"erlang_lock", pyerl_lock, METH_VARARGS, ""},
{"erlang_unlock", pyerl_unlock, METH_VARARGS, ""},
{"erlang_register_process", pyerl_register_process, METH_VARARGS, ""},
{NULL, NULL},
};

Expand Down

0 comments on commit c478b0a

Please sign in to comment.