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

tech.deplant.jacki.framework.subscription.SubscriptionHandle Maven / Gradle / Ivy

package tech.deplant.jacki.framework.subscription;

import com.fasterxml.jackson.databind.JsonNode;
import tech.deplant.jacki.binding.TvmSdkException;
import tech.deplant.jacki.binding.Net;

import java.util.Deque;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class SubscriptionHandle implements Subscriptions {

	private static System.Logger logger = System.getLogger(SubscriptionHandle.class.getName());
	private final int contextId;
	private final String queryText;
	private final boolean hasUnsubsribeCondition;

	public boolean hasUnsubsribeCondition() {
		return hasUnsubsribeCondition;
	}

	public Predicate unsubscribeCondition() {
		return unsubscribeCondition;
	}

	public Set> consumers() {
		return consumers;
	}

	public String queryText() {
		return queryText;
	}

	private final Predicate unsubscribeCondition;
	private final Set> consumeFilters;
	private final Set> consumers;
	protected long handle;
	private final boolean isCallbackToQueue;
	private final Deque callbackQueue = new ConcurrentLinkedDeque<>();

	protected SubscriptionHandle(int contextId,
	                             String queryText,
	                             boolean hasUnsubsribeCondition,
	                             Predicate unsubscribeCondition,
	                             Set> consumeFilters,
	                             Set> consumers,
	                             boolean isCallbackToQueue) {
		this.contextId = contextId;
		this.queryText = queryText;
		this.hasUnsubsribeCondition = hasUnsubsribeCondition;
		this.unsubscribeCondition = unsubscribeCondition;
		this.consumeFilters = consumeFilters;
		this.consumers = consumers;
		this.isCallbackToQueue = isCallbackToQueue;

	}

	@Override
	public int contextId() {
		return this.contextId;
	}

	@Override
	public long handle() {
		return this.handle;
	}

	@Override
	public boolean isCallbackToQueue() {
		return this.isCallbackToQueue;
	}

	@Override
	public Deque callbackQueue() {
		return this.callbackQueue;
	}

	public boolean testConsumeFilters(JsonNode jsonNode) {
		return this.consumeFilters.isEmpty() || this.consumeFilters.stream().allMatch(flt -> flt.test(jsonNode));
	}

	public void broadcastToConsumers(JsonNode jsonNode) {
		this.consumers.forEach(consumer -> consumer.accept(jsonNode));
	}

	/**
	 * Unsubscribe.
	 */
	public void unsubscribe() {
		try {
			logger.log(System.Logger.Level.TRACE, () -> "HANDLE:%d Unsubscribing...".formatted(this.handle));
			Net.unsubscribe(this.contextId, new Net.ResultOfSubscribeCollection(handle()));
		} catch (TvmSdkException e) {
			// I think there's no reason to fail everything if unsubscribe failed...
			logger.log(System.Logger.Level.ERROR, () -> "HANDLE:%d Unsubscribe failed!".formatted(this.handle));
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy