示例#1
0
 public TransferRateInterval DataXferRate(
     String header, String hostIP, int port, int timeout, int xferLength, int nTrials) {
   for (int trial = 0; trial < nTrials; trial++) {
     try {
       TransferRate.start("tcp");
       DataXfer(header, hostIP, port, timeout, xferLength);
       TransferRate.stop("tcp", xferLength);
     } catch (Exception e) {
       TransferRate.abort("tcp", xferLength);
       System.out.println("TCP trial failed: " + e.getMessage());
     }
   }
   return TransferRate.get("tcp");
 }
示例#2
0
  @Override
  public void run() {
    try {
      // Eclipse doesn't support System.console()
      BufferedReader console = new BufferedReader(new InputStreamReader(System.in));

      ConfigManager config = NetBase.theNetBase().config();
      String server = config.getProperty("net.server.ip");
      if (server == null) {
        System.out.print("Enter a host ip, or exit to exit: ");
        server = console.readLine();
        if (server == null) return;
        if (server.equals("exit")) return;
      }

      System.out.print("Enter port number, or empty line to exit: ");
      String portStr = console.readLine();
      if (portStr == null || portStr.trim().isEmpty()) return;
      int basePort = Integer.parseInt(portStr);

      int socketTimeout = config.getAsInt("net.timeout.socket", -1);
      if (socketTimeout < 0) {
        System.out.print("Enter socket timeout (in msec.): ");
        String timeoutStr = console.readLine();
        socketTimeout = Integer.parseInt(timeoutStr);
      }

      System.out.print("Enter number of trials: ");
      String trialStr = console.readLine();
      int nTrials = Integer.parseInt(trialStr);
      System.out.print("Enter transfer size: ");
      String xferLengthStr = console.readLine();
      int xferLength = Integer.parseInt(xferLengthStr);

      System.out.println("\n" + xferLength + " bytes");

      TransferRate.clear();
      TransferRateInterval tcpStats =
          DataXferRate(
              DataXferServiceBase.HEADER_STR, server, basePort, socketTimeout, xferLength, nTrials);
      System.out.println(
          "\nTCP: xfer rate = " + String.format("%9.0f", tcpStats.mean() * 1000.0) + " bytes/sec.");
      System.out.println(
          "TCP: failure rate = "
              + String.format("%5.1f", tcpStats.failureRate())
              + " ["
              + tcpStats.nAborted()
              + "/"
              + tcpStats.nTrials()
              + "]");

    } catch (Exception e) {
      System.out.println("Unanticipated exception: " + e.getMessage());
    }
  }
示例#3
0
  public byte[] DataXfer(String header, String hostIP, int port, int timeout, int xferLength)
      throws JSONException, IOException {
    Socket tcpSocket = new Socket(hostIP, port);
    tcpSocket.setSoTimeout(timeout);
    tcpSocket.setTcpNoDelay(true);

    // send header
    TCPMessageHandler tcpHandler = new TCPMessageHandler(tcpSocket);
    tcpHandler.sendMessage(header);

    // send message
    JSONObject controlMsg = new JSONObject();
    controlMsg.put("transferSize", xferLength);
    tcpHandler.sendMessage(controlMsg);

    tcpSocket.shutdownOutput();

    // start to read the response
    int dataLength = 0; // keep track of the total received data length
    byte[] receiveBuf = new byte[xferLength];
    ByteBuffer totalBuf = ByteBuffer.wrap(receiveBuf);

    // read the header
    String headerStr = tcpHandler.readMessageAsString();
    if (!headerStr.equalsIgnoreCase(EchoServiceBase.RESPONSE_OKAY_STR)) {
      TransferRate.abort("tcp", xferLength);
      System.out.println(
          "Bad response header: got "
              + headerStr
              + " but expected '"
              + EchoServiceBase.RESPONSE_OKAY_STR
              + "'");

    } else {

      // read the messages if the request amount of data is >= PACKET_SIZE bytes
      for (int i = 0; i < xferLength / PACKET_SIZE; i++) {
        byte[] buf = tcpHandler.readMessageAsBytes();
        if (buf.length > 0) {
          totalBuf.put(buf);
          dataLength += buf.length;
        } else {
          TransferRate.abort("tcp", xferLength);
          System.out.println(
              "Bad response: got " + dataLength + " data but expected " + xferLength + " data.");
        }
      }

      // read the first/last message if it's < PACKET_SIZE bytes
      if (xferLength % PACKET_SIZE != 0) {
        byte[] buf = tcpHandler.readMessageAsBytes();
        dataLength += buf.length;
        if (buf.length == xferLength % PACKET_SIZE) {
          totalBuf.put(buf);
        } else {
          TransferRate.abort("tcp", xferLength);
        }
      }

      if (dataLength != xferLength) {
        // server doesn't send back the requested amount of data
        TransferRate.abort("tcp", xferLength);
      }
    }
    tcpHandler.close();
    return totalBuf.array();
  }