blob: 5f552362db61e3ff72e0075c254bf6e25b84b397 [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
2 * File name: Client.java
3 *
4 * Purpose: Provide a client to simplify information retrieval over the NDN
5 * network.
6 *
7 * © Copyright Intel Corporation. All rights reserved.
8 * Intel Corporation, 2200 Mission College Boulevard,
9 * Santa Clara, CA 95052-8119, USA
10 */
11package com.intel.jndn.utils;
12
13import java.io.IOException;
Andrew Browndb457052015-02-21 15:41:58 -080014import java.util.concurrent.ExecutionException;
15import java.util.concurrent.TimeoutException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080016import net.named_data.jndn.Data;
17import net.named_data.jndn.Face;
Andrew Brown3f2521a2015-01-17 22:10:15 -080018import net.named_data.jndn.Interest;
19import net.named_data.jndn.Name;
20import net.named_data.jndn.OnData;
Andrew Brown3f2521a2015-01-17 22:10:15 -080021import net.named_data.jndn.OnTimeout;
Andrew Browndb457052015-02-21 15:41:58 -080022import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080023
24/**
Andrew Brown070dc892015-01-21 09:55:12 -080025 * Provide a client to simplify information retrieval over the NDN network.
Andrew Brown3f2521a2015-01-17 22:10:15 -080026 *
27 * @author Andrew Brown <andrew.brown@intel.com>
28 */
29public class Client {
30
Andrew Brown7b1daf32015-01-19 16:36:01 -080031 public static final long DEFAULT_SLEEP_TIME = 20;
32 public static final long DEFAULT_TIMEOUT = 2000;
Andrew Browndb457052015-02-21 15:41:58 -080033 private static final Logger logger = Logger.getLogger(Client.class.getName());
Andrew Brown070dc892015-01-21 09:55:12 -080034 private static Client defaultInstance;
35
36 /**
37 * Singleton access for simpler client use
38 *
39 * @return
40 */
41 public static Client getDefault() {
42 if (defaultInstance == null) {
43 defaultInstance = new Client();
44 }
45 return defaultInstance;
46 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080047
Andrew Brown7b1daf32015-01-19 16:36:01 -080048 /**
andrewsbrown8372eaa2015-02-23 10:08:17 -080049 * Asynchronously request the Data for an Interest. This will send the
Andrew Browndb457052015-02-21 15:41:58 -080050 * Interest and return immediately; use futureData.get() to block until the
51 * Data returns (see FutureData) or manage the event processing independently.
52 *
53 * @param face
54 * @param interest
55 * @return
56 */
57 public FutureData getAsync(Face face, Interest interest) {
58 final FutureData futureData = new FutureData(face, interest.getName());
59
60 // send interest
61 try {
62 face.expressInterest(interest, new OnData() {
63 @Override
64 public void onData(Interest interest, Data data) {
65 futureData.resolve(data);
66 }
67 }, new OnTimeout() {
68 @Override
69 public void onTimeout(Interest interest) {
70 futureData.reject(new TimeoutException());
71 }
72 });
73 } catch (IOException e) {
74 logger.warning("IO failure while sending interest: " + e);
75 futureData.reject(e);
76 }
77
78 return futureData;
79 }
80
81 /**
82 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
83 * second timeout); this will block until complete (i.e. either data is
84 * received or the interest times out).
85 *
86 * @param face
87 * @param name
88 * @return
89 */
90 public FutureData getAsync(Face face, Name name) {
91 return getAsync(face, getDefaultInterest(name));
92 }
93
94 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -080095 * Synchronously retrieve the Data for an Interest; this will block until
96 * complete (i.e. either data is received or the interest times out).
97 *
98 * @param face
99 * @param interest
100 * @return Data packet or null
101 */
102 public Data getSync(Face face, Interest interest) {
103 // setup event
104 long startTime = System.currentTimeMillis();
Andrew Brown3f2521a2015-01-17 22:10:15 -0800105
Andrew Browndb457052015-02-21 15:41:58 -0800106 // get future data
107 FutureData futureData = getAsync(face, interest);
Andrew Brown3f2521a2015-01-17 22:10:15 -0800108
Andrew Brown070dc892015-01-21 09:55:12 -0800109 // process eventCount until a response is received or timeout
Andrew Browndb457052015-02-21 15:41:58 -0800110 try {
111 Data data = futureData.get();
112 logger.fine("Request time (ms): " + (System.currentTimeMillis() - startTime));
113 return data;
114 } catch (ExecutionException | InterruptedException e) {
115 logger.warning("Failed to retrieve data: " + e);
116 return null;
Andrew Brown7b1daf32015-01-19 16:36:01 -0800117 }
Andrew Brown7b1daf32015-01-19 16:36:01 -0800118 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800119
Andrew Brown7b1daf32015-01-19 16:36:01 -0800120 /**
121 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
122 * second timeout); this will block until complete (i.e. either data is
123 * received or the interest times out).
124 *
125 * @param face
126 * @param name
127 * @return
128 */
129 public Data getSync(Face face, Name name) {
130 return getSync(face, getDefaultInterest(name));
131 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800132
Andrew Brown7b1daf32015-01-19 16:36:01 -0800133 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -0800134 * Create a default interest for a given Name using some common settings: -
135 * lifetime: 2 seconds
136 *
137 * @param name
138 * @return
139 */
Andrew Browndb457052015-02-21 15:41:58 -0800140 public static Interest getDefaultInterest(Name name) {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800141 Interest interest = new Interest(name, DEFAULT_TIMEOUT);
142 return interest;
143 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800144}