2009-07-10 15:18:12 +04:00
def __del__ ( self ) :
try :
if self . cb :
libvirtmod . virStreamEventRemoveCallback ( self . _o )
except AttributeError :
pass
2013-08-22 13:16:03 +04:00
if self . _o is not None :
2009-07-10 15:18:12 +04:00
libvirtmod . virStreamFree ( self . _o )
self . _o = None
2011-06-16 04:14:45 +04:00
def _dispatchStreamEventCallback ( self , events , cbData ) :
2011-06-14 21:49:22 +04:00
"""
Dispatches events to python user ' s stream event callbacks
"""
cb = cbData [ " cb " ]
opaque = cbData [ " opaque " ]
cb ( self , events , opaque )
return 0
def eventAddCallback ( self , events , cb , opaque ) :
self . cb = cb
cbData = { " stream " : self , " cb " : cb , " opaque " : opaque }
ret = libvirtmod . virStreamEventAddCallback ( self . _o , events , cbData )
if ret == - 1 : raise libvirtError ( ' virStreamEventAddCallback() failed ' )
2011-06-15 00:07:43 +04:00
2011-06-16 00:54:16 +04:00
def recvAll ( self , handler , opaque ) :
""" Receive the entire data stream, sending the data to the
requested data sink . This is simply a convenient alternative
2014-02-13 12:46:19 +04:00
to virStreamRecv , for apps that do blocking - I / O .
2011-06-16 00:54:16 +04:00
A hypothetical handler function looks like :
def handler ( stream , # virStream instance
buf , # string containing received data
opaque ) : # extra data passed to recvAll as opaque
fd = opaque
return os . write ( fd , buf )
"""
while True :
got = self . recv ( 1024 * 64 )
if got == - 2 :
raise libvirtError ( " cannot use recvAll with "
" nonblocking stream " )
if len ( got ) == 0 :
break
try :
ret = handler ( self , got , opaque )
if type ( ret ) is int and ret < 0 :
raise RuntimeError ( " recvAll handler returned %d " % ret )
2013-12-07 02:22:39 +04:00
except Exception :
e = sys . exc_info ( ) [ 1 ]
2011-06-16 00:54:16 +04:00
try :
self . abort ( )
except :
pass
raise e
def sendAll ( self , handler , opaque ) :
"""
Send the entire data stream , reading the data from the
requested data source . This is simply a convenient alternative
2014-02-13 12:46:19 +04:00
to virStreamSend , for apps that do blocking - I / O .
2011-06-16 00:54:16 +04:00
A hypothetical handler function looks like :
def handler ( stream , # virStream instance
nbytes , # int amt of data to read
opaque ) : # extra data passed to recvAll as opaque
fd = opaque
return os . read ( fd , nbytes )
"""
while True :
try :
got = handler ( self , 1024 * 64 , opaque )
except :
2014-01-11 07:11:04 +04:00
e = sys . exc_info ( ) [ 1 ]
2011-06-16 00:54:16 +04:00
try :
self . abort ( )
except :
pass
raise e
2017-05-05 17:39:34 +03:00
if not got :
2011-06-16 00:54:16 +04:00
break
ret = self . send ( got )
if ret == - 2 :
2012-02-01 11:00:12 +04:00
raise libvirtError ( " cannot use sendAll with "
2011-06-16 00:54:16 +04:00
" nonblocking stream " )
2011-06-15 00:07:43 +04:00
def recv ( self , nbytes ) :
2011-10-31 20:11:33 +04:00
""" Reads a series of bytes from the stream. This method may
2011-06-15 00:07:43 +04:00
block the calling application for an arbitrary amount
of time .
Errors are not guaranteed to be reported synchronously
with the call , but may instead be delayed until a
subsequent call .
On success , the received data is returned . On failure , an
exception is raised . If the stream is a NONBLOCK stream and
the request would block , integer - 2 is returned .
"""
ret = libvirtmod . virStreamRecv ( self . _o , nbytes )
2013-08-22 13:16:03 +04:00
if ret is None : raise libvirtError ( ' virStreamRecv() failed ' )
2011-06-15 00:07:43 +04:00
return ret
def send ( self , data ) :
""" Write a series of bytes to the stream. This method may
block the calling application for an arbitrary amount
of time . Once an application has finished sending data
it should call virStreamFinish to wait for successful
confirmation from the driver , or detect any error
This method may not be used if a stream source has been
registered
Errors are not guaranteed to be reported synchronously
with the call , but may instead be delayed until a
subsequent call .
"""
2014-01-23 18:24:12 +04:00
ret = libvirtmod . virStreamSend ( self . _o , data )
2011-06-15 00:07:43 +04:00
if ret == - 1 : raise libvirtError ( ' virStreamSend() failed ' )
return ret