import Six.Sixp.Client.*;

import java.util.concurrent.*;
import java.util.List;
import java.util.Iterator;
import java.util.Vector;
import java.io.IOException;

public class Program {
	
	public static void main(String[] args) {
		run(args);
	}

	public static void run(String[] args) {
		ConcurrentLinkedQueue<ICommand> queue = new ConcurrentLinkedQueue<ICommand>();
		SIXPClient sc = new SIXPClient(args, queue);
		CommandProcessor cp = new CommandProcessor(queue);
		Thread producer = new Thread(sc);
		Thread consumer = new Thread(cp);
		producer.start();
		consumer.start();
		
		System.out.println("Press (q) to clear queue and stop, press (s) to save queue to sqlite before exit, press (p) to print size of queue!");
		boolean quit = false;
        while (!quit)
        {
			int c = -1;
			try {
				c = System.in.read();
			} catch (IOException e) {
				e.printStackTrace();
			}
							
			if (c == 113) { // q key
				System.out.println("Stopping program!");
				queue.clear();
				cp.Stop();
				sc.Stop();
				quit = true;
				break;
			}
			else if (c == 115) { // s key
				System.out.println("Stopping program!");
				System.out.println("Waiting until the queue has written all data to sqlitedb!");
				System.out.println("Queue size: " + queue.size());
				cp.Stop();
				sc.Stop();
				quit = true;
				break;
			}
			else if (c == 112) // p key
			{
				System.out.println("Queue size: " + queue.size());
			}
        }
		
	}
}
