コード例 #1
0
ファイル: node_echoclient.py プロジェクト: hj91/cspace
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)
コード例 #2
0
ファイル: appletutil.py プロジェクト: AchillesA/cspace
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 )
コード例 #3
0
ファイル: node_echoclient.py プロジェクト: AchillesA/cspace
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 )
コード例 #4
0
ファイル: appletutil.py プロジェクト: hj91/cspace
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)
コード例 #5
0
ファイル: linestream.py プロジェクト: hj91/cspace
class TCPLineStream(object):
    def __init__(self, sock, reactor):
        self.reactor = reactor
        self.stream = TCPStream(sock, reactor)
        self.readEnabled = False
        self.readInitiated = False
        self.timerOp = None
        self.inputBuffer = ['']
        self.inputCallback = None
        self.stream.setInputCallback(self._onInput)

        self.getSock = self.stream.getSock
        self.setCloseCallback = self.stream.setCloseCallback
        self.setErrorCallback = self.stream.setErrorCallback
        self.setWriteCompleteCallback = self.stream.setWriteCompleteCallback
        self.hasShutdown = self.stream.hasShutdown
        self.writeData = self.stream.writeData

    def setInputCallback(self, inputCallback):
        self.inputCallback = inputCallback

    def shutdown(self):
        self.enableRead(False)
        self.stream.shutdown()

    def close(self, deferred=False, callback=None):
        self.enableRead(False)
        return self.stream.close(deferred, callback)

    def enableRead(self, enable=True):
        assert not self.hasShutdown()
        if enable != self.readEnabled:
            self.readEnabled = enable
            if enable:
                assert not self.readInitiated
                assert not self.timerOp
                if len(self.inputBuffer) > 1:
                    self.timerOp = self.reactor.addTimer(0, self._onTimer)
                else:
                    self.readInitiated = True
                    self.stream.initiateRead(4096)
            else:
                if self.timerOp:
                    self.timerOp.cancel()
                    self.timerOp = None
                else:
                    self.readInitiated = False
                    self.stream.cancelRead()

    def _onInput(self, data):
        while data:
            i = data.find('\n')
            if i < 0:
                self.inputBuffer[-1] += data
                break
            else:
                i += 1
                self.inputBuffer[-1] += data[:i]
                self.inputBuffer.append('')
                data = data[i:]
        while len(self.inputBuffer) > 1:
            line = self.inputBuffer.pop(0)
            self.inputCallback(line)
            if not self.readEnabled: return

    def _onTimer(self):
        while len(self.inputBuffer) > 1:
            line = self.inputBuffer.pop(0)
            self.inputCallback(line)
            if not self.readEnabled: return
        if self.timerOp:
            self.timerOp.cancel()
            self.timerOp = None
            self.readInitiated = True
            self.stream.initiateRead(4096)
コード例 #6
0
ファイル: linestream.py プロジェクト: AchillesA/cspace
class TCPLineStream( object ) :
    def __init__( self, sock, reactor ) :
        self.reactor = reactor
        self.stream = TCPStream( sock, reactor )
        self.readEnabled = False
        self.readInitiated = False
        self.timerOp = None
        self.inputBuffer = [ '' ]
        self.inputCallback = None
        self.stream.setInputCallback( self._onInput )

        self.getSock = self.stream.getSock
        self.setCloseCallback = self.stream.setCloseCallback
        self.setErrorCallback = self.stream.setErrorCallback
        self.setWriteCompleteCallback = self.stream.setWriteCompleteCallback
        self.hasShutdown = self.stream.hasShutdown
        self.writeData = self.stream.writeData

    def setInputCallback( self, inputCallback ) :
        self.inputCallback = inputCallback

    def shutdown( self ) :
        self.enableRead( False )
        self.stream.shutdown()

    def close( self, deferred=False, callback=None ) :
        self.enableRead( False )
        return self.stream.close( deferred, callback )

    def enableRead( self, enable=True ) :
        assert not self.hasShutdown()
        if enable != self.readEnabled :
            self.readEnabled = enable
            if enable :
                assert not self.readInitiated
                assert not self.timerOp
                if len(self.inputBuffer) > 1 :
                    self.timerOp = self.reactor.addTimer( 0, self._onTimer )
                else :
                    self.readInitiated = True
                    self.stream.initiateRead( 4096 )
            else :
                if self.timerOp :
                    self.timerOp.cancel()
                    self.timerOp = None
                else :
                    self.readInitiated = False
                    self.stream.cancelRead()

    def _onInput( self, data ) :
        while data :
            i = data.find( '\n' )
            if i < 0 :
                self.inputBuffer[-1] += data
                break
            else :
                i += 1
                self.inputBuffer[-1] += data[:i]
                self.inputBuffer.append( '' )
                data = data[i:]
        while len(self.inputBuffer) > 1 :
            line = self.inputBuffer.pop( 0 )
            self.inputCallback( line )
            if not self.readEnabled : return

    def _onTimer( self ) :
        while len(self.inputBuffer) > 1 :
            line = self.inputBuffer.pop( 0 )
            self.inputCallback( line )
            if not self.readEnabled : return
        if self.timerOp :
            self.timerOp.cancel()
            self.timerOp = None
            self.readInitiated = True
            self.stream.initiateRead( 4096 )