Example usage for java.lang String getBytes

List of usage examples for java.lang String getBytes

Introduction

In this page you can find the example usage for java.lang String getBytes.

Prototype

public byte[] getBytes(Charset charset) 

Source Link

Document

Encodes this String into a sequence of bytes using the given java.nio.charset.Charset charset , storing the result into a new byte array.

Usage

From source file:CTmousetrack.java

public static void main(String[] args) {

    String outLoc = new String("." + File.separator + "CTdata"); // Location of the base output data folder; only used when writing out CT data to a local folder
    String srcName = "CTmousetrack"; // name of the output CT source
    long blockPts = 10; // points per block flush
    long sampInterval = 10; // time between sampling updates, msec
    double trimTime = 0.0; // amount of data to keep (trim time), sec
    boolean debug = false; // turn on debug?

    // Specify the CT output connection
    CTWriteMode writeMode = CTWriteMode.LOCAL; // The selected mode for writing out CT data
    String serverHost = ""; // Server (FTP or HTTP/S) host:port
    String serverUser = ""; // Server (FTP or HTTPS) username
    String serverPassword = ""; // Server (FTP or HTTPS) password

    // For UDP output mode
    DatagramSocket udpServerSocket = null;
    InetAddress udpServerAddress = null;
    String udpHost = "";
    int udpPort = -1;

    // Concatenate all of the CTWriteMode types
    String possibleWriteModes = "";
    for (CTWriteMode wm : CTWriteMode.values()) {
        possibleWriteModes = possibleWriteModes + ", " + wm.name();
    }//from   ww  w  .  ja  v a  2s .  c  o m
    // Remove ", " from start of string
    possibleWriteModes = possibleWriteModes.substring(2);

    //
    // Argument processing using Apache Commons CLI
    //
    // 1. Setup command line options
    Options options = new Options();
    options.addOption("h", "help", false, "Print this message.");
    options.addOption(Option.builder("o").argName("base output dir").hasArg().desc(
            "Base output directory when writing data to local folder (i.e., this is the location of CTdata folder); default = \""
                    + outLoc + "\".")
            .build());
    options.addOption(Option.builder("s").argName("source name").hasArg()
            .desc("Name of source to write data to; default = \"" + srcName + "\".").build());
    options.addOption(Option.builder("b").argName("points per block").hasArg()
            .desc("Number of points per block; UDP output mode will use 1 point/block; default = "
                    + Long.toString(blockPts) + ".")
            .build());
    options.addOption(Option.builder("dt").argName("samp interval msec").hasArg()
            .desc("Sampling period in msec; default = " + Long.toString(sampInterval) + ".").build());
    options.addOption(Option.builder("t").argName("trim time sec").hasArg().desc(
            "Trim (ring-buffer loop) time (sec); this is only used when writing data to local folder; specify 0 for indefinite; default = "
                    + Double.toString(trimTime) + ".")
            .build());
    options.addOption(
            Option.builder("w").argName("write mode").hasArg()
                    .desc("Type of write connection; one of " + possibleWriteModes
                            + "; all but UDP mode write out to CT; default = " + writeMode.name() + ".")
                    .build());
    options.addOption(Option.builder("host").argName("host[:port]").hasArg()
            .desc("Host:port when writing via FTP, HTTP, HTTPS, UDP.").build());
    options.addOption(Option.builder("u").argName("username,password").hasArg()
            .desc("Comma-delimited username and password when writing to CT via FTP or HTTPS.").build());
    options.addOption("x", "debug", false, "Enable CloudTurbine debug output.");

    // 2. Parse command line options
    CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, args);
    } catch (ParseException exp) { // oops, something went wrong
        System.err.println("Command line argument parsing failed: " + exp.getMessage());
        return;
    }

    // 3. Retrieve the command line values
    if (line.hasOption("help")) { // Display help message and quit
        HelpFormatter formatter = new HelpFormatter();
        formatter.setWidth(120);
        formatter.printHelp("CTmousetrack", "", options,
                "NOTE: UDP output is a special non-CT output mode where single x,y points are sent via UDP to the specified host:port.");
        return;
    }

    outLoc = line.getOptionValue("o", outLoc);
    if (!outLoc.endsWith("\\") && !outLoc.endsWith("/")) {
        outLoc = outLoc + File.separator;
    }
    // Make sure the base output folder location ends in "CTdata"
    if (!outLoc.endsWith("CTdata\\") && !outLoc.endsWith("CTdata/")) {
        outLoc = outLoc + "CTdata" + File.separator;
    }

    srcName = line.getOptionValue("s", srcName);

    blockPts = Long.parseLong(line.getOptionValue("b", Long.toString(blockPts)));

    sampInterval = Long.parseLong(line.getOptionValue("dt", Long.toString(sampInterval)));

    trimTime = Double.parseDouble(line.getOptionValue("t", Double.toString(trimTime)));

    // Type of output connection
    String writeModeStr = line.getOptionValue("w", writeMode.name());
    boolean bMatch = false;
    for (CTWriteMode wm : CTWriteMode.values()) {
        if (wm.name().toLowerCase().equals(writeModeStr.toLowerCase())) {
            writeMode = wm;
            bMatch = true;
        }
    }
    if (!bMatch) {
        System.err.println("Unrecognized write mode, \"" + writeModeStr + "\"; write mode must be one of "
                + possibleWriteModes);
        System.exit(0);
    }
    if (writeMode != CTWriteMode.LOCAL) {
        // User must have specified the host
        // If FTP or HTTPS, they may also specify username/password
        serverHost = line.getOptionValue("host", serverHost);
        if (serverHost.isEmpty()) {
            System.err.println(
                    "When using write mode \"" + writeModeStr + "\", you must specify the server host.");
            System.exit(0);
        }
        if (writeMode == CTWriteMode.UDP) {
            // Force blockPts to be 1
            blockPts = 1;
            // User must have specified both host and port
            int colonIdx = serverHost.indexOf(':');
            if ((colonIdx == -1) || (colonIdx >= serverHost.length() - 1)) {
                System.err.println(
                        "For UDP output mode, both the host and port (<host>:<port>)) must be specified.");
                System.exit(0);
            }
            udpHost = serverHost.substring(0, colonIdx);
            String udpPortStr = serverHost.substring(colonIdx + 1);
            try {
                udpPort = Integer.parseInt(udpPortStr);
            } catch (NumberFormatException nfe) {
                System.err.println("The UDP port must be a positive integer.");
                System.exit(0);
            }
        }
        if ((writeMode == CTWriteMode.FTP) || (writeMode == CTWriteMode.HTTPS)) {
            String userpassStr = line.getOptionValue("u", "");
            if (!userpassStr.isEmpty()) {
                // This string should be comma-delimited username and password
                String[] userpassCSV = userpassStr.split(",");
                if (userpassCSV.length != 2) {
                    System.err.println("When specifying a username and password for write mode \""
                            + writeModeStr + "\", separate the username and password by a comma.");
                    System.exit(0);
                }
                serverUser = userpassCSV[0];
                serverPassword = userpassCSV[1];
            }
        }
    }

    debug = line.hasOption("debug");

    System.err.println("CTmousetrack parameters:");
    System.err.println("\toutput mode = " + writeMode.name());
    if (writeMode == CTWriteMode.UDP) {
        System.err.println("\twrite to " + udpHost + ":" + udpPort);
    } else {
        System.err.println("\tsource = " + srcName);
        System.err.println("\ttrim time = " + trimTime + " sec");
    }
    System.err.println("\tpoints per block = " + blockPts);
    System.err.println("\tsample interval = " + sampInterval + " msec");

    try {
        //
        // Setup CTwriter or UDP output
        //
        CTwriter ctw = null;
        CTinfo.setDebug(debug);
        if (writeMode == CTWriteMode.LOCAL) {
            ctw = new CTwriter(outLoc + srcName, trimTime);
            System.err.println("\tdata will be written to local folder \"" + outLoc + "\"");
        } else if (writeMode == CTWriteMode.FTP) {
            CTftp ctftp = new CTftp(srcName);
            try {
                ctftp.login(serverHost, serverUser, serverPassword);
            } catch (Exception e) {
                throw new IOException(
                        new String("Error logging into FTP server \"" + serverHost + "\":\n" + e.getMessage()));
            }
            ctw = ctftp; // upcast to CTWriter
            System.err.println("\tdata will be written to FTP server at " + serverHost);
        } else if (writeMode == CTWriteMode.HTTP) {
            // Don't send username/pw in HTTP mode since they will be unencrypted
            CThttp cthttp = new CThttp(srcName, "http://" + serverHost);
            ctw = cthttp; // upcast to CTWriter
            System.err.println("\tdata will be written to HTTP server at " + serverHost);
        } else if (writeMode == CTWriteMode.HTTPS) {
            CThttp cthttp = new CThttp(srcName, "https://" + serverHost);
            // Username/pw are optional for HTTPS mode; only use them if username is not empty
            if (!serverUser.isEmpty()) {
                try {
                    cthttp.login(serverUser, serverPassword);
                } catch (Exception e) {
                    throw new IOException(new String(
                            "Error logging into HTTP server \"" + serverHost + "\":\n" + e.getMessage()));
                }
            }
            ctw = cthttp; // upcast to CTWriter
            System.err.println("\tdata will be written to HTTPS server at " + serverHost);
        } else if (writeMode == CTWriteMode.UDP) {
            try {
                udpServerSocket = new DatagramSocket();
            } catch (SocketException se) {
                System.err.println("Error creating socket for UDP:\n" + se);
                System.exit(0);
            }
            try {
                udpServerAddress = InetAddress.getByName(udpHost);
            } catch (UnknownHostException uhe) {
                System.err.println("Error getting UDP server host address:\n" + uhe);
                System.exit(0);
            }
        }
        if (writeMode != CTWriteMode.UDP) {
            ctw.setBlockMode(blockPts > 1, blockPts > 1);
            ctw.autoFlush(0); // no autoflush
            ctw.autoSegment(1000);
        }

        // screen dims
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        double width = screenSize.getWidth();
        double height = screenSize.getHeight();

        // use Map for consolidated putData
        Map<String, Object> cmap = new LinkedHashMap<String, Object>();

        // loop and write some output
        for (int i = 0; i < 1000000; i++) { // go until killed
            long currentTime = System.currentTimeMillis();
            Point mousePos = MouseInfo.getPointerInfo().getLocation();
            float x_pt = (float) (mousePos.getX() / width); // normalize
            float y_pt = (float) ((height - mousePos.getY()) / height); // flip Y (so bottom=0)
            if (writeMode != CTWriteMode.UDP) {
                // CT output mode
                ctw.setTime(currentTime);
                cmap.clear();
                cmap.put("x", x_pt);
                cmap.put("y", y_pt);
                ctw.putData(cmap);
                if (((i + 1) % blockPts) == 0) {
                    ctw.flush();
                    System.err.print(".");
                }
            } else {
                // UDP output mode
                // We force blockPts to be 1 for UDP output mode, i.e. we "flush" the data every time
                // Write the following data (21 bytes total):
                //     header = "MOUSE", 5 bytes
                //     current time, long, 8 bytes
                //     2 floats (x,y) 4 bytes each, 8 bytes
                int len = 21;
                ByteBuffer bb = ByteBuffer.allocate(len);
                String headerStr = "MOUSE";
                bb.put(headerStr.getBytes("UTF-8"));
                bb.putLong(currentTime);
                bb.putFloat(x_pt);
                bb.putFloat(y_pt);
                // Might be able to use the following, but not sure:
                //     byte[] sendData = bb.array();
                byte[] sendData = new byte[len];
                bb.position(0);
                bb.get(sendData, 0, len);
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, udpServerAddress,
                        udpPort);
                try {
                    udpServerSocket.send(sendPacket);
                } catch (IOException e) {
                    System.err.println("Test server caught exception trying to send data to UDP client:\n" + e);
                }
                System.err.print(".");
            }
            try {
                Thread.sleep(sampInterval);
            } catch (Exception e) {
            }
            ;
        }
        if (writeMode != CTWriteMode.UDP) {
            ctw.flush(); // wrap up
        }
    } catch (Exception e) {
        System.err.println("CTmousetrack exception: " + e);
        e.printStackTrace();
    }
}

From source file:com.ok2c.lightmtp.examples.MailUserAgentExample.java

public static void main(final String[] args) throws Exception {

    String text1 = "From: root\r\n" + "To: testuser1\r\n" + "Subject: test message 1\r\n" + "\r\n"
            + "This is a short test message 1\r\n";
    String text2 = "From: root\r\n" + "To: testuser1, testuser2\r\n" + "Subject: test message 2\r\n" + "\r\n"
            + "This is a short test message 2\r\n";
    String text3 = "From: root\r\n" + "To: testuser1, testuser2, testuser3\r\n" + "Subject: test message 3\r\n"
            + "\r\n" + "This is a short test message 3\r\n";

    List<DeliveryRequest> requests = new ArrayList<DeliveryRequest>();
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1"),
            new ByteArraySource(text1.getBytes("US-ASCII"))));
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2"),
            new ByteArraySource(text2.getBytes("US-ASCII"))));
    requests.add(new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2", "testuser3"),
            new ByteArraySource(text3.getBytes("US-ASCII"))));

    MailUserAgent mua = new DefaultMailUserAgent(TransportType.SMTP, IOReactorConfig.DEFAULT);
    mua.start();//from   w ww  .  j  a v a2 s  . c  o m

    try {

        InetSocketAddress address = new InetSocketAddress("localhost", 2525);

        Queue<Future<DeliveryResult>> queue = new LinkedList<Future<DeliveryResult>>();
        for (DeliveryRequest request : requests) {
            queue.add(mua.deliver(new SessionEndpoint(address), 0, request, null));
        }

        while (!queue.isEmpty()) {
            Future<DeliveryResult> future = queue.remove();
            DeliveryResult result = future.get();
            System.out.println("Delivery result: " + result);
        }

    } finally {
        mua.shutdown();
    }
}

From source file:com.ok2c.lightmtp.examples.LocalMailClientTransportExample.java

public static void main(final String[] args) throws Exception {

    String text1 = "From: root\r\n" + "To: testuser1\r\n" + "Subject: test message 1\r\n" + "\r\n"
            + "This is a short test message 1\r\n";
    String text2 = "From: root\r\n" + "To: testuser1, testuser2\r\n" + "Subject: test message 2\r\n" + "\r\n"
            + "This is a short test message 2\r\n";
    String text3 = "From: root\r\n" + "To: testuser1, testuser2, testuser3\r\n" + "Subject: test message 3\r\n"
            + "\r\n" + "This is a short test message 3\r\n";

    DeliveryRequest request1 = new BasicDeliveryRequest("root", Arrays.asList("testuser1"),
            new ByteArraySource(text1.getBytes("US-ASCII")));

    DeliveryRequest request2 = new BasicDeliveryRequest("root", Arrays.asList("testuser1", "testuser2"),
            new ByteArraySource(text2.getBytes("US-ASCII")));

    DeliveryRequest request3 = new BasicDeliveryRequest("root",
            Arrays.asList("testuser1", "testuser2", "testuser3"),
            new ByteArraySource(text3.getBytes("US-ASCII")));

    Queue<DeliveryRequest> queue = new ConcurrentLinkedQueue<DeliveryRequest>();
    queue.add(request1);/*from  w w  w . j a  v  a  2s.  c  o m*/
    queue.add(request2);
    queue.add(request3);

    CountDownLatch messageCount = new CountDownLatch(queue.size());

    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(1).build();

    MailClientTransport mua = new LocalMailClientTransport(config);
    mua.start(new MyDeliveryRequestHandler(messageCount));

    SessionEndpoint endpoint = new SessionEndpoint(new InetSocketAddress("localhost", 2525));

    SessionRequest sessionRequest = mua.connect(endpoint, queue, null);
    sessionRequest.waitFor();

    IOSession iosession = sessionRequest.getSession();
    if (iosession != null) {
        messageCount.await();
    } else {
        IOException ex = sessionRequest.getException();
        if (ex != null) {
            System.out.println("Connection failed: " + ex.getMessage());
        }
    }

    System.out.println("Shutting down I/O reactor");
    try {
        mua.shutdown();
    } catch (IOException ex) {
        mua.forceShutdown();
    }
    System.out.println("Done");
}

From source file:com.netscape.cmstools.CMCSharedToken.java

public static void main(String args[]) throws Exception {
    boolean isVerificationMode = false; // developer debugging only

    Options options = createOptions();/*from w ww.j  a  va2 s  .c o  m*/
    CommandLine cmd = null;

    try {
        CommandLineParser parser = new PosixParser();
        cmd = parser.parse(options, args);

    } catch (Exception e) {
        printError(e.getMessage());
        System.exit(1);
    }

    if (cmd.hasOption("help")) {
        printHelp();
        System.exit(0);
    }

    boolean verbose = cmd.hasOption("v");

    String databaseDir = cmd.getOptionValue("d", ".");
    String passphrase = cmd.getOptionValue("s");
    if (passphrase == null) {
        printError("Missing passphrase");
        System.exit(1);
    }
    if (verbose) {
        System.out.println("passphrase String = " + passphrase);
        System.out.println("passphrase UTF-8 bytes = ");
        System.out.println(Arrays.toString(passphrase.getBytes("UTF-8")));
    }
    String tokenName = cmd.getOptionValue("h");
    String tokenPassword = cmd.getOptionValue("p");

    String issuanceProtCertFilename = cmd.getOptionValue("b");
    String issuanceProtCertNick = cmd.getOptionValue("n");
    String output = cmd.getOptionValue("o");

    try {
        CryptoManager.initialize(databaseDir);

        CryptoManager manager = CryptoManager.getInstance();

        CryptoToken token = CryptoUtil.getKeyStorageToken(tokenName);
        tokenName = token.getName();
        manager.setThreadToken(token);

        Password password = new Password(tokenPassword.toCharArray());
        token.login(password);

        X509Certificate issuanceProtCert = null;
        if (issuanceProtCertFilename != null) {
            if (verbose)
                System.out.println("Loading issuance protection certificate");
            String encoded = new String(Files.readAllBytes(Paths.get(issuanceProtCertFilename)));
            byte[] issuanceProtCertData = Cert.parseCertificate(encoded);

            issuanceProtCert = manager.importCACertPackage(issuanceProtCertData);
            if (verbose)
                System.out.println("issuance protection certificate imported");
        } else {
            // must have issuance protection cert nickname if file not provided
            if (verbose)
                System.out.println("Getting cert by nickname: " + issuanceProtCertNick);
            if (issuanceProtCertNick == null) {
                System.out.println(
                        "Invallid command: either nickname or PEM file must be provided for Issuance Protection Certificate");
                System.exit(1);
            }
            issuanceProtCert = getCertificate(tokenName, issuanceProtCertNick);
        }

        EncryptionAlgorithm encryptAlgorithm = EncryptionAlgorithm.AES_128_CBC_PAD;
        KeyWrapAlgorithm wrapAlgorithm = KeyWrapAlgorithm.RSA;

        if (verbose)
            System.out.println("Generating session key");
        SymmetricKey sessionKey = CryptoUtil.generateKey(token, KeyGenAlgorithm.AES, 128, null, true);

        if (verbose)
            System.out.println("Encrypting passphrase");
        byte iv[] = { 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1 };
        byte[] secret_data = CryptoUtil.encryptUsingSymmetricKey(token, sessionKey,
                passphrase.getBytes("UTF-8"), encryptAlgorithm, new IVParameterSpec(iv));

        if (verbose)
            System.out.println("Wrapping session key with issuance protection cert");
        byte[] issuanceProtWrappedSessionKey = CryptoUtil.wrapUsingPublicKey(token,
                issuanceProtCert.getPublicKey(), sessionKey, wrapAlgorithm);

        // final_data takes this format:
        // SEQUENCE {
        //     encryptedSession OCTET STRING,
        //     encryptedPrivate OCTET STRING
        // }

        DerOutputStream tmp = new DerOutputStream();

        tmp.putOctetString(issuanceProtWrappedSessionKey);
        tmp.putOctetString(secret_data);
        DerOutputStream out = new DerOutputStream();
        out.write(DerValue.tag_Sequence, tmp);

        byte[] final_data = out.toByteArray();
        String final_data_b64 = Utils.base64encode(final_data, true);
        if (final_data_b64 != null) {
            System.out.println("\nEncrypted Secret Data:");
            System.out.println(final_data_b64);
        } else
            System.out.println("Failed to produce final data");

        if (output != null) {
            System.out.println("\nStoring Base64 secret data into " + output);
            try (FileWriter fout = new FileWriter(output)) {
                fout.write(final_data_b64);
            }
        }

        if (isVerificationMode) { // developer use only
            PrivateKey wrappingKey = null;
            if (issuanceProtCertNick != null)
                wrappingKey = (org.mozilla.jss.crypto.PrivateKey) getPrivateKey(tokenName,
                        issuanceProtCertNick);
            else
                wrappingKey = CryptoManager.getInstance().findPrivKeyByCert(issuanceProtCert);

            System.out.println("\nVerification begins...");
            byte[] wrapped_secret_data = Utils.base64decode(final_data_b64);
            DerValue wrapped_val = new DerValue(wrapped_secret_data);
            // val.tag == DerValue.tag_Sequence
            DerInputStream wrapped_in = wrapped_val.data;
            DerValue wrapped_dSession = wrapped_in.getDerValue();
            byte wrapped_session[] = wrapped_dSession.getOctetString();
            System.out.println("wrapped session key retrieved");
            DerValue wrapped_dPassphrase = wrapped_in.getDerValue();
            byte wrapped_passphrase[] = wrapped_dPassphrase.getOctetString();
            System.out.println("wrapped passphrase retrieved");

            SymmetricKey ver_session = CryptoUtil.unwrap(token, SymmetricKey.AES, 128,
                    SymmetricKey.Usage.UNWRAP, wrappingKey, wrapped_session, wrapAlgorithm);
            byte[] ver_passphrase = CryptoUtil.decryptUsingSymmetricKey(token, new IVParameterSpec(iv),
                    wrapped_passphrase, ver_session, encryptAlgorithm);

            String ver_spassphrase = new String(ver_passphrase, "UTF-8");

            CryptoUtil.obscureBytes(ver_passphrase, "random");

            System.out.println("ver_passphrase String = " + ver_spassphrase);
            System.out.println("ver_passphrase UTF-8 bytes = ");
            System.out.println(Arrays.toString(ver_spassphrase.getBytes("UTF-8")));

            if (ver_spassphrase.equals(passphrase))
                System.out.println("Verification success!");
            else
                System.out.println("Verification failure! ver_spassphrase=" + ver_spassphrase);
        }

    } catch (Exception e) {
        if (verbose)
            e.printStackTrace();
        printError(e.getMessage());
        System.exit(1);
    }
}

From source file:net.cyllene.hackerrank.downloader.HackerrankDownloader.java

public static void main(String[] args) {
    // Parse arguments and set up the defaults
    DownloaderSettings.cmd = parseArguments(args);

    if (DownloaderSettings.cmd.hasOption("help")) {
        printHelp();/*from  w  ww .  j a  v  a2s . c om*/
        System.exit(0);
    }

    if (DownloaderSettings.cmd.hasOption("verbose")) {
        DownloaderSettings.beVerbose = true;
    }

    /**
     * Output directory logic:
     * 1) if directory exists, ask for -f option to overwrite, quit with message
     * 2) if -f flag is set, check if user has access to a parent directory
     * 3) if no access, quit with error
     * 4) if everything is OK, remember the path
     */
    String sDesiredPath = DownloaderSettings.outputDir;
    if (DownloaderSettings.cmd.hasOption("directory")) {
        sDesiredPath = DownloaderSettings.cmd.getOptionValue("d", DownloaderSettings.outputDir);
    }
    if (DownloaderSettings.beVerbose) {
        System.out.println("Checking output dir: " + sDesiredPath);
    }
    Path desiredPath = Paths.get(sDesiredPath);
    if (Files.exists(desiredPath) && Files.isDirectory(desiredPath)) {
        if (!DownloaderSettings.cmd.hasOption("f")) {
            System.out.println("I wouldn't like to overwrite existing directory: " + sDesiredPath
                    + ", set the --force flag if you are sure. May lead to data loss, be careful.");
            System.exit(0);
        } else {
            System.out.println(
                    "WARNING!" + System.lineSeparator() + "--force flag is set. Overwriting directory: "
                            + sDesiredPath + System.lineSeparator() + "WARNING!");
        }
    }
    if ((Files.exists(desiredPath) && !Files.isWritable(desiredPath))
            || !Files.isWritable(desiredPath.getParent())) {
        System.err
                .println("Fatal error: " + sDesiredPath + " cannot be created or modified. Check permissions.");
        // TODO: use Exceptions instead of system.exit
        System.exit(1);
    }
    DownloaderSettings.outputDir = sDesiredPath;

    Integer limit = DownloaderSettings.ITEMS_TO_DOWNLOAD;
    if (DownloaderSettings.cmd.hasOption("limit")) {
        try {
            limit = ((Number) DownloaderSettings.cmd.getParsedOptionValue("l")).intValue();
        } catch (ParseException e) {
            System.out.println("Incorrect limit: " + e.getMessage() + System.lineSeparator()
                    + "Using default value: " + limit);
        }
    }

    Integer offset = DownloaderSettings.ITEMS_TO_SKIP;
    if (DownloaderSettings.cmd.hasOption("offset")) {
        try {
            offset = ((Number) DownloaderSettings.cmd.getParsedOptionValue("o")).intValue();
        } catch (ParseException e) {
            System.out.println("Incorrect offset: " + e.getMessage() + " Using default value: " + offset);
        }
    }

    DownloaderCore dc = DownloaderCore.INSTANCE;

    List<HRChallenge> challenges = new LinkedList<>();

    // Download everything first
    Map<String, List<Integer>> structure = null;
    try {
        structure = dc.getStructure(offset, limit);
    } catch (IOException e) {
        System.err.println("Fatal Error: could not get data structure.");
        e.printStackTrace();
        System.exit(1);
    }

    challengesLoop: for (Map.Entry<String, List<Integer>> entry : structure.entrySet()) {
        String challengeSlug = entry.getKey();
        HRChallenge currentChallenge = null;
        try {
            currentChallenge = dc.getChallengeDetails(challengeSlug);
        } catch (IOException e) {
            System.err.println("Error: could not get challenge info for: " + challengeSlug);
            if (DownloaderSettings.beVerbose) {
                e.printStackTrace();
            }
            continue challengesLoop;
        }

        submissionsLoop: for (Integer submissionId : entry.getValue()) {
            HRSubmission submission = null;
            try {
                submission = dc.getSubmissionDetails(submissionId);
            } catch (IOException e) {
                System.err.println("Error: could not get submission info for: " + submissionId);
                if (DownloaderSettings.beVerbose) {
                    e.printStackTrace();
                }
                continue submissionsLoop;
            }

            // TODO: probably should move filtering logic elsewhere(getStructure, maybe)
            if (submission.getStatus().equalsIgnoreCase("Accepted")) {
                currentChallenge.getSubmissions().add(submission);
            }
        }

        challenges.add(currentChallenge);
    }

    // Now dump all data to disk
    try {
        for (HRChallenge currentChallenge : challenges) {
            if (currentChallenge.getSubmissions().isEmpty())
                continue;

            final String sChallengePath = DownloaderSettings.outputDir + "/" + currentChallenge.getSlug();
            final String sSolutionPath = sChallengePath + "/accepted_solutions";
            final String sDescriptionPath = sChallengePath + "/problem_description";

            Files.createDirectories(Paths.get(sDescriptionPath));
            Files.createDirectories(Paths.get(sSolutionPath));

            // FIXME: this should be done the other way
            String plainBody = currentChallenge.getDescriptions().get(0).getBody();
            String sFname;
            if (!plainBody.equals("null")) {
                sFname = sDescriptionPath + "/english.txt";
                if (DownloaderSettings.beVerbose) {
                    System.out.println("Writing to: " + sFname);
                }

                Files.write(Paths.get(sFname), plainBody.getBytes(StandardCharsets.UTF_8.name()));
            }

            String htmlBody = currentChallenge.getDescriptions().get(0).getBodyHTML();
            String temporaryHtmlTemplate = "<html></body>" + htmlBody + "</body></html>";

            sFname = sDescriptionPath + "/english.html";
            if (DownloaderSettings.beVerbose) {
                System.out.println("Writing to: " + sFname);
            }
            Files.write(Paths.get(sFname), temporaryHtmlTemplate.getBytes(StandardCharsets.UTF_8.name()));

            for (HRSubmission submission : currentChallenge.getSubmissions()) {
                sFname = String.format("%s/%d.%s", sSolutionPath, submission.getId(), submission.getLanguage());
                if (DownloaderSettings.beVerbose) {
                    System.out.println("Writing to: " + sFname);
                }

                Files.write(Paths.get(sFname),
                        submission.getSourceCode().getBytes(StandardCharsets.UTF_8.name()));
            }

        }
    } catch (IOException e) {
        System.err.println("Fatal Error: couldn't dump data to disk.");
        System.exit(1);
    }
}

From source file:com.yobidrive.diskmap.DiskMapStore.java

public static void main(String[] args) {

    try {//from   w  w  w  .jav  a 2s .co m
        BasicConfigurator.configure();
        //   (String storeName, String logPath, String keyPath, long ckeckPointPeriod, long logSize, int keySize,
        //      long mapSize, int packInterval, int readThreads, long needleCachedEntries, long bucketCachedBytes, short nodeId)

        /* String path = "/Users/david/Documents/NEEDLES";
        if ( args.length > 0) {
           path = args[0];
        }
        System.out.println("Using directory:" + path);
        */

        DiskMapStore dms = new DiskMapStore("teststore", "/drive_hd1;/drive_hd2", "/drive_ssd/storage_indexes",
                1000L, // Synching period
                60000 * 5L, // Checkpointing period
                2000000000L, // Log file size
                100, // Key max size
                2048, // Nb of buffers
                32768, // Nb of entries (buckets) per buffer
                60, // Compact interval
                8, // Read threads
                TEST_COUNT / 3 * 2 * 140, // NeedleHeaderCachedEntries (1/20 in cache, 1/2 of typical data set)
                TEST_COUNT / 3 * 2 * 140, // NeedleCachedBytes (3% of Map, weights 120 bytes/entry)
                true, // Use needle average age instead of needle yougest age
                (short) 0, 1, 1, 1);
        // dmm.getBtm().getCheckPoint().copyFrom(new NeedlePointer()) ; // Removes checkpoint
        dms.initialize();
        // System.out.println("Start read for "+TEST_COUNT+" pointers") ;
        Date startDate = new Date();
        Date lapDate = new Date();
        System.out.println("Start read test for " + TEST_COUNT + " pointers");
        long counter = 0;
        long lapCounter = 0;
        startDate = new Date();
        lapDate = new Date();
        long failCount = 0;
        counter = 0;
        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            List<Versioned<byte[]>> values = dms.get(new ByteArray(key.getBytes("UTF-8")), null);
            if (values.size() <= 0) {
                failCount++;
            } else {
                // Gets previous version   
                byte[] value = values.get(0).getValue();
                if (value == null)
                    failCount++;
                else if (value[0] != (byte) ((short) counter % 128))
                    failCount++;
            }
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPSR)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print(".") ;
                } else
                    // System.out.print("*") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }
        long timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nProcessed reading of " + TEST_COUNT + " pointers \n" + "\tTotal time: "
                + timeSpent / 1000 + "s\n" + "\tThroughput: " + (TEST_COUNT * 1000 / timeSpent) + " tps\n"
                + "\tBad results: " + failCount);

        startDate = new Date();
        lapDate = new Date();
        System.out.println("Start write test for " + TEST_COUNT + " pointers");
        counter = 0;
        lapCounter = 0;

        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            byte[] value = new byte[128000];
            value[0] = (byte) ((short) counter % 128);
            long chaseDurer = new Date().getTime();
            List<Versioned<byte[]>> previousValues = dms.get(new ByteArray(key.getBytes("UTF-8")), null);
            long chaseDurer2 = new Date().getTime();
            // System.out.println("Get in "+(chaseDurer2 -chaseDurer)+"ms") ;
            chaseDurer = chaseDurer2;
            Version newVersion = null;
            if (previousValues.size() <= 0) {
                newVersion = new VectorClock();
            } else {
                // Gets previous version
                newVersion = previousValues.get(0).cloneVersioned().getVersion();
            }
            // Increment version before writing
            ((VectorClock) newVersion).incrementVersion(0, new Date().getTime());
            Versioned<byte[]> versionned = new Versioned<byte[]>(
                    previousValues.size() <= 0 ? value : previousValues.get(0).cloneVersioned().getValue(),
                    newVersion);
            dms.put(new ByteArray(key.getBytes("UTF-8")), versionned, null);
            chaseDurer2 = new Date().getTime();
            // System.out.println("Put in "+(chaseDurer2 -chaseDurer)+"ms") ;
            // dmm.putValue(key.getBytes("UTF-8"), value) ;
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPS)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print("("+counter+")") ;
                } else
                    // System.out.print("["+counter+"]") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }

        timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nWriting before cache commit of " + TEST_COUNT + " pointers \n"
                + "\tTotal time: " + timeSpent / 1000 + "s\n" + "\tThroughput: "
                + (TEST_COUNT * 1000 / timeSpent) + " tps");

        System.out.println("Start read for " + TEST_COUNT + " pointers");
        startDate = new Date();
        lapDate = new Date();
        failCount = 0;
        counter = 0;
        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            List<Versioned<byte[]>> values = dms.get(new ByteArray(key.getBytes("UTF-8")), null);
            if (values.size() <= 0) {
                failCount++;
            } else {
                // Gets previous version   
                byte[] value = values.get(0).getValue();
                if (value == null)
                    failCount++;
                else if (value[0] != (byte) ((short) counter % 128))
                    failCount++;
            }
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPSR)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print(".") ;
                } else
                    // System.out.print("*") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }
        timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nProcessed reading of " + TEST_COUNT + " pointers \n" + "\tTotal time: "
                + timeSpent / 1000 + "s\n" + "\tThroughput: " + (TEST_COUNT * 1000 / timeSpent) + " tps\n"
                + "\tBad results: " + failCount);

        dms.close();
        // System.out.println("Max cycle time = "+ (dms.getBtm().getMaxCycleTimePass1()+dmm.getBtm().getMaxCycleTimePass2())) ;

    } catch (Throwable th) {
        th.printStackTrace();
    }

}

From source file:com.cloudhopper.sxmp.Post.java

static public void main(String[] args) throws Exception {

    String message = "Test With @ Character";
    //String message = "Tell Twitter what you're doing!\nStd msg charges apply. Send 'stop' to quit.\nVisit twitter.com or email help@twitter.com for help.";

    StringBuilder string0 = new StringBuilder(200).append("<?xml version=\"1.0\"?>\n")
            .append("<operation type=\"submit\">\n")
            .append(" <account username=\"customer1\" password=\"password1\"/>\n")
            .append(" <submitRequest referenceId=\"MYREF102020022\">\n")
            .append("  <operatorId>75</operatorId>\n").append("  <deliveryReport>true</deliveryReport>\n")
            .append("  <sourceAddress type=\"network\">40404</sourceAddress>\n")
            .append("  <destinationAddress type=\"international\">+13135551234</destinationAddress>\n")
            .append("  <text encoding=\"ISO-8859-1\">" + HexUtil.toHexString(message.getBytes("ISO-8859-1"))
                    + "</text>\n")
            .append(" </submitRequest>\n").append("</operation>\n").append("");

    /**//from   ww w. jav  a  2 s .  c  o  m
    //.append("<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML//EN\" \"../dtds/docbookx.dtd\">")
    //.append("<!DOCTYPE chapter PUBLIC \"-//OASIS//DTD DocBook XML//EN\">")
    .append("<submitRequest sequenceId=\"1000\">\n")
    .append("   <!-- this is a comment -->\n")
    .append("   <account username=\"testaccount\" password=\"testpassword\"/>\n")
    .append("   <option />\n")
    .append("   <messageRequest referenceId=\"MYMESSREF\">\n")
    //.append("       <sourceAddress>+13135551212</sourceAddress>\n")
    .append("       <destinationAddress>+13135551200</destinationAddress>\n")
    .append("       <text><![CDATA[Hello World]]></text>\n")
    .append("   </messageRequest>\n")
    .append("</submitRequest>")
    .append("");
     */

    // Get target URL
    String strURL = "http://localhost:9080/api/sxmp/1.0";

    // Get file to be posted
    //String strXMLFilename = args[1];
    //File input = new File(strXMLFilename);

    HttpClient client = new DefaultHttpClient();

    long totalStart = System.currentTimeMillis();

    for (int i = 0; i < 1; i++) {
        long start = System.currentTimeMillis();

        // execute request
        try {
            HttpPost post = new HttpPost(strURL);

            StringEntity entity = new StringEntity(string0.toString(), "ISO-8859-1");
            entity.setContentType("text/xml; charset=\"ISO-8859-1\"");
            post.setEntity(entity);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String responseBody = client.execute(post, responseHandler);
            long stop = System.currentTimeMillis();

            logger.debug("----------------------------------------");
            logger.debug("Response took " + (stop - start) + " ms");
            logger.debug(responseBody);
            logger.debug("----------------------------------------");
        } finally {
            // do nothing
        }
    }

    long totalEnd = System.currentTimeMillis();

    logger.debug("Response took " + (totalEnd - totalStart) + " ms");

}

From source file:com.cloudhopper.sxmp.PostUTF8MO.java

static public void main(String[] args) throws Exception {

    String URL = "https://sms-staging.twitter.com/receive/cloudhopper";

    // this is a Euro currency symbol
    //String text = "\u20AC";

    // shorter arabic
    //String text = "\u0623\u0647\u0644\u0627";

    // even longer arabic
    //String text = "\u0623\u0647\u0644\u0627\u0020\u0647\u0630\u0647\u0020\u0627\u0644\u062a\u062c\u0631\u0628\u0629\u0020\u0627\u0644\u0623\u0648\u0644\u0649";

    String text = "";
    for (int i = 0; i < 140; i++) {
        text += "\u0623";
    }//from w ww.  ja v  a 2 s .  c o m

    String srcAddr = "+14159129228";

    String ticketId = System.currentTimeMillis() + "";
    String operatorId = "23";

    //text += " " + ticketId;

    StringBuilder string0 = new StringBuilder(200).append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
            .append("<operation type=\"deliver\">\n")
            .append(" <account username=\"customer1\" password=\"password1\"/>\n").append(" <deliverRequest>\n")
            .append("  <ticketId>" + ticketId + "</ticketId>\n")
            .append("  <operatorId>" + operatorId + "</operatorId>\n")
            .append("  <sourceAddress type=\"international\">" + srcAddr + "</sourceAddress>\n")
            .append("  <destinationAddress type=\"network\">40404</destinationAddress>\n")
            .append("  <text encoding=\"UTF-8\">" + HexUtil.toHexString(text.getBytes("UTF-8")) + "</text>\n")
            .append(" </deliverRequest>\n").append("</operation>\n").append("");

    HttpClient client = new DefaultHttpClient();
    client.getParams().setBooleanParameter("http.protocol.expect-continue", false);

    long start = System.currentTimeMillis();

    // execute request
    try {
        HttpPost post = new HttpPost(URL);

        StringEntity entity = new StringEntity(string0.toString(), "ISO-8859-1");
        entity.setContentType("text/xml; charset=\"ISO-8859-1\"");
        post.setEntity(entity);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        String responseBody = client.execute(post, responseHandler);

        logger.debug("----------------------------------------");
        logger.debug(responseBody);
        logger.debug("----------------------------------------");
    } finally {
        // do nothing
    }

    long end = System.currentTimeMillis();

    logger.debug("Response took " + (end - start) + " ms");

}

From source file:org.waarp.openr66.protocol.http.rest.test.HttpTestRestR66Client.java

/**
 * @param args//ww w  . ja v a  2 s . c o  m
 */
@SuppressWarnings("unused")
public static void main(String[] args) {
    if (args.length > 2) {
        WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(WaarpLogLevel.DEBUG));
    } else {
        WaarpLoggerFactory.setDefaultFactory(new WaarpSlf4JLoggerFactory(null));
    }
    logger = WaarpLoggerFactory.getLogger(HttpTestRestR66Client.class);
    Configuration.configuration.HOST_ID = hostid;
    if (args.length > 0) {
        NB = Integer.parseInt(args[0]);
        if (Configuration.configuration.CLIENT_THREAD < NB) {
            Configuration.configuration.CLIENT_THREAD = NB + 1;
        }
        if (args.length > 1) {
            NBPERTHREAD = Integer.parseInt(args[1]);
        }
    }
    if (NB == 1 && NBPERTHREAD == 1) {
        DEBUG = true;
    }

    try {
        HttpTestR66PseudoMain.config = HttpTestR66PseudoMain.getTestConfiguration();
    } catch (CryptoException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
        return;
    } catch (IOException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
        return;
    }
    if (HttpTestR66PseudoMain.config.REST_ADDRESS != null) {
        host = HttpTestR66PseudoMain.config.REST_ADDRESS;
    }
    String filename = keydesfilename;
    Configuration.configuration.cryptoFile = filename;
    File keyfile = new File(filename);
    Des des = new Des();
    try {
        des.setSecretKey(keyfile);
    } catch (CryptoException e) {
        logger.error("Unable to load CryptoKey from Config file");
        return;
    } catch (IOException e) {
        logger.error("Unable to load CryptoKey from Config file");
        return;
    }
    Configuration.configuration.cryptoKey = des;
    HttpRestR66Handler.instantiateHandlers(HttpTestR66PseudoMain.config);
    // Configure the client.
    clientHelper = new HttpRestR66Client(baseURI, new HttpTestRestClientInitializer(null),
            Configuration.configuration.CLIENT_THREAD, Configuration.configuration.TIMEOUTCON);
    logger.warn("ClientHelper created");
    try {
        try {
            long start = System.currentTimeMillis();
            for (int i = 0; i < NBPERTHREAD; i++) {
                options(null);
            }
            long stop = System.currentTimeMillis();
            long diff = stop - start == 0 ? 1 : stop - start;
            logger.warn("Options: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?" + count.get());
        } catch (HttpInvalidAuthenticationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        count.set(0);
        long start = System.currentTimeMillis();
        if (false) {
            for (RESTHANDLERS handler : HttpRestR66Handler.RESTHANDLERS.values()) {
                try {
                    deleteData(handler);
                } catch (HttpInvalidAuthenticationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        long stop = System.currentTimeMillis();
        long diff = stop - start == 0 ? 1 : stop - start;
        if (false) {
            logger.warn("Delete: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?" + count.get());
        }
        count.set(0);
        start = System.currentTimeMillis();
        for (RestMethodHandler methodHandler : HttpTestR66PseudoMain.config.restHashMap.values()) {
            if (methodHandler instanceof DataModelRestMethodHandler<?>) {
                RESTHANDLERS handler = RESTHANDLERS.getRESTHANDLER(methodHandler.getPath());
                try {
                    multiDataRequests(handler);
                } catch (HttpInvalidAuthenticationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn("Create: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?" + count.get());
        count.set(0);
        start = System.currentTimeMillis();
        for (RestMethodHandler methodHandler : HttpTestR66PseudoMain.config.restHashMap.values()) {
            if (methodHandler instanceof DataModelRestMethodHandler<?>) {
                RESTHANDLERS handler = RESTHANDLERS.getRESTHANDLER(methodHandler.getPath());
                try {
                    realAllData(handler);
                } catch (HttpInvalidAuthenticationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn("ReadAll: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?" + count.get());

        count.set(0);
        start = System.currentTimeMillis();
        for (int i = 0; i < NBPERTHREAD; i++) {
            try {
                multiDataRequests(RESTHANDLERS.DbTaskRunner);
            } catch (HttpInvalidAuthenticationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn(
                "CreateMultiple: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?" + count.get());

        count.set(0);
        start = System.currentTimeMillis();
        launchThreads();
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn("CreateMultipleThread: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD * NB + "=?"
                + count.get());

        // Set usefull item first
        if (RestConfiguration.CRUD.UPDATE.isValid(
                HttpTestR66PseudoMain.config.RESTHANDLERS_CRUD[RESTHANDLERS.DbHostConfiguration.ordinal()])) {
            String key = null, value = null;
            Channel channel = clientHelper.getChannel(host, HttpTestR66PseudoMain.config.REST_PORT);
            if (channel != null) {
                String buz = null;
                if (HttpTestR66PseudoMain.config.REST_AUTHENTICATED) {
                    key = userAuthent;
                    value = keyAuthent;
                    // Need business
                    buz = "<business><businessid>hostas</businessid><businessid>hosta2</businessid><businessid>hostas2</businessid>"
                            + "<businessid>hosta</businessid><businessid>test</businessid><businessid>tests</businessid>"
                            + "<businessid>" + userAuthent + "</businessid></business>";
                } else {
                    // Need business
                    buz = "<business><businessid>hostas</businessid><businessid>hosta2</businessid><businessid>hostas2</businessid>"
                            + "<businessid>hosta</businessid><businessid>test</businessid><businessid>tests</businessid>"
                            + "<businessid>monadmin</businessid></business>";
                }
                ObjectNode node = JsonHandler.createObjectNode();
                node.put(DbHostConfiguration.Columns.BUSINESS.name(), buz);
                logger.warn("Send query: " + RESTHANDLERS.DbHostConfiguration.uri);
                RestFuture future = clientHelper.sendQuery(HttpTestR66PseudoMain.config, channel,
                        HttpMethod.PUT, host, RESTHANDLERS.DbHostConfiguration.uri + "/hosta", key, value, null,
                        JsonHandler.writeAsString(node));
                try {
                    future.await();
                } catch (InterruptedException e) {
                }
                WaarpSslUtility.closingSslChannel(channel);
            }
            // Need Hostzz
            channel = clientHelper.getChannel(host, HttpTestR66PseudoMain.config.REST_PORT);
            if (channel != null) {
                AbstractDbData dbData;
                dbData = new DbHostAuth(null, hostid, address, HttpTestR66PseudoMain.config.REST_PORT, false,
                        hostkey.getBytes(), true, false);
                logger.warn("Send query: " + RESTHANDLERS.DbHostAuth.uri);
                RestFuture future = clientHelper.sendQuery(HttpTestR66PseudoMain.config, channel,
                        HttpMethod.POST, host, RESTHANDLERS.DbHostAuth.uri, key, value, null, dbData.asJson());
                try {
                    future.await();
                } catch (InterruptedException e) {
                }
                WaarpSslUtility.closingSslChannel(channel);
            }
        }

        // Other Command as actions
        count.set(0);
        start = System.currentTimeMillis();
        for (RestMethodHandler methodHandler : HttpTestR66PseudoMain.config.restHashMap.values()) {
            if (methodHandler instanceof DataModelRestMethodHandler<?>) {
                RESTHANDLERS handler = RESTHANDLERS.getRESTHANDLER(methodHandler.getPath());
                try {
                    action(handler);
                } catch (HttpInvalidAuthenticationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        getStatus();
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn(
                "Commands: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD * NB + "=?" + count.get());

        count.set(0);
        start = System.currentTimeMillis();
        for (int i = 0; i < NBPERTHREAD; i++) {
            getStatus();
        }
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn("GetStatusMultiple: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD + "=?"
                + count.get());

        count.set(0);
        isStatus = true;
        start = System.currentTimeMillis();
        launchThreads();
        stop = System.currentTimeMillis();
        diff = stop - start == 0 ? 1 : stop - start;
        logger.warn("GetStatusMultipleThread: " + count.get() * 1000 / diff + " req/s " + NBPERTHREAD * NB
                + "=?" + count.get());

        // Clean
        if (RestConfiguration.CRUD.UPDATE.isValid(
                HttpTestR66PseudoMain.config.RESTHANDLERS_CRUD[RESTHANDLERS.DbHostConfiguration.ordinal()])) {
            String key = null, value = null;
            Channel channel = clientHelper.getChannel(host, HttpTestR66PseudoMain.config.REST_PORT);
            if (channel != null) {
                if (HttpTestR66PseudoMain.config.REST_AUTHENTICATED) {
                    key = userAuthent;
                    value = keyAuthent;
                }
                // Reset business
                String buz = "<business><businessid>hostas</businessid><businessid>hosta2</businessid><businessid>hostas2</businessid>"
                        + "<businessid>hosta</businessid><businessid>test</businessid><businessid>tests</businessid></business>";
                ObjectNode node = JsonHandler.createObjectNode();
                node.put(DbHostConfiguration.Columns.BUSINESS.name(), buz);
                logger.warn("Send query: " + RESTHANDLERS.DbHostConfiguration.uri);
                RestFuture future = clientHelper.sendQuery(HttpTestR66PseudoMain.config, channel,
                        HttpMethod.PUT, host, RESTHANDLERS.DbHostConfiguration.uri + "/hosta", key, value, null,
                        JsonHandler.writeAsString(node));
                try {
                    future.await();
                } catch (InterruptedException e) {
                }
                WaarpSslUtility.closingSslChannel(channel);
            }
            // Remove Hostzz
            channel = clientHelper.getChannel(host, HttpTestR66PseudoMain.config.REST_PORT);
            if (channel != null) {
                try {
                    RestFuture future = deleteData(channel, RESTHANDLERS.DbHostAuth);
                    try {
                        future.await();
                    } catch (InterruptedException e) {
                    }
                } catch (HttpInvalidAuthenticationException e1) {
                }
                WaarpSslUtility.closingSslChannel(channel);
            }
            // Shutdown
            channel = clientHelper.getChannel(host, HttpTestR66PseudoMain.config.REST_PORT);
            if (channel != null) {
                ShutdownOrBlockJsonPacket shutd = new ShutdownOrBlockJsonPacket();
                shutd.setRestartOrBlock(false);
                shutd.setShutdownOrBlock(true);
                shutd.setRequestUserPacket(LocalPacketFactory.SHUTDOWNPACKET);
                String pwd = "pwdhttp";
                byte[] bpwd = FilesystemBasedDigest.passwdCrypt(pwd.getBytes(WaarpStringUtils.UTF8));
                shutd.setKey(bpwd);
                logger.warn("Send query: " + RESTHANDLERS.Server.uri);
                RestFuture future = action(channel, HttpMethod.PUT, RESTHANDLERS.Server.uri, shutd);
                try {
                    future.await();
                } catch (InterruptedException e) {
                }
                WaarpSslUtility.closingSslChannel(channel);
            }
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {
        }
    } finally {
        logger.debug("ClientHelper closing");
        clientHelper.closeAll();
        logger.warn("ClientHelper closed");
    }
}

From source file:com.yobidrive.diskmap.DiskMapManager.java

public static void main(String[] args) {

    try {//w w  w. j  a  v  a2  s . c  o m
        BasicConfigurator.configure();
        //   (String storeName, String logPath, String keyPath, long ckeckPointPeriod, long logSize, int keySize,
        //      long mapSize, int packInterval, int readThreads, long needleCachedEntries, long bucketCachedBytes, short nodeId)

        /* String path = "/Users/david/Documents/NEEDLES";
        if ( args.length > 0) {
           path = args[0];
        }
        System.out.println("Using directory:" + path);
        */
        DiskMapManager dmm = new DiskMapManager("TestStore", "/drive_hd1", "/drive_ssd/storage_indexes", 1000, // Synching period
                60000 * 5, // Checkpointing period
                2000000000L, // Log file size
                100, // Key max size
                2048, // Nb of buffers
                32768, // Nb of entries (buckets) per buffer
                60, // Compact interval
                8, // Read threads
                TEST_COUNT / 3 * 2 * 140, // NeedleHeaderCachedEntries (1/20 in cache, 1/2 of typical data set)
                TEST_COUNT / 3 * 2 * 140, // NeedleCachedBytes (3% of Map, weights 120 bytes/entry)
                true, // Use needle average age instead of needle yougest age
                (short) 0, (short) 0, 0, new TokenSynchronizer(1), new TokenSynchronizer(1), null);
        // dmm.getBtm().getCheckPoint().copyFrom(new NeedlePointer()) ; // Removes checkpoint

        // System.out.println("Start read for "+TEST_COUNT+" pointers") ;
        Date startDate = new Date();
        Date lapDate = new Date();
        System.out.println("Start read test for " + TEST_COUNT + " pointers");
        long counter = 0;
        long lapCounter = 0;
        startDate = new Date();
        lapDate = new Date();
        long failCount = 0;
        counter = 0;
        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            List<Versioned<byte[]>> values = dmm.get(key.getBytes("UTF-8"));
            if (values.size() <= 0) {
                failCount++;
            } else {
                // Gets previous version   
                byte[] value = values.get(0).getValue();
                if (value == null)
                    failCount++;
                else if (value[0] != (byte) ((short) counter % 128))
                    failCount++;
            }
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPSR)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print(".") ;
                } else
                    // System.out.print("*") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }
        long timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nProcessed reading of " + TEST_COUNT + " pointers \n" + "\tTotal time: "
                + timeSpent / 1000 + "s\n" + "\tThroughput: " + (TEST_COUNT * 1000 / timeSpent) + " tps\n"
                + "\tBad results: " + failCount);

        startDate = new Date();
        lapDate = new Date();
        System.out.println("Start write test for " + TEST_COUNT + " pointers");
        counter = 0;
        lapCounter = 0;

        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            byte[] value = new byte[128000];
            value[0] = (byte) ((short) counter % 128);
            long chaseDurer = new Date().getTime();
            List<Versioned<byte[]>> previousValues = dmm.get(key.getBytes("UTF-8"));
            long chaseDurer2 = new Date().getTime();
            // System.out.println("Get in "+(chaseDurer2 -chaseDurer)+"ms") ;
            chaseDurer = chaseDurer2;
            Version newVersion = null;
            if (previousValues.size() <= 0) {
                newVersion = new VectorClock();
            } else {
                // Gets previous version
                newVersion = previousValues.get(0).cloneVersioned().getVersion();
            }
            // Increment version before writing
            ((VectorClock) newVersion).incrementVersion(dmm.getNodeId(), new Date().getTime());
            dmm.put(key.getBytes("UTF-8"), value, newVersion);
            chaseDurer2 = new Date().getTime();
            // System.out.println("Put in "+(chaseDurer2 -chaseDurer)+"ms") ;
            // dmm.putValue(key.getBytes("UTF-8"), value) ;
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPS)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print("("+counter+")") ;
                } else
                    // System.out.print("["+counter+"]") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }

        timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nWriting before cache commit of " + TEST_COUNT + " pointers \n"
                + "\tTotal time: " + timeSpent / 1000 + "s\n" + "\tThroughput: "
                + (TEST_COUNT * 1000 / timeSpent) + " tps");

        System.out.println("Start read for " + TEST_COUNT + " pointers");
        startDate = new Date();
        lapDate = new Date();
        failCount = 0;
        counter = 0;
        while (counter < TEST_COUNT) {
            String key = "MYVERYNICELEY" + counter;
            // System.out.println("key="+key+"...") ;
            List<Versioned<byte[]>> values = dmm.get(key.getBytes("UTF-8"));
            if (values.size() <= 0) {
                failCount++;
            } else {
                // Gets previous version   
                byte[] value = values.get(0).getValue();
                if (value == null)
                    failCount++;
                else if (value[0] != (byte) ((short) counter % 128))
                    failCount++;
            }
            counter++;
            Date lapDate2 = new Date();
            long spent = lapDate2.getTime() - lapDate.getTime();
            if (spent >= 1000 || (counter - lapCounter > TARGET_TPSR)) { // Check each second or target tps
                if (spent < 1000) { // pause when tps reached
                    Thread.sleep(1000 - spent);
                    // System.out.print(".") ;
                } else
                    // System.out.print("*") ;
                    lapDate = lapDate2; // Reset lap time
                lapCounter = counter; // Reset tps copunter
            }
        }
        timeSpent = new Date().getTime() - startDate.getTime();
        System.out.println("\n\nProcessed reading of " + TEST_COUNT + " pointers \n" + "\tTotal time: "
                + timeSpent / 1000 + "s\n" + "\tThroughput: " + (TEST_COUNT * 1000 / timeSpent) + " tps\n"
                + "\tBad results: " + failCount);

        dmm.stopService();
        System.out.println("Max cycle time = "
                + (dmm.getBtm().getMaxCycleTimePass1() + dmm.getBtm().getMaxCycleTimePass2()));

    } catch (Throwable th) {
        th.printStackTrace();
    }

}