Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 1 | /* |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 2 | * File name: NDNEvent.java |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 3 | * |
| 4 | * Purpose: Signals a Client event for observers to act on |
| 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 | /** |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 13 | * Signals an event (from Client or Server) for observers to act on |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 14 | * @author Andrew Brown <andrew.brown@intel.com> |
| 15 | */ |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 16 | public class NDNEvent<T> { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 17 | |
| 18 | protected boolean success; |
| 19 | protected long timestamp; |
| 20 | protected T packet; |
| 21 | |
| 22 | /** |
| 23 | * Constructor |
| 24 | */ |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 25 | public NDNEvent() { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 26 | timestamp = System.currentTimeMillis(); |
| 27 | success = false; // an event without a packet is a failed event |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Constructor |
| 32 | * |
| 33 | * @param packet |
| 34 | */ |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 35 | public NDNEvent(T packet) { |
Andrew Brown | 070dc89 | 2015-01-21 09:55:12 -0800 | [diff] [blame] | 36 | fromPacket(packet); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Build this event from a passed packet; the event is considered a failure |
| 41 | * if the packet is any type of Exception |
| 42 | * |
| 43 | * @param packet |
| 44 | */ |
| 45 | public final void fromPacket(T packet) { |
| 46 | this.timestamp = System.currentTimeMillis(); |
| 47 | this.success = !Exception.class.isInstance(packet); |
| 48 | this.packet = packet; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Retrieve success status |
| 53 | * |
| 54 | * @return |
| 55 | */ |
| 56 | public boolean isSuccess() { |
| 57 | return success; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Retrieve event timestamp |
| 62 | * |
| 63 | * @return |
| 64 | */ |
| 65 | public long getTimestamp() { |
| 66 | return timestamp; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Retrieve event packet |
| 71 | * |
| 72 | * @return |
| 73 | */ |
| 74 | public T getPacket() { |
| 75 | return packet; |
| 76 | } |
| 77 | } |