Search in sources :

Example 1 with Session

use of com.biglybt.plugin.net.netstatus.NetStatusProtocolTesterBT.Session in project BiglyBT by BiglySoftware.

the class NetStatusProtocolTester method runTest.

public NetStatusProtocolTesterBT runTest(String test_address, final NetStatusProtocolTesterListener listener) {
    final NetStatusProtocolTesterBT bt_tester = new NetStatusProtocolTesterBT(this, true);
    bt_tester.addListener(listener);
    bt_tester.start();
    addToActive(bt_tester);
    try {
        if (test_address.length() == 0) {
            DHT[] dhts = dht_plugin.getDHTs();
            DHT target_dht = null;
            int target_network = Constants.isCVSVersion() ? DHT.NW_AZ_CVS : DHT.NW_AZ_MAIN;
            for (int i = 0; i < dhts.length; i++) {
                if (dhts[i].getTransport().getNetwork() == target_network) {
                    target_dht = dhts[i];
                    break;
                }
            }
            if (target_dht == null) {
                listener.logError("Distributed database unavailable");
            } else {
                DHTTransportContact[] contacts = target_dht.getTransport().getReachableContacts();
                final List f_contacts = new ArrayList(Arrays.asList(contacts));
                final int[] ok = new int[] { 0 };
                final int num_threads = Math.min(BT_MAX_SLAVES, contacts.length);
                listener.log("Searching " + contacts.length + " contacts for " + num_threads + " test targets", false);
                final AESemaphore sem = new AESemaphore("NetStatusProbe");
                for (int i = 0; i < num_threads; i++) {
                    new AEThread2("NetStatusProbe", true) {

                        @Override
                        public void run() {
                            try {
                                while (!bt_tester.isDestroyed()) {
                                    DHTTransportContact contact = null;
                                    synchronized (ok) {
                                        if (ok[0] < num_threads && f_contacts.size() > 0) {
                                            contact = (DHTTransportContact) f_contacts.remove(0);
                                        }
                                    }
                                    if (contact == null) {
                                        break;
                                    }
                                    try {
                                        DistributedDatabaseContact ddb_contact = ddb.importContact(contact.getAddress());
                                        if (tryTest(bt_tester, ddb_contact)) {
                                            synchronized (ok) {
                                                ok[0]++;
                                            }
                                        }
                                    } catch (Throwable e) {
                                        listener.logError("Contact import for " + contact.getName() + " failed", e);
                                    }
                                }
                            } finally {
                                sem.release();
                            }
                        }
                    }.start();
                }
                for (int i = 0; i < num_threads; i++) {
                    sem.reserve();
                }
                listener.log("Searching complete, " + ok[0] + " targets found", false);
            }
        } else {
            String[] bits = test_address.split(":");
            if (bits.length != 2) {
                log("Invalid address - use <host>:<port> ");
                return (bt_tester);
            }
            InetSocketAddress address = new InetSocketAddress(bits[0].trim(), Integer.parseInt(bits[1].trim()));
            DistributedDatabaseContact contact = ddb.importContact(address);
            tryTest(bt_tester, contact);
        }
    } catch (Throwable e) {
        listener.logError("Test failed", e);
    } finally {
        bt_tester.addListener(new NetStatusProtocolTesterListener() {

            @Override
            public void sessionAdded(Session session) {
            }

            @Override
            public void complete(NetStatusProtocolTesterBT tester) {
                removeFromActive(tester);
            }

            @Override
            public void log(String str, boolean detailed) {
            }

            @Override
            public void logError(String str) {
            }

            @Override
            public void logError(String str, Throwable e) {
            }
        });
        bt_tester.setOutboundConnectionsComplete();
        new DelayedEvent("NetStatus:killer", 10 * 1000, new AERunnable() {

            @Override
            public void runSupport() {
                listener.log("Destroying tester", false);
                bt_tester.destroy();
            }
        });
    }
    return (bt_tester);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DHT(com.biglybt.core.dht.DHT) DHTTransportContact(com.biglybt.core.dht.transport.DHTTransportContact) Session(com.biglybt.plugin.net.netstatus.NetStatusProtocolTesterBT.Session)

Aggregations

DHT (com.biglybt.core.dht.DHT)1 DHTTransportContact (com.biglybt.core.dht.transport.DHTTransportContact)1 Session (com.biglybt.plugin.net.netstatus.NetStatusProtocolTesterBT.Session)1 InetSocketAddress (java.net.InetSocketAddress)1