Exemplo n.º 1
0
class CSpaceAcceptor( object ) :
    def __init__( self, sock, connectionId, reactor, callback ) :
        self.stream = TCPStream( sock, reactor )
        self.stream.setCloseCallback( self._onClose )
        self.stream.setErrorCallback( self._onError )
        self.stream.setInputCallback( self._onInput )
        self.stream.initiateRead( 1 )
        self.stream.writeData( 'ACCEPT %s\r\n' % connectionId )
        self.response = ''
        self.op = AsyncOp( callback, self.stream.close )

    def getOp( self ) : return self.op

    def _onClose( self ) :
        self.stream.close()
        self.op.notify( -1, None )

    def _onError( self, err, errMsg ) :
        self._onClose()

    def _onInput( self, data ) :
        self.response += data
        if self.response.endswith('\n') :
            if not self.response.startswith('OK') :
                self._onClose()
                return
            self.stream.shutdown()
            sock = self.stream.getSock()
            self.op.notify( 0, sock )
Exemplo n.º 2
0
class EchoClient(object):
    def __init__(self, sock, reactor):
        self.sock = sock
        self.reactor = reactor
        self.stream = TCPStream(self.sock, self.reactor)
        self.stream.setCloseCallback(self._onClose)
        self.stream.setErrorCallback(self._onError)
        self.stream.setInputCallback(self._onInput)
        self.timerOp = self.reactor.addTimer(1, self._onTimer)
        self.stream.initiateRead(8192)

    def _shutdown(self):
        self.stream.shutdown()
        self.sock.close()
        self.timerOp.cancel()
        self.reactor.stop()

    def _onClose(self):
        print 'closed'
        self._shutdown()

    def _onError(self, err, errMsg):
        print 'error(%d): %s' % (err, errMsg)
        self._shutdown()

    def _onInput(self, data):
        print 'received: %s' % data

    def _onTimer(self):
        msg = 'time() = %f' % time()
        print 'sending: %s' % msg
        self.stream.writeData(msg)
Exemplo n.º 3
0
class EchoClient(object) :
    def __init__( self, sock, reactor ) :
        self.sock = sock
        self.reactor = reactor
        self.stream = TCPStream( self.sock, self.reactor )
        self.stream.setCloseCallback( self._onClose )
        self.stream.setErrorCallback( self._onError )
        self.stream.setInputCallback( self._onInput )
        self.timerOp = self.reactor.addTimer( 1, self._onTimer )
        self.stream.initiateRead( 8192 )
    
    def _shutdown( self ) :
        self.stream.shutdown()
        self.sock.close()
        self.timerOp.cancel()
        self.reactor.stop()

    def _onClose( self ) :
        print 'closed'
        self._shutdown()

    def _onError( self, err, errMsg ) :
        print 'error(%d): %s' % (err,errMsg)
        self._shutdown()

    def _onInput( self, data ) :
        print 'received: %s' % data

    def _onTimer( self ) :
        msg = 'time() = %f' % time()
        print 'sending: %s' % msg
        self.stream.writeData( msg )
Exemplo n.º 4
0
class CSpaceAcceptor(object):
    def __init__(self, sock, connectionId, reactor, callback):
        self.stream = TCPStream(sock, reactor)
        self.stream.setCloseCallback(self._onClose)
        self.stream.setErrorCallback(self._onError)
        self.stream.setInputCallback(self._onInput)
        self.stream.initiateRead(1)
        self.stream.writeData('ACCEPT %s\r\n' % connectionId)
        self.response = ''
        self.op = AsyncOp(callback, self.stream.close)

    def getOp(self):
        return self.op

    def _onClose(self):
        self.stream.close()
        self.op.notify(-1, None)

    def _onError(self, err, errMsg):
        self._onClose()

    def _onInput(self, data):
        self.response += data
        if self.response.endswith('\n'):
            if not self.response.startswith('OK'):
                self._onClose()
                return
            self.stream.shutdown()
            sock = self.stream.getSock()
            self.op.notify(0, sock)
Exemplo n.º 5
0
 def _initStream( self, sock, pendingWrite ) :
     assert pendingWrite
     stream = TCPStream( sock, self.reactor )
     self.streams.append( stream )
     stream.setCloseCallback( self.close )
     stream.setErrorCallback( lambda e,m : self.close() )
     stream.setWriteCompleteCallback( self._onWritten )
     stream.writeData( pendingWrite )
Exemplo n.º 6
0
 def _initStream( self, sock, pendingWrite ) :
     assert pendingWrite
     stream = TCPStream( sock, self.reactor )
     self.streams.append( stream )
     stream.setCloseCallback( self.close )
     stream.setErrorCallback( lambda e,m : self.close() )
     stream.setWriteCompleteCallback( self._onWritten )
     stream.writeData( pendingWrite )
Exemplo n.º 7
0
class EchoHandler(object):
    def __init__(self, sock, reactor):
        self.sock = sock
        self.reactor = reactor
        self.stream = TCPStream(sock, reactor)
        self.stream.setInputCallback(self._onInput)
        self.stream.setCloseCallback(self._onClose)
        self.stream.setErrorCallback(self._onError)
        self.stream.initiateRead(8192)

    def _onClose(self):
        print 'EchoHandler: on close'
        self.stream.close(deferred=True)

    def _onError(self, err, errMsg):
        print 'EchoHandler: error(%d): %s' % (err, errMsg)
        self.stream.close()

    def _onInput(self, data):
        self.stream.writeData(data)
Exemplo n.º 8
0
class EchoHandler( object ) :
    def __init__( self, sock, reactor ) :
        self.sock = sock
        self.reactor = reactor
        self.stream = TCPStream( sock, reactor )
        self.stream.setInputCallback( self._onInput )
        self.stream.setCloseCallback( self._onClose )
        self.stream.setErrorCallback( self._onError )
        self.stream.initiateRead( 8192 )

    def _onClose( self ) :
        print 'EchoHandler: on close'
        self.stream.close( deferred=True )

    def _onError( self, err, errMsg ) :
        print 'EchoHandler: error(%d): %s' % (err,errMsg)
        self.stream.close()

    def _onInput( self, data ) :
        self.stream.writeData( data )