package ist.ass2;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;


public class NCBServer {
		private static ServerSocket serverSocket = null;
		private static Socket socket = null;
		private static final int PORT = 6565;
		private static final int maxClients = 10;
		private static final clientThread[] threads = new clientThread[maxClients];
		
		public static void main(String[] args){
			try{
				serverSocket = new ServerSocket(PORT);
			} catch(IOException e){
				e.printStackTrace();
			}
			
			while(true){
				try {
					socket = serverSocket.accept();
					PrintStream os = new PrintStream(socket.getOutputStream());
					os.println("hi");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				int i = 0;
				for(i = 0; i < maxClients; i++){
					if(threads[i] == null){
						threads[i] = new clientThread(socket, threads);
						threads[i].start();
						break;
					}
				}
				
				if(i == maxClients){
					PrintStream os;
					try {
						os = new PrintStream(socket.getOutputStream());
						os.println("Server too busy. Try later.");
						os.close();
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		
}

class clientThread extends Thread{
	
	private DataInputStream is = null;
	private PrintStream os = null;
	private Socket socket = null;
	private final clientThread[] threads;
	private int maxClients;
	
	public clientThread(Socket socket, clientThread[] threads){
		this.socket = socket;
		this.threads = threads;
		maxClients = threads.length;
	}
	
	public void run(){
	
		int maxClients = this.maxClients;
		clientThread[] threads = this.threads;
		
		try{
			is = new DataInputStream(socket.getInputStream());
			os = new PrintStream(socket.getOutputStream());
			
			while(true){
				//noch unklar
				break;
			}
			
			is.close();
			os.close();
			socket.close();
		} catch (IOException e){
			e.printStackTrace();
		}


	}
	
	
}

