blob: 9bfdd7e79fe2194f6b6742a8b5c2c333d3068ded [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
2 * File name: ClientObserver.java
3 *
Andrew Brown070dc892015-01-21 09:55:12 -08004 * Purpose: Track asynchronous events from Client and provide simplified API
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;
18import net.named_data.jndn.OnData;
19
20/**
21 *
22 * @author Andrew Brown <andrew.brown@intel.com>
23 */
24public class ClientObserver implements Observer {
25
Andrew Brown070dc892015-01-21 09:55:12 -080026 protected List<ClientEvent> events = new ArrayList<>();
Andrew Brown7b1daf32015-01-19 16:36:01 -080027 protected long timestamp;
Andrew Brown070dc892015-01-21 09:55:12 -080028 protected OnEvent then;
Andrew Brown7b1daf32015-01-19 16:36:01 -080029 protected boolean stopThread;
Andrew Brown3f2521a2015-01-17 22:10:15 -080030
Andrew Brown7b1daf32015-01-19 16:36:01 -080031 /**
32 * Constructor
33 */
34 public ClientObserver() {
35 timestamp = System.currentTimeMillis();
36 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080037
Andrew Brown7b1daf32015-01-19 16:36:01 -080038 /**
39 * Receive notifications from observables
40 *
41 * @param o
42 * @param arg
43 */
44 @Override
45 public void update(Observable o, Object arg) {
Andrew Brown070dc892015-01-21 09:55:12 -080046 ClientEvent event = (ClientEvent) arg;
Andrew Brown7b1daf32015-01-19 16:36:01 -080047 events.add(event);
48 // call onData callbacks
49 if (Data.class.isInstance(event.packet) && then != null) {
Andrew Brown070dc892015-01-21 09:55:12 -080050 then.onEvent(event);
Andrew Brown7b1daf32015-01-19 16:36:01 -080051 }
52 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080053
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 /**
Andrew Brown070dc892015-01-21 09:55:12 -080055 * Register a handler for events
Andrew Brown7b1daf32015-01-19 16:36:01 -080056 *
Andrew Brown070dc892015-01-21 09:55:12 -080057 * @param callback
Andrew Brown7b1daf32015-01-19 16:36:01 -080058 * @return
59 */
Andrew Brown070dc892015-01-21 09:55:12 -080060 public ClientObserver then(OnEvent callback) {
61 then = callback;
Andrew Brown7b1daf32015-01-19 16:36:01 -080062 return this;
63 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080064
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 /**
Andrew Brown070dc892015-01-21 09:55:12 -080066 * Count the number of eventCount observed
67 *
68 * @return
69 */
70 public int eventCount() {
71 return events.size();
72 }
73
74 /**
75 * Count the number of interest packets observed (received or sent)
76 *
77 * @return
78 */
79 public int interestCount() {
80 return count(Interest.class);
81 }
82
83 /**
84 * Count the number of Data packets observed
85 *
86 * @return
87 */
88 public int dataCount() {
89 return count(Data.class);
90 }
91
92 /**
93 * Count the number of errors observed
94 *
95 * @return
96 */
97 public int errorCount() {
98 return count(Exception.class);
99 }
100
101 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -0800102 * Count the number of observed packets by type
103 *
104 * @param type
105 * @return
106 */
107 public int count(Class type) {
108 int count = 0;
Andrew Brown070dc892015-01-21 09:55:12 -0800109 for (ClientEvent event : events) {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800110 if (type.isInstance(event.packet)) {
111 count++;
112 }
113 }
114 return count;
115 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800116
Andrew Brown7b1daf32015-01-19 16:36:01 -0800117 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800118 * Calculate time elapsed since observer started observing until this method
119 * is called
Andrew Brown7b1daf32015-01-19 16:36:01 -0800120 *
121 * @return
122 */
123 public long getTimeSinceStart() {
124 if (getLast() != null) {
125 return getLast().getTimestamp() - timestamp;
126 }
127 return -1;
128 }
129
130 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800131 * Retrieve a list of observed events
Andrew Brown7b1daf32015-01-19 16:36:01 -0800132 *
133 * @return event or null
134 */
Andrew Brown070dc892015-01-21 09:55:12 -0800135 public List<ClientEvent> getEvents() {
136 return events;
137 }
138
139 /**
140 * Retrieve the first event
141 *
142 * @return event or null
143 */
144 public ClientEvent getFirst() {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800145 if (events.size() > 0) {
146 return events.get(0);
147 }
148 return null;
149 }
150
151 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800152 * Retrieve the last event
Andrew Brown7b1daf32015-01-19 16:36:01 -0800153 *
154 * @return event or null
155 */
Andrew Brown070dc892015-01-21 09:55:12 -0800156 public ClientEvent getLast() {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800157 if (events.size() > 0) {
158 return events.get(events.size() - 1);
159 }
160 return null;
161 }
162
163 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800164 * Stop the current Client thread; used by asynchronous Client methods to
165 * stop the request/response thread
Andrew Brown7b1daf32015-01-19 16:36:01 -0800166 */
167 public void stop() {
168 stopThread = true;
169 }
170
171 /**
Andrew Brown070dc892015-01-21 09:55:12 -0800172 * Check the current stop status; used by asynchronous Client methods to
173 * stop the request/response thread
Andrew Brown7b1daf32015-01-19 16:36:01 -0800174 *
175 * @return
176 */
177 public boolean mustStop() {
178 return stopThread;
179 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800180}