protected char getPacketTypeCode(Class<? extends I2PBotePacket> dataType) { return dataType.getAnnotation(TypeCode.class).value(); }
@TypeCode('A') public class PeerListRequest extends CommunicationPacket { private Log log = new Log(PeerListRequest.class); public PeerListRequest() { } public PeerListRequest(byte[] data) { super(data); int remaining = data.length - CommunicationPacket.HEADER_LENGTH; if (remaining > 0) log.debug("Peer List Request packet has " + remaining + " extra bytes."); } @Override public byte[] toByteArray() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { writeHeader(outputStream); } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return outputStream.toByteArray(); } }
protected static Class<? extends I2PBotePacket> decodePacketTypeCode(char packetTypeCode) { for (Class<? extends I2PBotePacket> packetType: ALL_PACKET_TYPES) if (packetType.getAnnotation(TypeCode.class).value() == packetTypeCode) return packetType; Log log = new Log(I2PBotePacket.class); log.debug("Invalid type code for I2PBotePacket: <" + packetTypeCode + ">"); return null; }
@TypeCode('L') public class PeerList extends DataPacket { private Log log = new Log(PeerList.class);
/** * Overridden because the parent method doesn't work due to the {@link TypeCode} * annotation not being inherited. This method returns the correct * <code>EncryptedEmailPacket</code> type code. */ @Override protected char getPacketTypeCode(Class<? extends I2PBotePacket> dataType) { return EncryptedEmailPacket.class.getAnnotation(TypeCode.class).value(); } }
@TypeCode('Y') public class DeletionQuery extends CommunicationPacket { private Log log = new Log(DeletionQuery.class);
@Test public void testDecodePacketTypeCode() throws Exception { Field allPacketTypesField = I2PBotePacket.class.getDeclaredField("ALL_PACKET_TYPES"); allPacketTypesField.setAccessible(true); @SuppressWarnings("unchecked") Class<? extends I2PBotePacket>[] allPacketTypes = (Class<? extends I2PBotePacket>[])allPacketTypesField.get(null); for (Class<? extends I2PBotePacket> packetType: allPacketTypes) { TypeCode typeCode = packetType.getAnnotation(TypeCode.class); assertTrue(I2PBotePacket.decodePacketTypeCode(typeCode.value()).equals(packetType)); } } }
@TypeCode('Q') public class RetrieveRequest extends CommunicationPacket { private Log log = new Log(RetrieveRequest.class);
@TypeCode('F') public class FindClosePeersPacket extends CommunicationPacket { private Log log = new Log(FindClosePeersPacket.class);
@TypeCode('S') public class StoreRequest extends CommunicationPacket { private Log log = new Log(StoreRequest.class);
@TypeCode('N') public class ResponsePacket extends CommunicationPacket { private Log log = new Log(ResponsePacket.class);
@TypeCode('D') public class EmailPacketDeleteRequest extends DeleteRequest { private Log log = new Log(EmailPacketDeleteRequest.class);
@TypeCode('X') public class IndexPacketDeleteRequest extends DeleteRequest { private Log log = new Log(IndexPacketDeleteRequest.class);
@TypeCode('U') public class UnencryptedEmailPacket extends DataPacket { private final static int OVERHEAD = 72; // total packet size minus content size
@TypeCode('T') public class DeletionInfoPacket extends DataPacket implements Iterable<DeletionRecord> { private Collection<DeletionRecord> entries;
@TypeCode('E') public class EncryptedEmailPacket extends DhtStorablePacket { public static int MAX_OVERHEAD = 641; // The maximum number of bytes by which EncryptedEmailPacket can be bigger than the UnencryptedEmailPacket
@TypeCode('R') public class RelayRequest extends CommunicationPacket { private static Random random = new Random();
@TypeCode('C') public class Contact extends DhtStorablePacket { private final static Charset UTF8 = Charset.forName("UTF-8");
@TypeCode('I') public class IndexPacket extends DhtStorablePacket implements Iterable<IndexPacketEntry>, Splittable { private Log log = new Log(IndexPacket.class);