Skip to content

Commit c1a1bd0

Browse files
committed
Added classes for Autobahn tests.
Added send method for binary data.
1 parent cad84d0 commit c1a1bd0

File tree

3 files changed

+250
-13
lines changed

3 files changed

+250
-13
lines changed

example/AutobahnClientTest.java

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.net.URI;
5+
6+
import net.tootallnate.websocket.Draft;
7+
import net.tootallnate.websocket.WebSocket;
8+
import net.tootallnate.websocket.WebSocketClient;
9+
import net.tootallnate.websocket.drafts.Draft_10;
10+
import net.tootallnate.websocket.drafts.Draft_17;
11+
12+
13+
public class AutobahnClientTest extends WebSocketClient {
14+
15+
public AutobahnClientTest( Draft d, URI uri) {
16+
super( d , uri );
17+
}
18+
/**
19+
* @param args
20+
*/
21+
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+
31+
try {
32+
BufferedReader sysin= new BufferedReader( new InputStreamReader( System.in ) );
33+
34+
35+
/*First of the thinks a programmer might want to change*/
36+
Draft d = new Draft_17();
37+
String clientname = "Client";
38+
39+
String protocol= "ws";
40+
String host= "localhost";
41+
int port = 9001;
42+
43+
String serverlocation = protocol+"://"+host+":"+port;
44+
String line = "";
45+
AutobahnClientTest e ;
46+
URI uri = null;
47+
String perviousline = "";
48+
String nextline = null;
49+
Integer start = null;
50+
Integer end = null;
51+
52+
while( !line.contains( "ex" ) ){
53+
try {
54+
if(nextline!= null){
55+
line = nextline;
56+
nextline = null;
57+
WebSocket.DEBUG = false;
58+
}
59+
else{
60+
System.out.print(">");
61+
line = sysin.readLine();
62+
WebSocket.DEBUG = true;
63+
}
64+
if(line.equals( "l" )){
65+
line = perviousline;
66+
}
67+
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] );
72+
}
73+
if( start != null && end != null ){
74+
if(start > end){
75+
start = null;
76+
end = null;
77+
}
78+
else{
79+
nextline = "r "+start;
80+
start++;
81+
if( spl.length == 3 )
82+
continue;
83+
}
84+
}
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" )){
92+
try {
93+
d = ( Draft ) Class.forName( "Draft_"+spl[1] ).getConstructor( ).newInstance( );
94+
} catch ( Exception ex){
95+
System.out.println( "Could not change draft"+ ex );
96+
}
97+
}
98+
if( uri == null){
99+
System.out.println("Do not understand the input.");
100+
continue;
101+
}
102+
System.out.println("//////////////////////Exec: "+uri.getQuery());
103+
e = new AutobahnClientTest( d , uri );
104+
Thread t = new Thread( e );
105+
t.start();
106+
try {
107+
t.join(5000);
108+
} catch ( InterruptedException e1 ) {
109+
e1.printStackTrace();
110+
}
111+
} catch ( ArrayIndexOutOfBoundsException e1 ) {
112+
System.out.println("Bad Input r 1, u 1, d 10, ex");
113+
}
114+
catch (IllegalArgumentException e2) {
115+
e2.printStackTrace();
116+
}
117+
118+
}
119+
} catch ( ArrayIndexOutOfBoundsException e ) {
120+
System.out.println("Missing server uri");
121+
}
122+
catch (IllegalArgumentException e) {
123+
e.printStackTrace();
124+
System.out.println("URI should look like ws://localhost:8887 or wss://echo.websocket.org");
125+
} catch ( IOException e ) {
126+
e.printStackTrace();
127+
}
128+
System.exit( 0 );
129+
}
130+
131+
@Override
132+
public void onMessage( String message ) {
133+
try {
134+
send( message );
135+
} catch ( IOException e ) {
136+
e.printStackTrace();
137+
}
138+
139+
}
140+
141+
@Override
142+
public void onMessage( WebSocket conn , byte[] blob ) {
143+
try {
144+
conn.send( blob );
145+
} catch ( IOException e ) {
146+
e.printStackTrace();
147+
}
148+
}
149+
150+
@Override
151+
public void onError( Exception ex ) {
152+
System.out.println("Error: "+ex.toString());
153+
}
154+
155+
@Override
156+
public void onOpen( ) {
157+
}
158+
159+
@Override
160+
public void onClose( ) {
161+
}
162+
163+
}

example/AutobahnServerTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.IOException;
2+
import java.nio.channels.NotYetConnectedException;
3+
4+
import net.tootallnate.websocket.WebSocket;
5+
import net.tootallnate.websocket.WebSocketServer;
6+
7+
import net.tootallnate.websocket.Draft;
8+
import net.tootallnate.websocket.drafts.Draft_10;
9+
import net.tootallnate.websocket.drafts.Draft_17;
10+
11+
public class AutobahnServerTest extends WebSocketServer {
12+
13+
public AutobahnServerTest(int port, Draft d ) {
14+
super( port );
15+
}
16+
@Override
17+
public void onClientOpen( WebSocket conn ) {
18+
}
19+
20+
@Override
21+
public void onClientClose( WebSocket conn ) {
22+
}
23+
24+
@Override
25+
public void onClientMessage( WebSocket conn , String message ) {
26+
}
27+
28+
@Override
29+
public void onError( WebSocket conn , Exception ex ) {
30+
System.out.println("Error:");
31+
ex.printStackTrace();
32+
}
33+
34+
@Override
35+
public void onMessage( WebSocket conn , String message ) {
36+
try {
37+
conn.send( message );
38+
} catch ( IOException e ) {
39+
e.printStackTrace();
40+
}
41+
}
42+
@Override
43+
public void onMessage( WebSocket conn , byte[] blob ) {
44+
try {
45+
conn.send( blob );
46+
} catch ( IOException e ) {
47+
e.printStackTrace();
48+
}
49+
}
50+
51+
public static void main( String[] args ) {
52+
int port;
53+
try {
54+
port = new Integer( args[0] );
55+
} catch ( Exception e ) {
56+
System.out.println("No port specified. Defaulting to 9001");
57+
port = 9002;
58+
}
59+
new AutobahnServerTest( port, new Draft_17() ).start();
60+
}
61+
62+
}

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.charset.Charset;
88
import java.security.NoSuchAlgorithmException;
99
import java.util.ArrayList;
10+
import java.util.Collection;
1011
import java.util.List;
1112

1213
import net.tootallnate.websocket.Draft.HandshakeState;
@@ -323,21 +324,32 @@ public void close() {
323324
* False if some of the text had to be buffered to be sent later.
324325
* @throws IOException
325326
*/
326-
public boolean send(String text) throws IOException {
327-
if (!this.handshakeComplete) throw new NotYetConnectedException();
328-
if ( text == null ) throw new NullPointerException( "Cannot send 'null' data to a WebSocket." );
329-
boolean mask = role == Role.CLIENT;
330-
List<Framedata> frames = draft.createFrames ( text , mask );
331-
if( frames.isEmpty () ){
332-
return true;
333-
}
334-
boolean sendall = true;
335-
for( Framedata f : frames ){
336-
sendall = sendFrame ( f ); //TODO high frequently calls to sendFrame are inefficient.
337-
}
338-
return sendall;
327+
public boolean send( String text ) throws IOException, IllegalArgumentException, NotYetConnectedException {
328+
if ( text == null ) throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocket." );
329+
return send( draft.createFrames ( text , role == Role.CLIENT ) );
330+
}
331+
332+
//TODO there should be a send for bytebuffers
333+
public boolean send( byte[] bytes ) throws IOException, IllegalArgumentException, NotYetConnectedException {
334+
if ( bytes == null ) throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocket." );
335+
return send( draft.createFrames ( bytes , role == Role.CLIENT ) );
336+
}
337+
338+
339+
private boolean send( Collection<Framedata> frames ) throws IOException{ //TODO instead of throwing or returning an error this method maybe should block on queue jams
340+
if (!this.handshakeComplete) throw new NotYetConnectedException();
341+
if( frames.isEmpty () ){
342+
return true;
343+
}
344+
boolean sendall = true;
345+
for( Framedata f : frames ){
346+
sendall &= sendFrame ( f ); //TODO high frequently calls to sendFrame are inefficient.
347+
}
348+
return sendall;
339349
}
340350

351+
352+
341353
public boolean sendFrame( Framedata framedata ) throws IOException{
342354
ByteBuffer b = draft.createBinaryFrame ( framedata );
343355
// See if we have any backlog that needs to be sent first

0 commit comments

Comments
 (0)