Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * File name: Server.java |
| 3 | * |
| 4 | * Purpose: Provide a server to simplify serving data 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 | */ |
| 11 | package com.intel.jndn.utils; |
| 12 | |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 13 | import com.intel.jndn.utils.event.NDNEvent; |
| 14 | import com.intel.jndn.utils.event.NDNObservable; |
| 15 | import com.intel.jndn.utils.event.NDNObserver; |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 16 | import java.io.IOException; |
| 17 | import net.named_data.jndn.Data; |
| 18 | import net.named_data.jndn.Face; |
| 19 | import net.named_data.jndn.ForwardingFlags; |
| 20 | import net.named_data.jndn.Interest; |
| 21 | import net.named_data.jndn.Name; |
| 22 | import net.named_data.jndn.OnInterest; |
| 23 | import net.named_data.jndn.OnRegisterFailed; |
| 24 | import net.named_data.jndn.encoding.EncodingException; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.security.KeyChain; |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 26 | import net.named_data.jndn.transport.Transport; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 27 | import java.util.logging.Logger; |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Provide a server to simplify serving data over the NDN network. Exposes two |
| 31 | * main methods: put() for serving static, known data packets and on() for |
| 32 | * serving dynamically created packets on-demand. |
| 33 | * |
| 34 | * @author Andrew Brown <andrew.brown@intel.com> |
| 35 | */ |
| 36 | public class Server { |
| 37 | |
| 38 | public static final long DEFAULT_SLEEP_TIME = 20; |
| 39 | public static final long DEFAULT_TIMEOUT = 2000; |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 40 | private static final Logger logger = Logger.getLogger(Server.class.getName()); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 41 | private static Server defaultInstance; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 42 | private KeyChain keyChain; |
| 43 | private Name certificateName; |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Singleton access for simpler server use |
| 47 | * |
| 48 | * @return |
| 49 | */ |
| 50 | public static Server getDefault() { |
| 51 | if (defaultInstance == null) { |
| 52 | defaultInstance = new Server(); |
| 53 | } |
| 54 | return defaultInstance; |
| 55 | } |
| 56 | |
| 57 | /** |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 58 | * Constructor |
| 59 | */ |
| 60 | public Server() { |
| 61 | // no signing |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Constructor; enables signing |
| 66 | * |
| 67 | * @param keyChain |
| 68 | * @param certificateName |
| 69 | */ |
| 70 | public Server(KeyChain keyChain, Name certificateName) { |
| 71 | this.keyChain = keyChain; |
| 72 | this.certificateName = certificateName; |
| 73 | } |
| 74 | |
| 75 | /** |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 76 | * Synchronously serve a Data on the given face until one request accesses the |
| 77 | * data; will return incoming Interest request. |
| 78 | * <pre><code> Interest request = Client.putSync(face, data); </code></pre> |
| 79 | * |
| 80 | * @param face |
| 81 | * @param data |
| 82 | * @return |
| 83 | */ |
| 84 | public Interest putSync(Face face, final Data data) { |
| 85 | // setup event |
| 86 | long startTime = System.currentTimeMillis(); |
| 87 | final String dataName = data.getName().toUri(); |
| 88 | final NDNEvent event = new NDNEvent(); |
| 89 | |
| 90 | // setup flags |
| 91 | ForwardingFlags flags = new ForwardingFlags(); |
| 92 | flags.setCapture(true); |
| 93 | |
| 94 | // register the data name on the face |
| 95 | try { |
| 96 | face.registerPrefix(data.getName(), new OnInterest() { |
| 97 | @Override |
| 98 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 99 | // sign packet |
| 100 | if (keyChain != null) { |
| 101 | try { |
| 102 | keyChain.sign(data, certificateName != null ? certificateName : keyChain.getDefaultCertificateName()); |
| 103 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 104 | logger.severe("Failed to sign data for: " + dataName + e); |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 105 | event.fromPacket(e); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // send packet |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 110 | try { |
| 111 | transport.send(data.wireEncode().buf()); |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 112 | logger.fine("Sent data: " + dataName); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 113 | event.fromPacket(interest); |
| 114 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 115 | logger.severe("Failed to send data for: " + dataName); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 116 | event.fromPacket(e); |
| 117 | } |
| 118 | } |
| 119 | }, new OnRegisterFailed() { |
| 120 | @Override |
| 121 | public void onRegisterFailed(Name prefix) { |
| 122 | event.fromPacket(new Exception("Failed to register name: " + dataName)); |
| 123 | } |
| 124 | }, flags); |
| 125 | logger.info("Registered data: " + dataName); |
| 126 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 127 | logger.severe("Could not connect to face to register prefix: " + dataName + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 128 | event.fromPacket(e); |
| 129 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 130 | logger.severe("Error registering prefix: " + dataName + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 131 | event.fromPacket(e); |
| 132 | } |
| 133 | |
| 134 | // process eventCount until one response is sent or error |
| 135 | while (event.getPacket() == null) { |
| 136 | try { |
| 137 | synchronized (face) { |
| 138 | face.processEvents(); |
| 139 | } |
| 140 | } catch (IOException | EncodingException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 141 | logger.warning("Failed to process events." + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 142 | event.fromPacket(e); |
| 143 | } |
| 144 | sleep(); |
| 145 | } |
| 146 | |
| 147 | // return |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 148 | logger.fine("Request time (ms): " + (event.getTimestamp() - startTime)); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 149 | return (event.isSuccess()) ? (Interest) event.getPacket() : null; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Asynchronously serve a Data on the given face until an observer stops it. |
| 154 | * E.g.: NDNObserver observer = Client.put(face, data); // when finished |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 155 | * serving the data, stop the background thread observer.stop(); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 156 | * |
| 157 | * @param face |
| 158 | * @param data |
| 159 | * @return |
| 160 | */ |
| 161 | public NDNObserver put(final Face face, final Data data) { |
| 162 | // setup observer |
| 163 | final NDNObserver observer = new NDNObserver(); |
| 164 | final NDNObservable eventHandler = new NDNObservable(); |
| 165 | eventHandler.addObserver(observer); |
| 166 | |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 167 | // setup interest handler |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 168 | final OnInterest interestHandler = new OnInterest() { |
| 169 | @Override |
| 170 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 171 | // sign packet |
| 172 | if (keyChain != null) { |
| 173 | try { |
| 174 | keyChain.sign(data, certificateName != null ? certificateName : keyChain.getDefaultCertificateName()); |
| 175 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 176 | logger.severe("Failed to sign data for: " + data.getName().toUri() + e); |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 177 | eventHandler.notify(e); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // send packet |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 182 | try { |
| 183 | transport.send(data.wireEncode().buf()); |
| 184 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 185 | logger.severe("Failed to send data for: " + data.getName().toUri()); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 186 | eventHandler.notify(e); |
| 187 | } |
| 188 | } |
| 189 | }; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 190 | |
| 191 | // setup failure handler |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 192 | final OnRegisterFailed failureHandler = new OnRegisterFailed() { |
| 193 | @Override |
| 194 | public void onRegisterFailed(Name prefix) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 195 | logger.severe("Failed to register name to put: " + data.getName().toUri()); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 196 | eventHandler.notify(new Exception("Failed to register name to put: " + data.getName().toUri())); |
| 197 | } |
| 198 | }; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 199 | |
| 200 | // setup forwarding flags |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 201 | final ForwardingFlags flags = new ForwardingFlags(); |
| 202 | flags.setCapture(true); // no shorter routes will answer for this prefix, see http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Route-inheritance |
| 203 | flags.setChildInherit(false); // the interest name must be exact, no child components after the prefix |
| 204 | |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 205 | // start background thread |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 206 | Thread backgroundThread = new Thread(new Runnable() { |
| 207 | @Override |
| 208 | public void run() { |
| 209 | // register name on the face |
| 210 | try { |
| 211 | face.registerPrefix(data.getName(), interestHandler, failureHandler, flags); |
| 212 | logger.info("Registered data : " + data.getName().toUri()); |
| 213 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 214 | logger.severe("Could not connect to face to register prefix: " + data.getName().toUri() + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 215 | eventHandler.notify(e); |
| 216 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 217 | logger.severe("Error registering prefix: " + data.getName().toUri() + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 218 | eventHandler.notify(e); |
| 219 | } |
| 220 | |
| 221 | // process eventCount until a request is received |
| 222 | while (observer.interestCount() == 0 && observer.errorCount() == 0 && !observer.mustStop()) { |
| 223 | try { |
| 224 | synchronized (face) { |
| 225 | face.processEvents(); |
| 226 | } |
| 227 | } catch (IOException | EncodingException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 228 | logger.warning("Failed to process events." + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 229 | eventHandler.notify(e); |
| 230 | } |
| 231 | sleep(); |
| 232 | } |
| 233 | } |
| 234 | }); |
| 235 | backgroundThread.setName(String.format("Server.put(%s)", data.getName().toUri())); |
| 236 | backgroundThread.setDaemon(true); |
| 237 | backgroundThread.start(); |
| 238 | |
| 239 | return observer; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Register a prefix on the face to serve Data packets for incoming Interests. |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 244 | * This method will create a background thread to process events until the |
| 245 | * user calls stop() on the returned observer |
| 246 | * |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 247 | * @param face |
| 248 | * @param prefix |
| 249 | * @param handler |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 250 | * @return |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 251 | */ |
| 252 | public NDNObserver on(final Face face, final Name prefix, final OnServeInterest handler) { |
| 253 | // setup observer |
| 254 | final NDNObserver observer = new NDNObserver(); |
| 255 | final NDNObservable eventHandler = new NDNObservable(); |
| 256 | eventHandler.addObserver(observer); |
| 257 | |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 258 | // setup interest handler |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 259 | final OnInterest interestHandler = new OnInterest() { |
| 260 | @Override |
| 261 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 262 | // notify observers of interest received |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 263 | eventHandler.notify(interest); |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 264 | |
| 265 | // grab data from OnServeInterest handler |
| 266 | Data data = handler.onInterest(prefix, interest); |
| 267 | |
| 268 | // sign packet |
| 269 | if (keyChain != null) { |
| 270 | try { |
| 271 | keyChain.sign(data, certificateName != null ? certificateName : keyChain.getDefaultCertificateName()); |
| 272 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 273 | logger.severe("Failed to sign data for: " + interest.getName().toUri() + e); |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 274 | eventHandler.notify(e); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // send packet |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 279 | try { |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 280 | transport.send(data.wireEncode().buf()); |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 281 | eventHandler.notify(data); // notify observers of data sent |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 282 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 283 | logger.severe("Failed to send data for: " + interest.getName().toUri()); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 284 | eventHandler.notify(e); |
| 285 | } |
| 286 | } |
| 287 | }; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 288 | |
| 289 | // setup failure handler |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 290 | final OnRegisterFailed failureHandler = new OnRegisterFailed() { |
| 291 | @Override |
| 292 | public void onRegisterFailed(Name prefix) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 293 | logger.severe("Failed to register name to put: " + prefix.toUri()); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 294 | eventHandler.notify(new Exception("Failed to register name to put: " + prefix.toUri())); |
| 295 | } |
| 296 | }; |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 297 | |
| 298 | // setup forwarding flags |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 299 | final ForwardingFlags flags = new ForwardingFlags(); |
| 300 | flags.setCapture(true); // no shorter routes will answer for this prefix, see http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#Route-inheritance |
| 301 | flags.setChildInherit(true); // the interest name may have child components after the prefix |
| 302 | |
Andrew Brown | a88e6ef | 2015-01-22 11:43:44 -0800 | [diff] [blame] | 303 | // start background thread |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 304 | Thread backgroundThread = new Thread(new Runnable() { |
| 305 | @Override |
| 306 | public void run() { |
| 307 | // register name on the face |
| 308 | try { |
| 309 | face.registerPrefix(prefix, interestHandler, failureHandler, flags); |
| 310 | logger.info("Registered data : " + prefix.toUri()); |
| 311 | } catch (IOException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 312 | logger.severe("Could not connect to face to register prefix: " + prefix.toUri() + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 313 | eventHandler.notify(e); |
| 314 | } catch (net.named_data.jndn.security.SecurityException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 315 | logger.severe("Error registering prefix: " + prefix.toUri() + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 316 | eventHandler.notify(e); |
| 317 | } |
| 318 | |
| 319 | // process events until told to stop or error bubbles up |
| 320 | while (observer.errorCount() == 0 && !observer.mustStop()) { |
| 321 | try { |
| 322 | synchronized (face) { |
| 323 | face.processEvents(); |
| 324 | } |
| 325 | } catch (IOException | EncodingException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 326 | logger.warning("Failed to process events." + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 327 | eventHandler.notify(e); |
| 328 | } |
| 329 | sleep(); |
| 330 | } |
| 331 | } |
| 332 | }); |
Andrew Brown | 665c0c1 | 2015-01-22 15:57:53 -0800 | [diff] [blame] | 333 | backgroundThread.setName(String.format("Server.on(%s)", prefix.toUri())); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 334 | backgroundThread.setDaemon(true); |
| 335 | backgroundThread.start(); |
| 336 | |
| 337 | return observer; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Put the current thread to sleep to allow time for IO |
| 342 | */ |
| 343 | protected void sleep() { |
| 344 | try { |
| 345 | Thread.currentThread().sleep(DEFAULT_SLEEP_TIME); |
| 346 | } catch (InterruptedException e) { |
Andrew Brown | db45705 | 2015-02-21 15:41:58 -0800 | [diff] [blame] | 347 | logger.severe("Event loop interrupted." + e); |
Andrew Brown | a450fad | 2015-01-22 11:24:40 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
| 351 | } |