blob: cb90f6f4ddb214da37f138155fa70acfc8106c43 [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;
andrewsbrown0cf35f92015-03-09 12:00:00 -070019import java.util.logging.Level;
Andrew Brown3f2521a2015-01-17 22:10:15 -080020import net.named_data.jndn.Data;
21import net.named_data.jndn.Face;
Andrew Brown3f2521a2015-01-17 22:10:15 -080022import net.named_data.jndn.Interest;
23import net.named_data.jndn.Name;
24import net.named_data.jndn.OnData;
Andrew Brown3f2521a2015-01-17 22:10:15 -080025import net.named_data.jndn.OnTimeout;
Andrew Browndb457052015-02-21 15:41:58 -080026import java.util.logging.Logger;
Andrew Brown3f2521a2015-01-17 22:10:15 -080027
28/**
Andrew Brown070dc892015-01-21 09:55:12 -080029 * Provide a client to simplify information retrieval over the NDN network.
Andrew Brown3f2521a2015-01-17 22:10:15 -080030 *
31 * @author Andrew Brown <andrew.brown@intel.com>
32 */
33public class Client {
34
Andrew Brown7b1daf32015-01-19 16:36:01 -080035 public static final long DEFAULT_SLEEP_TIME = 20;
36 public static final long DEFAULT_TIMEOUT = 2000;
Andrew Browndb457052015-02-21 15:41:58 -080037 private static final Logger logger = Logger.getLogger(Client.class.getName());
Andrew Brown070dc892015-01-21 09:55:12 -080038 private static Client defaultInstance;
39
40 /**
41 * Singleton access for simpler client use
42 *
43 * @return
44 */
45 public static Client getDefault() {
46 if (defaultInstance == null) {
47 defaultInstance = new Client();
48 }
49 return defaultInstance;
50 }
Andrew Brown3f2521a2015-01-17 22:10:15 -080051
Andrew Brown7b1daf32015-01-19 16:36:01 -080052 /**
andrewsbrown8372eaa2015-02-23 10:08:17 -080053 * Asynchronously request the Data for an Interest. This will send the
Andrew Browndb457052015-02-21 15:41:58 -080054 * Interest and return immediately; use futureData.get() to block until the
55 * Data returns (see FutureData) or manage the event processing independently.
56 *
57 * @param face
58 * @param interest
59 * @return
60 */
61 public FutureData getAsync(Face face, Interest interest) {
62 final FutureData futureData = new FutureData(face, interest.getName());
63
64 // send interest
65 try {
66 face.expressInterest(interest, new OnData() {
67 @Override
68 public void onData(Interest interest, Data data) {
69 futureData.resolve(data);
70 }
71 }, new OnTimeout() {
72 @Override
73 public void onTimeout(Interest interest) {
74 futureData.reject(new TimeoutException());
75 }
76 });
77 } catch (IOException e) {
andrewsbrown0cf35f92015-03-09 12:00:00 -070078 logger.log(Level.WARNING, "IO failure while sending interest: ", e);
Andrew Browndb457052015-02-21 15:41:58 -080079 futureData.reject(e);
80 }
81
82 return futureData;
83 }
84
85 /**
86 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
87 * second timeout); this will block until complete (i.e. either data is
88 * received or the interest times out).
89 *
90 * @param face
91 * @param name
92 * @return
93 */
94 public FutureData getAsync(Face face, Name name) {
95 return getAsync(face, getDefaultInterest(name));
96 }
97
98 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -080099 * Synchronously retrieve the Data for an Interest; this will block until
100 * complete (i.e. either data is received or the interest times out).
101 *
102 * @param face
103 * @param interest
104 * @return Data packet or null
105 */
106 public Data getSync(Face face, Interest interest) {
107 // setup event
108 long startTime = System.currentTimeMillis();
Andrew Brown3f2521a2015-01-17 22:10:15 -0800109
Andrew Browndb457052015-02-21 15:41:58 -0800110 // get future data
111 FutureData futureData = getAsync(face, interest);
Andrew Brown3f2521a2015-01-17 22:10:15 -0800112
Andrew Brown070dc892015-01-21 09:55:12 -0800113 // process eventCount until a response is received or timeout
Andrew Browndb457052015-02-21 15:41:58 -0800114 try {
115 Data data = futureData.get();
116 logger.fine("Request time (ms): " + (System.currentTimeMillis() - startTime));
117 return data;
118 } catch (ExecutionException | InterruptedException e) {
andrewsbrown0cf35f92015-03-09 12:00:00 -0700119 logger.log(Level.WARNING, "Failed to retrieve data: ", e);
Andrew Browndb457052015-02-21 15:41:58 -0800120 return null;
Andrew Brown7b1daf32015-01-19 16:36:01 -0800121 }
Andrew Brown7b1daf32015-01-19 16:36:01 -0800122 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800123
Andrew Brown7b1daf32015-01-19 16:36:01 -0800124 /**
125 * Synchronously retrieve the Data for a Name using a default interest (e.g. 2
126 * second timeout); this will block until complete (i.e. either data is
127 * received or the interest times out).
128 *
129 * @param face
130 * @param name
131 * @return
132 */
133 public Data getSync(Face face, Name name) {
134 return getSync(face, getDefaultInterest(name));
135 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800136
Andrew Brown7b1daf32015-01-19 16:36:01 -0800137 /**
Andrew Brown7b1daf32015-01-19 16:36:01 -0800138 * Create a default interest for a given Name using some common settings: -
139 * lifetime: 2 seconds
140 *
141 * @param name
142 * @return
143 */
Andrew Browndb457052015-02-21 15:41:58 -0800144 public static Interest getDefaultInterest(Name name) {
Andrew Brown7b1daf32015-01-19 16:36:01 -0800145 Interest interest = new Interest(name, DEFAULT_TIMEOUT);
146 return interest;
147 }
Andrew Brown3f2521a2015-01-17 22:10:15 -0800148}