From f11b8b2a0d681a8ba5f278be6a74a5a644730f6b Mon Sep 17 00:00:00 2001 From: Nevo Hed Date: Wed, 6 May 2015 13:12:43 -0400 Subject: [PATCH] Init accepts multicastif, minissdpdsocket, discoverdelay + Created initialize for the UPnP class. Initializer accepts named arguments to override defaults. + multicastif & minissdpdsocket ars strduped and thus freed in the dealloc (tho could/should they be native py objects instead?) + The pymoduletest.py test has been modified to support these but I only tested multicastif & discoverdelay. --- miniupnpc/miniupnpcmodule.c | 31 ++++++++++++++++++++++++++++--- miniupnpc/pymoduletest.py | 10 ++++++++-- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/miniupnpc/miniupnpcmodule.c b/miniupnpc/miniupnpcmodule.c index 16c737b..5f46d5f 100644 --- a/miniupnpc/miniupnpcmodule.c +++ b/miniupnpc/miniupnpcmodule.c @@ -64,11 +64,36 @@ static PyMemberDef UPnP_members[] = { {NULL} }; + +static int UPnP_init(UPnPObject *self, PyObject *args, PyObject *kwds) +{ + char* multicastif = NULL; + char* minissdpdsocket = NULL; + static char *kwlist[] = { + "multicastif", "minissdpdsocket", "discoverdelay", NULL + }; + + if(!PyArg_ParseTupleAndKeywords(args, kwds, "|zzI", kwlist, + &multicastif, + &minissdpdsocket, + &self->discoverdelay)) + return -1; + + if(multicastif) + self->multicastif = strdup(multicastif); + if(minissdpdsocket) + self->minissdpdsocket = strdup(minissdpdsocket); + + return 0; +} + static void UPnPObject_dealloc(UPnPObject *self) { freeUPNPDevlist(self->devlist); FreeUPNPUrls(&self->urls); + free(self->multicastif); + free(self->minissdpdsocket); Py_TYPE(self)->tp_free((PyObject*)self); } @@ -85,8 +110,8 @@ UPnP_discover(UPnPObject *self) } Py_BEGIN_ALLOW_THREADS self->devlist = upnpDiscover((int)self->discoverdelay/*timeout in ms*/, - 0/* multicast if*/, - 0/*minissdpd socket*/, + self->multicastif, + self->minissdpdsocket, 0/*sameport flag*/, 0/*ip v6*/, 0/*error */); @@ -554,7 +579,7 @@ static PyTypeObject UPnPType = { 0, /* tp_descr_get */ 0, /* tp_descr_set */ 0, /* tp_dictoffset */ - 0,/*(initproc)UPnP_init,*/ /* tp_init */ + (initproc)UPnP_init, /* tp_init */ 0, /* tp_alloc */ #ifndef _WIN32 PyType_GenericNew,/*UPnP_new,*/ /* tp_new */ diff --git a/miniupnpc/pymoduletest.py b/miniupnpc/pymoduletest.py index d35a3b0..6387364 100755 --- a/miniupnpc/pymoduletest.py +++ b/miniupnpc/pymoduletest.py @@ -7,15 +7,21 @@ # import the python miniupnpc module import miniupnpc import sys +import argparse + + +parser = argparse.ArgumentParser() +parser.add_argument('-m', '--multicastif') +parser.add_argument('-p', '--minissdpdsocket') +parser.add_argument('-d', '--discoverdelay', type=int, default=200) # create the object -u = miniupnpc.UPnP() +u = miniupnpc.UPnP(**vars(parser.parse_args())) print 'inital(default) values :' print ' discoverdelay', u.discoverdelay print ' lanaddr', u.lanaddr print ' multicastif', u.multicastif print ' minissdpdsocket', u.minissdpdsocket -u.discoverdelay = 200; #u.minissdpdsocket = '../minissdpd/minissdpd.sock' # discovery process, it usualy takes several seconds (2 seconds or more) print 'Discovering... delay=%ums' % u.discoverdelay