@@ -161,8 +161,8 @@ public WebSocketServer( InetSocketAddress address , int decodercount , List<Draf
161161 */
162162 public void start () {
163163 if ( selectorthread != null )
164- throw new IllegalStateException ( "Already started" );
165- new Thread ( this ).start ();
164+ throw new IllegalStateException ( getClass (). getName () + " can only be started once. " );
165+ new Thread ( this ).start ();;
166166 }
167167
168168 /**
@@ -171,15 +171,20 @@ public void start() {
171171 * freeing the port the server was bound to.
172172 *
173173 * @throws IOException
174- * When socket related I/O errors occur.
174+ * When {@link ServerSocketChannel}.close throws an IOException
175+ * @throws InterruptedException
175176 */
176- public void stop () throws IOException {
177+ public void stop () throws IOException , InterruptedException {
177178 synchronized ( connections ) {
178179 for ( WebSocket ws : connections ) {
179180 ws .close ( CloseFrame .NORMAL );
180181 }
181182 }
182183 selectorthread .interrupt ();
184+ selectorthread .join ();
185+ for ( WebSocketWorker w : decoders ) {
186+ w .interrupt ();
187+ }
183188 this .server .close ();
184189
185190 }
@@ -228,9 +233,11 @@ public List<Draft> getDraft() {
228233
229234 // Runnable IMPLEMENTATION /////////////////////////////////////////////////
230235 public void run () {
231- if ( selectorthread != null )
232- throw new IllegalStateException ( "This instance of " + getClass ().getSimpleName () + " can only be started once the same time." );
233- selectorthread = Thread .currentThread ();
236+ synchronized ( this ) {
237+ if ( selectorthread != null )
238+ throw new IllegalStateException ( getClass ().getName () + " can only be started once." );
239+ selectorthread = Thread .currentThread ();
240+ }
234241 selectorthread .setName ( "WebsocketSelector" + selectorthread .getId () );
235242 try {
236243 server = ServerSocketChannel .open ();
@@ -388,12 +395,11 @@ private void handleIOException( WebSocket conn, IOException ex ) {
388395 private void handleFatal ( WebSocket conn , RuntimeException e ) {
389396 onError ( conn , e );
390397 try {
391- selector . close ();
398+ stop ();
392399 } catch ( IOException e1 ) {
393400 onError ( null , e1 );
394- }
395- for ( WebSocketWorker w : decoders ) {
396- w .interrupt ();
401+ } catch ( InterruptedException e1 ) {
402+ onError ( null , e1 );
397403 }
398404 }
399405
0 commit comments