public void run() { while (_datagramSocket != null && _datagramSocket == _socket && !_datagramSocket.isClosed()) { try { DatagramPacket packet = new DatagramPacket(new byte[MAX_PACKET_SIZE], MAX_PACKET_SIZE); _datagramSocket.receive(packet); DnsMessage answer = new DnsMessage(); answer.decode(ByteBuffer.wrap(packet.getData())); MsgContainer msgContainer; synchronized (_queries) { msgContainer = _queries.get(answer.getHeaderSection().getId()); } if (msgContainer != null) { synchronized (msgContainer.getQuery()) { msgContainer.setAnswer(answer); msgContainer.getQuery().notify(); } } else LOG.warn("Drop DNS Answser {}, as can not found a query with same ID", answer); } catch (IOException e) { close(); } } close(); LOG.debug("DNS acceptor done"); }
public void send(DnsMessage message) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(MAX_PACKET_SIZE); message.encode(buffer); DatagramPacket packet = new DatagramPacket(buffer.array(), buffer.capacity(), _remoteAddr, _remotePort); synchronized (_queries) { _queries.put(message.getHeaderSection().getId(), new MsgContainer(message)); } _socket = getDatagramSocket(); _socket.send(packet); }
public DnsMessage waitAnswer(DnsMessage request, int timeout) { synchronized (request) { try { request.wait(timeout); } catch (InterruptedException e) { } } MsgContainer messages; synchronized (_queries) { messages = _queries.remove(request.getHeaderSection().getId()); } if (messages == null) return null; return messages.getAnswer(); }