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; |
| 26 | import net.named_data.jndn.Name; |
| 27 | import net.named_data.jndn.transport.Transport; |
| 28 | import 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 | */ |
| 36 | public 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 | } |