Example usage for java.lang String notify

List of usage examples for java.lang String notify

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native void notify();

Source Link

Document

Wakes up a single thread that is waiting on this object's monitor.

Usage

From source file:tor.HiddenService.java

public static String fetchHSDescriptor(TorSocket sock, final String onion) throws IOException {
    // get list of ORs with resposibility for this HS
    OnionRouter ors[] = findResposibleDirectories(onion);
    // loop through responsible directories until successful
    for (int i = 0; i < ors.length; i++) {
        OnionRouter or = ors[i];/*  w  w w.j a  v a  2 s.c o m*/
        log.debug("Trying Directory Server: {}", or);

        // establish circuit to responsible director
        TorCircuit circ = sock.createCircuit(true);
        try {
            circ.create();
            circ.extend(ors[0]);
        } catch (TorCircuitException e) {
            log.error("HS fetched failed due to circuit failure - moving to next directory");
            continue;
        }

        final int replica = i < 3 ? 0 : 1;

        // asynchronous call
        TorStream st = circ.createDirStream(new TorStream.TorStreamListener() {
            @Override
            public void dataArrived(TorStream s) {
            }

            @Override
            public void connected(TorStream s) {
                try {
                    s.sendHTTPGETRequest("/tor/rendezvous2/"
                            + new Base32().encodeAsString(HiddenService.getDescId(onion, (byte) replica)),
                            "dirreq");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void disconnected(TorStream s) {
                synchronized (onion) {
                    onion.notify();
                }
            }

            @Override
            public void failure(TorStream s) {
                synchronized (onion) {
                    onion.notify();
                }
            }
        });

        // wait for notification from the above listener that data is here! (that remote side ended connection - data could be blank
        synchronized (onion) {
            try {
                onion.wait(1000);
                if (circ.state == TorCircuit.STATES.DESTROYED) {
                    System.out.println("HS - Desc Fetch - Circuit Destroyed");
                    throw new TorCircuitException("circuit destroyed");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // get HTTP response and body
        String data = IOUtils.toString(st.getInputStream());
        circ.destroy();

        // HTTP success code
        if (data.length() < 1 || !data.split(" ")[1].equals("200")) {
            continue;
        }

        int dataIndex = data.indexOf("\r\n\r\n");
        return data.substring(dataIndex);
    }

    log.warn("Not found hs descriptor!");
    return null;
}

From source file:tor.HiddenService.java

public static String HSDescriptorRequest(TorSocket sock, final String descriptor_id, final String fprint,
        final String descriptor) throws IOException {
    // Send GET and POST requests to specified HSDir.

    Consensus con = Consensus.getConsensus();
    OnionRouter or = null;//from   www .  ja  v a2  s .  co m
    // Try get the requested OR from the consensus
    if (con.routers.containsKey(fprint)) {
        or = con.routers.get(fprint);
    } else {
        log.error("Could not find the request HSDir in the consensus");
        throw new IOException(
                "Could not find the requested HSDir in the consensus. Check the fingerprint is correct");
    }

    log.debug("Trying Directory Server: {}", or);

    // establish circuit to responsible directory
    TorCircuit circ = sock.createCircuit(true);
    try {
        circ.create();
        circ.extend(or);
    } catch (TorCircuitException e) {
        throw new IOException("HS Fetch failed due to circuit failure, you should retry.");
    }

    // asynchronous call
    TorStream st = circ.createDirStream(new TorStream.TorStreamListener() {
        @Override
        public void dataArrived(TorStream s) {
        }

        @Override
        public void connected(TorStream s) {
            try {
                if (descriptor != null && !descriptor.isEmpty()) {
                    s.sendHTTPPOSTRequest("/tor/rendezvous2/publish", "dirreq", descriptor);
                } else {
                    s.sendHTTPGETRequest("/tor/rendezvous2/" + descriptor_id, "dirreq");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void disconnected(TorStream s) {
            synchronized (fprint) {
                fprint.notify();
            }
        }

        @Override
        public void failure(TorStream s) {
            synchronized (fprint) {
                fprint.notify();
            }
        }
    });

    // wait for notification from the above listener that data is here! (that remote side ended connection - data could be blank
    synchronized (fprint) {
        try {
            fprint.wait(1000);
            if (circ.state == TorCircuit.STATES.DESTROYED) {
                System.out.println("HS - Desc Fetch - Circuit Destroyed");
                throw new TorCircuitException("circuit destroyed");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new IOException("Circuit failed");
        }
    }

    // get HTTP response and body
    String data = IOUtils.toString(st.getInputStream());
    circ.destroy();

    // HTTP success code
    if (data.length() < 1 || !data.split(" ")[1].equals("200")) {
        throw new IOException("HTTPError: " + data); // Throw the error
    }

    int dataIndex = data.indexOf("\r\n\r\n");
    return data.substring(dataIndex);
}