blob: 446ed638c2cab8f3ca6617cefbdc0e8912551a6c [file] [log] [blame]
Andrew Brown3f2521a2015-01-17 22:10:15 -08001/*
andrewsbrown4feb2da2015-03-03 16:05:29 -08002 * 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.
Andrew Brown3f2521a2015-01-17 22:10:15 -080013 */
14package com.intel.jndn.utils;
15
16import java.io.IOException;
Andrew Browndb457052015-02-21 15:41:58 -080017import java.util.concurrent.ExecutionException;
18import java.util.concurrent.TimeoutException;
Andrew Brown3f2521a2015-01-17 22:10:15 -080019import net.named_data.jndn.Data;
20import net.named_data.jndn.Face;
Andrew Brown3f2521a2015-01-17 22:10:15 -080021import net.named_data.jndn.Interest;
22import net.named_data.jndn.Name;
23import net.named_data.jndn.OnData;
Andrew Brown3f2521a2015-01-17 22:10:15 -080024import net.named_data.jndn.OnTimeout;
Andrew Browndb457052015-02-21 15:41:58 -080025import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080026
27/**
Andrew Brown070dc892015-01-21 09:55:12 -080028 * Provide a client to simplify information retrieval over the NDN network.
Andrew Brown3f2521a2015-01-17 22:10:15 -080029 *
30 * @author Andrew Brown <andrew.brown@intel.com>
31 */
32public class Client {
33
Andrew Brown7b1daf32015-01-19 16:36:01 -080034 public static final long DEFAULT_SLEEP_TIME = 20;
35 public static final long DEFAULT_TIMEOUT = 2000;
Andrew Browndb457052015-02-21 15:41:58 -080036 private static final Logger logger = Logger.getLogger(Client.class.getName());
Andrew Brown070dc892015-01-21 09:55:12 -080037 private static Client defaultInstance;
38
39 /**
40 * Singleton access for simpler client use
41 *
42 * @return
43 */
44 public static Client getDefault() {
45 if (defaultInstance == null) {
46 defaultInstance = new Client();
47 }
48 return defaultInstance;
49 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080050
Andrew Brown7b1daf32015-01-19 16:36:01 -080051 /**
andrewsbrown8372eaa2015-02-23 10:08:17 -080052 * Asynchronously request the Data for an Interest. This will send the
Andrew Browndb457052015-02-21 15:41:58 -080053 * Interest and return immediately; use futureData.get() to block until the
54 * Data returns (see FutureData) or manage the event processing independently.
55 *
56 * @param face
57 * @param interest
58 * @return
59 */
60 public FutureData getAsync(Face face, Interest interest) {
61 final FutureData futureData = new FutureData(face, interest.getName());
62
63 // send interest
64 try {
65 face.expressInterest(interest, new OnData() {
66 @Override
67 public void onData(Interest interest, Data data) {
68 futureData.resolve(data);
69 }
70 }, new OnTimeout() {
71 @Override
72 public void onTimeout(Interest interest) {
73 futureData.reject(new TimeoutException());
74 }
75 });
76 } catch (IOException e) {
77 logger.warning("IO failure while sending interest: " + e);
78 futureData.reject(e);
79 }
80
81 return futureData;
82 }
83
84 /**
85 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
86 * second timeout); this will block until complete (i.e. either data is
87 * received or the interest times out).
88 *
89 * @param face
90 * @param name
91 * @return
92 */
93 public FutureData getAsync(Face face, Name name) {
94 return getAsync(face, getDefaultInterest(name));
95 }
96
97 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -080098 * Synchronously retrieve the Data for an Interest; this will block until
99 * complete (i.e. either data is received or the interest times out).
100 *
101 * @param face
102 * @param interest
103 * @return Data packet or null
104 */
105 public Data getSync(Face face, Interest interest) {
106 // setup event
107 long startTime = System.currentTimeMillis();
Andrew Brown3f2521a2015-01-17 22:10:15 -0800108
Andrew Browndb457052015-02-21 15:41:58 -0800109 // get future data
110 FutureData futureData = getAsync(face, interest);
Andrew Brown3f2521a2015-01-17 22:10:15 -0800111
Andrew Brown070dc892015-01-21 09:55:12 -0800112 // process eventCount until a response is received or timeout
Andrew Browndb457052015-02-21 15:41:58 -0800113 try {
114 Data data = futureData.get();
115 logger.fine("Request time (ms): " + (System.currentTimeMillis() - startTime));
116 return data;
117 } catch (ExecutionException | InterruptedException e) {
118 logger.warning("Failed to retrieve data: " + e);
119 return null;
Andrew Brown7b1daf32015-01-19 16:36:01 -0800120 }
Andrew Brown7b1daf32015-01-19 16:36:01 -0800121 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800122
Andrew Brown7b1daf32015-01-19 16:36:01 -0800123 /**
124 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
125 * second timeout); this will block until complete (i.e. either data is
126 * received or the interest times out).
127 *
128 * @param face
129 * @param name
130 * @return
131 */
132 public Data getSync(Face face, Name name) {
133 return getSync(face, getDefaultInterest(name));
134 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800135
Andrew Brown7b1daf32015-01-19 16:36:01 -0800136 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -0800137 * Create a default interest for a given Name using some common settings: -
138 * lifetime: 2 seconds
139 *
140 * @param name
141 * @return
142 */
Andrew Browndb457052015-02-21 15:41:58 -0800143 public static Interest getDefaultInterest(Name name) {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800144 Interest interest = new Interest(name, DEFAULT_TIMEOUT);
145 return interest;
146 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800147}