Server.java
1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
76
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();
}
}