Add Checkstyle to enforce codestyle rules
Rules are based on Sun style with a few minor modifications
Change-Id: I9457a669ee629ad6d92c5c3824e562723c7ef9d2
diff --git a/src/main/java/com/intel/jndn/mock/MockFace.java b/src/main/java/com/intel/jndn/mock/MockFace.java
index f47959c..442e453 100644
--- a/src/main/java/com/intel/jndn/mock/MockFace.java
+++ b/src/main/java/com/intel/jndn/mock/MockFace.java
@@ -34,68 +34,124 @@
import java.util.logging.Logger;
/**
- * A client-side face for unit testing
+ * A client-side face for unit testing.
*
* @author Alexander Afanasyev, <aa@cs.ucla.edu>
* @author Andrew Brown <andrew.brown@intel.com>
*/
public class MockFace extends Face {
+ /**
+ * Interests sent out of this MockFace.
+ * <p/>
+ * Sent Interests are appended to this container if options.enablePacketLogger
+ * is true. User of this class is responsible for cleaning up the container,
+ * if necessary. After .expressInterest, .processEvents must be called before
+ * the Interest would show up here.
+ */
+ public final List<Interest> sentInterests = new ArrayList<>();
/**
- * API for handling {@link Interest}s
+ * Data sent out of this MockFace.
+ * <p/>
+ * Sent Data are appended to this container if options.enablePacketLogger is
+ * true. User of this class is responsible for cleaning up the container, if
+ * necessary. After .put, .processEvents must be called before the Data would
+ * show up here.
+ */
+ public final List<Data> sentData = new ArrayList<>();
+
+ /**
+ * Emits whenever an Interest is sent.
+ * <p/>
+ * After .expressInterest, .processEvents must be called before this signal
+ * would be emitted.
+ */
+ public final List<SignalOnSendInterest> onSendInterest = new ArrayList<>();
+
+ /**
+ * Emits whenever a Data packet is sent.
+ * <p/>
+ * After .putData, .processEvents must be called before this signal would be
+ * emitted.
+ */
+ public final List<SignalOnSendData> onSendData = new ArrayList<>();
+
+ private static final Logger LOGGER = Logger.getLogger(MockFace.class.getName());
+ private MockFaceTransport transport;
+ private KeyChain keyChain;
+
+ /////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * API for handling {@link Interest}s.
*/
public interface SignalOnSendInterest {
- void emit(Interest interest) throws EncodingException, SecurityException;
+ /**
+ * Callback called when an Interest is sent out through face (towards NFD).
+ * @param interest interest being sent out
+ */
+ void emit(final Interest interest);
}
/**
- * API for handling {@link Data}s
+ * API for handling {@link Data}s.
*/
public interface SignalOnSendData {
- void emit(Data data);
+ /**
+ * Callback called when a Data is sent out through face (towards NFD).
+ *
+ * @param data data being sent out
+ */
+ void emit(final Data data);
}
/**
- * Options for MockFace
+ * Options for MockFace.
*/
public static class Options {
private boolean enablePacketLogging = false;
private boolean enableRegistrationReply = false;
+ /**
+ * @return true if packet logging is enabled
+ */
public boolean isEnablePacketLogging() {
return enablePacketLogging;
}
/**
- * Enable/disable packet logging
+ * Enable/disable packet logging.
*
* @param enablePacketLogging If true, packets sent out of MockFace will be appended to a container
* @return this
*/
- public Options setEnablePacketLogging(boolean enablePacketLogging) {
+ public Options setEnablePacketLogging(final boolean enablePacketLogging) {
this.enablePacketLogging = enablePacketLogging;
return this;
}
+ /**
+ * @return true if prefix registration mocking is enabled
+ */
public boolean isEnableRegistrationReply() {
return enableRegistrationReply;
}
/**
- * Enable/disable prefix registration mocking
+ * Enable/disable prefix registration mocking.
*
* @param enableRegistrationReply If true, prefix registration command will be automatically replied with a
* successful response
* @return this
*/
- public Options setEnableRegistrationReply(boolean enableRegistrationReply) {
+ public Options setEnableRegistrationReply(final boolean enableRegistrationReply) {
this.enableRegistrationReply = enableRegistrationReply;
return this;
}
}
/**
- * Default options
+ * Default options.
*/
public static final Options DEFAULT_OPTIONS = new Options()
.setEnablePacketLogging(true)
@@ -103,7 +159,7 @@
/**
* Create MockFace that logs packets in {@link #sentInterests} and
- * {@link #sentData} and emulates NFD prefix registration
+ * {@link #sentData} and emulates NFD prefix registration.
*
* @throws SecurityException should not be thrown by this test class
*/
@@ -112,7 +168,7 @@
}
/**
- * Create MockFace with the specified options
+ * Create MockFace with the specified options.
* <p>
* To create Face that does not log packets:
* <pre>
@@ -129,7 +185,7 @@
*
* @param options see {@link Options}
*/
- public MockFace(Options options) {
+ public MockFace(final Options options) {
super(new MockFaceTransport(), null);
transport = (MockFaceTransport) node_.getTransport();
transport.setOnSendBlock(new OnIncomingPacket());
@@ -145,14 +201,14 @@
if (options.enablePacketLogging) {
onSendInterest.add(new SignalOnSendInterest() {
@Override
- public void emit(Interest interest) {
+ public void emit(final Interest interest) {
sentInterests.add(interest);
}
});
onSendData.add(new SignalOnSendData() {
@Override
- public void emit(Data data) {
+ public void emit(final Data data) {
sentData.add(data);
}
});
@@ -164,89 +220,111 @@
}
/**
- * Route incoming packets to the correct callbacks
+ * Route incoming packets to the correct callbacks.
*/
private class OnIncomingPacket implements MockFaceTransport.OnSendBlockSignal {
-
+ /**
+ * {@inheritDoc}
+ */
@Override
- public void emit(ByteBuffer buffer) throws EncodingException, SecurityException {
+ public void emit(final ByteBuffer buffer) {
// @todo Implement NDNLP processing
- if (buffer.get(0) == Tlv.Interest || buffer.get(0) == Tlv.Data) {
- TlvDecoder decoder = new TlvDecoder(buffer);
- if (decoder.peekType(Tlv.Interest, buffer.remaining())) {
- Interest interest = new Interest();
- interest.wireDecode(buffer, TlvWireFormat.get());
+ try {
+ if (buffer.get(0) == Tlv.Interest || buffer.get(0) == Tlv.Data) {
+ TlvDecoder decoder = new TlvDecoder(buffer);
+ if (decoder.peekType(Tlv.Interest, buffer.remaining())) {
+ Interest interest = new Interest();
+ interest.wireDecode(buffer, TlvWireFormat.get());
- for (SignalOnSendInterest signal : onSendInterest) {
- signal.emit(interest);
- }
- } else if (decoder.peekType(Tlv.Data, buffer.remaining())) {
- Data data = new Data();
- data.wireDecode(buffer, TlvWireFormat.get());
+ for (SignalOnSendInterest signal : onSendInterest) {
+ signal.emit(interest);
+ }
+ } else if (decoder.peekType(Tlv.Data, buffer.remaining())) {
+ Data data = new Data();
+ data.wireDecode(buffer, TlvWireFormat.get());
- for (SignalOnSendData signal : onSendData) {
- signal.emit(data);
+ for (SignalOnSendData signal : onSendData) {
+ signal.emit(data);
+ }
}
+ } else {
+ LOGGER.info("Received an unknown packet");
}
- } else {
- LOGGER.info("Received an unknown packet");
+ } catch (EncodingException e) {
+ LOGGER.log(Level.INFO, "Failed to decode incoming packet", e);
}
}
}
/**
- * Handle prefix registration requests
+ * Handle prefix registration requests.
*/
private class OnPrefixRegistration implements SignalOnSendInterest {
+ private static final int STATUS_CODE_OK = 200;
+ private static final int CONTROL_PARAMETERS_NAME_OFFSET = -5;
+ private static final int CONTROL_COMMAND_NAME_OFFSET = 3;
+ /**
+ * {@inheritDoc}
+ */
@Override
- public void emit(Interest interest) throws EncodingException, SecurityException {
+ public void emit(final Interest interest) {
final Name localhostRegistration = new Name("/localhost/nfd/rib");
- if (!interest.getName().getPrefix(3).equals(localhostRegistration)) {
+ if (!interest.getName().getPrefix(localhostRegistration.size()).equals(localhostRegistration) ||
+ interest.getName().get(CONTROL_COMMAND_NAME_OFFSET).toString().equals("register")) {
return;
}
ControlParameters params = new ControlParameters();
- params.wireDecode(interest.getName().get(-5).getValue());
- params.setFaceId(1);
- params.setOrigin(0);
-
- if ("register".equals(interest.getName().get(3).toString())) {
+ try {
+ params.wireDecode(interest.getName().get(CONTROL_PARAMETERS_NAME_OFFSET).getValue());
+ params.setFaceId(1);
+ params.setOrigin(0);
params.setCost(0);
+ } catch (EncodingException e) {
+ throw new IllegalArgumentException("", e);
}
ControlResponse response = new ControlResponse();
- response.setStatusCode(200);
+ response.setStatusCode(STATUS_CODE_OK);
response.setStatusText("OK");
response.setBodyAsControlParameters(params);
Data data = new Data();
data.setName(interest.getName());
data.setContent(response.wireEncode());
- keyChain.sign(data);
+ try {
+ keyChain.sign(data);
+ } catch (SecurityException e) {
+ LOGGER.log(Level.FINE, "MockKeyChain signing failed", e);
+ }
- receive(data);
+ try {
+ receive(data);
+ } catch (EncodingException e) {
+ LOGGER.log(Level.INFO, "Failed to encode ControlReposnse data", e);
+ }
}
}
/**
- * Mock reception of the Interest packet on the Face (from transport)
+ * Mock reception of the Interest packet on the Face (from transport).
*
* @param interest the mock-remote interest to add to the PIT
* @throws EncodingException if packet encoding fails (it should not)
*/
- public void receive(Interest interest) throws EncodingException {
+ public void receive(final Interest interest) throws EncodingException {
transport.receive(interest.wireEncode().buf());
}
/**
- * Mock reception of the Data packet on the Face (from transport)
+ * Mock reception of the Data packet on the Face (from transport).
*
* @param data the mock-remote data to add to the CS
* @throws EncodingException if packet encoding fails (it should not)
*/
- public void receive(Data data) throws EncodingException {
+ public void receive(final Data data) throws EncodingException {
transport.receive(data.wireEncode().buf());
}
@@ -256,44 +334,4 @@
public Transport getTransport() {
return transport;
}
-
- /**
- * Interests sent out of this MockFace
- * <p>
- * Sent Interests are appended to this container if options.enablePacketLogger
- * is true. User of this class is responsible for cleaning up the container,
- * if necessary. After .expressInterest, .processEvents must be called before
- * the Interest would show up here.
- */
- public final List<Interest> sentInterests = new ArrayList<>();
-
- /**
- * Data sent out of this MockFace
- * <p>
- * Sent Data are appended to this container if options.enablePacketLogger is
- * true. User of this class is responsible for cleaning up the container, if
- * necessary. After .put, .processEvents must be called before the Data would
- * show up here.
- */
- public final List<Data> sentData = new ArrayList<>();
-
- /**
- * Emits whenever an Interest is sent
- * <p>
- * After .expressInterest, .processEvents must be called before this signal
- * would be emitted.
- */
- public final List<SignalOnSendInterest> onSendInterest = new ArrayList<>();
-
- /**
- * Emits whenever a Data packet is sent
- * <p>
- * After .putData, .processEvents must be called before this signal would be
- * emitted.
- */
- public final List<SignalOnSendData> onSendData = new ArrayList<>();
-
- private static final Logger LOGGER = Logger.getLogger(MockFace.class.getName());
- private MockFaceTransport transport;
- private KeyChain keyChain;
}