blob: ce6beda22def7cc30498f1aafa96e3aede1bc634 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
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 */
10package com.intel.jndn.utils;
11
12import java.util.ArrayList;
13import java.util.List;
14import java.util.Observable;
15import net.named_data.jndn.Data;
16import net.named_data.jndn.Interest;
17import net.named_data.jndn.Name;
18import net.named_data.jndn.OnData;
19import net.named_data.jndn.OnInterest;
20import net.named_data.jndn.OnTimeout;
21import net.named_data.jndn.transport.Transport;
22
23/**
24 *
25 * @author Andrew Brown <andrew.brown@intel.com>
26 */
27public class ClientObservable extends Observable implements OnData, OnTimeout, OnInterest {
28
Andrew Brown7b1daf32015-01-19 16:36:01 -080029 protected List<ClientObservableEvent> events = new ArrayList<>();
30 protected List<Interest> incomingInterestPackets = new ArrayList<>();
31 protected List<Data> incomingDataPackets;
Andrew Brown3f2521a2015-01-17 22:10:15 -080032
Andrew Brown7b1daf32015-01-19 16:36:01 -080033 /**
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 Brown3f2521a2015-01-17 22:10:15 -080043
Andrew Brown7b1daf32015-01-19 16:36:01 -080044 /**
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 Brown3f2521a2015-01-17 22:10:15 -0800130}