/*
 * Copyright 2013 Barzan Mozafari
 *
 * 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 dbseer.middleware.test;

import com.esotericsoftware.minlog.Log;
import dbseer.middleware.client.MiddlewareClient;
import org.apache.commons.cli.*;

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Created by Dong Young Yoon on 12/1/15.
 *
 * The test class for testing the middleware client.
 */
public class MiddlewareClientTest
{
	public static void main(String[] args)
	{
		ExecutorService clientExecutor = Executors.newSingleThreadExecutor();

		// set up logger
		Log.set(Log.LEVEL_DEBUG);

		// handle command-line options
		CommandLineParser clParser = new DefaultParser();
		Options options = new Options();

		Option hostOption = Option.builder("h")
				.hasArg()
				.argName("HOST")
				.required(true)
				.desc("middleware hostname")
				.build();

		Option portOption = Option.builder("p")
				.hasArg()
				.argName("PORT")
				.required(true)
				.desc("middleware port")
				.build();

		Option logOption = Option.builder("d")
				.hasArg()
				.argName("FILE")
				.required(true)
				.desc("path to print logs")
				.build();

		Option helpOption = Option.builder("?")
				.longOpt("help")
				.required(false)
				.desc("print this message")
				.build();

		options.addOption(hostOption);
		options.addOption(portOption);
		options.addOption(logOption);
		options.addOption(helpOption);

		HelpFormatter formatter = new HelpFormatter();
		try
		{
			CommandLine line = clParser.parse(options, args);
			if (line.hasOption("?"))
			{
				formatter.printHelp("MiddlewareClientTest", options, true);
				return;
			}

			int port;
			String host, logPath;

			port = Integer.parseInt(line.getOptionValue("p"));
			host = line.getOptionValue("h");
			logPath = line.getOptionValue("d");

			MiddlewareClient client = new MiddlewareClient(host, port, logPath);
			client.setLogLevel(Log.LEVEL_DEBUG);

			Future clientFuture = clientExecutor.submit(client);
			Thread.sleep(1000);
			Scanner scanner = new Scanner(System.in);
			while (scanner.hasNext())
			{
				String input = scanner.nextLine();
				if (input.equalsIgnoreCase("s"))
				{
					client.startMonitoring();
				}
				else if (input.equalsIgnoreCase("t"))
				{
					client.stopMonitoring();
				}
				else if (input.equalsIgnoreCase("q"))
				{
					clientExecutor.shutdownNow();
					break;
				}
			}
			clientFuture.get();
		}
		catch (ParseException e)
		{
			formatter.printHelp("MiddlewareClientTest", options, true);
			Log.error(e.getMessage());
		}
		catch (Exception e)
		{
			Log.error(e.getMessage());
		}

	}
}

@@ -1320,24 +1320,322 @@
 Options();%0A%0A
+%09%09Option idOption = Option.builder(%22i%22)%0A%09%09%09%09.hasArg()%0A%09%09%09%09.argName(%22ID%22)%0A%09%09%09%09.required(true)%0A%09%09%09%09.desc(%22middleware user id%22)%0A%09%09%09%09.build();%0A%0A%09%09Option passwordOption = Option.builder(%22w%22)%0A%09%09%09%09.hasArg()%0A%09%09%09%09.argName(%22PASSWORD%22)%0A%09%09%09%09.required(true)%0A%09%09%09%09.desc(%22middleware user password%22)%0A%09%09%09%09.build();%0A%0A
 %09%09Option hos
@@ -2182,16 +2182,84 @@
 ild();%0A%0A
+%09%09options.addOption(idOption);%0A%09%09options.addOption(passwordOption);%0A
 %09%09option
@@ -2645,18 +2645,106 @@
  logPath
+, id, password;%0A%0A%09%09%09id = line.getOptionValue(%22i%22);%0A%09%09%09password = line.getOptionValue(%22w%22)
 ;
-%0A
 %0A%09%09%09port
@@ -2916,16 +2916,30 @@
 eClient(
+id, password, 
 host, po

add support for simple id/password authentication.