blob: 0bd4667cc42b6e4092fe1f54a6a3c40e1eaaa3e6 [file] [log] [blame]
andrewsbrown4dddd472015-04-01 14:28:46 -07001/*
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.server.RespondWithData;
17import com.intel.jndn.utils.server.RespondWithBlob;
18import com.intel.jndn.utils.server.ServerBaseImpl;
19import java.io.IOException;
20import java.nio.ByteBuffer;
21import java.util.logging.Level;
22import java.util.logging.Logger;
23import net.named_data.jndn.Data;
24import net.named_data.jndn.Face;
25import net.named_data.jndn.Interest;
26import net.named_data.jndn.Name;
27import net.named_data.jndn.transport.Transport;
28import net.named_data.jndn.util.Blob;
29
30/**
31 * Implementation of a {@link DynamicServer} that wraps the {@link OnInterest}
32 * callback with some encoding and pipeline support.
33 *
34 * @author Andrew Brown <andrew.brown@intel.com>
35 */
36public class SimpleServer extends ServerBaseImpl implements DynamicServer {
37
38 private static final Logger logger = Logger.getLogger(SegmentedClient.class.getName());
39 private RespondWithData callback;
40
41 /**
42 * {@inheritDoc}
43 */
44 public SimpleServer(Face face, Name prefix) {
45 super(face, prefix);
46 }
47
48 /**
49 * {@inheritDoc}
50 */
51 @Override
52 public void respondUsing(RespondWithData callback) throws IOException {
53 if (!isRegistered()) {
54 register();
55 }
56 this.callback = callback;
57 }
58
59 /**
60 * Convenience method for responding to an {@link Interest} by returning the
61 * {@link Blob} content only; when an Interest arrives, this method wraps the
62 * returned Blob with a {@link Data} using the exact {@link Name} of the
63 * incoming Interest.
64 *
65 * @param callback the callback function to retrieve content when an
66 * {@link Interest} arrives
67 * @throws java.io.IOException if the server fails to register a prefix
68 */
69 public void respondUsing(final RespondWithBlob callback) throws IOException {
70 RespondWithData dataCallback = new RespondWithData() {
71 @Override
72 public Data onInterest(Name prefix, Interest interest) throws Exception {
73 Data data = new Data(interest.getName());
74 Blob content = callback.onInterest(prefix, interest);
75 data.setContent(content);
76 return data;
77 }
78 };
79 respondUsing(dataCallback);
80 }
81
82 /**
83 * {@inheritDoc}
84 */
85 @Override
86 public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) {
87 try {
88 Data data = callback.onInterest(prefix, interest);
89 data = processPipeline(data);
90 ByteBuffer buffer = data.wireEncode().buf();
91 transport.send(buffer);
92 } catch (Exception e) {
93 logger.log(Level.SEVERE, "Failed to send data for: " + interest.toUri(), e);
94 }
95 }
96}