1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.codehaus.spice.netserve.connection.impl; |
9 |
| |
10 |
| import java.io.IOException; |
11 |
| import java.io.InterruptedIOException; |
12 |
| import java.net.Socket; |
13 |
| |
14 |
| import org.codehaus.spice.netserve.connection.impl.AcceptorConfig; |
15 |
| import org.codehaus.spice.netserve.connection.impl.AcceptorMonitor; |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| class ConnectionAcceptor |
24 |
| implements Runnable |
25 |
| { |
26 |
| |
27 |
| |
28 |
| |
29 |
| private final AcceptorConfig m_config; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| private final AcceptorMonitor m_monitor; |
35 |
| |
36 |
| private boolean m_started; |
37 |
| |
38 |
| private boolean m_active; |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| private Thread m_thread; |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
36
| ConnectionAcceptor( final AcceptorConfig config,
|
54 |
| final AcceptorMonitor monitor ) |
55 |
| { |
56 |
36
| if( null == config )
|
57 |
| { |
58 |
3
| throw new NullPointerException( "config" );
|
59 |
| } |
60 |
33
| if( null == monitor )
|
61 |
| { |
62 |
3
| throw new NullPointerException( "monitor" );
|
63 |
| } |
64 |
30
| m_config = config;
|
65 |
30
| m_monitor = monitor;
|
66 |
30
| m_monitor.acceptorCreated( m_config.getName(),
|
67 |
| m_config.getServerSocket() ); |
68 |
| } |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
26
| synchronized boolean hasStarted()
|
76 |
| { |
77 |
26
| return m_started;
|
78 |
| } |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
24
| void close( final long timeout )
|
84 |
| { |
85 |
24
| synchronized( this )
|
86 |
| { |
87 |
24
| m_active = false;
|
88 |
24
| m_monitor.acceptorClosing( m_config.getName(),
|
89 |
| m_config.getServerSocket() ); |
90 |
24
| m_thread.interrupt();
|
91 |
24
| try
|
92 |
| { |
93 |
24
| wait( timeout );
|
94 |
| } |
95 |
| catch( InterruptedException e ) |
96 |
| { |
97 |
| |
98 |
| } |
99 |
| } |
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
24
| public void run()
|
106 |
| { |
107 |
| |
108 |
24
| synchronized( this )
|
109 |
| { |
110 |
24
| m_started = true;
|
111 |
24
| m_active = true;
|
112 |
24
| m_thread = Thread.currentThread();
|
113 |
| } |
114 |
24
| while( isRunning() )
|
115 |
| { |
116 |
133262
| m_monitor.serverSocketListening( m_config.getName(),
|
117 |
| m_config.getServerSocket() ); |
118 |
133262
| try
|
119 |
| { |
120 |
133262
| final Socket socket = m_config.getServerSocket().accept();
|
121 |
6
| if( isRunning() )
|
122 |
| { |
123 |
3
| m_config.getHandler().handleConnection( socket );
|
124 |
| } |
125 |
| else |
126 |
| { |
127 |
3
| try
|
128 |
| { |
129 |
3
| socket.close();
|
130 |
| } |
131 |
| catch( final Exception e ) |
132 |
| { |
133 |
| |
134 |
| } |
135 |
| } |
136 |
| } |
137 |
| catch( final InterruptedIOException iioe ) |
138 |
| { |
139 |
| |
140 |
| } |
141 |
| catch( final IOException ioe ) |
142 |
| { |
143 |
8256
| m_monitor.errorAcceptingConnection( m_config.getName(), ioe );
|
144 |
| } |
145 |
| } |
146 |
| |
147 |
24
| shutdownServerSocket();
|
148 |
24
| synchronized( this )
|
149 |
| { |
150 |
24
| m_thread = null;
|
151 |
24
| notifyAll();
|
152 |
| } |
153 |
| } |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
30
| void shutdownServerSocket()
|
159 |
| { |
160 |
30
| try
|
161 |
| { |
162 |
30
| m_config.getServerSocket().close();
|
163 |
| } |
164 |
| catch( final IOException ioe ) |
165 |
| { |
166 |
3
| m_monitor.errorClosingServerSocket( m_config.getName(), ioe );
|
167 |
| } |
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
133310
| synchronized boolean isRunning()
|
176 |
| { |
177 |
133310
| return m_active;
|
178 |
| } |
179 |
| } |