Exemple #1
0
def connectCB(connect, status):
    status = intmask(status)
    stream = connect.c_handle

    try:
        vat, resolvers = ruv.unstashStream(stream)
        fountResolver, drainResolver = unwrapList(resolvers)
        assert isinstance(fountResolver, LocalResolver)
        assert isinstance(drainResolver, LocalResolver)

        with scopedVat(vat):
            if status >= 0:
                debug_print("Made connection!")
                fountResolver.resolve(StreamFount(stream, vat))
                drainResolver.resolve(StreamDrain(stream, vat))
            else:
                error = "Connection failed: " + ruv.formatError(status)
                debug_print(error)
                fountResolver.smash(StrObject(error.decode("utf-8")))
                drainResolver.smash(StrObject(error.decode("utf-8")))
                # Done with stream.
                ruv.closeAndFree(stream)
    except:
        if not we_are_translated():
            raise
Exemple #2
0
def connectStreamCB(connect, status):
    status = intmask(status)
    stream = connect.c_handle

    try:
        vat, resolvers = ruv.unstashStream(stream)
        sourceResolver, sinkResolver = unwrapList(resolvers)
        assert isinstance(sourceResolver, LocalResolver)
        assert isinstance(sinkResolver, LocalResolver)

        with scopedVat(vat):
            if status >= 0:
                debug_print("Made connection!")
                wrappedStream = ruv.wrapStream(stream, 2)
                sourceResolver.resolve(StreamSource(wrappedStream, vat))
                sinkResolver.resolve(StreamSink(wrappedStream, vat))
            else:
                error = "Connection failed: " + ruv.formatError(status)
                debug_print(error)
                sourceResolver.smash(StrObject(error.decode("utf-8")))
                sinkResolver.smash(StrObject(error.decode("utf-8")))
                # Done with stream.
                ruv.closeAndFree(stream)
    except:
        if not we_are_translated():
            raise
Exemple #3
0
def writeCB(fs):
    try:
        with ruv.unstashingFS(fs) as (vat, drain):
            assert isinstance(drain, FileDrain)
            size = intmask(fs.c_result)
            if size > 0:
                drain.written(size)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                drain.abort(u"libuv error: %s" % msg)
    except:
        print "Exception in writeCB"
Exemple #4
0
def writeCB(fs):
    try:
        with ruv.unstashingFS(fs) as (vat, drain):
            assert isinstance(drain, FileDrain)
            size = intmask(fs.c_result)
            if size > 0:
                drain.written(size)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                drain.abort(u"libuv error: %s" % msg)
    except:
        print "Exception in writeCB"
Exemple #5
0
def writeSetContentsCB(fs):
    try:
        with ruv.unstashingFS(fs) as (vat, sc):
            assert isinstance(sc, SetContents)
            size = intmask(fs.c_result)
            if size > 0:
                sc.written(size)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                sc.fail(u"libuv error: %s" % msg)
    except:
        print "Exception in writeSetContentsCB"
Exemple #6
0
def writeSetContentsCB(fs):
    try:
        with ruv.unstashingFS(fs) as (vat, sc):
            assert isinstance(sc, SetContents)
            size = intmask(fs.c_result)
            if size > 0:
                sc.written(size)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                sc.fail(u"libuv error: %s" % msg)
    except:
        print "Exception in writeSetContentsCB"
Exemple #7
0
def writeFileCB(fs):
    try:
        with ruv.unstashingFS(fs) as (vat, sink):
            assert isinstance(sink, FileSink)
            size = intmask(fs.c_result)
            if size > 0:
                # XXX backpressure drain.written(size)
                pass
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                sink.abort(StrObject(u"libuv error: %s" % msg))
    except:
        print "Exception in writeFileCB"
Exemple #8
0
def gaiCB(gai, status, ai):
    status = intmask(status)
    vat, resolver = ruv.unstashGAI(gai)
    with scopedVat(vat):
        assert isinstance(resolver, LocalResolver), "implementation error"
        if status < 0:
            msg = ruv.formatError(status).decode("utf-8")
            resolver.smash(StrObject(u"libuv error: %s" % msg))
        else:
            gaiList = walkAI(ai)
            resolver.resolve(ConstList(gaiList[:]))
    ruv.freeAddrInfo(ai)
    ruv.free(gai)
Exemple #9
0
def renameCB(fs):
    try:
        success = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        if success < 0:
            msg = ruv.formatError(success).decode("utf-8")
            r.smash(StrObject(u"Couldn't rename file: %s" % msg))
        else:
            r.resolve(NullObject)
        # Done with fs.
        ruv.fsDiscard(fs)
    except:
        print "Exception in renameCB"
Exemple #10
0
def renameCB(fs):
    try:
        success = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        if success < 0:
            msg = ruv.formatError(success).decode("utf-8")
            r.smash(StrObject(u"Couldn't rename file: %s" % msg))
        else:
            r.resolve(NullObject)
        # Done with fs.
        ruv.fsDiscard(fs)
    except:
        print "Exception in renameCB"
Exemple #11
0
def gaiCB(gai, status, ai):
    status = intmask(status)
    vat, resolver = ruv.unstashGAI(gai)
    with scopedVat(vat):
        assert isinstance(resolver, LocalResolver), "implementation error"
        if status < 0:
            msg = ruv.formatError(status).decode("utf-8")
            resolver.smash(StrObject(u"libuv error: %s" % msg))
        else:
            gaiList = walkAI(ai)
            resolver.resolve(ConstList(gaiList[:]))
    ruv.freeAddrInfo(ai)
    ruv.free(gai)
Exemple #12
0
def readFileCB(fs):
    size = intmask(fs.c_result)
    with ruv.unstashingFS(fs) as (vat, source):
        assert isinstance(source, FileSource)
        with scopedVat(vat):
            if size > 0:
                data = charpsize2str(source._buf.c_base, size)
                source.deliver(data)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                source.abort(u"libuv error: %s" % msg)
            else:
                # EOF.
                source.complete()
Exemple #13
0
def openSetContentsCB(fs):
    try:
        fd = intmask(fs.c_result)
        with ruv.unstashingFS(fs) as (vat, sc):
            assert isinstance(sc, SetContents)
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                sc.fail(u"Couldn't open file fount: %s" % msg)
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                sc.startWriting(fd, fs)
    except:
        print "Exception in openSetContentsCB"
Exemple #14
0
def openSetContentsCB(fs):
    try:
        fd = intmask(fs.c_result)
        with ruv.unstashingFS(fs) as (vat, sc):
            assert isinstance(sc, SetContents)
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                sc.fail(u"Couldn't open file fount: %s" % msg)
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                sc.startWriting(fd, fs)
    except:
        print "Exception in openSetContentsCB"
Exemple #15
0
def closeSetContentsCB(fs):
    try:
        vat, sc = ruv.unstashFS(fs)
        # Need to scope vat here.
        with scopedVat(vat):
            assert isinstance(sc, SetContents)
            size = intmask(fs.c_result)
            if size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                sc.fail(u"libuv error: %s" % msg)
            else:
                # Success.
                sc.rename()
    except:
        print "Exception in closeSetContentsCB"
Exemple #16
0
def closeSetContentsCB(fs):
    try:
        vat, sc = ruv.unstashFS(fs)
        # Need to scope vat here.
        with scopedVat(vat):
            assert isinstance(sc, SetContents)
            size = intmask(fs.c_result)
            if size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                sc.fail(u"libuv error: %s" % msg)
            else:
                # Success.
                sc.rename()
    except:
        print "Exception in closeSetContentsCB"
Exemple #17
0
def readFileCB(fs):
    size = intmask(fs.c_result)
    vat, source = ruv.unstashFS(fs)
    assert isinstance(source, FileSource)
    with scopedVat(vat):
        if size > 0:
            data = charpsize2str(source._buf.c_base, size)
            source.deliver(data)
        elif size < 0:
            msg = ruv.formatError(size).decode("utf-8")
            source.abort(u"libuv error: %s" % msg)
        else:
            # EOF.
            source.complete()
    ruv.fsDiscard(fs)
Exemple #18
0
def openDrainCB(fs):
    # As above.
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file drain: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                r.resolve(FileDrain(fs, fd, vat))
    except:
        print "Exception in openDrainCB"
Exemple #19
0
def writeFileCB(fs):
    try:
        vat, sb = ruv.unstashFS(fs)
        sink = sb.obj
        assert isinstance(sink, FileSink)
        size = intmask(fs.c_result)
        if size > 0:
            # XXX backpressure drain.written(size)
            pass
        elif size < 0:
            msg = ruv.formatError(size).decode("utf-8")
            sink.abort(StrObject(u"libuv error: %s" % msg))
        sb.deallocate()
        ruv.fsDiscard(fs)
    except:
        print "Exception in writeFileCB"
Exemple #20
0
def openDrainCB(fs):
    # As above.
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file drain: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                r.resolve(FileDrain(fs, fd, vat))
    except:
        print "Exception in openDrainCB"
Exemple #21
0
def readCB(fs):
    # Does *not* invoke user code.
    try:
        size = intmask(fs.c_result)
        with ruv.unstashingFS(fs) as (vat, fount):
            assert isinstance(fount, FileFount)
            # Done with fs, but don't free it; it belongs to the fount.
            if size > 0:
                data = charpsize2str(fount.buf.c_base, size)
                fount.receive(data)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                fount.abort(u"libuv error: %s" % msg)
            else:
                fount.stop(u"End of file")
    except:
        print "Exception in readCB"
Exemple #22
0
def openFountCB(fs):
    # Does *not* run user-level code. The scoped vat is only for promise
    # resolution.
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file fount: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                r.resolve(FileFount(fs, fd, vat))
    except:
        print "Exception in openFountCB"
Exemple #23
0
def openFountCB(fs):
    # Does *not* run user-level code. The scoped vat is only for promise
    # resolution.
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file fount: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                r.resolve(FileFount(fs, fd, vat))
    except:
        print "Exception in openFountCB"
Exemple #24
0
def readCB(fs):
    # Does *not* invoke user code.
    try:
        size = intmask(fs.c_result)
        with ruv.unstashingFS(fs) as (vat, fount):
            assert isinstance(fount, FileFount)
            # Done with fs, but don't free it; it belongs to the fount.
            if size > 0:
                data = charpsize2str(fount.buf.c_base, size)
                fount.receive(data)
            elif size < 0:
                msg = ruv.formatError(size).decode("utf-8")
                fount.abort(u"libuv error: %s" % msg)
            else:
                fount.stop(u"End of file")
    except:
        print "Exception in readCB"
Exemple #25
0
def getContentsCB(fs):
    try:
        size = intmask(fs.c_result)
        # Don't use with-statements here; instead, each next action in
        # GetContents will re-stash if necessary. ~ C.
        vat, self = ruv.unstashFS(fs)
        assert isinstance(self, GetContents)
        if size > 0:
            data = charpsize2str(self.buf.c_base, size)
            self.append(data)
        elif size < 0:
            msg = ruv.formatError(size).decode("utf-8")
            self.fail(msg)
        else:
            # End of file! Complete the callback.
            self.succeed()
    except Exception:
        print "Exception in getContentsCB"
Exemple #26
0
def getContentsCB(fs):
    try:
        size = intmask(fs.c_result)
        # Don't use with-statements here; instead, each next action in
        # GetContents will re-stash if necessary. ~ C.
        vat, self = ruv.unstashFS(fs)
        assert isinstance(self, GetContents)
        if size > 0:
            data = charpsize2str(self.buf.c_base, size)
            self.append(data)
        elif size < 0:
            msg = ruv.formatError(size).decode("utf-8")
            self.fail(msg)
        else:
            # End of file! Complete the callback.
            self.succeed()
    except Exception:
        print "Exception in getContentsCB"
Exemple #27
0
def openGetContentsCB(fs):
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file fount: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                # Strategy: Read and use the callback to queue additional reads
                # until done. This call is known to its caller to be expensive, so
                # there's not much point in trying to be clever about things yet.
                gc = GetContents(vat, fs, fd, r)
                gc.queueRead()
    except:
        print "Exception in openGetContentsCB"
Exemple #28
0
def openGetContentsCB(fs):
    try:
        fd = intmask(fs.c_result)
        vat, r = ruv.unstashFS(fs)
        assert isinstance(r, LocalResolver)
        with scopedVat(vat):
            if fd < 0:
                msg = ruv.formatError(fd).decode("utf-8")
                r.smash(StrObject(u"Couldn't open file fount: %s" % msg))
                # Done with fs.
                ruv.fsDiscard(fs)
            else:
                # Strategy: Read and use the callback to queue additional reads
                # until done. This call is known to its caller to be expensive, so
                # there's not much point in trying to be clever about things yet.
                gc = GetContents(vat, fs, fd, r)
                gc.queueRead()
    except:
        print "Exception in openGetContentsCB"
Exemple #29
0
def readStreamCB(stream, status, buf):
    status = intmask(status)
    # We only restash in the success case, not the error cases.
    vat, source = ruv.unstashStream(stream)
    assert isinstance(source, StreamSource), "Implementation error"
    # Don't read any more. We'll call .readStart() when we're interested in
    # reading again.
    ruv.readStop(stream)
    with scopedVat(vat):
        if status > 0:
            # Restash required.
            ruv.stashStream(stream, (vat, source))
            data = charpsize2str(buf.c_base, status)
            source.deliver(data)
        elif status == -4095:
            # EOF.
            source.complete()
        else:
            msg = ruv.formatError(status).decode("utf-8")
            source.abort(u"libuv error: %s" % msg)
Exemple #30
0
def readStreamCB(stream, status, buf):
    status = intmask(status)
    # We only restash in the success case, not the error cases.
    vat, source = ruv.unstashStream(stream)
    assert isinstance(source, StreamSource), "Implementation error"
    # Don't read any more. We'll call .readStart() when we're interested in
    # reading again.
    ruv.readStop(stream)
    with scopedVat(vat):
        if status > 0:
            # Restash required.
            ruv.stashStream(stream, (vat, source))
            data = charpsize2str(buf.c_base, status)
            source.deliver(data)
        elif status == -4095:
            # EOF.
            source.complete()
        else:
            msg = ruv.formatError(status).decode("utf-8")
            source.abort(u"libuv error: %s" % msg)
Exemple #31
0
def readCB(stream, status, buf):
    status = intmask(status)
    try:
        # We only restash in the success case, not the error cases.
        vat, fount = ruv.unstashStream(stream)
        assert isinstance(fount, StreamFount), "Implementation error"
        with scopedVat(vat):
            if status > 0:
                # Restash required.
                ruv.stashStream(stream, (vat, fount))
                data = charpsize2str(buf.c_base, status)
                fount.receive(data)
            elif status == 0:
                # EOF.
                fount.stop(u"End of stream")
            else:
                msg = ruv.formatError(status).decode("utf-8")
                fount.abort(u"libuv error: %s" % msg)
    except:
        if not we_are_translated():
            raise