package serverprocesses;
import java.util.Scanner; 
/** Bill Byrne */

class incomingMessagesThread extends Thread
{ 
    static String portNumber;
    incomingMessagesThread(String threadName) { super(threadName); }
    public void run() 
    { 
 	while (true)
	{
            System.out.println(Thread.currentThread().getName()+ " Enter Port: ");  
            ServerProcesses.port = ServerProcesses.scan.next();
            System.out.println(Thread.currentThread().getName()+ " Enter Message: ");  
            ServerProcesses.message = ServerProcesses.scan.next();           

            portNumber = ServerProcesses.port;
            switch(portNumber) 
            {
              case "http":  { synchronized(ServerProcesses.httpSemaphore)  { ServerProcesses.httpSemaphore.notify();  } } break; 
              case "https": { synchronized(ServerProcesses.httpsSemaphore) { ServerProcesses.httpsSemaphore.notify(); } } break;
              default:      { System.out.println(Thread.currentThread().getName()+ " Service: " + portNumber + " is not running!!!: "); }
            }
	} 
    }
}

class httpThread extends Thread
{
    httpThread(String threadName) { super(threadName); }
    public void run() 
    { 
 	while (true)
	{
            System.out.println(Thread.currentThread().getName()+ " Going to Wait!!!");
            synchronized(ServerProcesses.httpSemaphore) { try { ServerProcesses.httpSemaphore.wait(); } catch ( InterruptedException e) { }; }
            System.out.println(Thread.currentThread().getName()+ " handling message: " +  ServerProcesses.message);
	} 
    }
}

class httpsThread extends Thread
{
    httpsThread(String threadName) { super(threadName); }
    public void run() 
    { 
 	while (true)
	{
            System.out.println(Thread.currentThread().getName()+ " Going to Wait!!!");
            synchronized(ServerProcesses.httpsSemaphore) { try { ServerProcesses.httpsSemaphore.wait(); } catch ( InterruptedException e) { }; }
            System.out.println(Thread.currentThread().getName()+ " handling message: " +  ServerProcesses.message);
	} 
    }
}

public class ServerProcesses 
{
    static Scanner scan = new Scanner(System.in);
    static public Integer httpSemaphore  = 1;
    static public Integer httpsSemaphore = 2;
    static public String port;
    static public String message;
    public static void main(String[] args) 
    {
        // create threads
        Thread incomingMessagesThread = new incomingMessagesThread("Incoming Messages");
        Thread httpThread             = new httpThread("http server");
        Thread httpsThread            = new httpsThread("https server");
        //Start the threads
        httpThread.start();
        httpsThread.start();
        incomingMessagesThread.start();

    }
}
