All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hertsstack.rpcclient.HertsRpcClientCStreamingMethodHandler Maven / Gradle / Ivy

The newest version!
package org.hertsstack.rpcclient;

import org.hertsstack.core.context.HertsType;
import org.hertsstack.core.descriptor.CustomGrpcDescriptor;
import org.hertsstack.core.exception.ServiceNotFoundException;
import org.hertsstack.core.exception.StreamBodyException;
import org.hertsstack.core.service.HertsService;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.MethodDescriptor;
import io.grpc.stub.ClientCalls;
import io.grpc.stub.StreamObserver;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * Herts rpc client handler
 * Client streaming Method Handler
 *
 * @author Herts Contributer
 */
class HertsRpcClientCStreamingMethodHandler extends io.grpc.stub.AbstractBlockingStub implements InvocationHandler {
    private final Map> methodTypes = new HashMap<>();
    private final ConcurrentMap> descriptors = new ConcurrentHashMap<>();
    private final Class hertsRpcService;
    private final String serviceName;

    public HertsRpcClientCStreamingMethodHandler(Channel channel, CallOptions callOptions, Class hertsRpcService) {
        super(channel, callOptions);
        this.hertsRpcService = hertsRpcService;
        this.serviceName = hertsRpcService.getName();

        Class hertsServiceClass;
        try {
            hertsServiceClass = Class.forName(this.serviceName);
        } catch (ClassNotFoundException ignore) {
            throw new ServiceNotFoundException("Unknown class name. Allowed class is " + HertsService.class.getName());
        }

        Method[] methods = hertsServiceClass.getDeclaredMethods();
        for (Method method : methods) {
            methodTypes.put(method.getName(), method.getReturnType());
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        String methodName = method.getName();
        MethodDescriptor methodDescriptor = this.descriptors.get(methodName);
        if (methodDescriptor == null) {
            methodDescriptor = CustomGrpcDescriptor
                    .generateStramingMethodDescriptor(HertsType.ClientStreaming, this.serviceName, methodName);
            this.descriptors.put(methodName, methodDescriptor);
        }

        StreamObserver bytes = null;
        if (args != null) {
            bytes = (StreamObserver) args[0];
        }
        if (bytes == null) {
            throw new StreamBodyException("Streaming body data is null");
        }
        return ClientCalls.asyncClientStreamingCall(getChannel().newCall(methodDescriptor, getCallOptions()), bytes);
    }

    @Override
    protected HertsRpcClientCStreamingMethodHandler build(Channel channel, CallOptions callOptions) {
        return new HertsRpcClientCStreamingMethodHandler(channel, callOptions, hertsRpcService);
    }
}