home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / hplip / base / async_qt.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  7.8 KB  |  275 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''
  5. Basic infrastructure for asynchronous socket service clients and servers.
  6.  
  7. There are only two ways to have a program on a single processor do "more
  8. than one thing at a time".  Multi-threaded programming is the simplest and
  9. most popular way to do it, but there is another very different technique,
  10. that lets you have nearly all the advantages of multi-threading, without
  11. actually using multiple threads. it\'s really only practical if your program
  12. is largely I/O bound. If your program is CPU bound, then pre-emptive
  13. scheduled threads are probably what you really need. Network servers are
  14. rarely CPU-bound, however.
  15.  
  16. If your operating system supports the select() system call in its I/O
  17. library (and nearly all do), then you can use it to juggle multiple
  18. communication channels at once; doing other work while your I/O is taking
  19. place in the "background."  Although this strategy can seem strange and
  20. complex, especially at first, it is in many ways easier to understand and
  21. control than multi-threaded programming. The module documented here solves
  22. many of the difficult problems for you, making the task of building
  23. sophisticated high-performance network servers and clients a snap.
  24.  
  25. NOTICE: This copy of asyncore has been modified from the Python Std Lib version.
  26.  
  27. '''
  28. import select
  29. import socket
  30. import sys
  31. import time
  32. import os
  33. from qt import *
  34. from g import *
  35. from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EAGAIN
  36.  
  37. class ExitNow(Exception):
  38.     pass
  39.  
  40. channels = { }
  41.  
  42. class dispatcher(QObject):
  43.     connected = False
  44.     accepting = False
  45.     closing = False
  46.     addr = None
  47.     
  48.     def __init__(self, sock = None):
  49.         self.sock_write_notifier = None
  50.         self.sock_read_notifier = None
  51.         if sock:
  52.             self.set_socket(sock)
  53.             self.socket.setblocking(0)
  54.             self.connected = True
  55.             
  56.             try:
  57.                 self.addr = sock.getpeername()
  58.             except socket.error:
  59.                 pass
  60.             except:
  61.                 None<EXCEPTION MATCH>socket.error
  62.             
  63.  
  64.         None<EXCEPTION MATCH>socket.error
  65.         self.socket = None
  66.  
  67.     
  68.     def add_channel(self):
  69.         channels[self._fileno] = self
  70.         self.sock_read_notifier = QSocketNotifier(self._fileno, QSocketNotifier.Read)
  71.         QObject.connect(self.sock_read_notifier, SIGNAL('activated(int)'), self.handle_read_event)
  72.         self.sock_read_notifier.setEnabled(True)
  73.         self.sock_write_notifier = QSocketNotifier(self._fileno, QSocketNotifier.Write)
  74.         QObject.connect(self.sock_write_notifier, SIGNAL('activated(int)'), self.handle_write_event)
  75.         self.sock_write_notifier.setEnabled(False)
  76.  
  77.     
  78.     def del_channel(self):
  79.         QObject.disconnect(self.sock_read_notifier, SIGNAL('activated(int)'), self.handle_read_event)
  80.         QObject.disconnect(self.sock_write_notifier, SIGNAL('activated(int)'), self.handle_write_event)
  81.         self.sock_write_notifier.setEnabled(False)
  82.         self.sock_read_notifier.setEnabled(False)
  83.         
  84.         try:
  85.             del channels[self._fileno]
  86.         except KeyError:
  87.             pass
  88.  
  89.         self._fileno = 0
  90.  
  91.     
  92.     def create_socket(self, family, type):
  93.         self.family_and_type = (family, type)
  94.         self.socket = socket.socket(family, type)
  95.         self.socket.setblocking(0)
  96.         self._fileno = self.socket.fileno()
  97.         self.add_channel()
  98.  
  99.     
  100.     def set_socket(self, sock):
  101.         self.socket = sock
  102.         self._fileno = sock.fileno()
  103.         self.add_channel()
  104.  
  105.     
  106.     def set_reuse_addr(self):
  107.         
  108.         try:
  109.             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR) | 1)
  110.         except socket.error:
  111.             pass
  112.  
  113.  
  114.     
  115.     def listen(self, num):
  116.         self.accepting = True
  117.         return self.socket.listen(num)
  118.  
  119.     
  120.     def bind(self, addr):
  121.         self.addr = addr
  122.         return self.socket.bind(addr)
  123.  
  124.     
  125.     def connect(self, address):
  126.         self.connected = False
  127.         err = self.socket.connect_ex(address)
  128.         if err in (EINPROGRESS, EALREADY, EWOULDBLOCK):
  129.             (r, w, e) = select.select([], [
  130.                 self.socket.fileno()], [], 5.0)
  131.             err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  132.         
  133.         if err in (0, EISCONN):
  134.             self.addr = address
  135.             self.connected = True
  136.             self.handle_connect()
  137.         else:
  138.             raise socket.error, err
  139.  
  140.     
  141.     def accept(self):
  142.         
  143.         try:
  144.             (conn, addr) = self.socket.accept()
  145.             return (conn, addr)
  146.         except socket.error:
  147.             why = None
  148.             if why[0] == EWOULDBLOCK:
  149.                 pass
  150.             else:
  151.                 raise socket.error, why
  152.         except:
  153.             why[0] == EWOULDBLOCK
  154.  
  155.  
  156.     
  157.     def send(self, data):
  158.         
  159.         try:
  160.             result = self.socket.send(data)
  161.         except socket.error:
  162.             why = None
  163.             if why[0] == EWOULDBLOCK:
  164.                 return 0
  165.             elif why[0] == EAGAIN:
  166.                 self.sock_write_notifier.setEnabled(True)
  167.                 return 0
  168.             else:
  169.                 raise socket.error, why
  170.         except:
  171.             why[0] == EWOULDBLOCK
  172.  
  173.         self.sock_write_notifier.setEnabled(False)
  174.         return result
  175.  
  176.     
  177.     def recv(self, buffer_size):
  178.         
  179.         try:
  180.             data = self.socket.recv(buffer_size)
  181.             if not data:
  182.                 self.handle_close()
  183.                 return ''
  184.             else:
  185.                 return data
  186.         except socket.error:
  187.             why = None
  188.             if why[0] in [
  189.                 ECONNRESET,
  190.                 ENOTCONN,
  191.                 ESHUTDOWN]:
  192.                 self.handle_close()
  193.                 return ''
  194.             else:
  195.                 raise socket.error, why
  196.         except:
  197.             why[0] in [
  198.                 ECONNRESET,
  199.                 ENOTCONN,
  200.                 ESHUTDOWN]
  201.  
  202.  
  203.     
  204.     def close(self):
  205.         self.del_channel()
  206.         self.connected = False
  207.         self.socket.close()
  208.  
  209.     
  210.     def __getattr__(self, attr):
  211.         return getattr(self.socket, attr)
  212.  
  213.     
  214.     def handle_read_event(self):
  215.         if self.accepting:
  216.             if not self.connected:
  217.                 self.connected = True
  218.             
  219.             self.handle_accept()
  220.         elif not self.connected:
  221.             self.handle_connect()
  222.             self.connected = True
  223.             self.handle_read()
  224.         else:
  225.             self.handle_read()
  226.  
  227.     
  228.     def handle_write_event(self):
  229.         if not self.connected:
  230.             self.handle_connect()
  231.             self.connected = True
  232.         
  233.         self.handle_write()
  234.  
  235.     
  236.     def handle_expt_event(self):
  237.         self.handle_expt()
  238.  
  239.     
  240.     def handle_error(self):
  241.         self.handle_close()
  242.  
  243.     
  244.     def handle_expt(self):
  245.         raise Error
  246.  
  247.     
  248.     def handle_read(self):
  249.         raise Error
  250.  
  251.     
  252.     def handle_write(self):
  253.         raise Error
  254.  
  255.     
  256.     def handle_connect(self):
  257.         pass
  258.  
  259.     
  260.     def handle_accept(self):
  261.         raise Error
  262.  
  263.     
  264.     def handle_close(self):
  265.         self.close()
  266.  
  267.  
  268.  
  269. def close_all():
  270.     for x in channels.values():
  271.         x.channels.close()
  272.     
  273.     channels.clear()
  274.  
  275.