package vs;

import jakarta.jms.Connection;
import jakarta.jms.ConnectionFactory;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageProducer;
import jakarta.jms.Queue;
import jakarta.jms.Session;
import jakarta.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * requester node for the echo service
 *
 * @author Sandro Leuchter
 *
 */
public class EchoRequesterNode {
	private Connection connection;
	private Session session;
	private MessageProducer producer;
	private MessageConsumer consumer;
	private Queue replyQueue;

	/**
	 * constructor, establishes and starts connection to JMS provider specified in
	 * JNDI (via jndi.properties), afterwards producer and consumer are ready
	 *
	 * @throws NamingException JNDI exceptions
	 * @throws JMSException    JMS exceptions
	 */
	public EchoRequesterNode() throws NamingException, JMSException {
		Context ctx = new InitialContext();
		ConnectionFactory factory = (ConnectionFactory) ctx.lookup("ConnectionFactory");
		this.connection = factory.createConnection();
		this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue = (Queue) ctx.lookup(Conf.QUEUE);
		this.producer = this.session.createProducer(queue);
		this.replyQueue = this.session.createTemporaryQueue();
		this.consumer = this.session.createConsumer(this.replyQueue);
		this.connection.start();
	}

	/**
	 * synchronously receives the TextMessages in an infinite loop and prints
	 * payload text to StdOut
	 *
	 * @see jakarta.jms.TextMessage
	 * @throws JMSException JMS exceptions
	 */
	public void receiveAndPrintMessages() throws JMSException {
		Message request;
		while ((request = this.consumer.receive()) != null) {
			try {
				if (request instanceof TextMessage) {
					TextMessage requestText = (TextMessage) request;
					String messageText = requestText.getText();
					System.out.println("empfangen: " + messageText);
				}
			} catch (JMSException e) {
				System.err.println(e);
			}
		}
	}

	/**
	 * creates TextMessage for JMS provider with temporary reply queue set and sends
	 * it to Destination of producer, which is configured in Conf.QUEUE
	 *
	 * @param text text to be send
	 * @throws JMSException JMS exceptions
	 */
	public void sendMessage(String text) throws JMSException {
		TextMessage message = this.session.createTextMessage();
		message.setText(text);
		message.setJMSReplyTo(this.replyQueue);
		this.producer.send(message);
	}

	/**
	 * main routine and starting point of program
	 *
	 * @param args[0] text to be send to echo service replier
	 */
	public static void main(String[] args) {
		String text = args[0];
		EchoRequesterNode node = null;
		try {
			node = new EchoRequesterNode();
			node.sendMessage(text);
			node.receiveAndPrintMessages();
		} catch (NamingException | JMSException e) {
			System.err.println(e);
		} finally {
			try {
				if ((node != null) && (node.producer != null)) {
					node.producer.close();
				}
				if ((node != null) && (node.consumer != null)) {
					node.consumer.close();
				}
				if ((node != null) && (node.session != null)) {
					node.session.close();
				}
				if ((node != null) && (node.connection != null)) {
					node.connection.close();
				}
			} catch (JMSException e) {
				System.err.println(e);
			}
		}
	}
}
