001    /**
002     * Copyright (C) 2009-2013 Barchart, Inc. <http://www.barchart.com/>
003     *
004     * All rights reserved. Licensed under the OSI BSD License.
005     *
006     * http://www.opensource.org/licenses/bsd-license.php
007     */
008    package com.barchart.udt;
009    
010    import java.io.IOException;
011    import java.io.OutputStream;
012    import java.net.InetSocketAddress;
013    import java.util.concurrent.Callable;
014    import java.util.concurrent.ExecutionException;
015    import java.util.concurrent.Executors;
016    import java.util.concurrent.Future;
017    
018    import com.barchart.udt.net.NetSocketUDT;
019    
020    public class AppClient {
021    
022            static boolean finished = false;
023    
024            /**
025             * @param args
026             * @throws IOException
027             */
028            public static void main(final String[] args) {
029    
030                    String host;
031                    int port = 9000;
032                    final int size = 10000;
033                    final byte[] data = new byte[size];
034                    Future<Boolean> monResult = null;
035    
036                    if (args.length != 2) {
037                            System.out.println("usage: appclient server_host server_port");
038                            return;
039                    }
040    
041                    host = args[0];
042                    port = Integer.parseInt(args[1]);
043    
044                    try {
045    
046                            final NetSocketUDT socket = new NetSocketUDT();
047    
048                            if (System.getProperty("os.name").contains("win"))
049                                    socket.socketUDT().setOption(OptionUDT.UDT_MSS, 1052);
050    
051                            socket.connect(new InetSocketAddress(host, port));
052                            final OutputStream os = socket.getOutputStream();
053    
054                            // Start the monitor background task
055                            monResult = Executors.newSingleThreadExecutor().submit(
056                                            new Callable<Boolean>() {
057                                                    @Override
058                                                    public Boolean call() {
059                                                            return monitor(socket.socketUDT());
060                                                    }
061                                            });
062    
063                            for (int i = 0; i < 1000000; i++) {
064                                    os.write(data);
065                            }
066    
067                            finished = true;
068                            if (monResult != null)
069                                    monResult.get();
070    
071                    } catch (final IOException ioe) {
072                            ioe.printStackTrace();
073                    } catch (final InterruptedException e) {
074                            e.printStackTrace();
075                    } catch (final ExecutionException e) {
076                            e.printStackTrace();
077                    }
078    
079            }
080    
081            public static boolean monitor(final SocketUDT socket) {
082    
083                    System.out
084                                    .println("SendRate(Mb/s)\tRTT(ms)\tCWnd\tPktSndPeriod(us)\tRecvACK\tRecvNAK");
085    
086                    try {
087    
088                            while (!finished) {
089    
090                                    Thread.sleep(1000);
091    
092                                    socket.updateMonitor(false);
093    
094                                    System.out.printf("%.2f\t\t" + "%.2f\t" + "%d\t" + "%.2f\t\t\t"
095                                                    + "%d\t" + "%d\n", socket.monitor().mbpsSendRate,
096                                                    socket.monitor().msRTT,
097                                                    socket.monitor().pktCongestionWindow,
098                                                    socket.monitor().usPktSndPeriod,
099                                                    socket.monitor().pktRecvACK,
100                                                    socket.monitor().pktRecvNAK);
101                            }
102    
103                            return true;
104    
105                    } catch (final Exception e) {
106                            e.printStackTrace();
107                            return false;
108                    }
109            }
110    }