|
| 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 | +} |
0 commit comments