public class Network {
private static final String STR_TABLE_LIST = "table_list_";
public static serverObjects respond(final RequestHeader requestHeader, final serverObjects post, final serverSwitch switchboard) {
final Switchboard sb = (Switchboard) switchboard;
final long start = System.currentTimeMillis();
final serverObjects prop = new serverObjects();
prop.put("menu", post == null ? 2 : (post.get("menu", "").equals("embed")) ? 0 : (post.get("menu","").equals("simple")) ? 1 : 2);
if (sb.peers.mySeed() != null) prop.put("menu_newpeer_peerhash", sb.peers.mySeed().hash);
prop.setLocalized(!(requestHeader.get(HeaderFramework.CONNECTION_PROP_PATH)).endsWith(".xml"));
prop.putHTML("page_networkTitle", sb.getConfig("network.unit.description", "unspecified"));
prop.putHTML("page_networkName", sb.getConfig(SwitchboardConstants.NETWORK_NAME, "unspecified"));
final boolean overview = (post == null) || (post.get("page", "0").equals("0"));
final String mySeedType = sb.peers.mySeed().get(Seed.PEERTYPE, Seed.PEERTYPE_VIRGIN);
final boolean iAmActive = (mySeedType.equals(Seed.PEERTYPE_SENIOR) || mySeedType.equals(Seed.PEERTYPE_PRINCIPAL));
if (overview) {
long accActLinks = sb.peers.countActiveURL();
long accActWords = sb.peers.countActiveRWI();
final long accPassLinks = sb.peers.countPassiveURL();
final long accPassWords = sb.peers.countPassiveRWI();
long accPotLinks = sb.peers.countPotentialURL();
long accPotWords = sb.peers.countPotentialRWI();
int conCount = sb.peers.sizeConnected();
final int disconCount = sb.peers.sizeDisconnected();
int potCount = sb.peers.sizePotential();
// final boolean complete = ((post == null) ? false : post.get("links", "false").equals("true"));
final long otherppm = sb.peers.countActivePPM();
final double otherqpm = sb.peers.countActiveQPM();
long myppm = 0;
double myqph = 0d;
// create own peer info
final Seed seed = sb.peers.mySeed();
if (sb.peers.mySeed() != null){ //our Peer
// update seed info
sb.updateMySeed();
final long LCount = seed.getLinkCount();
final long ICount = seed.getWordCount();
final long RCount = seed.getLong(Seed.RCOUNT, 0L);
// my-info
prop.putHTML("table_my-name", seed.get(Seed.NAME, "-") );
prop.put("table_my-hash", seed.hash );
if (sb.peers.mySeed().isVirgin()) {
prop.put("table_my-info", 0);
} else if(sb.peers.mySeed().isJunior()) {
prop.put("table_my-info", 1);
accPotLinks += LCount;
accPotWords += ICount;
} else if(sb.peers.mySeed().isSenior()) {
prop.put("table_my-info", 2);
accActLinks += LCount;
accActWords += ICount;
} else if(sb.peers.mySeed().isPrincipal()) {
prop.put("table_my-info", 3);
accActLinks += LCount;
accActWords += ICount;
}
prop.put("table_my-acceptcrawl", seed.getFlagAcceptRemoteCrawl() ? 1 : 0);
prop.put("table_my-dhtreceive", seed.getFlagAcceptRemoteIndex() ? 1 : 0);
myppm = sb.currentPPM();
myqph = 60d * sb.averageQPM();
prop.put("table_my-version", seed.get(Seed.VERSION, "-"));
prop.put("table_my-utc", seed.get(Seed.UTC, "-"));
prop.put("table_my-uptime", PeerActions.formatInterval(60000 * seed.getLong(Seed.UPTIME, 0)));
prop.putNum("table_my-LCount", LCount);
prop.putNum("table_my-ICount", ICount);
prop.putNum("table_my-RCount", RCount);
prop.putNum("table_my-sI", seed.getLong(Seed.INDEX_OUT, 0L));
prop.putNum("table_my-sU", seed.getLong(Seed.URL_OUT, 0L));
prop.putNum("table_my-rI", seed.getLong(Seed.INDEX_IN, 0L));
prop.putNum("table_my-rU", seed.getLong(Seed.URL_IN, 0L));
prop.putNum("table_my-ppm", myppm);
prop.putNum("table_my-qph", Math.round(100d * myqph) / 100d);
prop.putNum("table_my-qph-publocal", Math.round(6000d * sb.averageQPMPublicLocal()) / 100d);
prop.putNum("table_my-qph-pubremote", Math.round(6000d * sb.averageQPMGlobal()) / 100d);
prop.putNum("table_my-seeds", seed.getLong(Seed.SCOUNT, 0L));
prop.putNum("table_my-connects", seed.getFloat(Seed.CCOUNT, 0F));
prop.put("table_my-url", seed.get(Seed.SEEDLISTURL, ""));
// generating the location string
prop.putHTML("table_my-location", ClientIdentification.generateLocation());
}
// overall results: Network statistics
if (iAmActive) conCount++; else if (mySeedType.equals(Seed.PEERTYPE_JUNIOR)) potCount++;
final int activeLastMonth = sb.peers.sizeActiveSince(30 * 1440);
final int activeLastWeek = sb.peers.sizeActiveSince(7 * 1440);
final int activeLastDay = sb.peers.sizeActiveSince(1440);
final int activeSwitch =
(activeLastDay <= conCount) ? 0 :
(activeLastWeek <= activeLastDay) ? 1 :
(activeLastMonth <= activeLastWeek) ? 2 : 3;
prop.putNum("table_active-switch", activeSwitch);
prop.putNum("table_active-switch_last-month", activeLastMonth);
prop.putNum("table_active-switch_last-week", activeLastWeek);
prop.putNum("table_active-switch_last-day", activeLastDay);
prop.putNum("table_active-count", conCount);
prop.putNum("table_active-links", accActLinks);
prop.putNum("table_active-words", accActWords);
prop.putNum("table_passive-count", disconCount);
prop.putNum("table_passive-links", accPassLinks);
prop.putNum("table_passive-words", accPassWords);
prop.putNum("table_potential-count", potCount);
prop.putNum("table_potential-links", accPotLinks);
prop.putNum("table_potential-words", accPotWords);
prop.putNum("table_all-count", conCount + disconCount + potCount);
prop.putNum("table_all-links", accActLinks + accPassLinks + accPotLinks);
prop.putNum("table_all-words", accActWords + accPassWords + accPotWords);
prop.putNum("table_gppm", otherppm + ((iAmActive) ? myppm : 0));
prop.putNum("table_gqph", Math.round(6000d * otherqpm + 100d * ((iAmActive) ? myqph : 0d)) / 100d);
prop.put("table", 2); // triggers overview
prop.put("page", 0);
} else if (post != null && post.getInt("page", 1) == 4) {
prop.put("table", 4); // triggers overview
prop.put("page", 4);
if (sb.peers.mySeed() != null) {
prop.put("table_my-hash", sb.peers.mySeed().hash );
prop.put("table_my-ip", sb.peers.mySeed().getIP() );
prop.put("table_my-port", sb.peers.mySeed().getPort() );
}
if (post.containsKey("addPeer")) {
// AUTHENTICATE
if (!requestHeader.containsKey(RequestHeader.AUTHORIZATION)) {
prop.putHTML("AUTHENTICATE","log-in");
return prop;
}
final ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();
map.put(Seed.IP, post.get("peerIP"));
map.put(Seed.PORT, post.get("peerPort"));
Seed peer = new Seed(post.get("peerHash"), map);
sb.updateMySeed();
final int added = Protocol.hello(sb.peers.mySeed(), sb.peers.peerActions, peer.getPublicAddress(), peer.hash, peer.getName());
if (added <= 0) {
prop.put("table_comment",1);
prop.putHTML("table_comment_status","publish: disconnected peer '" + peer.getName() + "/" + post.get("peerHash") + "' from " + peer.getPublicAddress());
} else {
peer = sb.peers.getConnected(peer.hash);
if (peer == null) {
prop.put("table_comment",1);
prop.putHTML("table_comment_status","publish: disconnected peer 'UNKNOWN/" + post.get("peerHash") + "' from UNKNOWN");
} else {
prop.put("table_comment",2);
prop.putHTML("table_comment_status","publish: handshaked " + peer.get(Seed.PEERTYPE, Seed.PEERTYPE_SENIOR) + " peer '" + peer.getName() + "' at " + peer.getPublicAddress());
prop.putHTML("table_comment_details",peer.toString());
}
}
prop.putHTML("table_peerHash",post.get("peerHash"));
prop.putHTML("table_peerIP",post.get("peerIP"));
prop.putHTML("table_peerPort",post.get("peerPort"));
} else {
prop.put("table_peerHash","");
prop.put("table_peerIP","");
prop.put("table_peerPort","");
prop.put("table_comment",0);
}
} else {
// generate table
final int page = (post == null ? 1 : post.getInt("page", 1));
final int maxCount = (post == null ? 9000 : post.getInt("maxCount", 9000));
int conCount = 0;
if (sb.peers == null) {
prop.put("table", 0);//no remote senior/principal proxies known"
} else {
int size = 0;
switch (page) {
case 1 : size = sb.peers.sizeConnected(); break;
case 2 : size = sb.peers.sizeDisconnected(); break;
case 3 : size = sb.peers.sizePotential(); break;
default: break;
}
if (size == 0) {
prop.put("table", 0);//no remote senior/principal proxies known"
} else {
// add temporary the own seed to the database
if (iAmActive) {
sb.updateMySeed();
sb.peers.addConnected(sb.peers.mySeed());
}
// find updated Information using YaCyNews
final HashSet<String> updatedProfile = new HashSet<String>();