Initial pubsub; waiting on MockFace to fully test
diff --git a/src/main/java/com/intel/jndn/utils/ClientObserver.java b/src/main/java/com/intel/jndn/utils/ClientObserver.java
index ed78476..0ed4e11 100644
--- a/src/main/java/com/intel/jndn/utils/ClientObserver.java
+++ b/src/main/java/com/intel/jndn/utils/ClientObserver.java
@@ -23,81 +23,137 @@
*/
public class ClientObserver implements Observer {
- protected List<ClientObservableEvent> events = new ArrayList<>();
- protected long timestamp;
- protected OnData then;
- protected boolean stopThread;
-
- public ClientObserver(){
- timestamp = System.currentTimeMillis();
- }
+ protected List<ClientObservableEvent> events = new ArrayList<>();
+ protected long timestamp;
+ protected OnData then;
+ protected boolean stopThread;
- @Override
- public void update(Observable o, Object arg) {
- ClientObservableEvent event = (ClientObservableEvent) arg;
- events.add(event);
- if(Data.class.isInstance(event.packet)){
- then.onData(null, (Data) event.packet);
- }
- }
-
- public ClientObserver then(OnData handler){
- then = handler;
- return this;
- }
+ /**
+ * Constructor
+ */
+ public ClientObserver() {
+ timestamp = System.currentTimeMillis();
+ }
- public int count(Class type) {
- int count = 0;
- for (ClientObservableEvent event : events) {
- if (type.isInstance(event.packet)) {
- count++;
- }
- }
- return count;
- }
+ /**
+ * Receive notifications from observables
+ *
+ * @param o
+ * @param arg
+ */
+ @Override
+ public void update(Observable o, Object arg) {
+ ClientObservableEvent event = (ClientObservableEvent) arg;
+ events.add(event);
+ // call onData callbacks
+ if (Data.class.isInstance(event.packet) && then != null) {
+ then.onData(null, (Data) event.packet);
+ }
+ }
- public int requests() {
- return count(Interest.class);
- }
+ /**
+ * Register a handler for data packets
+ *
+ * @param handler
+ * @return
+ */
+ public ClientObserver then(OnData handler) {
+ then = handler;
+ return this;
+ }
- public int responses() {
- return count(Data.class);
- }
-
- public int errors(){
- return count(Exception.class);
- }
-
- /**
- * Get time since observer start
- * @return
- */
- public long getTimeSinceStart(){
- if(getLast() != null){
- return getLast().getTimestamp() - timestamp;
- }
- return -1;
- }
+ /**
+ * Count the number of observed packets by type
+ *
+ * @param type
+ * @return
+ */
+ public int count(Class type) {
+ int count = 0;
+ for (ClientObservableEvent event : events) {
+ if (type.isInstance(event.packet)) {
+ count++;
+ }
+ }
+ return count;
+ }
- public ClientObservableEvent getFirst() {
- if (events.size() > 0) {
- return events.get(0);
- }
- return null;
- }
+ /**
+ * Count the number of interest packets observed (received or sent)
+ *
+ * @return
+ */
+ public int requests() {
+ return count(Interest.class);
+ }
- public ClientObservableEvent getLast() {
- if (events.size() > 0) {
- return events.get(events.size() - 1);
- }
- return null;
- }
-
- public void stop(){
- stopThread = true;
- }
-
- public boolean mustStop(){
- return stopThread;
- }
+ /**
+ * Count the number of Data packets observed
+ *
+ * @return
+ */
+ public int responses() {
+ return count(Data.class);
+ }
+
+ /**
+ * Count the number of errors
+ *
+ * @return
+ */
+ public int errors() {
+ return count(Exception.class);
+ }
+
+ /**
+ * Get time since observer start
+ *
+ * @return
+ */
+ public long getTimeSinceStart() {
+ if (getLast() != null) {
+ return getLast().getTimestamp() - timestamp;
+ }
+ return -1;
+ }
+
+ /**
+ * Retrieve first event
+ *
+ * @return event or null
+ */
+ public ClientObservableEvent getFirst() {
+ if (events.size() > 0) {
+ return events.get(0);
+ }
+ return null;
+ }
+
+ /**
+ * Retrieve last event
+ *
+ * @return event or null
+ */
+ public ClientObservableEvent getLast() {
+ if (events.size() > 0) {
+ return events.get(events.size() - 1);
+ }
+ return null;
+ }
+
+ /**
+ * Stop the current Client thread
+ */
+ public void stop() {
+ stopThread = true;
+ }
+
+ /**
+ * Check the current stop status
+ *
+ * @return
+ */
+ public boolean mustStop() {
+ return stopThread;
+ }
}