home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Modules / palm_selectmodule.c < prev    next >
C/C++ Source or Header  |  2000-12-27  |  11KB  |  409 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* select - Module containing unix select(2) call.
  33.    Under Unix, the file descriptors are small integers.
  34.    Under Win32, select only exists for sockets, and sockets may
  35.    have any value except INVALID_SOCKET.
  36.    Under BeOS, we suffer the same dichotomy as Win32; sockets can be anything
  37.    >= 0.
  38. */
  39. #include "kludge.h"
  40.  
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44.  
  45. #ifndef DONT_HAVE_SYS_TYPES_H
  46. #include <sys/types.h>
  47. #endif
  48.  
  49. #ifdef MS_WINDOWS
  50. #include <winsock.h>
  51. #else
  52. #ifdef __BEOS__
  53. #include <net/socket.h>
  54. #define SOCKET int
  55. #else
  56. #include "myselect.h" /* Also includes mytime.h */
  57. #define SOCKET int
  58. #endif
  59. #endif
  60.  
  61. static PyObject *SelectError;
  62.  
  63. /* list of Python objects and their file descriptor */
  64. typedef struct {
  65.     PyObject *obj;                 /* owned reference */
  66.     SOCKET fd;
  67.     int sentinel;                 /* -1 == sentinel */
  68. } pylist;
  69.  
  70.  
  71. DL_EXPORT(void) initselect() SEG_SELECTMODULE_C;
  72. static PyObject *select_select(PyObject *self, PyObject *args) SEG_SELECTMODULE_C;
  73. static PyObject *set2list(fd_set *set, pylist fd2obj[FD_SETSIZE + 3]) SEG_SELECTMODULE_C;
  74. static int list2set(PyObject *list, fd_set *set, pylist fd2obj[FD_SETSIZE + 3]) SEG_SELECTMODULE_C;
  75. static void reap_obj( pylist fd2obj[FD_SETSIZE + 3] ) SEG_SELECTMODULE_C;
  76.  
  77. static void
  78. reap_obj(fd2obj)
  79.     pylist fd2obj[FD_SETSIZE + 3];
  80. {
  81.     int i;
  82.     for (i = 0; i < FD_SETSIZE + 3 && fd2obj[i].sentinel >= 0; i++) {
  83.         Py_XDECREF(fd2obj[i].obj);
  84.         fd2obj[i].obj = NULL;
  85.     }
  86.     fd2obj[0].sentinel = -1;
  87. }
  88.  
  89.  
  90. /* returns -1 and sets the Python exception if an error occurred, otherwise
  91.    returns a number >= 0
  92. */
  93. static int
  94. list2set(list, set, fd2obj)
  95.     PyObject *list;
  96.     fd_set *set;
  97.     pylist fd2obj[FD_SETSIZE + 3];
  98. {
  99.     int i;
  100.     int max = -1;
  101.     int index = 0;
  102.     int len = PyList_Size(list);
  103.     PyObject* o = NULL;
  104.  
  105.     fd2obj[0].obj = (PyObject*)0;         /* set list to zero size */
  106.     FD_ZERO(set);
  107.  
  108.     for (i = 0; i < len; i++)  {
  109.         PyObject *meth;
  110.         SOCKET v;
  111.  
  112.         /* any intervening fileno() calls could decr this refcnt */
  113.         if (!(o = PyList_GetItem(list, i)))
  114.                     return -1;
  115.  
  116.         Py_INCREF(o);
  117.  
  118.         if (PyInt_Check(o)) {
  119.             v = PyInt_AsLong(o);
  120.         }
  121.         else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
  122.         {
  123.             PyObject *fno = PyEval_CallObject(meth, NULL);
  124.             Py_DECREF(meth);
  125.             if (fno == NULL)
  126.                 goto finally;
  127.  
  128.                         if (!PyInt_Check(fno)) {
  129.                 PyErr_SetString(PyExc_TypeError,
  130.                                        "fileno method returned a non-integer");
  131.                 Py_DECREF(fno);
  132.                 goto finally;
  133.                         }
  134.                         v = PyInt_AsLong(fno);
  135.             Py_DECREF(fno);
  136.         }
  137.         else {
  138.             PyErr_SetString(PyExc_TypeError,
  139.             "argument must be an int, or have a fileno() method.");
  140.             goto finally;
  141.         }
  142. #if defined(_MSC_VER)
  143.         max = 0;             /* not used for Win32 */
  144. #else  /* !_MSC_VER */
  145.         if (v < 0 || v >= FD_SETSIZE) {
  146.             PyErr_SetString(PyExc_ValueError,
  147.                     "filedescriptor out of range in select()");
  148.             goto finally;
  149.         }
  150.         if (v > max)
  151.             max = v;
  152. #endif /* _MSC_VER */
  153.         FD_SET(v, set);
  154.  
  155.         /* add object and its file descriptor to the list */
  156.         if (index >= FD_SETSIZE) {
  157.             PyErr_SetString(PyExc_ValueError,
  158.                       "too many file descriptors in select()");
  159.             goto finally;
  160.         }
  161.         fd2obj[index].obj = o;
  162.         fd2obj[index].fd = v;
  163.         fd2obj[index].sentinel = 0;
  164.         fd2obj[++index].sentinel = -1;
  165.     }
  166.     return max+1;
  167.  
  168.   finally:
  169.     Py_XDECREF(o);
  170.     return -1;
  171. }
  172.  
  173. /* returns NULL and sets the Python exception if an error occurred */
  174. static PyObject *
  175. set2list(set, fd2obj)
  176.     fd_set *set;
  177.     pylist fd2obj[FD_SETSIZE + 3];
  178. {
  179.     int i, j, count=0;
  180.     PyObject *list, *o;
  181.     SOCKET fd;
  182.  
  183.     for (j = 0; fd2obj[j].sentinel >= 0; j++) {
  184.         if (FD_ISSET(fd2obj[j].fd, set))
  185.             count++;
  186.     }
  187.     list = PyList_New(count);
  188.     if (!list)
  189.         return NULL;
  190.  
  191.     i = 0;
  192.     for (j = 0; fd2obj[j].sentinel >= 0; j++) {
  193.         fd = fd2obj[j].fd;
  194.         if (FD_ISSET(fd, set)) {
  195. #ifndef _MSC_VER
  196.             if (fd > FD_SETSIZE) {
  197.                 PyErr_SetString(PyExc_SystemError,
  198.                "filedescriptor out of range returned in select()");
  199.                 goto finally;
  200.             }
  201. #endif
  202.             o = fd2obj[j].obj;
  203.             fd2obj[j].obj = NULL;
  204.             /* transfer ownership */
  205.             if (PyList_SetItem(list, i, o) < 0)
  206.                 goto finally;
  207.  
  208.             i++;
  209.         }
  210.     }
  211.     return list;
  212.   finally:
  213.     Py_DECREF(list);
  214.     return NULL;
  215. }
  216.  
  217.     
  218. static PyObject *
  219. select_select(self, args)
  220.     PyObject *self;
  221.     PyObject *args;
  222. {
  223. #if defined(MS_WINDOWS) || defined(PALMOS)
  224.     /* This would be an awful lot of stack space on Windows! */
  225.     pylist *rfd2obj, *wfd2obj, *efd2obj;
  226. #else
  227.     pylist rfd2obj[FD_SETSIZE + 3];
  228.     pylist wfd2obj[FD_SETSIZE + 3];
  229.     pylist efd2obj[FD_SETSIZE + 3];
  230. #endif
  231.     PyObject *ifdlist, *ofdlist, *efdlist;
  232.     PyObject *ret = NULL;
  233.     PyObject *tout = Py_None;
  234.     fd_set ifdset, ofdset, efdset;
  235. #ifndef PALMOS
  236.     double timeout;
  237.     int seconds;
  238. #endif /* PALMOS */
  239.     struct timeval tv, *tvp;
  240.     int imax, omax, emax, max;
  241.     int n;
  242.  
  243.     /* convert arguments */
  244.     if (!PyArg_ParseTuple(args, "OOO|O:select",
  245.                   &ifdlist, &ofdlist, &efdlist, &tout))
  246.         return NULL;
  247.  
  248.     if (tout == Py_None)
  249.         tvp = (struct timeval *)0;
  250. #ifdef PALMOS
  251.     else if (!PyArg_ParseTuple(tout, "ll", &tv.tv_sec, &tv.tv_usec)) {
  252.         PyErr_SetString(PyExc_TypeError,
  253.                 "timeout must be a tuple (sec, usec) or None");
  254.         return NULL;
  255.     }
  256.     else {
  257.         tvp = &tv;
  258.     }
  259. #else /* !PALMOS */
  260.     else if (!PyArg_Parse(tout, "d", &timeout)) {
  261.         PyErr_SetString(PyExc_TypeError,
  262.                 "timeout must be a float or None");
  263.         return NULL;
  264.     }
  265.     else {
  266.         seconds = (int)timeout;
  267.         timeout = timeout - (double)seconds;
  268.         tv.tv_sec = seconds;
  269.         tv.tv_usec = (int)(timeout*1000000.0);
  270.         tvp = &tv;
  271.     }
  272. #endif /* !PALMOS */
  273.  
  274.     /* sanity check first three arguments */
  275.     if (!PyList_Check(ifdlist) ||
  276.         !PyList_Check(ofdlist) ||
  277.         !PyList_Check(efdlist))
  278.     {
  279.         PyErr_SetString(PyExc_TypeError,
  280.                 "arguments 1-3 must be lists");
  281.         return NULL;
  282.     }
  283.  
  284. #if defined(MS_WINDOWS) || defined(PALMOS)
  285.     /* Allocate memory for the lists */
  286.     rfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  287.     wfd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  288.     efd2obj = PyMem_NEW(pylist, FD_SETSIZE + 3);
  289.     if (rfd2obj == NULL || wfd2obj == NULL || efd2obj == NULL) {
  290.         PyMem_XDEL(rfd2obj);
  291.         PyMem_XDEL(wfd2obj);
  292.         PyMem_XDEL(efd2obj);
  293.         return NULL;
  294.     }
  295. #endif
  296.     /* Convert lists to fd_sets, and get maximum fd number
  297.      * propagates the Python exception set in list2set()
  298.      */
  299.     rfd2obj[0].sentinel = -1;
  300.     wfd2obj[0].sentinel = -1;
  301.     efd2obj[0].sentinel = -1;
  302.     if ((imax=list2set(ifdlist, &ifdset, rfd2obj)) < 0) 
  303.         goto finally;
  304.     if ((omax=list2set(ofdlist, &ofdset, wfd2obj)) < 0) 
  305.         goto finally;
  306.     if ((emax=list2set(efdlist, &efdset, efd2obj)) < 0) 
  307.         goto finally;
  308.     max = imax;
  309.     if (omax > max) max = omax;
  310.     if (emax > max) max = emax;
  311.  
  312. /*     printf("before: max, ifdset, ofdset, efdset = %d %ld %ld %ld\n",  */
  313. /*            max, ifdset, ofdset, efdset); */
  314. /*     printf("timeout = %ld %ld\n", tvp->tv_sec, tvp->tv_usec); */
  315.  
  316.     Py_BEGIN_ALLOW_THREADS
  317.     n = select(max, &ifdset, &ofdset, &efdset, tvp);
  318.     Py_END_ALLOW_THREADS
  319.  
  320. /*     printf("after: max, ifdset, ofdset, efdset, n = %d %ld %ld %ld %d\n",  */
  321. /*            max, ifdset, ofdset, efdset, n); */
  322.  
  323.  
  324.     if (n < 0) {
  325.         PyErr_SetFromErrno(SelectError);
  326.     }
  327.     else if (n == 0) {
  328.                 /* optimization */
  329.         ifdlist = PyList_New(0);
  330.         if (ifdlist) {
  331.             ret = Py_BuildValue("OOO", ifdlist, ifdlist, ifdlist);
  332.             Py_DECREF(ifdlist);
  333.         }
  334.     }
  335.     else {
  336.         /* any of these three calls can raise an exception.  it's more
  337.            convenient to test for this after all three calls... but
  338.            is that acceptable?
  339.         */
  340.         ifdlist = set2list(&ifdset, rfd2obj);
  341.         ofdlist = set2list(&ofdset, wfd2obj);
  342.         efdlist = set2list(&efdset, efd2obj);
  343.         if (PyErr_Occurred())
  344.             ret = NULL;
  345.         else
  346.             ret = Py_BuildValue("OOO", ifdlist, ofdlist, efdlist);
  347.  
  348.         Py_DECREF(ifdlist);
  349.         Py_DECREF(ofdlist);
  350.         Py_DECREF(efdlist);
  351.     }
  352.     
  353.   finally:
  354.     reap_obj(rfd2obj);
  355.     reap_obj(wfd2obj);
  356.     reap_obj(efd2obj);
  357. #if defined(MS_WINDOWS) || defined(PALMOS)
  358.     PyMem_DEL(rfd2obj);
  359.     PyMem_DEL(wfd2obj);
  360.     PyMem_DEL(efd2obj);
  361. #endif
  362.     return ret;
  363. }
  364.  
  365. DEF_DOC(select_doc, 
  366. "select(rlist, wlist, xlist[, timeout]) -> (rlist, wlist, xlist)\n\
  367. \n\
  368. Wait until one or more file descriptors are ready for some kind of I/O.\n\
  369. The first three arguments are lists of file descriptors to be waited for:\n\
  370. rlist -- wait until ready for reading\n\
  371. wlist -- wait until ready for writing\n\
  372. xlist -- wait for an ``exceptional condition''\n\
  373. If only one kind of condition is required, pass [] for the other lists.\n\
  374. A file descriptor is either a socket or file object, or a small integer\n\
  375. gotten from a fileno() method call on one of those.\n\
  376. \n\
  377. The optional 4th argument specifies a timeout in seconds; it may be\n\
  378. a floating point number to specify fractions of seconds.  If it is absent\n\
  379. or None, the call will never time out.\n\
  380. \n\
  381. The return value is a tuple of three lists corresponding to the first three\n\
  382. arguments; each contains the subset of the corresponding file descriptors\n\
  383. that are ready.\n\
  384. \n\
  385. *** IMPORTANT NOTICE ***\n\
  386. On Windows, only sockets are supported; on Unix, all file descriptors.");
  387.  
  388.  
  389. static PyMethodDef select_methods[] = {
  390.     {"select",    select_select, 1, USE_DOC(select_doc)},
  391.     {0,      0},                 /* sentinel */
  392. };
  393.  
  394. DEF_DOC(module_doc, 
  395. "This module supports asynchronous I/O on multiple file descriptors.\n\
  396. \n\
  397. *** IMPORTANT NOTICE ***\n\
  398. On Windows, only sockets are supported; on Unix, all file descriptors.");
  399.  
  400. DL_EXPORT(void)
  401. initselect()
  402. {
  403.     PyObject *m, *d;
  404.     m = Py_InitModule3("select", select_methods, USE_DOC(module_doc));
  405.     d = PyModule_GetDict(m);
  406.     SelectError = PyErr_NewException("select.error", NULL, NULL);
  407.     PyDict_SetItemString(d, "error", SelectError);
  408. }
  409.