for (int i = 0; i < clientCount; i++) {
byte clientID[] = new byte[16];
rawData.get(clientID);
int tagCount = shortToInt(rawData.get());
TagList tagList = new TagList();
for (int k = 0; k < tagCount; k++) {
Tag tag = TagScanner.scanTag(rawData);
tagList.addTag(tag);
}
ClientID client_id = new ClientID(clientID);
Source source = new Source(client_id, tagList);
source.setAddress(new IPAddress(packet.getAddress()));
source.setUDPPort(packet.getAddress().getPort());
KadContact contact = routing_table.getContact(client_id);
if (contact != null)
source.setTCPPort(contact.getTCPPort());
list.add(source);
}
boolean source_load = false;
for (Source source : list) {
boolean isSourcePublish = false;
isSourcePublish = source.getTagList()
.hasTag(TAG_SOURCETYPE);
if (isSourcePublish) {
indexer.addFileSource(targetID, source);
source_load = true;
} else
indexer.addFileSource(targetID, source);
}
KadPacket response = null;
if (source_load)
response = PacketFactory.getPublishResPacket(targetID,
indexer.getFileSourcesLoad());
else
response = PacketFactory.getPublishResPacket(targetID,
indexer.getKeywordLoad());
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA_PUBLISH_RES) {
byte targetID[] = new byte[16];
rawData.get(targetID);
int load = byteToInt(rawData.get());
publisher.processGenericResponse(new ClientID(targetID), load);
} else if (packetOPCode == KADEMLIA_SEARCH_NOTES_REQ) {
byte[] targetID = new byte[16];
rawData.get(targetID);
List<Source> source_list = indexer.getNoteSources(new Int128(
targetID));
KadPacket response = PacketFactory.getNotesRes(new Int128(
targetID), source_list);
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA_SEARCH_NOTES_RES) {
byte[] noteID = new byte[16];
rawData.get(noteID);
int resultCount = shortToInt(rawData.getShort());
List<Source> sourceList = new LinkedList<Source>();
for (int i = 0; i < resultCount; i++) {
byte[] clientID = new byte[16];
rawData.get(clientID);
Convert.updateSearchID(clientID);
int tagCount = shortToInt(rawData.get());
TagList tagList = new TagList();
for (int k = 0; k < tagCount; k++) {
Tag tag = TagScanner.scanTag(rawData);
tagList.addTag(tag);
}
Source source = new Source(new ClientID(clientID), tagList);
source.setAddress(new IPAddress(packet.getAddress()));
source.setUDPPort(packet.getAddress().getPort());
KadContact contact = RoutingTable.getSingleton()
.getContact(new ClientID(clientID));
if (contact != null)
source.setTCPPort(contact.getTCPPort());
sourceList.add(source);
}
search.processResults(packet.getAddress(), new Int128(noteID),
sourceList);
} else if (packetOPCode == KADEMLIA_PUBLISH_NOTES_REQ) {
byte[] noteID = new byte[16];
rawData.get(noteID);
byte[] publisherID = new byte[16];
rawData.get(publisherID);
int tagCount = byteToInt(rawData.get());
TagList tagList = new TagList();
for (int i = 0; i < tagCount; i++) {
Tag tag = TagScanner.scanTag(rawData);
tagList.addTag(tag);
}
ClientID publisher_id = new ClientID(publisherID);
Source source = new Source(publisher_id, tagList);
source.setAddress(new IPAddress(packet.getAddress()));
source.setUDPPort(packet.getAddress().getPort());
KadContact contact = routing_table.getContact(publisher_id);
if (contact != null)
source.setTCPPort(contact.getTCPPort());
indexer.addNoteSource(new Int128(noteID), source);
KadPacket response = PacketFactory.getPublishNotesRes(
new Int128(noteID), indexer.getNoteLoad());
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA_PUBLISH_NOTES_RES) {
byte[] noteID = new byte[16];
rawData.get(noteID);
int load = byteToInt(rawData.get());
publisher.processNoteResponse(new Int128(noteID), load);
} else if (packetOPCode == KADEMLIA_SEARCH_REQ) {
byte targetID[] = new byte[16];
rawData.get(targetID);
boolean sourceSearch = false;
if (rawData.limit() == 17)
if (rawData.get() == 1)
sourceSearch = true;
List<Source> source_list;
if (sourceSearch)
source_list = indexer.getFileSources(new Int128(targetID));
else
source_list = indexer
.getKeywordSources(new Int128(targetID));
KadPacket response = PacketFactory.getSearchResPacket(
new Int128(targetID), source_list);
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA_SEARCH_RES) {
byte targetID[] = new byte[16];
rawData.get(targetID);
int resultCount = shortToInt(rawData.getShort());
List<Source> sourceList = new LinkedList<Source>();
for (int i = 0; i < resultCount; i++) {
byte[] contactID = new byte[16];
rawData.get(contactID);
int tagCount = byteToInt(rawData.get());
TagList tagList = new TagList();
for (int k = 0; k < tagCount; k++) {
try {
Tag tag = TagScanner.scanTag(rawData);
if (tag == null)
continue;
tagList.addTag(tag);
} catch (Throwable t) {
t.printStackTrace();
}
}
ClientID client_id = new ClientID(contactID);
Source source = new Source(client_id, tagList);
KadContact contact = routing_table.getContact(client_id);
if (contact != null) {
source.setUDPPort(contact.getUDPPort());
source.setTCPPort(contact.getTCPPort());
}
sourceList.add(source);
}
search.processResults(packet.getAddress(),
new Int128(targetID), sourceList);
} else if (packetOPCode == KADEMLIA_FIREWALLED_REQ) {
int tcpPort = shortToInt(rawData.getShort());
firewallChecker.processFirewallRequest(packet.getAddress(),
tcpPort);
} else if (packetOPCode == KADEMLIA_FIREWALLED_RES) {
byte[] address = new byte[4];
rawData.get(address);
firewallChecker.porcessFirewallResponse(packet.getAddress(),
new IPAddress(address));
} else if (packetOPCode == KADEMLIA2_HELLO_REQ) {
byte[] client_id_raw = new byte[16];
rawData.get(client_id_raw);
ClientID clientID = new ClientID(client_id_raw);
int tcpPort = shortToInt(rawData.getShort());
byte version = rawData.get();
byte tag_count = rawData.get();
List<Tag> tag_list = new LinkedList<Tag>();
for (byte i = 0; i < tag_count; i++) {
Tag tag = TagScanner.scanTag(rawData);
if (tag == null)
throw new CorruptedPacketException();
tag_list.add(tag);
}
KadContact contact = routing_table.getContact(clientID);
if (contact != null) {
contact.setTCPPort(tcpPort);
contact.setUDPPort(packet.getAddress().getPort());
contact.setVersion(version);
}
KadPacket response = PacketFactory
.getHello2ResPacket(TagList.EMPTY_TAG_LIST);
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA2_HELLO_RES) {
byte[] client_id_raw = new byte[16];
rawData.get(client_id_raw);
ClientID clientID = new ClientID(client_id_raw);
int tcpPort = shortToInt(rawData.getShort());
byte version = rawData.get();
byte tag_count = rawData.get();
List<Tag> tag_list = new LinkedList<Tag>();
for (byte i = 0; i < tag_count; i++) {
Tag tag = TagScanner.scanTag(rawData);
if (tag == null)
throw new CorruptedPacketException();
tag_list.add(tag);
}
KadContact contact = routing_table.getContact(clientID);
if (contact != null) {
contact.setTCPPort(tcpPort);
contact.setVersion(version);
}
// ignore message if contact is not in routing table
/*
* ContactAddress address = new ContactAddress(new
* IPAddress(packet.getSender()),packet.getSender().getPort());
* KadContact add_contact = new KadContact(clientID, address,
* tcpPort,version, null);
* routing_table.addContact(add_contact);
*
* add_contact.setConnected(true);
*/
} else if (packetOPCode == KADEMLIA2_REQ) {
byte type = rawData.get();
byte[] client_id_raw = new byte[16];
rawData.get(client_id_raw);
ClientID targetClientID = new ClientID(client_id_raw);
client_id_raw = new byte[16];
rawData.get(client_id_raw);
ClientID receiverClientID = new ClientID(client_id_raw);
RequestType requestType = RequestType.FIND_VALUE;
switch (type) {
case JKadConstants.FIND_VALUE:
requestType = RequestType.FIND_VALUE;
break;
case JKadConstants.STORE:
requestType = RequestType.STORE;
break;
case JKadConstants.FIND_NODE:
requestType = RequestType.FIND_NODE;
break;
}
lookup.processRequest(packet.getAddress(), requestType,
targetClientID, receiverClientID, 2);
} else if (packetOPCode == KADEMLIA2_RES) {
byte[] client_id_raw = new byte[16];
rawData.get(client_id_raw);
int contactCount = byteToInt(rawData.get());
List<KadContact> contact_list = new LinkedList<KadContact>();
for (int i = 0; i < contactCount; i++) {
contact_list.add(getContact(rawData));
}
lookup.processResponse(packet.getAddress(), new ClientID(
client_id_raw), contact_list);
} else if (packetOPCode == KADEMLIA2_PUBLISH_KEY_REQ) {
byte[] client_id = new byte[16];
rawData.get(client_id);
ClientID clientID = new ClientID(client_id);
int count = rawData.getShort();
for (int i = 0; i < count; i++) {
byte[] hash = new byte[16];
rawData.get(hash);
byte tagCount = rawData.get();
TagList tag_list = new TagList();
for (int j = 0; j < tagCount; j++) {
Tag tag = TagScanner.scanTag(rawData);
if (tag == null)
throw new CorruptedPacketException();
tag_list.addTag(tag);
}
Source source = new Source(clientID, tag_list);
source.setAddress(new IPAddress(packet.getAddress()));
source.setUDPPort(packet.getAddress().getPort());
KadContact contact = routing_table.getContact(clientID);
if (contact != null)
source.setTCPPort(contact.getTCPPort());
indexer.addKeywordSource(new Int128(hash), source);
}
KadPacket response = PacketFactory.getPublishRes2Packet(
clientID, indexer.getKeywordLoad());
_network_manager.sendKadPacket(response, sender_address,
sender_port);
} else if (packetOPCode == KADEMLIA2_PUBLISH_RES) {
byte targetID[] = new byte[16];
rawData.get(targetID);
int load = byteToInt(rawData.get());
publisher.processGenericResponse(new ClientID(targetID), load);
} else if (packetOPCode == KADEMLIA2_PUBLISH_SOURCE_REQ) {
byte[] client_id = new byte[16];
byte[] source_id = new byte[16];
rawData.get(client_id);
rawData.get(source_id);
int tagCount = rawData.get();
TagList tag_list = new TagList();
for (int i = 0; i < tagCount; i++) {
Tag tag = TagScanner.scanTag(rawData);
if (tag == null)
throw new CorruptedPacketException();
tag_list.addTag(tag);
}
Source source = new Source(new ClientID(client_id), tag_list);
source.setAddress(new IPAddress(packet.getAddress()));
source.setUDPPort(packet.getAddress().getPort());