LOG.debug("handleResponse on {}", message.recipient());
if (!((message.type() == Type.REQUEST_1 || message.type() == Type.REQUEST_3) && message.key(0) != null && message
.key(1) != null)) {
throw new IllegalArgumentException("Message content is wrong");
}
final Message responseMessage = createResponseMessage(message, Type.OK);
// get data
Number160 locationKey = message.key(0);
Number160 domainKey = message.key(1);
SimpleBloomFilter<Number160> knownPeers = message.bloomFilter(0);
PublicKey publicKey = message.publicKey(0);
//
TrackerData meshPeers = trackerStorage.peers(new Number320(locationKey, domainKey));
LOG.debug("found peers on tracker: {}", meshPeers == null ? "null " : meshPeers.peerAddresses());
boolean couldProvideMoreData = false;
if (meshPeers != null) {
if (knownPeers != null) {
meshPeers = UtilsTracker.disjunction(meshPeers, knownPeers);
}
int size = meshPeers.size();
meshPeers = UtilsTracker.limit(meshPeers, TrackerRPC.MAX_MSG_SIZE_UDP);
couldProvideMoreData = size > meshPeers.size();
responseMessage.trackerData(meshPeers);
}
if (couldProvideMoreData) {
responseMessage.type(Message.Type.PARTIALLY_OK);
}
if (message.command() == RPC.Commands.TRACKER_ADD.getNr()) {
TrackerData trackerData = message.trackerData(0);
if (trackerData.size() != 1) {
responseMessage.type(Message.Type.EXCEPTION);
} else {
Map.Entry<PeerStatistic, Data> entry = trackerData.peerAddresses().entrySet().iterator().next();
if (!trackerStorage.put(new Number320(locationKey, domainKey), entry.getKey().peerAddress(), publicKey,
entry.getValue())) {
responseMessage.type(Message.Type.DENIED);
LOG.debug("tracker NOT put on({}) locationKey:{}, domainKey:{}, address:{}", peerBean()
.serverPeerAddress(), locationKey, domainKey, entry.getKey());
} else {
LOG.debug("tracker put on({}) locationKey:{}, domainKey:{}, address: {}", peerBean()
.serverPeerAddress(), locationKey, domainKey, entry.getKey());
}
}
} else {
LOG.debug("tracker get on({}) locationKey:{}, domainKey:{}, address:{}, returning: {}", peerBean()
.serverPeerAddress(), locationKey, domainKey, message.sender(), (meshPeers == null ? "0"
: meshPeers.size()));
}
if (sign) {
responseMessage.publicKeyAndSign(peerBean().getKeyPair());
}
responder.response(responseMessage);
}