blob: 7c2bf987620ddc5eaa46fd17c831e22e6a8ab78b [file] [log] [blame]
andrewsbrown69d53292015-03-17 19:37:34 +01001/*
2 * jndn-utils
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 */
14package com.intel.jndn.utils;
15
16import com.intel.jndn.utils.client.FutureData;
17import java.io.IOException;
18import java.util.concurrent.ExecutionException;
19import java.util.concurrent.Future;
20import java.util.concurrent.TimeoutException;
21import java.util.logging.Level;
22import net.named_data.jndn.Data;
23import net.named_data.jndn.Face;
24import net.named_data.jndn.Interest;
25import net.named_data.jndn.Name;
26import net.named_data.jndn.OnData;
27import net.named_data.jndn.OnTimeout;
28import java.util.logging.Logger;
29
30/**
31 * Provide a client to simplify information retrieval over the NDN network.
32 *
33 * @author Andrew Brown <andrew.brown@intel.com>
34 */
35public class SimpleClient implements Client {
36
37 public static final long DEFAULT_SLEEP_TIME = 20;
38 public static final long DEFAULT_TIMEOUT = 2000;
39 private static final Logger logger = Logger.getLogger(SimpleClient.class.getName());
40 private static SimpleClient defaultInstance;
41
42 /**
43 * Singleton access for simpler client use
44 *
45 * @return
46 */
47 public static SimpleClient getDefault() {
48 if (defaultInstance == null) {
49 defaultInstance = new SimpleClient();
50 }
51 return defaultInstance;
52 }
53
54 /**
55 * Asynchronously request the Data for an Interest. This will send the
56 * Interest and return immediately; use futureData.get() to block until the
57 * Data returns (see FutureData) or manage the event processing independently.
58 *
59 * @param face
60 * @param interest
61 * @return
62 */
63 @Override
64 public Future<Data> getAsync(Face face, Interest interest) {
65 final FutureData futureData = new FutureData(face, interest.getName());
66
67 // send interest
andrewsbrown3f669972015-04-20 13:39:37 -070068 logger.log(Level.FINER, "Sending interest for: " + interest.getName().toUri());
andrewsbrown69d53292015-03-17 19:37:34 +010069 try {
70 face.expressInterest(interest, new OnData() {
71 @Override
72 public void onData(Interest interest, Data data) {
73 futureData.resolve(data);
74 }
75 }, new OnTimeout() {
76 @Override
77 public void onTimeout(Interest interest) {
78 futureData.reject(new TimeoutException());
79 }
80 });
81 } catch (IOException e) {
andrewsbrown0bb5d5f2015-04-07 09:08:54 -070082 logger.log(Level.FINE, "IO failure while sending interest: ", e);
andrewsbrown69d53292015-03-17 19:37:34 +010083 futureData.reject(e);
84 }
85
86 return futureData;
87 }
88
89 /**
90 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
91 * second timeout); this will block until complete (i.e. either data is
92 * received or the interest times out).
93 *
94 * @param face
95 * @param name
96 * @return
97 */
98 public Future<Data> getAsync(Face face, Name name) {
99 return getAsync(face, getDefaultInterest(name));
100 }
101
102 /**
103 * Synchronously retrieve the Data for an Interest; this will block until
104 * complete (i.e. either data is received or the interest times out).
105 *
106 * @param face
107 * @param interest
108 * @return Data packet or null
109 * @throws java.io.IOException
110 */
111 @Override
112 public Data getSync(Face face, Interest interest) throws IOException {
113 try {
114 return getAsync(face, interest).get();
115 } catch (ExecutionException | InterruptedException e) {
andrewsbrown0bb5d5f2015-04-07 09:08:54 -0700116 logger.log(Level.FINE, "Failed to retrieve data.", e);
andrewsbrown69d53292015-03-17 19:37:34 +0100117 throw new IOException("Failed to retrieve data.", e);
118 }
119 }
120
121 /**
122 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
123 * second timeout); this will block until complete (i.e. either data is
124 * received or the interest times out).
125 *
126 * @param face
127 * @param name
128 * @return
129 * @throws java.io.IOException
130 */
131 public Data getSync(Face face, Name name) throws IOException {
132 return getSync(face, getDefaultInterest(name));
133 }
134
135 /**
136 * Create a default interest for a given Name using some common settings: -
137 * lifetime: 2 seconds
138 *
139 * @param name
140 * @return
141 */
142 public static Interest getDefaultInterest(Name name) {
143 Interest interest = new Interest(name, DEFAULT_TIMEOUT);
144 return interest;
145 }
146}