package jkind.engines.messages;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.function.Supplier;

import jkind.JKindException;

public abstract class MessageHandler {
	private BlockingQueue<Message> incoming = new LinkedBlockingQueue<>();

	public void receiveMessage(Message message) {
		if (incoming != null) {
			incoming.add(message);
		}
	}

	protected void stopReceivingMessages() {
		incoming = null;
	}

	protected void processMessages() {
		while (!incoming.isEmpty()) {
			handleMessage(incoming.poll());
		}
	}

	protected void handleMessage(Message message) {
		message.accept(this);
	}

	protected void processMessagesAndWaitUntil(Supplier<Boolean> stoppingCondition) {
		try {
			while (!incoming.isEmpty() || !stoppingCondition.get()) {
				handleMessage(incoming.take());
			}
		} catch (InterruptedException e) {
			throw new JKindException("Interrupted while waiting for message", e);
		}
	}

	protected abstract void handleMessage(BaseStepMessage bsm);

	protected abstract void handleMessage(InductiveCounterexampleMessage icm);

	protected abstract void handleMessage(InvalidMessage im);

	protected abstract void handleMessage(InvariantMessage im);

	protected abstract void handleMessage(UnknownMessage um);

	protected abstract void handleMessage(ValidMessage vm);
}

@@ -110,32 +110,70 @@
 dBlockingQueue;%0A
+import java.util.concurrent.TimeUnit;%0A
 import java.util
@@ -851,30 +851,34 @@
 ) %7B%0A%09%09%09%09
-handleM
+Message m
 essage
-(
+ = 
 incoming
@@ -882,16 +882,103 @@
 ing.
-take());
+poll(100, TimeUnit.MILLISECONDS);%0A%09%09%09%09if (message != null) %7B%0A%09%09%09%09%09handleMessage(message);%0A%09%09%09%09%7D
 %0A%09%09%09

Check wait condition periodically