<commit_before>/**
    Copyright 2013 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package net.sf.xenqtt.examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.xenqtt.client.AsyncClientListener;
import net.sf.xenqtt.client.AsyncMqttClient;
import net.sf.xenqtt.client.MqttClient;
import net.sf.xenqtt.client.PublishMessage;
import net.sf.xenqtt.client.Subscription;
import net.sf.xenqtt.message.ConnectReturnCode;
import net.sf.xenqtt.message.QoS;

import org.apache.log4j.Logger;

/**
 * Builds music catalogs from years gone by.
 */
public class MusicSubscriberAsync {

	private static final Logger log = Logger.getLogger(MusicSubscriberAsync.class);

	public static void main(String... args) throws Throwable {
		final List<String> catalog = new ArrayList<String>();
		AsyncClientListener listener = new AsyncClientListener() {

			@Override
			public void publishReceived(MqttClient client, PublishMessage message) {
				catalog.add(message.getPayloadString());
				message.ack();
			}

			@Override
			public void disconnected(MqttClient client, Throwable cause, boolean reconnecting) {
				if (cause != null) {
					log.error("Disconnected from the broker due to an exception.", cause);
				} else {
					log.info("Disconnecting from the broker.");
				}

				if (reconnecting) {
					log.info("Attempting to reconnect to the broker.");
				}
			}

			@Override
			public void connected(MqttClient client, ConnectReturnCode returnCode) {
				if (returnCode != ConnectReturnCode.ACCEPTED) {
					log.error("Unable to connect to the broker. Reason: " + returnCode);
					// TODO - Handle this per your application's requirements
				}
			}

			@Override
			public void published(MqttClient client, PublishMessage message) {
				// We do not publish so this should never be called, in theory ;).
			}

			@Override
			public void subscribed(MqttClient client, Subscription[] requestedSubscriptions, Subscription[] grantedSubscriptions, boolean requestsGranted) {
				if (!requestsGranted) {
					log.error("Unable to subscribe to the following subscriptions: " + Arrays.toString(requestedSubscriptions));
					// TODO - Handle this per-application requirements
				}

				log.debug("Granted subscriptions: " + Arrays.toString(grantedSubscriptions));
			}

			@Override
			public void unsubscribed(MqttClient client, String[] topics) {
				log.debug("Unsubscribed from the following topics: " + Arrays.toString(topics));
				// TODO - Implement application-specific logic here, if any
			}

		};

		// Build your client. This client is a synchronous one so all interaction with the broker will block until said interaction completes.
		AsyncMqttClient client = new AsyncMqttClient("tcp://mqtt.broker:1883", listener, 5);

		// Connect to the broker with a specific client ID. Only if the broker accepted the connection shall we proceed.
		ConnectReturnCode returnCode = client.connect("musicLover", true);
		if (returnCode != ConnectReturnCode.ACCEPTED) {
			log.error("Unable to connect to the MQTT broker. Reason: " + returnCode);
			return;<commit_msg>removed TODO comments from code examples<commit_after>/**
    Copyright 2013 <NAME>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package net.sf.xenqtt.examples;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.sf.xenqtt.client.AsyncClientListener;
import net.sf.xenqtt.client.AsyncMqttClient;
import net.sf.xenqtt.client.MqttClient;
import net.sf.xenqtt.client.PublishMessage;
import net.sf.xenqtt.client.Subscription;
import net.sf.xenqtt.message.ConnectReturnCode;
import net.sf.xenqtt.message.QoS;

import org.apache.log4j.Logger;

/**
 * Builds music catalogs from years gone by.
 */
public class MusicSubscriberAsync {

	private static final Logger log = Logger.getLogger(MusicSubscriberAsync.class);

	public static void main(String... args) throws Throwable {
		final List<String> catalog = new ArrayList<String>();
		AsyncClientListener listener = new AsyncClientListener() {

			@Override
			public void publishReceived(MqttClient client, PublishMessage message) {
				catalog.add(message.getPayloadString());
				message.ack();
			}

			@Override
			public void disconnected(MqttClient client, Throwable cause, boolean reconnecting) {
				if (cause != null) {
					log.error("Disconnected from the broker due to an exception.", cause);
				} else {
					log.info("Disconnecting from the broker.");
				}

				if (reconnecting) {
					log.info("Attempting to reconnect to the broker.");
				}
			}

			@Override
			public void connected(MqttClient client, ConnectReturnCode returnCode) {
				if (returnCode != ConnectReturnCode.ACCEPTED) {
					log.error("Unable to connect to the broker. Reason: " + returnCode);
				}
			}

			@Override
			public void published(MqttClient client, PublishMessage message) {
				// We do not publish so this should never be called, in theory ;).
			}

			@Override
			public void subscribed(MqttClient client, Subscription[] requestedSubscriptions, Subscription[] grantedSubscriptions, boolean requestsGranted) {
				if (!requestsGranted) {
					log.error("Unable to subscribe to the following subscriptions: " + Arrays.toString(requestedSubscriptions));
				}

				log.debug("Granted subscriptions: " + Arrays.toString(grantedSubscriptions));
			}

			@Override
			public void unsubscribed(MqttClient client, String[] topics) {
				log.debug("Unsubscribed from the following topics: " + Arrays.toString(topics));
			}

		};

		// Build your client. This client is a synchronous one so all interaction with the broker will block until said interaction completes.
		AsyncMqttClient client = new AsyncMqttClient("tcp://mqtt.broker:1883", listener, 5);

		// Connect to the broker with a specific client ID. Only if the broker accepted the connection shall we proceed.
		ConnectReturnCode returnCode = client.connect("musicLover", true);
		if (returnCode != ConnectReturnCode.ACCEPTED) {
			log.error("Unable to connect to the MQTT broker. Reason: " + returnCode);
			return;
{'fraction_non_alphanumeric': 0.09071670428893905, 'fraction_numerical': 0.0039503386004514675, 'mean_word_length': 3.9712482468443198, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '6815244', 'n_tokens_mistral': 2253, 'n_tokens_neox': 1959, 'n_words': 659}