package meerkat.bulletinboard;

import meerkat.rest.ProtobufMessageBodyReader;
import meerkat.rest.ProtobufMessageBodyWriter;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import java.util.concurrent.Callable;

/**
 * Created by Arbel Deutsch Peled on 02-Jan-16.
 */
public abstract class SingleServerWorker<IN, OUT> extends BulletinClientWorker<IN> implements Callable<OUT>{

    // This resource enabled creation of a single Client per thread.
    protected static final ThreadLocal<Client> clientLocal =
            new ThreadLocal<Client> () {
                @Override protected Client initialValue() {
                    Client client;
                    client = ClientBuilder.newClient();
                    client.register(ProtobufMessageBodyReader.class);
                    client.register(ProtobufMessageBodyWriter.class);

                    return client;
                }
            };

    protected final String serverAddress;

    public SingleServerWorker(String serverAddress, IN payload, int maxRetry) {
        super(payload, maxRetry);
        this.serverAddress = serverAddress;
    }

    public String getServerAddress() {
        return serverAddress;
    }

}
