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