paramiko.pipe

Package paramiko :: Module pipe |
[frames] | no frames]

# Source Code for Module paramiko.pipe

      1  # Copyright (C) 2003-2007  Robey Pointer <robey@lag.net>
      2  #
      3  # This file is part of paramiko.
      4  #
      5  # Paramiko is free software; you can redistribute it and/or modify it under the
      6  # terms of the GNU Lesser General Public License as published by the Free
      7  # Software Foundation; either version 2.1 of the License, or (at your option)
      8  # any later version.
      9  #
     10  # Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY
     11  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     12  # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
     13  # details.
     14  #
     15  # You should have received a copy of the GNU Lesser General Public License
     16  # along with Paramiko; if not, write to the Free Software Foundation, Inc.,
     17  # 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
     18
     19  """
     20  Abstraction of a one-way pipe where the read end can be used in select().
     21  Normally this is trivial, but Windows makes it nearly impossible.
     22
     23  The pipe acts like an Event, which can be set or cleared. When set, the pipe
     24  will trigger as readable in select().
     25  """
     26
     27  import sys
     28  import os
     29  import socket
     30
     31
     32 -def make_pipe ():
     33      if sys.platform[:3] != 'win':
     34          p = PosixPipe()
     35      else:
     36          p = WindowsPipe()
     37      return p
     38
     39
     40 -class PosixPipe (object):
     41 -    def __init__ (self):
     42          self._rfd, self._wfd = os.pipe()
     43          self._set = False
     44          self._forever = False
     45          self._closed = False
     46
     47 -    def close (self):
     48          os.close(self._rfd)
     49          os.close(self._wfd)
     50          # used for unit tests:
     51          self._closed = True
     52
     53 -    def fileno (self):
     54          return self._rfd
     55
     56 -    def clear (self):
     57          if not self._set or self._forever:
     58              return
     59          os.read(self._rfd, 1)
     60          self._set = False
     61
     62 -    def set (self):
     63          if self._set or self._closed:
     64              return
     65          self._set = True
     66          os.write(self._wfd, '*')
     67
     68 -    def set_forever (self):
     69          self._forever = True
     70          self.set()
     71
     72
     73 -class WindowsPipe (object):
     74      """
     75      On Windows, only an OS-level "WinSock" may be used in select(), but reads
     76      and writes must be to the actual socket object.
     77      """
     78 -    def __init__ (self):
     79          serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     80          serv.bind(('127.0.0.1', 0))
     81          serv.listen(1)
     82
     83          # need to save sockets in _rsock/_wsock so they don't get closed
     84          self._rsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     85          self._rsock.connect(('127.0.0.1', serv.getsockname()[1]))
     86
     87          self._wsock, addr = serv.accept()
     88          serv.close()
     89          self._set = False
     90          self._forever = False
     91          self._closed = False
     92
     93 -    def close (self):
     94          self._rsock.close()
     95          self._wsock.close()
     96          # used for unit tests:
     97          self._closed = True
     98
     99 -    def fileno (self):
    100          return self._rsock.fileno()
    101
    102 -    def clear (self):
    103          if not self._set or self._forever:
    104              return
    105          self._rsock.recv(1)
    106          self._set = False
    107
    108 -    def set (self):
    109          if self._set or self._closed:
    110              return
    111          self._set = True
    112          self._wsock.send('*')
    113
    114 -    def set_forever (self):
    115          self._forever = True
    116          self.set()
    117
    118
    119 -class OrPipe (object):
    120 -    def __init__(self, pipe):
    121          self._set = False
    122          self._partner = None
    123          self._pipe = pipe
    124
    125 -    def set(self):
    126          self._set = True
    127          if not self._partner._set:
    128              self._pipe.set()
    129
    130 -    def clear(self):
    131          self._set = False
    132          if not self._partner._set:
    133              self._pipe.clear()
    134
    135
    136 -def make_or_pipe(pipe):
    137      """
    138      wraps a pipe into two pipe-like objects which are "or"d together to
    139      affect the real pipe. if either returned pipe is set, the wrapped pipe
    140      is set. when both are cleared, the wrapped pipe is cleared.
    141      """
    142      p1 = OrPipe(pipe)
    143      p2 = OrPipe(pipe)
    144      p1._partner = p2
    145      p2._partner = p1
    146      return p1, p2
    147