Blame view

JChatServer/src/pad/prac2/Server.java 1.42 KB
1
2
3
4
5
6
package pad.prac2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
7
8
9
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
10
11
12
13

public class Server
{
	private MyServerSocket ss;
14
15
16
17
	private BufferedReader stdin;
	private PrintWriter stdout;
	private int roomSize;
	private boolean run;
18
19
20
21
22
23
24
	private Worker[] rWorkerPool;
	private Worker[] wWorkerPool;

	private ConcurrentHashMap<String,MySocket> activeConnections;

	public Server(String ip, int port, int rS)
25
26
27
	{
		try
		{
28
29
30
31
32
33
			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];
34
35
36
37
38
39
		}
		catch(IOException ioExc)
		{
			System.out.println("TCP: Error initializating server socket");
		}
	}
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

	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();
	}
76
}