Skip to content

Commit fa0eaa3

Browse files
committed
changed buffer handling from byte[] to Bytebuffer
removed io operations from user thread to selector thread continued code reformatting & reordering
1 parent 194418f commit fa0eaa3

11 files changed

Lines changed: 1513 additions & 1518 deletions

example/AutobahnClientTest.java

Lines changed: 68 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,122 +6,116 @@
66
import net.tootallnate.websocket.Draft;
77
import net.tootallnate.websocket.WebSocket;
88
import net.tootallnate.websocket.WebSocketClient;
9-
import net.tootallnate.websocket.drafts.Draft_10;
109
import net.tootallnate.websocket.drafts.Draft_17;
1110

12-
1311
public class AutobahnClientTest extends WebSocketClient {
1412

15-
public AutobahnClientTest( Draft d, URI uri) {
16-
super( d , uri );
13+
public AutobahnClientTest( Draft d , URI uri ) {
14+
super( uri, d );
1715
}
1816
/**
1917
* @param args
2018
*/
2119
public static void main( String[] args ) {
22-
System.out.println("Testutility to profile/test this implementation using the Autobahn suit.\n");
23-
System.out.println("Type 'r <casenumber>' to run a testcase. Example: r 1");
24-
System.out.println("Type 'r <first casenumber> <last casenumber>' to run a testcase. Example: r 1 295");
25-
System.out.println("Type 'u' to update the test results.");
26-
System.out.println("Type 'ex' to terminate the program.");
27-
System.out.println("During sequences of cases the debugoutput will be turned of.");
28-
29-
System.out.println("You can now enter in your commands:");
30-
20+
System.out.println( "Testutility to profile/test this implementation using the Autobahn suit.\n" );
21+
System.out.println( "Type 'r <casenumber>' to run a testcase. Example: r 1" );
22+
System.out.println( "Type 'r <first casenumber> <last casenumber>' to run a testcase. Example: r 1 295" );
23+
System.out.println( "Type 'u' to update the test results." );
24+
System.out.println( "Type 'ex' to terminate the program." );
25+
System.out.println( "During sequences of cases the debugoutput will be turned of." );
26+
27+
System.out.println( "You can now enter in your commands:" );
28+
3129
try {
32-
BufferedReader sysin= new BufferedReader( new InputStreamReader( System.in ) );
33-
30+
BufferedReader sysin = new BufferedReader( new InputStreamReader( System.in ) );
3431

3532
/*First of the thinks a programmer might want to change*/
3633
Draft d = new Draft_17();
37-
String clientname = "Client";
38-
39-
String protocol= "ws";
40-
String host= "localhost";
34+
String clientname = "tootallnate/websocket";
35+
36+
String protocol = "ws";
37+
String host = "localhost";
4138
int port = 9001;
42-
43-
String serverlocation = protocol+"://"+host+":"+port;
39+
40+
String serverlocation = protocol + "://" + host + ":" + port;
4441
String line = "";
45-
AutobahnClientTest e ;
42+
AutobahnClientTest e;
4643
URI uri = null;
4744
String perviousline = "";
4845
String nextline = null;
49-
Integer start = null;
46+
Integer start = null;
5047
Integer end = null;
51-
52-
while( !line.contains( "ex" ) ){
48+
49+
while ( !line.contains( "ex" ) ) {
5350
try {
54-
if(nextline!= null){
51+
if( nextline != null ) {
5552
line = nextline;
5653
nextline = null;
5754
WebSocket.DEBUG = false;
58-
}
59-
else{
60-
System.out.print(">");
55+
} else {
56+
System.out.print( ">" );
6157
line = sysin.readLine();
6258
WebSocket.DEBUG = true;
6359
}
64-
if(line.equals( "l" )){
60+
if( line.equals( "l" ) ) {
6561
line = perviousline;
6662
}
6763
String[] spl = line.split( " " );
68-
if(line.startsWith( "r" )){
69-
if( spl.length == 3){
70-
start = new Integer( spl[1] );
71-
end = new Integer( spl[2] );
64+
if( line.startsWith( "r" ) ) {
65+
if( spl.length == 3 ) {
66+
start = new Integer( spl[ 1 ] );
67+
end = new Integer( spl[ 2 ] );
7268
}
73-
if( start != null && end != null ){
74-
if(start > end){
69+
if( start != null && end != null ) {
70+
if( start > end ) {
7571
start = null;
7672
end = null;
77-
}
78-
else{
79-
nextline = "r "+start;
73+
} else {
74+
nextline = "r " + start;
8075
start++;
8176
if( spl.length == 3 )
8277
continue;
8378
}
8479
}
85-
uri = URI.create( serverlocation+"/runCase?case="+spl[1]+"&agent="+clientname );
86-
87-
}
88-
else if(line.startsWith( "u" )){
89-
uri = URI.create( serverlocation+"/updateReports?agent="+clientname );
90-
}
91-
else if(line.startsWith( "d" )){
80+
uri = URI.create( serverlocation + "/runCase?case=" + spl[ 1 ] + "&agent=" + clientname );
81+
82+
} else if( line.startsWith( "u" ) ) {
83+
uri = URI.create( serverlocation + "/updateReports?agent=" + clientname );
84+
} else if( line.startsWith( "d" ) ) {
9285
try {
93-
d = ( Draft ) Class.forName( "Draft_"+spl[1] ).getConstructor( ).newInstance( );
94-
} catch ( Exception ex){
95-
System.out.println( "Could not change draft"+ ex );
86+
d = (Draft) Class.forName( "Draft_" + spl[ 1 ] ).getConstructor().newInstance();
87+
} catch ( Exception ex ) {
88+
System.out.println( "Could not change draft" + ex );
9689
}
9790
}
98-
if( uri == null){
99-
System.out.println("Do not understand the input.");
91+
if( uri == null ) {
92+
System.out.println( "Do not understand the input." );
10093
continue;
10194
}
102-
System.out.println("//////////////////////Exec: "+uri.getQuery());
103-
e = new AutobahnClientTest( d , uri );
95+
System.out.println( "//////////////////////Exec: " + uri.getQuery() );
96+
e = new AutobahnClientTest( d, uri );
10497
Thread t = new Thread( e );
10598
t.start();
10699
try {
107-
t.join(5000);
100+
t.join( 15000 );
101+
108102
} catch ( InterruptedException e1 ) {
109103
e1.printStackTrace();
104+
} finally {
105+
e.close();
110106
}
111107
} catch ( ArrayIndexOutOfBoundsException e1 ) {
112-
System.out.println("Bad Input r 1, u 1, d 10, ex");
113-
}
114-
catch (IllegalArgumentException e2) {
108+
System.out.println( "Bad Input r 1, u 1, d 10, ex" );
109+
} catch ( IllegalArgumentException e2 ) {
115110
e2.printStackTrace();
116111
}
117-
112+
118113
}
119114
} catch ( ArrayIndexOutOfBoundsException e ) {
120-
System.out.println("Missing server uri");
121-
}
122-
catch (IllegalArgumentException e) {
115+
System.out.println( "Missing server uri" );
116+
} catch ( IllegalArgumentException e ) {
123117
e.printStackTrace();
124-
System.out.println("URI should look like ws://localhost:8887 or wss://echo.websocket.org");
118+
System.out.println( "URI should look like ws://localhost:8887 or wss://echo.websocket.org" );
125119
} catch ( IOException e ) {
126120
e.printStackTrace();
127121
}
@@ -132,32 +126,33 @@ else if(line.startsWith( "d" )){
132126
public void onMessage( String message ) {
133127
try {
134128
send( message );
135-
} catch ( IOException e ) {
129+
} catch ( InterruptedException e ) {
136130
e.printStackTrace();
137131
}
138-
132+
139133
}
140-
134+
141135
@Override
142-
public void onMessage( WebSocket conn , byte[] blob ) {
136+
public void onMessage( WebSocket conn, byte[] blob ) {
143137
try {
144138
conn.send( blob );
145-
} catch ( IOException e ) {
139+
} catch ( InterruptedException e ) {
146140
e.printStackTrace();
147141
}
148142
}
149143

150144
@Override
151145
public void onError( Exception ex ) {
152-
System.out.println("Error: "+ex.toString());
146+
System.out.println( "Error: " );
147+
ex.printStackTrace();
153148
}
154-
149+
155150
@Override
156-
public void onOpen( ) {
151+
public void onOpen() {
157152
}
158-
153+
159154
@Override
160-
public void onClose( ) {
155+
public void onClose() {
161156
}
162-
157+
163158
}

example/AutobahnServerTest.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,62 @@
11
import java.io.IOException;
2-
import java.nio.channels.NotYetConnectedException;
32

3+
import net.tootallnate.websocket.Draft;
44
import net.tootallnate.websocket.WebSocket;
55
import net.tootallnate.websocket.WebSocketServer;
6-
7-
import net.tootallnate.websocket.Draft;
8-
import net.tootallnate.websocket.drafts.Draft_10;
96
import net.tootallnate.websocket.drafts.Draft_17;
107

118
public class AutobahnServerTest extends WebSocketServer {
9+
private static int counter = 0;
1210

13-
public AutobahnServerTest(int port, Draft d ) {
11+
public AutobahnServerTest( int port , Draft d ) {
1412
super( port );
1513
}
14+
1615
@Override
1716
public void onClientOpen( WebSocket conn ) {
17+
counter++;
18+
System.out.println( "Opened connection number" + counter );
1819
}
1920

2021
@Override
2122
public void onClientClose( WebSocket conn ) {
2223
}
2324

2425
@Override
25-
public void onClientMessage( WebSocket conn , String message ) {
26+
public void onClientMessage( WebSocket conn, String message ) {
2627
}
2728

2829
@Override
29-
public void onError( WebSocket conn , Exception ex ) {
30-
System.out.println("Error:");
30+
public void onError( WebSocket conn, Exception ex ) {
31+
System.out.println( "Error:" );
3132
ex.printStackTrace();
3233
}
33-
34+
3435
@Override
35-
public void onMessage( WebSocket conn , String message ) {
36+
public void onMessage( WebSocket conn, String message ) {
3637
try {
3738
conn.send( message );
38-
} catch ( IOException e ) {
39+
} catch ( InterruptedException e ) {
3940
e.printStackTrace();
4041
}
4142
}
4243
@Override
43-
public void onMessage( WebSocket conn , byte[] blob ) {
44+
public void onMessage( WebSocket conn, byte[] blob ) {
4445
try {
4546
conn.send( blob );
46-
} catch ( IOException e ) {
47+
} catch ( InterruptedException e ) {
4748
e.printStackTrace();
4849
}
4950
}
50-
51+
5152
public static void main( String[] args ) {
53+
WebSocket.DEBUG = true;
5254
int port;
5355
try {
54-
port = new Integer( args[0] );
56+
port = new Integer( args[ 0 ] );
5557
} catch ( Exception e ) {
56-
System.out.println("No port specified. Defaulting to 9001");
57-
port = 9002;
58+
System.out.println( "No port specified. Defaulting to 9001" );
59+
port = 9003;
5860
}
5961
new AutobahnServerTest( port, new Draft_17() ).start();
6062
}

0 commit comments

Comments
 (0)