blob: b983fa3dd3163641bf3ef7274f489ed83daf1400 [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;
andrewsbrown86088d72015-05-07 01:38:29 +010026import net.named_data.jndn.InterestFilter;
andrewsbrown4dddd472015-04-01 14:28:46 -070027import net.named_data.jndn.Name;
28import net.named_data.jndn.transport.Transport;
29import net.named_data.jndn.util.Blob;
30
31/**
32 * Implementation of a {@link DynamicServer} that wraps the {@link OnInterest}
33 * callback with some encoding and pipeline support.
34 *
35 * @author Andrew Brown <andrew.brown@intel.com>
36 */
37public class SimpleServer extends ServerBaseImpl implements DynamicServer {
38
andrewsbrowna2a877d2015-04-14 14:20:45 -070039 private static final Logger logger = Logger.getLogger(SimpleServer.class.getName());
andrewsbrown4dddd472015-04-01 14:28:46 -070040 private RespondWithData callback;
41
42 /**
43 * {@inheritDoc}
44 */
45 public SimpleServer(Face face, Name prefix) {
46 super(face, prefix);
47 }
48
49 /**
50 * {@inheritDoc}
51 */
52 @Override
53 public void respondUsing(RespondWithData callback) throws IOException {
54 if (!isRegistered()) {
55 register();
56 }
57 this.callback = callback;
58 }
59
60 /**
61 * Convenience method for responding to an {@link Interest} by returning the
62 * {@link Blob} content only; when an Interest arrives, this method wraps the
63 * returned Blob with a {@link Data} using the exact {@link Name} of the
64 * incoming Interest.
65 *
66 * @param callback the callback function to retrieve content when an
67 * {@link Interest} arrives
68 * @throws java.io.IOException if the server fails to register a prefix
69 */
70 public void respondUsing(final RespondWithBlob callback) throws IOException {
71 RespondWithData dataCallback = new RespondWithData() {
72 @Override
73 public Data onInterest(Name prefix, Interest interest) throws Exception {
74 Data data = new Data(interest.getName());
75 Blob content = callback.onInterest(prefix, interest);
76 data.setContent(content);
77 return data;
78 }
79 };
80 respondUsing(dataCallback);
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
andrewsbrown86088d72015-05-07 01:38:29 +010087 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter){
andrewsbrown4dddd472015-04-01 14:28:46 -070088 try {
89 Data data = callback.onInterest(prefix, interest);
90 data = processPipeline(data);
andrewsbrown86088d72015-05-07 01:38:29 +010091 face.putData(data);
andrewsbrown4dddd472015-04-01 14:28:46 -070092 } catch (Exception e) {
andrewsbrown86088d72015-05-07 01:38:29 +010093 logger.log(Level.FINE, "Failed to send data for: " + interest.toUri(), e);
andrewsbrown4dddd472015-04-01 14:28:46 -070094 }
95 }
96}