package vs;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

/**
 * asynchronous subscriber for the log service
 *
 * @author Sandro Leuchter
 *
 */
public class Subscriber {

	/**
	 * main routine and starting point of program
	 *
	 * @param args not used
	 */
	public static void main(String... args) {
		try (var client = new MqttClient(Conf.BROKER, MqttClient.generateClientId())) {
			client.setCallback(new MqttCallback() {

				@Override
				public void connectionLost(Throwable arg0) {
				}

				@Override
				public void deliveryComplete(IMqttDeliveryToken arg0) {
				}

				@Override
				public void messageArrived(String topic, MqttMessage m) throws Exception {
					System.out.println("Topic: " + topic + ",  Message: " + m.toString());
				}
			});
			client.connect();
			client.subscribe(Conf.TOPIC);
			while (true) {
				Thread.sleep(1000);
			}
		} catch (MqttException e) {
			System.err.println(e.getMessage());
		} catch (InterruptedException e) {
			Thread.currentThread().interrupt();
		}
	}
}
