예제 #1
0
 public static void main(String[] args) throws Exception {
   OptionParser parser = new OptionParser();
   parser.accepts("no-message", "do not decode message(utf-8 strings)");
   parser.accepts("no-offset", "do not print message offset");
   parser.accepts("no-size", "do not print message size");
   ArgumentAcceptingOptionSpec<Integer> countOpt =
       parser
           .accepts("c", "max count mesages.") //
           .withRequiredArg()
           .describedAs("count") //
           .ofType(Integer.class)
           .defaultsTo(-1);
   ArgumentAcceptingOptionSpec<String> fileOpt =
       parser
           .accepts("file", "decode file list") //
           .withRequiredArg()
           .ofType(String.class)
           .describedAs("filepath");
   OptionSet options = parser.parse(args);
   if (!options.has(fileOpt)) {
     System.err.println("Usage: [options] --file <file>...");
     parser.printHelpOn(System.err);
     System.exit(1);
   }
   final boolean decode = !options.has("no-message");
   final boolean withOffset = !options.has("no-offset");
   final boolean withSize = !options.has("no-size");
   int count = countOpt.value(options);
   count = count <= 0 ? Integer.MAX_VALUE : count;
   int index = 0;
   final String logformat = "%s|%s|%s";
   for (String filepath : fileOpt.values(options)) {
     if (index >= count) break;
     File file = new File(filepath);
     String filename = file.getName();
     final long startOffset = Long.parseLong(filename.substring(0, filename.lastIndexOf('.')));
     FileMessageSet messageSet = new FileMessageSet(file, false);
     long offset = 0L;
     long totalSize = 0L;
     try {
       int messageCount = 0;
       for (MessageAndOffset mao : messageSet) {
         final Message msg = mao.message;
         if (index >= count) {
           break;
         }
         if (decode || withOffset || withSize) {
           System.out.println(
               format(
                   logformat, //
                   withOffset ? "" + (startOffset + offset) : "", //
                   withSize ? "" + msg.payloadSize() : "", //
                   decode ? Utils.toString(msg.payload(), "UTF-8") : ""));
         }
         offset = mao.offset;
         totalSize += msg.payloadSize();
         messageCount++;
         index++;
       }
       System.out.println(
           "-----------------------------------------------------------------------------");
       System.out.println(filepath);
       System.out.println("total message count: " + messageCount);
       System.out.println("total message size: " + totalSize);
       System.out.println("=============================================");
     } finally {
       messageSet.close();
     }
   }
 }
  @Override
  public void configure(Binder binder) {
    OptionParser parser = new OptionParser();

    ArgumentAcceptingOptionSpec<Integer> portSpec =
        parser
            .accepts("port", "The port to listen on")
            .withOptionalArg()
            .ofType(Integer.class)
            .defaultsTo(8080);

    ArgumentAcceptingOptionSpec<String> addressSpec =
        parser
            .accepts("address", "The address to bind to")
            .withOptionalArg()
            .ofType(String.class)
            .defaultsTo("0.0.0.0");

    ArgumentAcceptingOptionSpec<Integer> proxyPortRange =
        parser
            .accepts("proxyPortRange", "The range of ports to use for proxies")
            .withOptionalArg()
            .ofType(Integer.class)
            .defaultsTo(8081, 8581)
            .withValuesSeparatedBy('-');

    ArgumentAcceptingOptionSpec<Integer> ttlSpec =
        parser
            .accepts("ttl", "Time in seconds until an unused proxy is deleted")
            .withOptionalArg()
            .ofType(Integer.class)
            .defaultsTo(0);

    parser.acceptsAll(asList("help", "?"), "This help text");

    OptionSet options = parser.parse(args);

    if (options.has("?")) {
      try {
        parser.printHelpOn(System.out);
        System.exit(0);
      } catch (IOException e) {
        // should never happen, but...
        e.printStackTrace();
      }
      return;
    }

    List<Integer> ports = options.valuesOf(proxyPortRange);
    if (ports.size() < 2) {
      throw new IllegalArgumentException();
    }
    Integer minPort;
    Integer maxPort;
    if (ports.get(1) > ports.get(0)) {
      minPort = ports.get(0);
      maxPort = ports.get(1);
    } else {
      minPort = ports.get(1);
      maxPort = ports.get(0);
    }
    Integer port = portSpec.value(options);
    if (port >= minPort && port <= maxPort) {
      int num = maxPort - minPort;
      minPort = port + 1;
      maxPort = minPort + num;
    }

    binder.bind(Key.get(Integer.class, new NamedImpl("port"))).toInstance(port);
    binder
        .bind(Key.get(String.class, new NamedImpl("address")))
        .toInstance(addressSpec.value(options));
    binder.bind(Key.get(Integer.class, new NamedImpl("minPort"))).toInstance(minPort);
    binder.bind(Key.get(Integer.class, new NamedImpl("maxPort"))).toInstance(maxPort);
    binder.bind(Key.get(Integer.class, new NamedImpl("ttl"))).toInstance(ttlSpec.value(options));

    /*
     * Init User Agent String Parser, update of the UAS datastore will run in background.
     */
    // temporarily disabled because user-agent-string.info no longer exists
    // BrowserMobHttpClient.getUserAgentStringParser();
  }