11package net .tootallnate .websocket ;
22
33import java .io .IOException ;
4+ import java .net .ConnectException ;
45import java .net .InetSocketAddress ;
56import java .net .URI ;
67import java .nio .ByteBuffer ;
78import java .nio .channels .SelectionKey ;
89import java .nio .channels .Selector ;
910import java .nio .channels .SocketChannel ;
10- import java .security .MessageDigest ;
11- import java .security .NoSuchAlgorithmException ;
1211import java .util .Iterator ;
13- import java .util .Random ;
1412import java .util .Set ;
1513import java .util .concurrent .LinkedBlockingQueue ;
1614
17- import net .tootallnate .websocket .WebSocket .Role ;
18- import net .tootallnate .websocket .drafts .*;
15+ import net .tootallnate .websocket .drafts .Draft_10 ;
1916import net .tootallnate .websocket .exeptions .InvalidHandshakeException ;
2017
2118/**
2724 * <var>send</var> method.
2825 * @author Nathan Rajlich
2926 */
30- public abstract class WebSocketClient extends WebSocketAdapter implements Runnable {
27+ public abstract class WebSocketClient extends WebSocketAdapter implements Runnable {
3128
3229
3330 // INSTANCE PROPERTIES /////////////////////////////////////////////////////
@@ -55,23 +52,15 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
5552 * The Draft of the WebSocket protocol the Client is adhering to.
5653 */
5754 private Draft draft = null ;
58- /**
59- * Number 1 used in handshake
60- */
61- private int number1 = 0 ;
62- /**
63- * Number 2 used in handshake
64- */
65- private int number2 = 0 ;
66- /**
67- * Key3 used in handshake
68- */
69- private byte [] key3 = null ;
7055
7156 // CONSTRUCTORS ////////////////////////////////////////////////////////////
7257 public WebSocketClient (URI serverURI ) {
7358 this (serverURI , new Draft_10 () );
7459 }
60+
61+ public WebSocketClient ( Draft draft , URI serverURI ) {
62+ this (serverURI , draft );
63+ }
7564
7665 /**
7766 * Constructs a WebSocketClient instance and sets it to the connect to the
@@ -117,9 +106,8 @@ public void connect() {
117106 /**
118107 * Calls <var>close</var> on the underlying SocketChannel, which in turn
119108 * closes the socket connection, and ends the client socket thread.
120- * @throws IOException When socket related I/O errors occur.
121109 */
122- public void close () throws IOException
110+ public void close ()
123111 {
124112 if (running )
125113 {
@@ -165,9 +153,6 @@ public void releaseAndInitialize()
165153 selector = null ;
166154 running = false ;
167155 draft = null ;
168- number1 = 0 ;
169- number2 = 0 ;
170- key3 = null ;
171156 }
172157
173158 private boolean tryToConnect (InetSocketAddress remote ) {
@@ -190,7 +175,7 @@ private boolean tryToConnect(InetSocketAddress remote) {
190175 }
191176
192177 } catch (IOException ex ) {
193- onIOError (conn , ex );
178+ onError (conn , ex );
194179 return false ;
195180 }
196181
@@ -199,7 +184,18 @@ private boolean tryToConnect(InetSocketAddress remote) {
199184
200185 // Runnable IMPLEMENTATION /////////////////////////////////////////////////
201186 public void run () {
202- running = tryToConnect (new InetSocketAddress (uri .getHost (), getPort ()));
187+ try {
188+ running = tryToConnect ( new InetSocketAddress ( uri .getHost () , getPort () ) );
189+ } catch ( IllegalArgumentException e ) {//
190+ onError (conn , e );
191+ conn .close ();
192+ return ;
193+ }
194+ catch (SecurityException e ) {
195+ onError (conn , e );
196+ conn .close ();
197+ return ;
198+ }
203199
204200 while (this .running ) {
205201 SelectionKey key = null ;
@@ -220,16 +216,18 @@ public void run() {
220216 conn .handleRead ();
221217 }
222218 }
223- } catch (IOException ex ) {
224- if ( key != null )
225- key .cancel ();
226- onIOError (conn , ex );
227- } catch (Exception ex ) {
228- onError ( ex );
219+ } catch (InvalidHandshakeException e ) {
220+ onError ( e );
221+ close ();
222+ return ;
223+ }
224+ catch (IOException e ) {
225+ //if(e instanceof ConnectException == false)
226+ onError ( e );
227+ close ();
228+ return ;
229229 }
230230 }
231-
232- //System.err.println("WebSocketClient thread ended!");
233231 }
234232
235233 private int getPort () {
@@ -255,12 +253,12 @@ private void sendHandshake() throws IOException, InvalidHandshakeException {
255253 }
256254 int port = getPort ();
257255 String host = uri .getHost () + (port != WebSocket .DEFAULT_PORT ? ":" + port : "" );
258- String origin = "x" ; // TODO: Make 'origin' configurable
256+ // String origin = "x"; // TODO: Make 'origin' configurable
259257
260258 HandshakedataImpl1 handshake = new HandshakedataImpl1 ();
261259 handshake .setResourceDescriptor ( path );
262260 handshake .put ( "Host" , host );
263- handshake .put ( "Origin" , origin );
261+ // handshake.put ( "Origin" , origin );
264262 conn .startHandshake ( handshake );
265263 }
266264
@@ -295,15 +293,15 @@ public void onClose(WebSocket conn)
295293 * Calls subclass' implementation of <var>onIOError</var>.
296294 * @param conn
297295 */
298- public void onIOError (WebSocket conn , IOException ex )
296+ public void onError (WebSocket conn , IOException ex )
299297 {
300298 releaseAndInitialize ();
301- onIOError (ex );
299+ onError (ex );
302300 }
303301
304302 // ABTRACT METHODS /////////////////////////////////////////////////////////
305303 public abstract void onMessage (String message );
306304 public abstract void onOpen ();
307305 public abstract void onClose ();
308- public abstract void onIOError ( IOException ex );
306+ public abstract void onError ( Exception ex );
309307}
0 commit comments