@Test public void parseTest() throws TrackerMessage.MessageValidationException { final int peersCount = 3; ByteBuffer response = ByteBuffer.allocate(20 + peersCount * 6); response.putInt(1); response.putInt(3); response.putInt(5); response.putInt(1); response.putInt(2); String ipPrefix = "192.168.1.1"; final int firstPort = 6881; for (int i = 0; i < peersCount; i++) { String ip = ipPrefix + i; InetSocketAddress peerAddress = new InetSocketAddress(ip, firstPort); response.put(peerAddress.getAddress().getAddress()); response.putShort((short) (firstPort + i)); } response.rewind(); UDPAnnounceResponseMessage parsedResponse = UDPAnnounceResponseMessage.parse(response); assertEquals(parsedResponse.getActionId(), 1); assertEquals(parsedResponse.getTransactionId(), 3); assertEquals(parsedResponse.getInterval(), 5); assertEquals(parsedResponse.getComplete(), 2); assertEquals(parsedResponse.getIncomplete(), 1); List<Peer> peers = parsedResponse.getPeers(); assertEquals(peers.size(), peersCount); for (int i = 0; i < peersCount; i++) { Peer peer = peers.get(i); assertEquals(peer.getIp(), ipPrefix + i); assertEquals(peer.getPort(), firstPort + i); } }
public static UDPAnnounceResponseMessage parse(ByteBuffer data) throws MessageValidationException { if (data.remaining() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (data.remaining() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException( "Invalid announce response message size!"); } if (data.getInt() != Type.ANNOUNCE_RESPONSE.getId()) { throw new MessageValidationException( "Invalid action code for announce response!"); } int transactionId = data.getInt(); int interval = data.getInt(); int incomplete = data.getInt(); int complete = data.getInt(); List<Peer> peers = new LinkedList<Peer>(); int totalPeersInResponse = data.remaining() / 6; for (int i = 0; i < totalPeersInResponse; i++) { try { byte[] ipBytes = new byte[4]; data.get(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = (0xFF & (int) data.get()) << 8 | (0xFF & (int) data.get()); peers.add(new Peer(new InetSocketAddress(ip, port))); } catch (UnknownHostException uhe) { throw new MessageValidationException( "Invalid IP address in announce request!"); } } return new UDPAnnounceResponseMessage(data, transactionId, interval, complete, incomplete, peers); }
UDPAnnounceResponseMessage extends UDPTrackerMessage.UDPTrackerResponseMessage implements AnnounceResponseMessage { public static UDPAnnounceResponseMessage parse(ByteBuffer data) throws MessageValidationException { if (data.remaining() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (data.remaining() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException( "Invalid announce response message size!"); } if (data.getInt() != Type.ANNOUNCE_RESPONSE.getId()) { throw new MessageValidationException( "Invalid action code for announce response!"); } int transactionId = data.getInt(); int interval = data.getInt(); int incomplete = data.getInt(); int complete = data.getInt(); List<Peer> peers = new LinkedList<Peer>(); int totalPeersInResponse = data.remaining() / 6; for (int i = 0; i < totalPeersInResponse; i++) { try { byte[] ipBytes = new byte[4]; data.get(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = (0xFF & (int) data.get()) << 8 | (0xFF & (int) data.get()); peers.add(new Peer(new InetSocketAddress(ip, port))); } catch (UnknownHostException uhe) { throw new MessageValidationException( "Invalid IP address in announce request!"); } } return new UDPAnnounceResponseMessage(data, transactionId, interval, complete, incomplete, peers); } }
UDPAnnounceResponseMessage extends UDPTrackerMessage.UDPTrackerResponseMessage implements AnnounceResponseMessage { public static UDPAnnounceResponseMessage parse(ByteBuffer data) throws MessageValidationException { if (data.remaining() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (data.remaining() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException( "Invalid announce response message size!"); } if (data.getInt() != Type.ANNOUNCE_RESPONSE.getId()) { throw new MessageValidationException( "Invalid action code for announce response!"); } int transactionId = data.getInt(); int interval = data.getInt(); int incomplete = data.getInt(); int complete = data.getInt(); List<Peer> peers = new LinkedList<Peer>(); int totalPeersInResponse = data.remaining() / 6; for (int i = 0; i < totalPeersInResponse; i++) { try { byte[] ipBytes = new byte[4]; data.get(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = (0xFF & (int) data.get()) << 8 | (0xFF & (int) data.get()); peers.add(new Peer(new InetSocketAddress(ip, port))); } catch (UnknownHostException uhe) { throw new MessageValidationException( "Invalid IP address in announce request!"); } } return new UDPAnnounceResponseMessage(data, transactionId, interval, complete, incomplete, peers); } private  UDPAnnounceResponseMessage(ByteBuffer data, int transactionId,
                                     int interval, int complete, int incomplete, List<Peer> peers); }
UDPAnnounceResponseMessage extends UDPTrackerMessage.UDPTrackerResponseMessage implements AnnounceResponseMessage { public static UDPAnnounceResponseMessage parse(ByteBuffer data) throws MessageValidationException { if (data.remaining() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (data.remaining() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException( "Invalid announce response message size!"); } if (data.getInt() != Type.ANNOUNCE_RESPONSE.getId()) { throw new MessageValidationException( "Invalid action code for announce response!"); } int transactionId = data.getInt(); int interval = data.getInt(); int incomplete = data.getInt(); int complete = data.getInt(); List<Peer> peers = new LinkedList<Peer>(); int totalPeersInResponse = data.remaining() / 6; for (int i = 0; i < totalPeersInResponse; i++) { try { byte[] ipBytes = new byte[4]; data.get(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = (0xFF & (int) data.get()) << 8 | (0xFF & (int) data.get()); peers.add(new Peer(new InetSocketAddress(ip, port))); } catch (UnknownHostException uhe) { throw new MessageValidationException( "Invalid IP address in announce request!"); } } return new UDPAnnounceResponseMessage(data, transactionId, interval, complete, incomplete, peers); } private  UDPAnnounceResponseMessage(ByteBuffer data, int transactionId,
                                     int interval, int complete, int incomplete, List<Peer> peers); @Override int getActionId(); @Override int getTransactionId(); @Override int getInterval(); @Override int getComplete(); @Override int getIncomplete(); @Override List<Peer> getPeers(); String getHexInfoHash(); static UDPAnnounceResponseMessage parse(ByteBuffer data); static UDPAnnounceResponseMessage craft(int transactionId,
                                                 int interval, int complete, int incomplete, List<Peer> peers); }
UDPAnnounceResponseMessage extends UDPTrackerMessage.UDPTrackerResponseMessage implements AnnounceResponseMessage { public static UDPAnnounceResponseMessage parse(ByteBuffer data) throws MessageValidationException { if (data.remaining() < UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE || (data.remaining() - UDP_ANNOUNCE_RESPONSE_MIN_MESSAGE_SIZE) % 6 != 0) { throw new MessageValidationException( "Invalid announce response message size!"); } if (data.getInt() != Type.ANNOUNCE_RESPONSE.getId()) { throw new MessageValidationException( "Invalid action code for announce response!"); } int transactionId = data.getInt(); int interval = data.getInt(); int incomplete = data.getInt(); int complete = data.getInt(); List<Peer> peers = new LinkedList<Peer>(); int totalPeersInResponse = data.remaining() / 6; for (int i = 0; i < totalPeersInResponse; i++) { try { byte[] ipBytes = new byte[4]; data.get(ipBytes); InetAddress ip = InetAddress.getByAddress(ipBytes); int port = (0xFF & (int) data.get()) << 8 | (0xFF & (int) data.get()); peers.add(new Peer(new InetSocketAddress(ip, port))); } catch (UnknownHostException uhe) { throw new MessageValidationException( "Invalid IP address in announce request!"); } } return new UDPAnnounceResponseMessage(data, transactionId, interval, complete, incomplete, peers); } private  UDPAnnounceResponseMessage(ByteBuffer data, int transactionId,
                                     int interval, int complete, int incomplete, List<Peer> peers); @Override int getActionId(); @Override int getTransactionId(); @Override int getInterval(); @Override int getComplete(); @Override int getIncomplete(); @Override List<Peer> getPeers(); String getHexInfoHash(); static UDPAnnounceResponseMessage parse(ByteBuffer data); static UDPAnnounceResponseMessage craft(int transactionId,
                                                 int interval, int complete, int incomplete, List<Peer> peers);  }