|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| package org.jconfig.server; |
|
7 |
| |
|
8 |
| import java.io.IOException; |
|
9 |
| import java.net.ServerSocket; |
|
10 |
| import java.net.Socket; |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| public class BaseServer extends Thread { |
|
19 |
| |
|
20 |
| private boolean active = false; |
|
21 |
| private ServerSocket socket; |
|
22 |
| private ThreadPool pool; |
|
23 |
| private Class clazz; |
|
24 |
| private ServerContext serverContext; |
|
25 |
| |
|
26 |
0
| public BaseServer(int port,Class handler,ServerContext serverContext) {
|
|
27 |
0
| this.serverContext = serverContext;
|
|
28 |
0
| try {
|
|
29 |
0
| socket = new ServerSocket(port);
|
|
30 |
0
| pool = new ThreadPool();
|
|
31 |
0
| active = true;
|
|
32 |
| |
|
33 |
| |
|
34 |
0
| clazz = handler;
|
|
35 |
| } |
|
36 |
| catch (IOException ie) { |
|
37 |
0
| ie.printStackTrace();
|
|
38 |
| } |
|
39 |
| } |
|
40 |
| |
|
41 |
0
| public void run() {
|
|
42 |
0
| while (active) {
|
|
43 |
0
| try {
|
|
44 |
0
| Socket clientSocket = socket.accept();
|
|
45 |
0
| clientSocket.setTcpNoDelay(true);
|
|
46 |
0
| handleConnection(clientSocket);
|
|
47 |
| } catch (IOException ie) { |
|
48 |
0
| ie.printStackTrace();
|
|
49 |
| } |
|
50 |
| } |
|
51 |
| } |
|
52 |
| |
|
53 |
0
| private void handleConnection(Socket clientSocket) throws IOException {
|
|
54 |
| |
|
55 |
0
| WorkerThread wt = pool.getWorker();
|
|
56 |
| |
|
57 |
0
| try {
|
|
58 |
0
| ProtocolHandler handler = (ProtocolHandler)clazz.newInstance();
|
|
59 |
0
| wt.execute(clientSocket,handler,serverContext);
|
|
60 |
| } |
|
61 |
| catch (Exception e) { |
|
62 |
0
| e.printStackTrace();
|
|
63 |
| } |
|
64 |
| |
|
65 |
0
| pool.releaseWorkerThread(wt);
|
|
66 |
| } |
|
67 |
| |
|
68 |
0
| public void shutdown() {
|
|
69 |
0
| active = false;
|
|
70 |
| } |
|
71 |
| |
|
72 |
0
| public void setAsDaemon(boolean isDaemon) {
|
|
73 |
0
| super.setDaemon(isDaemon);
|
|
74 |
| } |
|
75 |
| |
|
76 |
| } |