author | Alberto Bertogli
<albertito@gmail.com> 2007-06-01 15:54:46 UTC |
committer | Alberto Bertogli
<albertito@gmail.com> 2007-06-01 15:54:46 UTC |
parent | 33377d037dde7814e36ce1995a66137e3ea2a254 |
bindings/python/nmdb.py | +17 | -11 |
bindings/python/nmdb_ll.c | +31 | -12 |
diff --git a/bindings/python/nmdb.py b/bindings/python/nmdb.py index 1363a54..13a1dcd 100644 --- a/bindings/python/nmdb.py +++ b/bindings/python/nmdb.py @@ -24,8 +24,7 @@ Here is an example using the DB class: >>> import nmdb >>> db = nmdb.DB() ->>> import nmdb ->>> db = nmdb.DB() +>>> db.add_tipc_server() >>> db[1] = { 'english': 'one', 'castellano': 'uno', 'quechua': 'huk' } >>> print db[1] {'english': 'one', 'castellano': 'uno', 'quechua': 'huk'} @@ -58,9 +57,16 @@ class _nmdbDict (object): self._cas = op_cas self.autopickle = True - def add_server(self, port): - "Adds a server to the server pool." - rv = self._db.add_server(port) + def add_tipc_server(self, port = -1): + "Adds a TIPC server to the server pool." + rv = self._db.add_tipc_server(port) + if not rv: + raise NetworkError + return rv + + def add_tcp_server(self, addr, port = -1): + "Adds a TCP server to the server pool." + rv = self._db.add_tcp_server(addr, port) if not rv: raise NetworkError return rv @@ -137,19 +143,19 @@ class _nmdbDict (object): class Cache (_nmdbDict): - def __init__(self, port = -1): - db = nmdb_ll.connect(port) + def __init__(self): + db = nmdb_ll.connect() _nmdbDict.__init__(self, db, db.cache_get, db.cache_set, db.cache_delete, db.cache_cas) class DB (_nmdbDict): - def __init__(self, port = -1): - db = nmdb_ll.connect(port) + def __init__(self): + db = nmdb_ll.connect() _nmdbDict.__init__(self, db, db.get, db.set, db.delete, db.cas) class SyncDB (_nmdbDict): - def __init__(self, port = -1): - db = nmdb_ll.connect(port) + def __init__(self): + db = nmdb_ll.connect() _nmdbDict.__init__(self, db, db.get, db.set_sync, db.delete_sync, db.cas) diff --git a/bindings/python/nmdb_ll.c b/bindings/python/nmdb_ll.c index 387ea87..b81785c 100644 --- a/bindings/python/nmdb_ll.c +++ b/bindings/python/nmdb_ll.c @@ -35,18 +35,36 @@ static void db_dealloc(nmdbobject *db) } -/* add server */ -static PyObject *db_add_server(nmdbobject *db, PyObject *args) +/* add tipc server */ +static PyObject *db_add_tipc_server(nmdbobject *db, PyObject *args) { int port; int rv; - if (!PyArg_ParseTuple(args, "i:add_server", &port)) { + if (!PyArg_ParseTuple(args, "i:add_tipc_server", &port)) { return NULL; } Py_BEGIN_ALLOW_THREADS - rv = nmdb_add_server(db->db, port); + rv = nmdb_add_tipc_server(db->db, port); + Py_END_ALLOW_THREADS + + return PyLong_FromLong(rv); +} + +/* add tcp server */ +static PyObject *db_add_tcp_server(nmdbobject *db, PyObject *args) +{ + int port; + char *addr; + int rv; + + if (!PyArg_ParseTuple(args, "si:add_tcp_server", &addr, &port)) { + return NULL; + } + + Py_BEGIN_ALLOW_THREADS + rv = nmdb_add_tcp_server(db->db, addr, port); Py_END_ALLOW_THREADS return PyLong_FromLong(rv); @@ -281,7 +299,10 @@ static PyObject *db_delete_sync(nmdbobject *db, PyObject *args) /* nmdb method table */ static PyMethodDef nmdb_methods[] = { - { "add_server", (PyCFunction) db_add_server, METH_VARARGS, NULL }, + { "add_tipc_server", (PyCFunction) db_add_tipc_server, + METH_VARARGS, NULL }, + { "add_tcp_server", (PyCFunction) db_add_tcp_server, + METH_VARARGS, NULL }, { "cache_set", (PyCFunction) db_cache_set, METH_VARARGS, NULL }, { "cache_get", (PyCFunction) db_cache_get, METH_VARARGS, NULL }, { "cache_delete", (PyCFunction) db_cache_delete, METH_VARARGS, NULL }, @@ -321,18 +342,16 @@ static PyTypeObject nmdbType = { static PyObject *db_connect(PyObject *self, PyObject *args) { nmdbobject *db; - long port; - - if (!PyArg_ParseTuple(args, "i:connect", &port)) { - return NULL; - } - db = PyObject_New(nmdbobject, &nmdbType); if (db == NULL) return NULL; - db->db = nmdb_init(port); + if (!PyArg_ParseTuple(args, ":connect")) { + return NULL; + } + + db->db = nmdb_init(); if (db->db == NULL) { return PyErr_NoMemory(); }