blob: c6d820e174e1c30805750b4b2283593459fe54f4 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
Andrew Browna450fad2015-01-22 11:24:40 -08002 * File name: NDNObserver.java
Andrew Brown3f2521a2015-01-17 22:10:15 -08003 *
Andrew Browna450fad2015-01-22 11:24:40 -08004 * Purpose: Track asynchronous events from Client and Server
Andrew Brown3f2521a2015-01-17 22:10:15 -08005 *
6 * © Copyright Intel Corporation. All rights reserved.
7 * Intel Corporation, 2200 Mission College Boulevard,
8 * Santa Clara, CA 95052-8119, USA
9 */
10package com.intel.jndn.utils;
11
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Observable;
15import java.util.Observer;
16import net.named_data.jndn.Data;
17import net.named_data.jndn.Interest;
Andrew Brown3f2521a2015-01-17 22:10:15 -080018
19/**
Andrew Browna450fad2015-01-22 11:24:40 -080020 * Track asynchronous events from Client and Server
Andrew Brown3f2521a2015-01-17 22:10:15 -080021 * @author Andrew Brown <andrew.brown@intel.com>
22 */
Andrew Browna450fad2015-01-22 11:24:40 -080023public class NDNObserver implements Observer {
Andrew Brown3f2521a2015-01-17 22:10:15 -080024
Andrew Browna450fad2015-01-22 11:24:40 -080025 protected List<NDNEvent> events = new ArrayList<>();
Andrew Brown7b1daf32015-01-19 16:36:01 -080026 protected long timestamp;
Andrew Brown070dc892015-01-21 09:55:12 -080027 protected OnEvent then;
Andrew Brown7b1daf32015-01-19 16:36:01 -080028 protected boolean stopThread;
Andrew Brown3f2521a2015-01-17 22:10:15 -080029
Andrew Brown7b1daf32015-01-19 16:36:01 -080030 /**
31 * Constructor
32 */
Andrew Browna450fad2015-01-22 11:24:40 -080033 public NDNObserver() {
Andrew Brown7b1daf32015-01-19 16:36:01 -080034 timestamp = System.currentTimeMillis();
35 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080036
Andrew Brown7b1daf32015-01-19 16:36:01 -080037 /**
38 * Receive notifications from observables
39 *
40 * @param o
41 * @param arg
42 */
43 @Override
44 public void update(Observable o, Object arg) {
Andrew Browna450fad2015-01-22 11:24:40 -080045 NDNEvent event = (NDNEvent) arg;
Andrew Brown7b1daf32015-01-19 16:36:01 -080046 events.add(event);
47 // call onData callbacks
Andrew Browncf2df322015-01-27 09:27:52 -080048 if (then != null) {
Andrew Brown070dc892015-01-21 09:55:12 -080049 then.onEvent(event);
Andrew Brown7b1daf32015-01-19 16:36:01 -080050 }
51 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080052
Andrew Brown7b1daf32015-01-19 16:36:01 -080053 /**
Andrew Brown070dc892015-01-21 09:55:12 -080054 * Register a handler for events
Andrew Brown7b1daf32015-01-19 16:36:01 -080055 *
Andrew Brown070dc892015-01-21 09:55:12 -080056 * @param callback
Andrew Brown7b1daf32015-01-19 16:36:01 -080057 * @return
58 */
Andrew Browna450fad2015-01-22 11:24:40 -080059 public NDNObserver then(OnEvent callback) {
Andrew Brown070dc892015-01-21 09:55:12 -080060 then = callback;
Andrew Brown7b1daf32015-01-19 16:36:01 -080061 return this;
62 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080063
Andrew Brown7b1daf32015-01-19 16:36:01 -080064 /**
Andrew Brown070dc892015-01-21 09:55:12 -080065 * Count the number of eventCount observed
66 *
67 * @return
68 */
69 public int eventCount() {
70 return events.size();
71 }
72
73 /**
74 * Count the number of interest packets observed (received or sent)
75 *
76 * @return
77 */
78 public int interestCount() {
79 return count(Interest.class);
80 }
81
82 /**
83 * Count the number of Data packets observed
84 *
85 * @return
86 */
87 public int dataCount() {
88 return count(Data.class);
89 }
90
91 /**
92 * Count the number of errors observed
93 *
94 * @return
95 */
96 public int errorCount() {
97 return count(Exception.class);
98 }
99
100 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -0800101 * Count the number of observed packets by type
102 *
103 * @param type
104 * @return
105 */
106 public int count(Class type) {
107 int count = 0;
Andrew Browna450fad2015-01-22 11:24:40 -0800108 for (NDNEvent event : events) {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800109 if (type.isInstance(event.packet)) {
110 count++;
111 }
112 }
113 return count;
114 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800115
Andrew Brown7b1daf32015-01-19 16:36:01 -0800116 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800117 * Calculate time elapsed since observer started observing until this method
118 * is called
Andrew Brown7b1daf32015-01-19 16:36:01 -0800119 *
120 * @return
121 */
122 public long getTimeSinceStart() {
123 if (getLast() != null) {
124 return getLast().getTimestamp() - timestamp;
125 }
126 return -1;
127 }
128
129 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800130 * Retrieve a list of observed events
Andrew Brown7b1daf32015-01-19 16:36:01 -0800131 *
132 * @return event or null
133 */
Andrew Browna450fad2015-01-22 11:24:40 -0800134 public List<NDNEvent> getEvents() {
Andrew Brown070dc892015-01-21 09:55:12 -0800135 return events;
136 }
137
138 /**
139 * Retrieve the first event
140 *
141 * @return event or null
142 */
Andrew Browna450fad2015-01-22 11:24:40 -0800143 public NDNEvent getFirst() {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800144 if (events.size() > 0) {
145 return events.get(0);
146 }
147 return null;
148 }
149
150 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800151 * Retrieve the last event
Andrew Brown7b1daf32015-01-19 16:36:01 -0800152 *
153 * @return event or null
154 */
Andrew Browna450fad2015-01-22 11:24:40 -0800155 public NDNEvent getLast() {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800156 if (events.size() > 0) {
157 return events.get(events.size() - 1);
158 }
159 return null;
160 }
161
162 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800163 * Stop the current Client thread; used by asynchronous Client methods to
164 * stop the request/response thread
Andrew Brown7b1daf32015-01-19 16:36:01 -0800165 */
166 public void stop() {
167 stopThread = true;
168 }
169
170 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800171 * Check the current stop status; used by asynchronous Client methods to
172 * stop the request/response thread
Andrew Brown7b1daf32015-01-19 16:36:01 -0800173 *
174 * @return
175 */
176 public boolean mustStop() {
177 return stopThread;
178 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800179}