andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 14 | package com.intel.jndn.utils; |
| 15 | |
| 16 | import com.intel.jndn.utils.server.RespondWithData; |
| 17 | import com.intel.jndn.utils.server.RespondWithBlob; |
| 18 | import com.intel.jndn.utils.server.ServerBaseImpl; |
| 19 | import java.io.IOException; |
| 20 | import java.nio.ByteBuffer; |
| 21 | import java.util.logging.Level; |
| 22 | import java.util.logging.Logger; |
| 23 | import net.named_data.jndn.Data; |
| 24 | import net.named_data.jndn.Face; |
| 25 | import net.named_data.jndn.Interest; |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame] | 26 | import net.named_data.jndn.InterestFilter; |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 27 | import net.named_data.jndn.Name; |
| 28 | import net.named_data.jndn.transport.Transport; |
| 29 | import 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 | */ |
| 37 | public class SimpleServer extends ServerBaseImpl implements DynamicServer { |
| 38 | |
andrewsbrown | a2a877d | 2015-04-14 14:20:45 -0700 | [diff] [blame] | 39 | private static final Logger logger = Logger.getLogger(SimpleServer.class.getName()); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 40 | 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 |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame] | 87 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter){ |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 88 | try { |
| 89 | Data data = callback.onInterest(prefix, interest); |
| 90 | data = processPipeline(data); |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame] | 91 | face.putData(data); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 92 | } catch (Exception e) { |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame] | 93 | logger.log(Level.FINE, "Failed to send data for: " + interest.toUri(), e); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | } |