Search in sources :

Example 1 with DHTOperationListener

use of com.biglybt.core.dht.DHTOperationListener in project BiglyBT by BiglySoftware.

the class DHTNATPuncherImpl method runRendezvousSupport.

protected void runRendezvousSupport() {
    try {
        DHTTransportContact latest_local;
        DHTTransportContact latest_target;
        try {
            pub_mon.enter();
            latest_local = rendezvous_local_contact;
            latest_target = rendezvous_target;
        } finally {
            pub_mon.exit();
        }
        if (current_local != null || latest_local != null) {
            if (current_local != latest_local) {
                if (current_local != null) {
                    if (!is_secondary) {
                        log("Removing publish for " + current_local.getString() + " -> " + current_target.getString());
                        dht.remove(getPublishKey(current_local), "DHTNatPuncher: removal of publish", new DHTOperationListener() {

                            @Override
                            public void searching(DHTTransportContact contact, int level, int active_searches) {
                            }

                            @Override
                            public void found(DHTTransportContact contact, boolean is_closest) {
                            }

                            @Override
                            public boolean diversified(String desc) {
                                return (true);
                            }

                            @Override
                            public void read(DHTTransportContact contact, DHTTransportValue value) {
                            }

                            @Override
                            public void wrote(DHTTransportContact contact, DHTTransportValue value) {
                            }

                            @Override
                            public void complete(boolean timeout) {
                            }
                        });
                    }
                }
                if (latest_local != null) {
                    // only 2 attempts to start with
                    rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT - 2;
                    if (!is_secondary) {
                        log("Adding publish for " + latest_local.getString() + " -> " + latest_target.getString());
                        final byte[] publish_key = getPublishKey(latest_local);
                        dht.put(publish_key, "NAT Traversal: rendezvous publish", encodePublishValue(latest_target), DHT.FLAG_SINGLE_VALUE, new DHTOperationListener() {

                            private final List<DHTTransportContact> written_to = new ArrayList<>();

                            @Override
                            public void searching(DHTTransportContact contact, int level, int active_searches) {
                            }

                            @Override
                            public void found(DHTTransportContact contact, boolean is_closest) {
                            }

                            @Override
                            public boolean diversified(String desc) {
                                return (true);
                            }

                            @Override
                            public void read(DHTTransportContact contact, DHTTransportValue value) {
                            }

                            @Override
                            public void wrote(DHTTransportContact contact, DHTTransportValue value) {
                                synchronized (written_to) {
                                    written_to.add(contact);
                                }
                            }

                            @Override
                            public void complete(boolean timeout) {
                                synchronized (written_to) {
                                    last_publish_key = publish_key;
                                    last_write_set = written_to;
                                }
                            }
                        });
                    }
                }
            } else if (current_target != latest_target) {
                // here current_local == latest_local and neither is null!
                // target changed, update publish
                // only 2 attempts to start with
                rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT - 2;
                if (!is_secondary) {
                    log("Updating publish for " + latest_local.getString() + " -> " + latest_target.getString());
                    final byte[] publish_key = getPublishKey(latest_local);
                    dht.put(publish_key, "DHTNatPuncher: update publish", encodePublishValue(latest_target), DHT.FLAG_SINGLE_VALUE, new DHTOperationListener() {

                        private final List<DHTTransportContact> written_to = new ArrayList<>();

                        @Override
                        public void searching(DHTTransportContact contact, int level, int active_searches) {
                        }

                        @Override
                        public void found(DHTTransportContact contact, boolean is_closest) {
                        }

                        @Override
                        public boolean diversified(String desc) {
                            return (true);
                        }

                        @Override
                        public void read(DHTTransportContact contact, DHTTransportValue value) {
                        }

                        @Override
                        public void wrote(DHTTransportContact contact, DHTTransportValue value) {
                            synchronized (written_to) {
                                written_to.add(contact);
                            }
                        }

                        @Override
                        public void complete(boolean timeout) {
                            synchronized (written_to) {
                                last_publish_key = publish_key;
                                last_write_set = written_to;
                            }
                        }
                    });
                }
            }
        }
        current_local = latest_local;
        current_target = latest_target;
        if (current_target != null) {
            long now = SystemTime.getMonotonousTime();
            int bind_result = sendBind(current_target);
            if (bind_result == RESP_OK) {
                trace("Rendezvous:" + current_target.getString() + " OK");
                rendevzous_fail_count = 0;
                rendezvous_last_ok_time = now;
                if (last_ok_rendezvous != current_target) {
                    last_ok_rendezvous = current_target;
                    log("Rendezvous " + latest_target.getString() + " operational");
                    for (DHTNATPuncherListener l : listeners) {
                        l.rendezvousChanged(current_target);
                    }
                }
            } else {
                rendezvous_last_fail_time = now;
                if (bind_result == RESP_NOT_OK) {
                    // denied access
                    rendevzous_fail_count = RENDEZVOUS_PING_FAIL_LIMIT;
                } else {
                    rendevzous_fail_count++;
                }
                if (rendevzous_fail_count == RENDEZVOUS_PING_FAIL_LIMIT) {
                    rendezvousFailed(current_target, false);
                }
            }
        }
    } catch (Throwable e) {
        log(e);
    }
}
Also used : DHTNATPuncherListener(com.biglybt.core.dht.nat.DHTNATPuncherListener) DHTOperationListener(com.biglybt.core.dht.DHTOperationListener)

Aggregations

DHTOperationListener (com.biglybt.core.dht.DHTOperationListener)1 DHTNATPuncherListener (com.biglybt.core.dht.nat.DHTNATPuncherListener)1