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.
This commit is contained in:
Nevo Hed 2015-05-06 13:12:43 -04:00
parent 82969aec1d
commit f11b8b2a0d
2 changed files with 36 additions and 5 deletions

View File

@ -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 */

View File

@ -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