/** Sends the message to a recipient */ private void sendMessage( DatagramSocket socket, String label, InetSocketAddress to, NameServicesChannelMessage message) { if (isSameSocketAddress(socket, to)) _logger.info("{} sending message. to=self, message={}", label, message); else _logger.info("{} sending message. to={}, message={}", label, to, message); if (socket == null) { _logger.info("{} is not available yet; ignoring send request.", label); return; } // convert into bytes String json = Serialisation.serialise(message); byte[] bytes = json.getBytes(UTF8Charset.instance()); DatagramPacket packet = new DatagramPacket(bytes, bytes.length); packet.setSocketAddress(to); try { socket.send(packet); if (to.getAddress().isMulticastAddress()) { s_multicastOutData.addAndGet(bytes.length); s_multicastOutOps.incrementAndGet(); } else { s_unicastOutData.addAndGet(bytes.length); s_unicastOutOps.incrementAndGet(); } } catch (IOException exc) { if (!_enabled) return; if (socket.isClosed()) _logger.info(s_sendSocketLabel + " send() ignored as socket is being recycled."); else _logger.warn(s_sendSocketLabel + " send() failed. ", exc); } } // (method)
/** Parses the incoming packet. */ private NameServicesChannelMessage parsePacket(DatagramPacket dp) { String packetString = new String(dp.getData(), 0, dp.getLength(), UTF8Charset.instance()); return (NameServicesChannelMessage) Serialisation.coerceFromJSON(NameServicesChannelMessage.class, packetString); }