package examples.example5_chat_OO_library;

import java.io.IOException;

import simpleNetworking.Server;

/**
 * TODO Put here a description of what this class does.
 *
 * @author mutchler.
 *         Created May 12, 2009.
 */
public class ServerRunner implements Runnable {
	private Server server;

	/**
	 * TODO Put here a description of what this constructor does.
	 *
	 * @param server
	 */
	public ServerRunner(Server server) {
		this.server = server;
	}

	@Override
	public void run() {
		while (true) {
			try {
				for (int k = 0; k < 2; ++k) {
					String messageFromClient = this.server.readLineIfReady(k);
					if (messageFromClient != null) {
						this.server.writeLine(messageFromClient, k);
					}
				}
				
			} catch (IndexOutOfBoundsException exception) {
				System.out.println("Somehow the ServerRunner started without first obtaining its two Clients, it seems.");
				exception.printStackTrace();
				break;
				
			} catch (IOException exception) {
				System.out.println("The Server had an error in communicating with a Client.");
				exception.printStackTrace();	// Keep running.  Maybe not the best choice.
			}
		}
	}
}