// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package chat.simple.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private List<ClientHandler> active;
    private ServerSocket ss;
    public Server(int port) throws IOException {
        active = new ArrayList<ClientHandler>();
        ss = new ServerSocket(12345);
    }
    public void run() throws IOException {
        while(true) {
            Socket s = ss.accept();
            ClientHandler cli = new ClientHandler(s, this);
            synchronized(active) {
                active.add(cli);
            }
            cli.start();
        }
    }
    public void sendAll(String msg) {
        synchronized(active) {
            for(ClientHandler ch : active) {
                ch.send(msg);
            }
        }
    }
    public void removeClient(ClientHandler ch) {
        synchronized(active) {
            active.remove(ch);
        }
    }
    public static void main(String[] args) throws IOException {
        Server srv = new Server(12345);
        srv.run();
    }
}
