Advertisement
Guest User

Test Client

a guest
May 29th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. import java.net.InetSocketAddress;
  2. import java.util.concurrent.Future;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import org.eclipse.jetty.spdy.SPDYClient;
  6. import org.eclipse.jetty.spdy.api.DataInfo;
  7. import org.eclipse.jetty.spdy.api.SPDY;
  8. import org.eclipse.jetty.spdy.api.Session;
  9. import org.eclipse.jetty.spdy.api.Stream;
  10. import org.eclipse.jetty.spdy.api.StreamFrameListener;
  11. import org.eclipse.jetty.spdy.api.SynInfo;
  12.  
  13. /**
  14.  * Client. <br/>
  15.  * Put <code>-Xbootclasspath/p:lib/spdy/npn-boot-7.6.2.v20120308.jar</code> as
  16.  * vmarg.
  17.  */
  18. public class BugTestClient {
  19.  
  20.    /**
  21.     * The message that the server is expected to send
  22.     */
  23.    public static final String MSG = "This is the message that the server is supposed to send.";
  24.  
  25.    private static final String SERVER_ADDRESS = "localhost";
  26.    private static final int SERVER_PORT = 8181;
  27.  
  28.    public static void main(final String[] args) throws Exception {
  29.       // create client
  30.       SPDYClient.Factory clientFactory = new SPDYClient.Factory();
  31.       try {
  32.          clientFactory.start();
  33.       } catch (Exception e) {
  34.          System.err.println("Couldn't start client factory. " + e.toString());
  35.          System.exit(1);
  36.       }
  37.       SPDYClient client = clientFactory.newSPDYClient(SPDY.V3);
  38.  
  39.       // create a session to the server
  40.       Session session = null;
  41.       try {
  42.          Future<Session> future = client.connect(new InetSocketAddress(
  43.                SERVER_ADDRESS, SERVER_PORT), null);
  44.          session = future.get();
  45.       } catch (Exception e) {
  46.          System.err.println("Couldn't connect to server at " + SERVER_ADDRESS
  47.                + ":" + SERVER_PORT + ". Aborting.");
  48.          System.exit(1);
  49.       }
  50.  
  51.       // this listener receives data from the server and prints it
  52.       StreamFrameListener streamListener = new StreamFrameListener.Adapter() {
  53.  
  54.          private long numArrived = 0;
  55.  
  56.          public void onData(final Stream stream, final DataInfo dataInfo) {
  57.             // Data received from server
  58.             String content = dataInfo.asString("UTF-8", true);
  59.  
  60.             if (!content.equals(MSG)) {
  61.                // bug?
  62.                System.err
  63.                      .println("The server did not send the correct message: '"
  64.                            + content + "'");
  65.             }
  66.             numArrived++;
  67.             if (numArrived % 20000 == 0) {
  68.                System.out.println(numArrived + " messages arrrived");
  69.             }
  70.          }
  71.       };
  72.  
  73.       // Start a new session, and configure the stream listener
  74.       try {
  75.          session.syn(new SynInfo(false), streamListener).get(5,
  76.                TimeUnit.SECONDS);
  77.          System.out.println("Stream opened.");
  78.       } catch (Exception e) {
  79.          System.err.println("Problem with stream. " + e.toString());
  80.       }
  81.    }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement