Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 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 | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame^] | 26 | protected List<ClientObservableEvent> events = new ArrayList<>(); |
| 27 | protected long timestamp; |
| 28 | protected OnData then; |
| 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) { |
| 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 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 | /** |
| 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 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 | /** |
| 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 Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 80 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame^] | 81 | /** |
| 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 Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 89 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame^] | 90 | /** |
| 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 Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 159 | } |