Search in sources :

Example 1 with PlatformCallbackGateway

use of org.apache.ignite.internal.processors.platform.callback.PlatformCallbackGateway in project ignite by apache.

the class PlatformFutureUtils method listen.

/**
 * Listen future.
 *
 * @param ctx Context.
 * @param listenable Listenable entry.
 * @param futPtr Native future pointer.
 * @param typ Expected return type.
 * @param writer Optional writer.
 */
public static void listen(final PlatformContext ctx, PlatformListenable listenable, final long futPtr, final int typ, @Nullable final Writer writer, final PlatformTarget target) {
    final PlatformCallbackGateway gate = ctx.gateway();
    listenable.listen(new IgniteBiInClosure<Object, Throwable>() {

        private static final long serialVersionUID = 0L;

        @Override
        public void apply(Object res, Throwable err) {
            if (err instanceof Exception)
                err = target.convertException((Exception) err);
            if (writer != null && writeToWriter(res, err, ctx, writer, futPtr))
                return;
            if (err != null) {
                writeFutureError(ctx, futPtr, err);
                return;
            }
            try {
                if (typ == TYP_OBJ) {
                    if (res == null)
                        gate.futureNullResult(futPtr);
                    else {
                        try (PlatformMemory mem = ctx.memory().allocate()) {
                            PlatformOutputStream out = mem.output();
                            BinaryRawWriterEx outWriter = ctx.writer(out);
                            outWriter.writeObjectDetached(res);
                            out.synchronize();
                            gate.futureObjectResult(futPtr, mem.pointer());
                        }
                    }
                } else if (res == null)
                    gate.futureNullResult(futPtr);
                else {
                    switch(typ) {
                        case TYP_BYTE:
                            gate.futureByteResult(futPtr, (byte) res);
                            break;
                        case TYP_BOOL:
                            gate.futureBoolResult(futPtr, (boolean) res ? 1 : 0);
                            break;
                        case TYP_SHORT:
                            gate.futureShortResult(futPtr, (short) res);
                            break;
                        case TYP_CHAR:
                            gate.futureCharResult(futPtr, (char) res);
                            break;
                        case TYP_INT:
                            gate.futureIntResult(futPtr, (int) res);
                            break;
                        case TYP_FLOAT:
                            gate.futureFloatResult(futPtr, Float.floatToIntBits((float) res));
                            break;
                        case TYP_LONG:
                            gate.futureLongResult(futPtr, (long) res);
                            break;
                        case TYP_DOUBLE:
                            gate.futureDoubleResult(futPtr, Double.doubleToLongBits((double) res));
                            break;
                        default:
                            assert false : "Should not reach this: " + typ;
                    }
                }
            } catch (Throwable t) {
                writeFutureError(ctx, futPtr, t);
                if (t instanceof Error)
                    throw t;
            }
        }
    });
}
Also used : PlatformCallbackGateway(org.apache.ignite.internal.processors.platform.callback.PlatformCallbackGateway) PlatformOutputStream(org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream) BinaryRawWriterEx(org.apache.ignite.internal.binary.BinaryRawWriterEx) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) PlatformMemory(org.apache.ignite.internal.processors.platform.memory.PlatformMemory)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 BinaryRawWriterEx (org.apache.ignite.internal.binary.BinaryRawWriterEx)1 PlatformCallbackGateway (org.apache.ignite.internal.processors.platform.callback.PlatformCallbackGateway)1 PlatformMemory (org.apache.ignite.internal.processors.platform.memory.PlatformMemory)1 PlatformOutputStream (org.apache.ignite.internal.processors.platform.memory.PlatformOutputStream)1