|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.codehaus.spice.netserve.connection.impl; |
|
9 |
| |
|
10 |
| import java.net.ServerSocket; |
|
11 |
| import java.util.Hashtable; |
|
12 |
| import java.util.Map; |
|
13 |
| import org.codehaus.spice.netserve.connection.RequestHandler; |
|
14 |
| import org.codehaus.spice.netserve.connection.SocketAcceptorManager; |
|
15 |
| import org.codehaus.spice.netserve.connection.impl.AcceptorConfig; |
|
16 |
| import org.codehaus.spice.netserve.connection.impl.AcceptorMonitor; |
|
17 |
| import org.codehaus.spice.netserve.connection.impl.ConnectionAcceptor; |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| public class DefaultAcceptorManager |
|
34 |
| implements SocketAcceptorManager |
|
35 |
| { |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| private final Map m_acceptors = new Hashtable(); |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| private AcceptorMonitor m_monitor = NullAcceptorMonitor.MONITOR; |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| private int m_soTimeout = 1000; |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| private int m_shutdownTimeout; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
24
| public void setMonitor( final AcceptorMonitor monitor )
|
|
66 |
| { |
|
67 |
24
| m_monitor = monitor;
|
|
68 |
| } |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
12
| public void setSoTimeout( final int soTimeout )
|
|
77 |
| { |
|
78 |
12
| m_soTimeout = soTimeout;
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
15
| public void setShutdownTimeout( final int shutdownTimeout )
|
|
88 |
| { |
|
89 |
15
| m_shutdownTimeout = shutdownTimeout;
|
|
90 |
| } |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
33
| protected int getShutdownTimeout()
|
|
98 |
| { |
|
99 |
33
| return m_shutdownTimeout;
|
|
100 |
| } |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
24
| public void shutdownAcceptors()
|
|
107 |
| { |
|
108 |
24
| final String[] names;
|
|
109 |
24
| synchronized( m_acceptors )
|
|
110 |
| { |
|
111 |
24
| names = (String[])m_acceptors.keySet().toArray( new String[ 0 ] );
|
|
112 |
| } |
|
113 |
24
| for( int i = 0; i < names.length; i++ )
|
|
114 |
| { |
|
115 |
6
| disconnect( names[ i ] );
|
|
116 |
| } |
|
117 |
| } |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
27
| public void connect( final String name,
|
|
131 |
| final ServerSocket socket, |
|
132 |
| final RequestHandler handler ) |
|
133 |
| throws Exception |
|
134 |
| { |
|
135 |
27
| if( null == name )
|
|
136 |
| { |
|
137 |
3
| throw new NullPointerException( "name" );
|
|
138 |
| } |
|
139 |
24
| if( null == socket )
|
|
140 |
| { |
|
141 |
3
| throw new NullPointerException( "socket" );
|
|
142 |
| } |
|
143 |
21
| if( null == handler )
|
|
144 |
| { |
|
145 |
3
| throw new NullPointerException( "handler" );
|
|
146 |
| } |
|
147 |
| |
|
148 |
18
| if( 0 == socket.getSoTimeout() )
|
|
149 |
| { |
|
150 |
15
| socket.setSoTimeout( m_soTimeout );
|
|
151 |
| } |
|
152 |
| |
|
153 |
18
| final ConnectionAcceptor acceptor;
|
|
154 |
18
| synchronized( m_acceptors )
|
|
155 |
| { |
|
156 |
18
| if( isConnected( name ) )
|
|
157 |
| { |
|
158 |
3
| final String message =
|
|
159 |
| "Connection already exists with name " + name; |
|
160 |
3
| throw new IllegalArgumentException( message );
|
|
161 |
| } |
|
162 |
| |
|
163 |
15
| final AcceptorConfig config = new AcceptorConfig( name, socket, handler );
|
|
164 |
15
| acceptor = new ConnectionAcceptor( config, getMonitor() );
|
|
165 |
15
| m_acceptors.put( name, acceptor );
|
|
166 |
| } |
|
167 |
| |
|
168 |
15
| final Thread thread =
|
|
169 |
| new Thread( acceptor, "Acceptor[" + name + "]" ); |
|
170 |
15
| thread.start();
|
|
171 |
15
| while( !acceptor.hasStarted() )
|
|
172 |
| { |
|
173 |
11
| Thread.sleep( 5 );
|
|
174 |
| } |
|
175 |
| } |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
63
| public boolean isConnected( final String name )
|
|
184 |
| { |
|
185 |
63
| return m_acceptors.containsKey( name );
|
|
186 |
| } |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
18
| public void disconnect( final String name )
|
|
195 |
| { |
|
196 |
18
| final ConnectionAcceptor acceptor =
|
|
197 |
| (ConnectionAcceptor)m_acceptors.remove( name ); |
|
198 |
18
| if( null == acceptor )
|
|
199 |
| { |
|
200 |
3
| final String message = "No connection with name " + name;
|
|
201 |
3
| throw new IllegalArgumentException( message );
|
|
202 |
| } |
|
203 |
| |
|
204 |
15
| acceptor.close( getShutdownTimeout() );
|
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
33
| protected AcceptorMonitor getMonitor()
|
|
213 |
| { |
|
214 |
33
| return m_monitor;
|
|
215 |
| } |
|
216 |
| } |