package com.github.noxan.aves.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.HashSet;
import java.util.Set;

import com.github.noxan.aves.net.Connection;
import com.github.noxan.aves.net.SocketConnection;

public class SocketServer implements Server, Runnable {
    private String host;
    private int port;

    private Set<Connection> connections;

    private ServerSocket server;
    private Thread serverThread;

    public SocketServer() {
	this("0.0.0.0", 1666);
    }

    public SocketServer(String host, int port) {
	this.host = host;
	this.port = port;
	connections = new HashSet<Connection>();
    }

    @Override
    public void start() {
	try {
	    server = new ServerSocket();
	    server.setSoTimeout(1000);
	    server.bind(new InetSocketAddress(host, port));
	    serverThread = new Thread(this);
	    serverThread.start();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }

    @Override
    public void run() {
	while (true) {
	    try {
		Socket socket = server.accept();
		Connection connection = new SocketConnection(this, socket);
		connections.add(connection);
		connection.start();
	    } catch (SocketTimeoutException e) {
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }

    @Override
    public String getHost() {
	return host;
    }

    @Override
    public int getPort() {
	return port;
    }
}

@@ -439,16 +439,52 @@
  port;%0A%0A
+    private ServerHandler handler;%0A%0A
     priv
@@ -608,16 +608,37 @@
 tServer(
+ServerHandler handler
 ) %7B%0A%09thi
@@ -654,16 +654,25 @@
 0%22, 1666
+, handler
 );%0A    %7D
@@ -718,16 +718,39 @@
 int port
+, ServerHandler handler
 ) %7B%0A%09thi
@@ -783,16 +783,41 @@
 = port;%0A
+%09this.handler = handler;%0A
 %09connect

add serverhandler to socketserver and add parameter to constructor