view src/main/java/net/borgac/clusterrpc/client/Client.java @ 10:b99a9821115c

Implement high-level functionality for Client and Request
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Sep 2016 15:18:18 +0200
parents 0e608c466a58
children
line wrap: on
line source

package net.borgac.clusterrpc.client;

import java.time.Instant;
import java.util.Random;

/**
 * The entry point for all client requests.
 *
 * This class and its dependencies are not thread-safe; it is not intended to be
 * shared between threads. Rather use one client per thread; because the
 * underlying connections are long-lived, the overhead is small.
 *
 * @author lbo
 */
public class Client {
    // Simple seed is good enough here.

    private static final Random ID_GENERATOR = new Random(
            Instant.now().getEpochSecond() + Instant.now().getNano());

    private ClientChannel channel;
    private String callerId;
    private long deadline;

    // TODO: Build filter stack
    private OutgoingFilter filterStack;

    public Client(ClientChannel channel) {
        this.channel = channel;
        this.callerId = Long.toHexString(ID_GENERATOR.nextLong()).toUpperCase();
    }

    public void setCallerId(String id) {
        this.callerId = id;
    }

    public void setDeadline(long millis) {
        this.deadline = millis;
    }

    public Request newRequest(String service, String method) throws RpcException {
        return new Request(service, method, this);
    }

    public Response sendRequest(Request request) throws RpcException {
        return filterStack.go(request);
    }

    String getCallerId() {
        return callerId;
    }
}