1 package org.sourceforge.jemm.comm.server;
2
3 import java.util.Map;
4 import java.util.concurrent.CountDownLatch;
5 import java.util.concurrent.ExecutorService;
6
7 import org.sourceforge.jemm.comm.connection.Connection;
8 import org.sourceforge.jemm.comm.shared.RPCHandler;
9 import org.sourceforge.jemm.comm.shared.RPCHandlerListener;
10
11
12
13
14
15
16
17
18
19 public class ServerThread implements RPCHandlerListener {
20
21 protected CountDownLatch shutdownLatch = new CountDownLatch(1);
22
23 protected volatile boolean connected;
24
25 protected RPCClientId clientId;
26
27 protected RPCServer server;
28
29 protected RPCHandler rpcHandler;
30
31 protected Connection connection;
32
33
34
35
36
37
38
39
40
41
42 public ServerThread(RPCServer server, Connection connection,
43 RPCClientId clientId,Map<Class<?>,Object> offeredIfs,ExecutorService threadPool) {
44 this.server = server;
45 this.connection = connection;
46 this.clientId = clientId;
47 this.connected = false;
48
49 rpcHandler = new RPCHandler(false,connection,offeredIfs,threadPool,clientId);
50 rpcHandler.setHandlerListener(this);
51 }
52
53
54
55
56
57 public void start() {
58 connected = true;
59 rpcHandler.initialise();
60 server.notifyNewClient(clientId,connection.getConnectionName());
61 rpcHandler.start();
62 }
63
64
65
66
67 public void connectionTerminated() {
68 if(connected)
69 shutdown();
70 }
71
72
73
74
75 public void shutdown() {
76 if(connected) {
77 connected = false;
78 rpcHandler.close();
79 server.clientDisconnected(clientId);
80 shutdownLatch.countDown();
81 }
82 }
83
84
85
86
87
88
89 public void waitForShutdown() {
90 try {
91 shutdownLatch.await();
92 } catch (InterruptedException e) {
93
94 }
95 }
96
97
98
99
100
101 public Object getClientIF(Class<?> ifClass) {
102 return rpcHandler.getRemoteIF(ifClass);
103 }
104 }