blob: 0ed4e11513297ef1ba95a581fe43a69325ba70fd [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
2 * File name: ClientObserver.java
3 *
4 * Purpose:
5 *
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 Brown7b1daf32015-01-19 16:36:01 -080026 protected List<ClientObservableEvent> events = new ArrayList<>();
27 protected long timestamp;
28 protected OnData then;
29 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) {
46 ClientObservableEvent event = (ClientObservableEvent) arg;
47 events.add(event);
48 // call onData callbacks
49 if (Data.class.isInstance(event.packet) && then != null) {
50 then.onData(null, (Data) event.packet);
51 }
52 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080053
Andrew Brown7b1daf32015-01-19 16:36:01 -080054 /**
55 * Register a handler for data packets
56 *
57 * @param handler
58 * @return
59 */
60 public ClientObserver then(OnData handler) {
61 then = handler;
62 return this;
63 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080064
Andrew Brown7b1daf32015-01-19 16:36:01 -080065 /**
66 * Count the number of observed packets by type
67 *
68 * @param type
69 * @return
70 */
71 public int count(Class type) {
72 int count = 0;
73 for (ClientObservableEvent event : events) {
74 if (type.isInstance(event.packet)) {
75 count++;
76 }
77 }
78 return count;
79 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080080
Andrew Brown7b1daf32015-01-19 16:36:01 -080081 /**
82 * Count the number of interest packets observed (received or sent)
83 *
84 * @return
85 */
86 public int requests() {
87 return count(Interest.class);
88 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080089
Andrew Brown7b1daf32015-01-19 16:36:01 -080090 /**
91 * Count the number of Data packets observed
92 *
93 * @return
94 */
95 public int responses() {
96 return count(Data.class);
97 }
98
99 /**
100 * Count the number of errors
101 *
102 * @return
103 */
104 public int errors() {
105 return count(Exception.class);
106 }
107
108 /**
109 * Get time since observer start
110 *
111 * @return
112 */
113 public long getTimeSinceStart() {
114 if (getLast() != null) {
115 return getLast().getTimestamp() - timestamp;
116 }
117 return -1;
118 }
119
120 /**
121 * Retrieve first event
122 *
123 * @return event or null
124 */
125 public ClientObservableEvent getFirst() {
126 if (events.size() > 0) {
127 return events.get(0);
128 }
129 return null;
130 }
131
132 /**
133 * Retrieve last event
134 *
135 * @return event or null
136 */
137 public ClientObservableEvent getLast() {
138 if (events.size() > 0) {
139 return events.get(events.size() - 1);
140 }
141 return null;
142 }
143
144 /**
145 * Stop the current Client thread
146 */
147 public void stop() {
148 stopThread = true;
149 }
150
151 /**
152 * Check the current stop status
153 *
154 * @return
155 */
156 public boolean mustStop() {
157 return stopThread;
158 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800159}