Server.java 1.42 KB
package pad.prac2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

public class Server
{
	private MyServerSocket ss;
	private BufferedReader stdin;
	private PrintWriter stdout;
	private int roomSize;
	private boolean run;
	
	private Worker[] rWorkerPool;
	private Worker[] wWorkerPool;
	
	private ConcurrentHashMap<String,MySocket> activeConnections;
	
	public Server(String ip, int port, int rS)
	{
		try
		{
			ss = new MyServerSocket();
			stdout = new PrintWriter(System.out);
			stdin = new BufferedReader(new InputStreamReader(System.in));
			roomSize = rS;
			rWorkerPool = new Worker[roomSize];
			wWorkerPool = new Worker[roomSize];
		}
		catch(IOException ioExc)
		{
			System.out.println("TCP: Error initializating server socket");
		}
	}
	
	public void startServer()
	{
		
	}
	
	public void startWorker(Worker r, Worker w, MySocket s)
	{
		if(r == null)
		{
			r = new Worker(s,null,stdout);
		}
		r.awake();
		if(w == null)
		{
			w = new Worker(s,stdin,null);
		}
		w.awake();
	}
	
	public void finishWorkers()
	{
		Collection<Worker> worerke= activeConnections.values();
		Iterator<MySocket> it = sockets.iterator();
		while(it.hasNext())
		{
			it.next().close();
		}
	}
	
	public void killServer()
	{
		run = false;
		ss.close();
		finishWorkers();
	}
}