}
private RoutingTable() {
root = new Node(null, new Int128(), 0, new KBucket());
maintenanceTask = new Task() {
private LookupTask lookup_new_contacts = null;
public void run() {
if (getTotalContacts() < ROUTING_TABLE_DIFICIT_CONTACTS) {
if ((lookup_new_contacts == null)||(!lookup_new_contacts.isLookupStarted())) {
Int128 fake_target = new Int128(Utils.getRandomInt128());
lookup_new_contacts = new LookupTask(RequestType.FIND_NODE, fake_target, JKadConstants.toleranceZone) {
public void lookupTimeout() {
}
public void stopLookupEvent() {
}
public void processToleranceContacts(
ContactAddress sender,
List<KadContact> results) {
for(KadContact contact : results) {
addContact(contact);
}
if (getTotalContacts() > ROUTING_TABLE_DIFICIT_CONTACTS_STOP) {
Lookup.getSingleton().removeLookupTask(targetID);
}
}
};
Lookup.getSingleton().addLookupTask(lookup_new_contacts);
}
}
for(MaintenanceContact maintenance_contact : maintenanceContacts.values()) {
KadContact contact = maintenance_contact.kadContact;
long contact_time = System.currentTimeMillis() - contact.getLastResponse();
if (contact_time >= ROUTING_TABLE_CONTACT_TIMEOUT) {
int rcount = maintenance_contact.requestCount;
if (rcount<=contact.getContactType().toByte()+1) {
KadPacket hello_packet;
try {
hello_packet = PacketFactory.getHello2ReqPacket(TagList.EMPTY_TAG_LIST);
_network_manager.sendKadPacket(hello_packet, maintenance_contact.kadContact.getIPAddress(), maintenance_contact.kadContact.getUDPPort());
} catch (JMException e) {
e.printStackTrace();
}
maintenance_contact.requestCount++;
continue;
}
int beta = maintenance_contact.requestCount - maintenance_contact.responseCount;
boolean downgrate = false;
if (beta>contact.getContactType().toByte()+1)
downgrate = true;
if (downgrate) {
if (contact.getContactType() == ContactType.ScheduledForRemoval) {
maintenanceContacts.remove(contact.getContactAddress());
removeNode(contact);
continue;
}
contact.downgrateType();
notifyContactUpdated(contact);
if (contact.getContactType()!=ContactType.ScheduledForRemoval)
maintenanceContacts.remove(contact.getContactAddress());
continue;
}
maintenanceContacts.remove(contact.getContactAddress());
continue;
}
}
if (maintenanceContacts.size()>=ROUTING_TABLE_MAX_MAINTENANCE_CONTACTS) return ;
List<KadContact> candidatList = getContactsWithTimeout(ROUTING_TABLE_CONTACT_IGNORE_TIME);
if (candidatList.size() == 0) return ;
for(int i = 0;i<ROUTING_TABLE_MAINTENANCE_CONTACTS; i++) {
KadContact addContact = null;
do {
int id = Utils.getRandom(candidatList.size());
addContact = candidatList.get(id);
}while(maintenanceContacts.containsKey(addContact.getContactAddress()));
MaintenanceContact c = new MaintenanceContact(addContact);
maintenanceContacts.put(addContact.getContactAddress(), c);
KadPacket hello_packet;
try {
hello_packet = PacketFactory.getHello2ReqPacket(TagList.EMPTY_TAG_LIST);
_network_manager.sendKadPacket(hello_packet, addContact.getIPAddress(), addContact.getUDPPort());
} catch (JMException e) {
e.printStackTrace();
}
c.requestCount++;
}
}
};
routingTableSave = new Task() {
public void run() {
storeContacts();
}
};
contact_checker = new Task() {
public void run() {
for(KadContact contact : tree_nodes) {
long contact_time = System.currentTimeMillis() - contact.getLastResponse();
if (contact_time > Active2MoreHours.timeRequired()) {
if (contact.getContactType()==Active2MoreHours) continue;