<commit_before>package nl.dulsoft.iot.mqtt.paho;

import nl.dulsoft.iot.mqtt.service.MessageException;
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.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.inject.Default;
import java.util.Optional;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Default
public class PahoClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(PahoClient.class);

    private static final String BROKER = "tcp://192.168.1.150";
    private static final String CLIENT_ID = "IoT-Client";
    private MqttClient mqttClient;

    public void send(String topic, String message) throws MqttException {
        LOGGER.info("send({}, {})", topic, message);
        getMqttClient().publish(topic, message.getBytes(), 2, false);
    }

    MqttClient getMqttClient() throws MqttException {
        if (!Optional.ofNullable(this.mqttClient)
            .isPresent()) {
            setMqttClient(createClient());
        }

        if (!this.mqttClient.isConnected()) {
            LOGGER.info("Connect MQTT client");
            this.mqttClient.connect(createConnectionOptions());
        }

        return this.mqttClient;
    }

    void setMqttClient(MqttClient mqttClient) {
        this.mqttClient = mqttClient;
    }

    MqttClient createClient() {
        LOGGER.info("Create MQTT client");
        try {
            return new MqttClient(BROKER, CLIENT_ID, new MemoryPersistence());
        } catch (MqttException e) {
            throw new MessageException(
                String.format("Cannot create client %s for %s",
                    CLIENT_ID, BROKER));
        }
    }

    private MqttConnectOptions createConnectionOptions() {
        MqttConnectOptions options = new MqttConnectOptions();<commit_msg>The MQTT client ID is based on the thread ID<commit_after>package nl.dulsoft.iot.mqtt.paho;

import nl.dulsoft.iot.mqtt.service.MessageException;
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.persist.MemoryPersistence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.inject.Default;
import java.util.Optional;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Default
public class PahoClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(PahoClient.class);

    private static final String BROKER = "tcp://192.168.1.150";
    private static final String CLIENT_ID_FMT = "IoT-Client_%d";
    private MqttClient mqttClient;

    public void send(String topic, String message) throws MqttException {
        LOGGER.info("send {}, {}", topic, message);
        getMqttClient().publish(topic, message.getBytes(), 2, false);
        LOGGER.info("sent {}, {}", topic, message);
    }

    MqttClient getMqttClient() throws MqttException {
        if (!Optional.ofNullable(this.mqttClient)
            .isPresent()) {
            setMqttClient(createClient());
        }

        if (!this.mqttClient.isConnected()) {
            LOGGER.info("Connect MQTT client");
            this.mqttClient.connect(createConnectionOptions());
        }

        return this.mqttClient;
    }

    void setMqttClient(MqttClient mqttClient) {
        this.mqttClient = mqttClient;
    }

    MqttClient createClient() {
        String clientId = createClientId();
        LOGGER.info("Create MQTT client: {}", clientId);
        try {
            return new MqttClient(BROKER, clientId, new MemoryPersistence());
        } catch (MqttException e) {
            throw new MessageException(
                String.format("Cannot create client %s for %s",
                    clientId, BROKER));
        }
    }

    private String createClientId() {
        return String.format(CLIENT_ID_FMT, Thread.currentThread().getId());
    }

    private MqttConnectOptions createConnectionOptions() {
        MqttConnectOptions options = new MqttConnectOptions();
{'fraction_non_alphanumeric': 0.11337278106508876, 'fraction_numerical': 0.008047337278106508, 'mean_word_length': 3.0673724735322425, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368648', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1315, 'n_words': 255}