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

hu.akarnokd.reactiverpc.RpcClient Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
package hu.akarnokd.reactiverpc;

import java.io.IOException;
import java.net.*;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import rsc.flow.Cancellation;
import rsc.scheduler.*;

public final class RpcClient {

    final Class remoteAPI;
    
    final Object localAPI;
    
    static Scheduler scheduler = new ExecutorServiceScheduler(Executors.newCachedThreadPool(r -> {
        Thread t = new Thread(r, "akarnokd-reactive-rpc-clientpool");
        t.setDaemon(true);
        return t;
    }));
    
    private RpcClient(Class remoteAPI, Object localAPI) {
        this.remoteAPI = remoteAPI;
        this.localAPI = localAPI;
    }
    
    public static RpcClient createLocal(Object localAPI) {
        Objects.requireNonNull(localAPI, "localAPI");
        return new RpcClient<>(null, localAPI);
    }
    
    public static  RpcClient createRemote(Class remoteAPI) {
        Objects.requireNonNull(remoteAPI, "remoteAPI");
        return new RpcClient<>(remoteAPI, null);
    }
    
    public static  RpcClient createBidirectional(Class remoteAPI, Object localAPI) {
        Objects.requireNonNull(remoteAPI, "remoteAPI");
        Objects.requireNonNull(localAPI, "localAPI");
        return new RpcClient<>(remoteAPI, localAPI);
    }
    
    public T connect(InetAddress endpoint, int port, Consumer close) {
        Socket socket;
        
        try {
            socket = new Socket(endpoint, port);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return RpcSocketManager.connect(socket, endpoint, port, remoteAPI, localAPI, close, scheduler, false);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy