package org.genericsystem.distributed.cacheonclient;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

import org.genericsystem.api.core.exceptions.ConcurrencyControlException;
import org.genericsystem.api.core.exceptions.OptimisticLockConstraintViolationException;
import org.genericsystem.common.Vertex;
import org.genericsystem.distributed.AbstractGSClient;
import org.genericsystem.distributed.GSBuffer;

public abstract class AbstractCocClient extends AbstractGSClient implements CocProtocole {

	@Override
	public Vertex[] getDependencies(long ts, long id) {
		return unsafe(() -> getDependenciesPromise(ts, id).get());

	}

	public CompletableFuture<Vertex[]> getDependenciesPromise(long ts, long id) {
		return promise(GET_DEPENDENCIES, buff -> buff.getGSVertexArray(), buffer -> buffer.appendLong(ts).appendLong(id));
	}

	@Override
	public void apply(long ts, long[] removes, Vertex[] adds) throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
		if (!Arrays.stream(adds).allMatch(v -> (v.getBirthTs() == Long.MAX_VALUE)))
			throw new IllegalStateException("");
		Object res = unsafe(() -> applyPromise(ts, removes, adds).get());
		if (res instanceof OptimisticLockConstraintViolationException)
			throw (OptimisticLockConstraintViolationException) res;
		if (res instanceof ConcurrencyControlException)
			throw (ConcurrencyControlException) res;
		if (res instanceof Throwable)
			throw new IllegalStateException((Throwable) res);
	}

	public CompletableFuture<Object> applyPromise(long ts, long[] removes, Vertex[] adds) {
		return promise(APPLY, buff -> buff.getLongThrowException(), buffer -> {
			GSBuffer gsBuffer = new GSBuffer(buffer);
			gsBuffer.appendLong(ts);
			gsBuffer.appendGSLongArray(removes);
			gsBuffer.appendGSVertexArray(adds);
			return gsBuffer;
		});
	}

}

@@ -424,16 +424,57 @@
 SBuffer;
+%0Aimport org.genericsystem.kernel.Statics;
 %0A%0Apublic
@@ -676,24 +676,75 @@
 ts, id).get(
+Statics.SERVER_TIMEOUT, Statics.SERVER_TIMEOUT_UNIT
 ));%0A%0A%09%7D%0A%0A%09pu
@@ -1273,16 +1273,67 @@
 ds).get(
+Statics.SERVER_TIMEOUT, Statics.SERVER_TIMEOUT_UNIT
 ));%0A%09%09if

Add timeout on promise of cacheonclient