Skip to content

Commit d23d08f

Browse files
committed
made websocket callback functions provide more informations.
InvalidDataException will now be thrown in cases a CharacterCodingException had been thrown before
1 parent 1201512 commit d23d08f

12 files changed

Lines changed: 79 additions & 77 deletions

‎example/AutobahnClientTest.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.URI;
55

66
import net.tootallnate.websocket.Draft;
7+
import net.tootallnate.websocket.Handshakedata;
78
import net.tootallnate.websocket.WebSocket;
89
import net.tootallnate.websocket.WebSocketClient;
910
import net.tootallnate.websocket.drafts.Draft_17;
@@ -149,11 +150,11 @@ public void onError( Exception ex ) {
149150
}
150151

151152
@Override
152-
public void onOpen() {
153+
public void onOpen( Handshakedata handshake ) {
153154
}
154155

155156
@Override
156-
public void onClose( int code, String reason ) {
157+
public void onClose( int code, String reason, boolean remote ) {
157158
System.out.println( "Closed: " + code + " " + reason );
158159
}
159160

‎example/AutobahnServerTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import java.net.InetAddress;
21
import java.net.InetSocketAddress;
32
import java.net.UnknownHostException;
43

54
import net.tootallnate.websocket.Draft;
5+
import net.tootallnate.websocket.Handshakedata;
66
import net.tootallnate.websocket.WebSocket;
77
import net.tootallnate.websocket.WebSocketServer;
88
import net.tootallnate.websocket.drafts.Draft_17;
@@ -11,21 +11,21 @@ public class AutobahnServerTest extends WebSocketServer {
1111
private static int counter = 0;
1212

1313
public AutobahnServerTest( int port, Draft d ) throws UnknownHostException {
14-
super( new InetSocketAddress( InetAddress.getLocalHost(), port ),d );
14+
super( new InetSocketAddress( "localhost", port ), d );
1515
}
1616

1717
public AutobahnServerTest( InetSocketAddress address, Draft d ) {
1818
super( address, d );
1919
}
2020

2121
@Override
22-
public void onClientOpen( WebSocket conn ) {
22+
public void onClientOpen( WebSocket conn, Handshakedata handshake ) {
2323
counter++;
24-
System.out.println( "Opened connection number" + counter );
24+
System.out.println( "///////////Opened connection number" + counter );
2525
}
2626

2727
@Override
28-
public void onClientClose( WebSocket conn ) {
28+
public void onClientClose( WebSocket conn, int code, String reason, boolean remote ) {
2929
}
3030

3131
@Override
@@ -56,12 +56,12 @@ public void onMessage( WebSocket conn, byte[] blob ) {
5656
}
5757

5858
public static void main( String[] args ) throws UnknownHostException {
59-
WebSocket.DEBUG = true;
59+
WebSocket.DEBUG = false;
6060
int port;
6161
try {
6262
port = new Integer( args[ 0 ] );
6363
} catch ( Exception e ) {
64-
System.out.println( "No port specified. Defaulting to 9001" );
64+
System.out.println( "No port specified. Defaulting to 9003" );
6565
port = 9003;
6666
}
6767
new AutobahnServerTest( port, new Draft_17() ).start();

‎example/ChatClient.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import javax.swing.JTextField;
1515

1616
import net.tootallnate.websocket.Draft;
17+
import net.tootallnate.websocket.Handshakedata;
1718
import net.tootallnate.websocket.WebSocket;
1819
import net.tootallnate.websocket.WebSocketClient;
1920
import net.tootallnate.websocket.drafts.Draft_10;
@@ -108,12 +109,12 @@ public void onMessage( String message ) {
108109
ta.setCaretPosition( ta.getDocument().getLength() );
109110
}
110111

111-
public void onOpen() {
112+
public void onOpen( Handshakedata handshake ) {
112113
ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
113114
ta.setCaretPosition( ta.getDocument().getLength() );
114115
}
115116

116-
public void onClose( int code, String reason ) {
117+
public void onClose( int code, String reason, boolean remote ) {
117118
ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
118119
ta.setCaretPosition( ta.getDocument().getLength() );
119120
connect.setEnabled( true );

‎example/ChatServer.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.InetSocketAddress;
66
import java.net.UnknownHostException;
77

8+
import net.tootallnate.websocket.Handshakedata;
89
import net.tootallnate.websocket.WebSocket;
910
import net.tootallnate.websocket.WebSocketServer;
1011

@@ -21,7 +22,7 @@ public ChatServer( InetSocketAddress address ) {
2122
super( address );
2223
}
2324

24-
public void onClientOpen( WebSocket conn ) {
25+
public void onClientOpen( WebSocket conn, Handshakedata handshake ) {
2526
try {
2627
this.sendToAll( conn + " entered the room!" );
2728
} catch ( InterruptedException ex ) {
@@ -30,7 +31,7 @@ public void onClientOpen( WebSocket conn ) {
3031
System.out.println( conn + " entered the room!" );
3132
}
3233

33-
public void onClientClose( WebSocket conn ) {
34+
public void onClientClose( WebSocket conn, int code, String reason, boolean remote ) {
3435
try {
3536
this.sendToAll( conn + " has left the room!" );
3637
} catch ( InterruptedException ex ) {

‎src/net/tootallnate/websocket/Charsetfunctions.java‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.nio.charset.CharsetDecoder;
88
import java.nio.charset.CodingErrorAction;
99

10+
import net.tootallnate.websocket.exeptions.InvalidDataException;
11+
1012
public class Charsetfunctions {
1113

1214
public static CodingErrorAction codingErrorAction = CodingErrorAction.REPORT;
@@ -45,16 +47,21 @@ public static String stringAscii( byte[] bytes, int offset, int length ){
4547
}
4648
}
4749

48-
public static String stringUtf8( byte[] bytes ) throws CharacterCodingException {
50+
public static String stringUtf8( byte[] bytes ) throws InvalidDataException {
4951
return stringUtf8( bytes, 0, bytes.length );
5052
}
5153

52-
public static String stringUtf8( byte[] bytes, int off, int length ) throws CharacterCodingException {
54+
public static String stringUtf8( byte[] bytes, int off, int length ) throws InvalidDataException {
5355
CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder();
5456
decode.onMalformedInput( codingErrorAction );
5557
decode.onUnmappableCharacter( codingErrorAction );
5658
//decode.replaceWith( "X" );
57-
String s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
59+
String s;
60+
try {
61+
s = decode.decode( ByteBuffer.wrap( bytes, off, length ) ).toString();
62+
} catch ( CharacterCodingException e ) {
63+
throw new InvalidDataException( CloseFrame.NO_UTF8, e );
64+
}
5865
return s;
5966
}
6067
}

‎src/net/tootallnate/websocket/CloseFrame.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.tootallnate.websocket;
22

3-
import java.nio.charset.CharacterCodingException;
4-
3+
import net.tootallnate.websocket.exeptions.InvalidDataException;
54
import net.tootallnate.websocket.exeptions.InvalidFrameException;
65

76
public interface CloseFrame extends Framedata {
@@ -80,5 +79,5 @@ public interface CloseFrame extends Framedata {
8079
public static final int BUGGYCLOSE = -2;
8180

8281
public int getCloseCode() throws InvalidFrameException;
83-
public String getMessage() throws CharacterCodingException , InvalidFrameException;
82+
public String getMessage() throws InvalidDataException;
8483
}

‎src/net/tootallnate/websocket/CloseFrameBuilder.java‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.tootallnate.websocket;
22

33
import java.nio.ByteBuffer;
4-
import java.nio.charset.CharacterCodingException;
54

65
import net.tootallnate.websocket.exeptions.InvalidDataException;
76
import net.tootallnate.websocket.exeptions.InvalidFrameException;
@@ -61,7 +60,7 @@ public int getCloseCode() {
6160
return code;
6261
}
6362

64-
private void initMessage() throws CharacterCodingException , InvalidFrameException {
63+
private void initMessage() throws InvalidDataException {
6564
if( code == CloseFrame.NOCODE ) {
6665
reason = Charsetfunctions.stringUtf8( getPayloadData() );
6766
} else {
@@ -84,11 +83,7 @@ public String toString() {
8483
public void setPayload( byte[] payload ) throws InvalidDataException {
8584
super.setPayload( payload );
8685
initCloseCode();
87-
try {
88-
initMessage();
89-
} catch ( CharacterCodingException e ) {
90-
throw new InvalidDataException( CloseFrame.NO_UTF8 );
91-
}
86+
initMessage();
9287
}
9388
@Override
9489
public byte[] getPayloadData() {

‎src/net/tootallnate/websocket/WebSocket.java‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.nio.ByteBuffer;
66
import java.nio.channels.NotYetConnectedException;
77
import java.nio.channels.SocketChannel;
8-
import java.nio.charset.CharacterCodingException;
98
import java.util.ArrayList;
109
import java.util.Collection;
1110
import java.util.List;
@@ -171,7 +170,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
171170
HandshakeBuilder response = wsl.onHandshakeRecievedAsServer( this, d, handshake );
172171
writeDirect( d.createHandshake( d.postProcessHandshakeResponseAsServer( handshake, response ), role ) );
173172
draft = d;
174-
open();
173+
open( handshake );
175174
handleRead();
176175
return;
177176
} else if( handshakestate == HandshakeState.MATCHING ) {
@@ -202,7 +201,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
202201
handshakestate = draft.acceptHandshakeAsServer( handshake );
203202

204203
if( handshakestate == HandshakeState.MATCHED ) {
205-
open();
204+
open( handshake );
206205
handleRead();
207206
} else if( handshakestate != HandshakeState.MATCHING ) {
208207
close( CloseFrame.PROTOCOL_ERROR, "the handshake did finaly not match" );
@@ -214,7 +213,7 @@ private void init( WebSocketListener listener, Draft draft, SocketChannel sockch
214213
handshake = draft.translateHandshake( socketBuffer );
215214
handshakestate = draft.acceptHandshakeAsClient( handshakerequest, handshake );
216215
if( handshakestate == HandshakeState.MATCHED ) {
217-
open();
216+
open( handshake );
218217
handleRead();
219218
} else if( handshakestate == HandshakeState.MATCHING ) {
220219
return;
@@ -313,6 +312,8 @@ public void closeDirect( int code, String message ) throws IOException {
313312
wsl.onError( this, e );
314313
closeConnection( CloseFrame.ABNROMAL_CLOSE, "generated frame is invalid", false );
315314
}
315+
} else {
316+
closeConnection( CloseFrame.NEVERCONNECTED, false );
316317
}
317318
if( code == CloseFrame.PROTOCOL_ERROR )// this endpoint found a PROTOCOL_ERROR
318319
closeConnection( code, false );
@@ -342,7 +343,7 @@ public void closeConnection( int code, String message, boolean remote ) {
342343
} catch ( IOException e ) {
343344
wsl.onError( this, e );
344345
}
345-
this.wsl.onClose( this, code, message );
346+
this.wsl.onClose( this, code, message, remote );
346347
if( draft != null )
347348
draft.reset();
348349
currentframe = null;
@@ -380,11 +381,11 @@ public void send( byte[] bytes ) throws IllegalArgumentException , NotYetConnect
380381
send( draft.createFrames( bytes, role == Role.CLIENT ) );
381382
}
382383

383-
private void send( Collection<Framedata> frames ) throws InterruptedException { // TODO instead of throwing or returning an error this method maybe should block on queue jams
384+
private void send( Collection<Framedata> frames ) throws InterruptedException {
384385
if( !this.handshakeComplete )
385386
throw new NotYetConnectedException();
386387
for( Framedata f : frames ) {
387-
sendFrame( f ); // TODO high frequently calls to sendFrame are inefficient.
388+
sendFrame( f );
388389
}
389390
}
390391

@@ -464,7 +465,7 @@ private void writeDirect( List<ByteBuffer> bufs ) throws IOException {
464465
channelWriteDirect( b );
465466
}
466467
}
467-
private void deliverMessage( Framedata d ) throws CharacterCodingException {
468+
private void deliverMessage( Framedata d ) throws InvalidDataException {
468469
if( d.getOpcode() == Opcode.TEXT ) {
469470
wsl.onMessage( this, Charsetfunctions.stringUtf8( d.getPayloadData() ) );
470471
} else if( d.getOpcode() == Opcode.BINARY ) {
@@ -476,11 +477,11 @@ private void deliverMessage( Framedata d ) throws CharacterCodingException {
476477
}
477478
}
478479

479-
private void open() throws IOException {
480+
private void open( Handshakedata d ) throws IOException {
480481
if( DEBUG )
481482
System.out.println( "open using draft: " + draft.getClass().getSimpleName() );
482483
handshakeComplete = true;
483-
wsl.onOpen( this );
484+
wsl.onOpen( this, d );
484485
}
485486

486487
public InetSocketAddress getRemoteSocketAddress() {

‎src/net/tootallnate/websocket/WebSocketAdapter.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ public void onMessage( WebSocket conn, String message ) {
2121
}
2222

2323
@Override
24-
public void onOpen( WebSocket conn ) {
24+
public void onOpen( WebSocket conn, Handshakedata handshake ) {
2525
}
2626

2727
@Override
28-
public void onClose( WebSocket conn, int code, String reason ) {
28+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
2929
}
3030

3131
@Override

‎src/net/tootallnate/websocket/WebSocketClient.java‎

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import java.nio.channels.Selector;
1010
import java.nio.channels.SocketChannel;
1111
import java.nio.channels.UnresolvedAddressException;
12-
import java.nio.charset.CharacterCodingException;
1312
import java.util.Iterator;
1413
import java.util.Set;
1514

@@ -156,31 +155,27 @@ protected void interruptableRun() {
156155
Set<SelectionKey> keys = selector.selectedKeys();
157156
Iterator<SelectionKey> i = keys.iterator();
158157
while ( i.hasNext() ) {
159-
try {
160-
key = i.next();
161-
i.remove();
162-
if( key.isReadable() ) {
163-
conn.handleRead();
164-
}
165-
if( !key.isValid() ) {
166-
continue;
167-
}
168-
if( key.isWritable() ) {
158+
key = i.next();
159+
i.remove();
160+
if( key.isReadable() ) {
161+
conn.handleRead();
162+
}
163+
if( !key.isValid() ) {
164+
continue;
165+
}
166+
if( key.isWritable() ) {
167+
conn.flush();
168+
}
169+
if( key.isConnectable() ) {
170+
try {
171+
finishConnect();
172+
} catch ( InterruptedException e ) {
173+
conn.close( CloseFrame.NEVERCONNECTED );// report error to only
174+
break;
175+
} catch ( InvalidHandshakeException e ) {
176+
conn.close( e ); // http error
169177
conn.flush();
170178
}
171-
if( key.isConnectable() ) {
172-
try {
173-
finishConnect();
174-
} catch ( InterruptedException e ) {
175-
conn.close( CloseFrame.NEVERCONNECTED );// report error to only
176-
break;
177-
} catch ( InvalidHandshakeException e ) {
178-
conn.close( e ); // http error
179-
conn.flush();
180-
}
181-
}
182-
} catch ( CharacterCodingException e ) {
183-
conn.close( CloseFrame.NO_UTF8 );
184179
}
185180
}
186181
}
@@ -261,8 +256,8 @@ public void onMessage( WebSocket conn, String message ) {
261256
* @param conn
262257
*/
263258
@Override
264-
public void onOpen( WebSocket conn ) {
265-
onOpen();
259+
public void onOpen( WebSocket conn, Handshakedata handshake ) {
260+
onOpen( handshake );
266261
}
267262

268263
/**
@@ -271,9 +266,9 @@ public void onOpen( WebSocket conn ) {
271266
* @param conn
272267
*/
273268
@Override
274-
public void onClose( WebSocket conn, int code, String reason ) {
269+
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
275270
thread.interrupt();
276-
onClose( code, reason );
271+
onClose( code, reason, remote );
277272
}
278273

279274
/**
@@ -293,7 +288,7 @@ public void onWriteDemand( WebSocket conn ) {
293288

294289
// ABTRACT METHODS /////////////////////////////////////////////////////////
295290
public abstract void onMessage( String message );
296-
public abstract void onOpen();
297-
public abstract void onClose( int code, String reason );
291+
public abstract void onOpen( Handshakedata handshakedata );
292+
public abstract void onClose( int code, String reason, boolean remote );
298293
public abstract void onError( Exception ex );
299294
}

0 commit comments

Comments
 (0)