package vs;

import java.util.Date;

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;

/**
 * client for the log service
 *
 * @author Sandro Leuchter
 *
 */
public class Publisher {

	/**
	 * main routine and starting point of program
	 *
	 * @param args not used
	 */
	public static void main(String... args) {
		var clientId = MqttClient.generateClientId();
		try (var client = new MqttClient(Conf.BROKER, clientId)) {
			var options = new MqttConnectOptions();
			options.setWill(Conf.TOPIC, "LWT: Das war das Ende. Hilfe!".getBytes(), 2, false);
			client.connect(options);
			var message = new MqttMessage();
			for (var i = 29; i >= 0; i--) {
				var m = "[" + clientId + "] message " + i + " (" + 29 / i + "): " + (new Date()).toString();
				message.setPayload(m.getBytes());
				message.setRetained(true);
				client.publish(Conf.TOPIC, message);
			}
			client.disconnect();
		} catch (MqttException e) {
			System.err.println(e.getMessage());
		}
	}
}
