<commit_before>/* 
 * Copyright (c) 2009, 2012 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation and/or initial documentation
 */

package org.eclipse.paho.sample.mqttv3app;

import java.io.IOException;
import java.sql.Timestamp;

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.MqttDefaultFilePersistence;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;


/**
 * This sample application demonstrates basic usage
 * of the MQTT v3 Client api.
 * 
 * It can be run in one of two modes:
 *  - as a publisher, sending a single message to a topic on the server
 *  - as a subscriber, listening for messages from the server
 *
 */
public class Sample implements MqttCallback {
	Object waiter = new Object();
	
	/**
	 * The main entry point of the sample.
	 * 
	 * This method handles parsing the arguments specified on the
	 * command-line before performing the specified action.
	 */
	public static void main(String[] args) {
		
		// Default settings:
		boolean quietMode = false;
		String action = "publish";
		String topic = "";
		String message = "Message from MQTTv3 Java client";
		int qos = 2;
		String broker = "m2m.eclipse.org";
		int port = 1883;
		String clientId = null;
		
		// Parse the arguments - 
		for (int i=0; i<args.length; i++) {
			// Check this is a valid argument
			if (args[i].length() == 2 && args[i].startsWith("-")) {
				char arg = args[i].charAt(1);
				// Handle the no-value arguments
				switch(arg) {
				case 'h': case '?':	printHelp(); return;
				case 'q': quietMode = true;	continue;
				}
				// Validate there is a value associated with the argument
				if (i == args.length -1 || args[i+1].charAt(0) == '-') {
					System.out.println("Missing value for argument: "+args[i]);
					printHelp();
					return;
				}
				switch(arg) {
				case 'a': action = args[++i];                 break;
				case 't': topic = args[++i];                  break;
				case 'm': message = args[++i];                break;
				case 's': qos = Integer.parseInt(args[++i]);  break;
				case 'b': broker = args[++i];                 break;
				case 'p': port = Integer.parseInt(args[++i]); break;
				case 'i': clientId = args[++i];               break;
				default: 
					System.out.println("Unrecognised argument: "+args[i]);
					printHelp(); 
					return;
				}
			} else {
				System.out.println("Unrecognised argument: "+args[i]);
				printHelp(); 
				return;
			}
		}
		
		// Validate the provided arguments
		if (!action.equals("publish") && !action.equals("subscribe")) {
			System.out.println("Invalid action: "+action);
			printHelp();
			return;
		}
		if (qos < 0 || qos > 2) {
			System.out.println("Invalid QoS: "+qos);
			printHelp();
			return;
		}
		if (topic.equals("")) {
			// Set the default topic according to the specified action
			if (action.equals("publish")) {
				topic = "Sample/Java/v3";
			} else {
				topic = "Sample/#";
			}
		}
		
		String url = "tcp://"+broker+":"+port;
		
		if (clientId == null || clientId.equals("")) {
			clientId = "SampleJavaV3_"+action;
		}

		// With a valid set of arguments, the real work of 
		// driving the client API can begin
		
		try {
			// Create an instance of the Sample client wrapper
			Sample sampleClient = new Sample(url,clientId,quietMode);
			
			// Perform the specified action
			if (action.equals("publish")) {
				sampleClient.publish(topic,qos,message.getBytes());
			} else if (action.equals("subscribe")) {
				sampleClient.subscribe(topic,qos);
			}
		} catch(MqttException me) {
			me.printStackTrace();
		}
	}
    
	// Private instance variables
	private MqttClient client;
	private String brokerUrl;
	private boolean quietMode;
	private MqttConnectOptions conOpt;
	
	/**
	 * Constructs an instance of the sample client wrapper
	 * @param brokerUrl the url to connect to
	 * @param clientId the client id to connect with
	 * @param quietMode whether debug should be printed to standard out
	 * @throws MqttException
	 */
    public Sample(String brokerUrl, String clientId, boolean quietMode) throws MqttException {
    	this.brokerUrl = brokerUrl;
    	this.quietMode = quietMode;
    	
    	//This sample stores files in a temporary directory...
    	//..a real application ought to store them somewhere 
    	//where they are not likely to get deleted or tampered with<commit_msg>Fix for 390928 + add additional info in the event of an error. <commit_after>/* 
 * Copyright (c) 2009, 2012 IBM Corp.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    <NAME> - initial API and implementation and/or initial documentation
 */

package org.eclipse.paho.sample.mqttv3app;

import java.io.IOException;
import java.sql.Timestamp;

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.MqttDefaultFilePersistence;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;


/**
 * This sample application demonstrates basic usage
 * of the MQTT v3 Client api.
 * 
 * It can be run in one of two modes:
 *  - as a publisher, sending a single message to a topic on the server
 *  - as a subscriber, listening for messages from the server
 *
 */
public class Sample implements MqttCallback {
	
	/**
	 * The main entry point of the sample.
	 * 
	 * This method handles parsing the arguments specified on the
	 * command-line before performing the specified action.
	 */
	public static void main(String[] args) {
		
		// Default settings:
		boolean quietMode = false;
		String action = "publish";
		String topic = "";
		String message = "Message from MQTTv3 Java client";
		int qos = 2;
		String broker = "m2m.eclipse.org";
		int port = 1883;
		String clientId = null;
		
		// Parse the arguments - 
		for (int i=0; i<args.length; i++) {
			// Check this is a valid argument
			if (args[i].length() == 2 && args[i].startsWith("-")) {
				char arg = args[i].charAt(1);
				// Handle the no-value arguments
				switch(arg) {
				case 'h': case '?':	printHelp(); return;
				case 'q': quietMode = true;	continue;
				}
				// Validate there is a value associated with the argument
				if (i == args.length -1 || args[i+1].charAt(0) == '-') {
					System.out.println("Missing value for argument: "+args[i]);
					printHelp();
					return;
				}
				switch(arg) {
				case 'a': action = args[++i];                 break;
				case 't': topic = args[++i];                  break;
				case 'm': message = args[++i];                break;
				case 's': qos = Integer.parseInt(args[++i]);  break;
				case 'b': broker = args[++i];                 break;
				case 'p': port = Integer.parseInt(args[++i]); break;
				case 'i': clientId = args[++i];               break;
				default: 
					System.out.println("Unrecognised argument: "+args[i]);
					printHelp(); 
					return;
				}
			} else {
				System.out.println("Unrecognised argument: "+args[i]);
				printHelp(); 
				return;
			}
		}
		
		// Validate the provided arguments
		if (!action.equals("publish") && !action.equals("subscribe")) {
			System.out.println("Invalid action: "+action);
			printHelp();
			return;
		}
		if (qos < 0 || qos > 2) {
			System.out.println("Invalid QoS: "+qos);
			printHelp();
			return;
		}
		if (topic.equals("")) {
			// Set the default topic according to the specified action
			if (action.equals("publish")) {
				topic = "Sample/Java/v3";
			} else {
				topic = "Sample/#";
			}
		}
		
		String url = "tcp://"+broker+":"+port;
		
		if (clientId == null || clientId.equals("")) {
			clientId = "SampleJavaV3_"+action;
		}

		// With a valid set of arguments, the real work of 
		// driving the client API can begin
		
		try {
			// Create an instance of the Sample client wrapper
			Sample sampleClient = new Sample(url,clientId,quietMode);
			
			// Perform the specified action
			if (action.equals("publish")) {
				sampleClient.publish(topic,qos,message.getBytes());
			} else if (action.equals("subscribe")) {
				sampleClient.subscribe(topic,qos);
			}
		} catch(MqttException me) {
			System.out.println("reason "+me.getReasonCode());
			System.out.println("msg "+me.getMessage());
			System.out.println("loc "+me.getLocalizedMessage());
			System.out.println("casue "+me.getCause());
			System.out.println("excep "+me);
			me.printStackTrace();
		}
	}
    
	// Private instance variables
	private MqttClient client;
	private String brokerUrl;
	private boolean quietMode;
	private MqttConnectOptions conOpt;
	
	/**
	 * Constructs an instance of the sample client wrapper
	 * @param brokerUrl the url to connect to
	 * @param clientId the client id to connect with
	 * @param quietMode whether debug should be printed to standard out
	 * @throws MqttException
	 */
    public Sample(String brokerUrl, String clientId, boolean quietMode) throws MqttException {
    	this.brokerUrl = brokerUrl;
    	this.quietMode = quietMode;
    	
    	//This sample stores files in a temporary directory...
    	//..a real application ought to store them somewhere 
    	//where they are not likely to get deleted or tampered with
{'fraction_non_alphanumeric': 0.12552468518888668, 'fraction_numerical': 0.008394963022186688, 'mean_word_length': 3.064581640942323, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 7, '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': '1569493', 'n_tokens_mistral': 3545, 'n_tokens_neox': 3052, 'n_words': 943}