/** Performs the probe asynchronously. */ private void sendProbe() { final NameServicesChannelMessage message = new NameServicesChannelMessage(); message.agent = Nodel.getAgent(); List<String> discoveryList = new ArrayList<String>(1); discoveryList.add("*"); List<String> typesList = new ArrayList<String>(2); typesList.add("tcp"); typesList.add("http"); message.discovery = discoveryList; message.types = typesList; _lastProbe.set(System.nanoTime()); // IO is involved so use a thread-pool _threadPool.execute( new Runnable() { @Override public void run() { sendMessage(_sendSocket, s_sendSocketLabel, _groupSocketAddress, message); // check if hard links (direct "multicasting") are enabled for some hosts if (_hardLinksAddresses != null && _hardLinksSocket != null) { for (InetSocketAddress socketAddress : _hardLinksAddresses) { sendMessage(_hardLinksSocket, s_hardLinksSocketlabel, socketAddress, message); } } } }); }
/** Complete the necessary response, also a timer entry-point. */ private void completeResponse() { // prepare a message final NameServicesChannelMessage message = new NameServicesChannelMessage(); message.present = new ArrayList<String>(); // try keep the packets relatively small by roughly guessing how much space // its JSON advertisement packet might take up // account for "present"... // and // "addresses":["tcp://136.154.27.100:65017","http://136.154.27.100:8085/index.htm?node=%NODE%"] // so start off with approx. 110 chars long roughTotalSize = 110; while (_serviceIterator.hasNext()) { ServiceItem si = _serviceIterator.next(); String name = si._name.getOriginalName(); // calculate size to be the name, two inverted-commas, and a comma and a possible space in // between. int size = name.length() + 4; roughTotalSize += size; message.present.add(name); // make sure we're not going anywhere near UDP MTU (64K), // in fact, squeeze them into packets similar to size of Ethernet MTU (~1400 MTU) if (roughTotalSize > 1200) break; } // (while) message.addresses = new ArrayList<String>(); message.addresses.add(_nodelAddress); message.addresses.add(_httpAddress); // IO is involved so use thread-pool // (and only send if the 'present' list is not empty) if (message.present.size() > 0) { _threadPool.execute( new Runnable() { @Override public void run() { sendMessage(_sendSocket, s_sendSocketLabel, _recipient, message); } }); } // do we need this completed in the very near future? // if so, space out by at least 333ms. if (_serviceIterator.hasNext()) { _timerThread.schedule( new TimerTask() { @Override public void run() { completeResponse(); } }, 333); } else { synchronized (_responders) { _responders.remove(_recipient); } } } // (method)