@Test public void parseTest() throws IOException, TrackerMessage.MessageValidationException { Map<String, BEValue> trackerResponse = new HashMap<String, BEValue>(); trackerResponse.put("interval", new BEValue(5)); trackerResponse.put("complete", new BEValue(1)); trackerResponse.put("incomplete", new BEValue(0)); String ip = "192.168.1.1"; int port = 6881; InetSocketAddress peerAddress = new InetSocketAddress(ip, port); ByteBuffer binaryPeerAddress = ByteBuffer.allocate(6); binaryPeerAddress.put(peerAddress.getAddress().getAddress()); binaryPeerAddress.putShort((short) port); trackerResponse.put("peers", new BEValue(binaryPeerAddress.array())); HTTPAnnounceResponseMessage parsedResponse = (HTTPAnnounceResponseMessage) HTTPAnnounceResponseMessage.parse( new ByteArrayInputStream(BEncoder.bencode(trackerResponse).array())); assertEquals(parsedResponse.getInterval(), 5); assertEquals(parsedResponse.getComplete(), 1); assertEquals(parsedResponse.getIncomplete(), 0); List<Peer> peers = parsedResponse.getPeers(); assertEquals(peers.size(), 1); Peer peer = peers.get(0); assertEquals(peer.getIp(), ip); assertEquals(peer.getPort(), port); }
public static HTTPAnnounceResponseMessage parse(BEValue decoded) throws IOException, MessageValidationException { if (decoded == null) { throw new MessageValidationException( "Could not decode tracker message (not B-encoded?)!"); } Map<String, BEValue> params = decoded.getMap(); if (params.get("interval") == null) { throw new MessageValidationException("Tracker message missing mandatory field 'interval'!"); } try { List<Peer> peers; try { peers = toPeerList(params.get("peers").getBytes()); } catch (InvalidBEncodingException ibee) { peers = toPeerList(params.get("peers").getList()); } if (params.get("torrentIdentifier") != null) { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers, params.get("torrentIdentifier").getString()); } else { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers); } } catch (InvalidBEncodingException ibee) { throw new MessageValidationException("Invalid response " + "from tracker!", ibee); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid peer " + "in tracker response!", uhe); } }
HTTPAnnounceResponseMessage extends HTTPTrackerMessage implements AnnounceResponseMessage { public static HTTPAnnounceResponseMessage parse(BEValue decoded) throws IOException, MessageValidationException { if (decoded == null) { throw new MessageValidationException( "Could not decode tracker message (not B-encoded?)!"); } Map<String, BEValue> params = decoded.getMap(); if (params.get("interval") == null) { throw new MessageValidationException("Tracker message missing mandatory field 'interval'!"); } try { List<Peer> peers; try { peers = toPeerList(params.get("peers").getBytes()); } catch (InvalidBEncodingException ibee) { peers = toPeerList(params.get("peers").getList()); } if (params.get("torrentIdentifier") != null) { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers, params.get("torrentIdentifier").getString()); } else { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers); } } catch (InvalidBEncodingException ibee) { throw new MessageValidationException("Invalid response " + "from tracker!", ibee); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid peer " + "in tracker response!", uhe); } } }
HTTPAnnounceResponseMessage extends HTTPTrackerMessage implements AnnounceResponseMessage { public static HTTPAnnounceResponseMessage parse(BEValue decoded) throws IOException, MessageValidationException { if (decoded == null) { throw new MessageValidationException( "Could not decode tracker message (not B-encoded?)!"); } Map<String, BEValue> params = decoded.getMap(); if (params.get("interval") == null) { throw new MessageValidationException("Tracker message missing mandatory field 'interval'!"); } try { List<Peer> peers; try { peers = toPeerList(params.get("peers").getBytes()); } catch (InvalidBEncodingException ibee) { peers = toPeerList(params.get("peers").getList()); } if (params.get("torrentIdentifier") != null) { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers, params.get("torrentIdentifier").getString()); } else { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers); } } catch (InvalidBEncodingException ibee) { throw new MessageValidationException("Invalid response " + "from tracker!", ibee); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid peer " + "in tracker response!", uhe); } } private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers); private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers, String hexInfoHash); }
HTTPAnnounceResponseMessage extends HTTPTrackerMessage implements AnnounceResponseMessage { public static HTTPAnnounceResponseMessage parse(BEValue decoded) throws IOException, MessageValidationException { if (decoded == null) { throw new MessageValidationException( "Could not decode tracker message (not B-encoded?)!"); } Map<String, BEValue> params = decoded.getMap(); if (params.get("interval") == null) { throw new MessageValidationException("Tracker message missing mandatory field 'interval'!"); } try { List<Peer> peers; try { peers = toPeerList(params.get("peers").getBytes()); } catch (InvalidBEncodingException ibee) { peers = toPeerList(params.get("peers").getList()); } if (params.get("torrentIdentifier") != null) { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers, params.get("torrentIdentifier").getString()); } else { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers); } } catch (InvalidBEncodingException ibee) { throw new MessageValidationException("Invalid response " + "from tracker!", ibee); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid peer " + "in tracker response!", uhe); } } private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers); private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers, String hexInfoHash); @Override int getInterval(); @Override int getComplete(); @Override int getIncomplete(); @Override List<Peer> getPeers(); String getHexInfoHash(); static HTTPAnnounceResponseMessage parse(BEValue decoded); static HTTPAnnounceResponseMessage craft(int interval,
                                                  int complete, int incomplete,
                                                  List<Peer> peers, String hexInfoHash); }
HTTPAnnounceResponseMessage extends HTTPTrackerMessage implements AnnounceResponseMessage { public static HTTPAnnounceResponseMessage parse(BEValue decoded) throws IOException, MessageValidationException { if (decoded == null) { throw new MessageValidationException( "Could not decode tracker message (not B-encoded?)!"); } Map<String, BEValue> params = decoded.getMap(); if (params.get("interval") == null) { throw new MessageValidationException("Tracker message missing mandatory field 'interval'!"); } try { List<Peer> peers; try { peers = toPeerList(params.get("peers").getBytes()); } catch (InvalidBEncodingException ibee) { peers = toPeerList(params.get("peers").getList()); } if (params.get("torrentIdentifier") != null) { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers, params.get("torrentIdentifier").getString()); } else { return new HTTPAnnounceResponseMessage(Constants.EMPTY_BUFFER, params.get("interval").getInt(), params.get("complete") != null ? params.get("complete").getInt() : 0, params.get("incomplete") != null ? params.get("incomplete").getInt() : 0, peers); } } catch (InvalidBEncodingException ibee) { throw new MessageValidationException("Invalid response " + "from tracker!", ibee); } catch (UnknownHostException uhe) { throw new MessageValidationException("Invalid peer " + "in tracker response!", uhe); } } private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers); private  HTTPAnnounceResponseMessage(ByteBuffer data,
                                      int interval, int complete, int incomplete, List<Peer> peers, String hexInfoHash); @Override int getInterval(); @Override int getComplete(); @Override int getIncomplete(); @Override List<Peer> getPeers(); String getHexInfoHash(); static HTTPAnnounceResponseMessage parse(BEValue decoded); static HTTPAnnounceResponseMessage craft(int interval,
                                                  int complete, int incomplete,
                                                  List<Peer> peers, String hexInfoHash);  }