Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 2 | * File name: NDNObserver.java |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 3 | * |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 4 | * Purpose: Track asynchronous events from Client and Server |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 5 | * |
6 | * © Copyright Intel Corporation. All rights reserved. | ||||
7 | * Intel Corporation, 2200 Mission College Boulevard, | ||||
8 | * Santa Clara, CA 95052-8119, USA | ||||
9 | */ | ||||
10 | package com.intel.jndn.utils; | ||||
11 | |||||
12 | import java.util.ArrayList; | ||||
13 | import java.util.List; | ||||
14 | import java.util.Observable; | ||||
15 | import java.util.Observer; | ||||
16 | import net.named_data.jndn.Data; | ||||
17 | import net.named_data.jndn.Interest; | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 18 | |
19 | /** | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 20 | * Track asynchronous events from Client and Server |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 21 | * @author Andrew Brown <andrew.brown@intel.com> |
22 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 23 | public class NDNObserver implements Observer { |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 24 | |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 25 | protected List<NDNEvent> events = new ArrayList<>(); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 26 | protected long timestamp; |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 27 | protected OnEvent then; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 28 | protected boolean stopThread; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 29 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 30 | /** |
31 | * Constructor | ||||
32 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 33 | public NDNObserver() { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 34 | timestamp = System.currentTimeMillis(); |
35 | } | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 36 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 37 | /** |
38 | * Receive notifications from observables | ||||
39 | * | ||||
40 | * @param o | ||||
41 | * @param arg | ||||
42 | */ | ||||
43 | @Override | ||||
44 | public void update(Observable o, Object arg) { | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 45 | NDNEvent event = (NDNEvent) arg; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 46 | events.add(event); |
47 | // call onData callbacks | ||||
48 | if (Data.class.isInstance(event.packet) && then != null) { | ||||
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 49 | then.onEvent(event); |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 50 | } |
51 | } | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 52 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 53 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 54 | * Register a handler for events |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 55 | * |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 56 | * @param callback |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 57 | * @return |
58 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 59 | public NDNObserver then(OnEvent callback) { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 60 | then = callback; |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 61 | return this; |
62 | } | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 63 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 64 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 65 | * 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 Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 101 | * 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 Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 108 | for (NDNEvent event : events) { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 109 | if (type.isInstance(event.packet)) { |
110 | count++; | ||||
111 | } | ||||
112 | } | ||||
113 | return count; | ||||
114 | } | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 115 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 116 | /** |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 117 | * Calculate time elapsed since observer started observing until this method |
118 | * is called | ||||
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 119 | * |
120 | * @return | ||||
121 | */ | ||||
122 | public long getTimeSinceStart() { | ||||
123 | if (getLast() != null) { | ||||
124 | return getLast().getTimestamp() - timestamp; | ||||
125 | } | ||||
126 | return -1; | ||||
127 | } | ||||
128 | |||||
129 | /** | ||||
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 130 | * Retrieve a list of observed events |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 131 | * |
132 | * @return event or null | ||||
133 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 134 | public List<NDNEvent> getEvents() { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 135 | return events; |
136 | } | ||||
137 | |||||
138 | /** | ||||
139 | * Retrieve the first event | ||||
140 | * | ||||
141 | * @return event or null | ||||
142 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 143 | public NDNEvent getFirst() { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 144 | if (events.size() > 0) { |
145 | return events.get(0); | ||||
146 | } | ||||
147 | return null; | ||||
148 | } | ||||
149 | |||||
150 | /** | ||||
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 151 | * Retrieve the last event |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 152 | * |
153 | * @return event or null | ||||
154 | */ | ||||
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 155 | public NDNEvent getLast() { |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 156 | if (events.size() > 0) { |
157 | return events.get(events.size() - 1); | ||||
158 | } | ||||
159 | return null; | ||||
160 | } | ||||
161 | |||||
162 | /** | ||||
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 163 | * Stop the current Client thread; used by asynchronous Client methods to |
164 | * stop the request/response thread | ||||
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 165 | */ |
166 | public void stop() { | ||||
167 | stopThread = true; | ||||
168 | } | ||||
169 | |||||
170 | /** | ||||
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 171 | * Check the current stop status; used by asynchronous Client methods to |
172 | * stop the request/response thread | ||||
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 173 | * |
174 | * @return | ||||
175 | */ | ||||
176 | public boolean mustStop() { | ||||
177 | return stopThread; | ||||
178 | } | ||||
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 179 | } |