Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: ClientObservable.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 net.named_data.jndn.Data; |
| 16 | import net.named_data.jndn.Interest; |
| 17 | import net.named_data.jndn.Name; |
| 18 | import net.named_data.jndn.OnData; |
| 19 | import net.named_data.jndn.OnInterest; |
| 20 | import net.named_data.jndn.OnTimeout; |
| 21 | import net.named_data.jndn.transport.Transport; |
| 22 | |
| 23 | /** |
| 24 | * |
| 25 | * @author Andrew Brown <andrew.brown@intel.com> |
| 26 | */ |
| 27 | public class ClientObservable extends Observable implements OnData, OnTimeout, OnInterest { |
| 28 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 29 | protected List<ClientObservableEvent> events = new ArrayList<>(); |
| 30 | protected List<Interest> incomingInterestPackets = new ArrayList<>(); |
| 31 | protected List<Data> incomingDataPackets; |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 32 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Generic notification |
| 35 | * |
| 36 | * @param <T> |
| 37 | * @param packet |
| 38 | */ |
| 39 | public <T> void notify(T packet) { |
| 40 | setChanged(); |
| 41 | notifyObservers(new ClientObservableEvent(packet)); |
| 42 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 43 | |
Andrew Brown | 7b1daf3 | 2015-01-19 16:36:01 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Handle data packets |
| 46 | * |
| 47 | * @param interest |
| 48 | * @param data |
| 49 | */ |
| 50 | @Override |
| 51 | public void onData(Interest interest, Data data) { |
| 52 | notify(data); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handle exceptions |
| 57 | * |
| 58 | * @param e |
| 59 | */ |
| 60 | public void onError(Exception e) { |
| 61 | notify(e); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Handle timeouts |
| 66 | * |
| 67 | * @param interest |
| 68 | */ |
| 69 | @Override |
| 70 | public void onTimeout(Interest interest) { |
| 71 | notify(interest); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Handle incoming interests |
| 76 | * |
| 77 | * @param prefix |
| 78 | * @param interest |
| 79 | * @param transport |
| 80 | * @param registeredPrefixId |
| 81 | */ |
| 82 | @Override |
| 83 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 84 | notify(interest); // TODO wrap |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Helper to reference both outgoing interest and incoming data packets |
| 89 | */ |
| 90 | class InterestDataPacket { |
| 91 | |
| 92 | private Interest interest; |
| 93 | private Data data; |
| 94 | |
| 95 | public InterestDataPacket(Interest interest, Data data) { |
| 96 | this.interest = interest; |
| 97 | this.data = data; |
| 98 | } |
| 99 | |
| 100 | public Data getData() { |
| 101 | return data; |
| 102 | } |
| 103 | |
| 104 | public Interest getInterest() { |
| 105 | return interest; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Helper to reference both incoming interest and the transport to send data on |
| 111 | */ |
| 112 | class InterestTransportPacket{ |
| 113 | private Interest interest; |
| 114 | private Transport transport; |
| 115 | |
| 116 | public InterestTransportPacket(Interest interest, Transport transport) { |
| 117 | this.interest = interest; |
| 118 | this.transport = transport; |
| 119 | } |
| 120 | |
| 121 | public Interest getInterest() { |
| 122 | return interest; |
| 123 | } |
| 124 | |
| 125 | public Transport getTransport() { |
| 126 | return transport; |
| 127 | } |
| 128 | |
| 129 | } |
Andrew Brown | 3f2521a | 2015-01-17 22:10:15 -0800 | [diff] [blame] | 130 | } |