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.repository.ForLoopRepository; |
| 17 | import com.intel.jndn.utils.repository.Repository; |
| 18 | import com.intel.jndn.utils.server.SegmentedServerHelper; |
| 19 | import com.intel.jndn.utils.server.ServerBaseImpl; |
| 20 | import java.io.ByteArrayInputStream; |
| 21 | import java.io.IOException; |
| 22 | import java.io.InputStream; |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 23 | import java.util.List; |
| 24 | import java.util.logging.Level; |
| 25 | import java.util.logging.Logger; |
| 26 | import net.named_data.jndn.Data; |
| 27 | import net.named_data.jndn.Face; |
| 28 | import net.named_data.jndn.Interest; |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame^] | 29 | import net.named_data.jndn.InterestFilter; |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 30 | import net.named_data.jndn.Name; |
| 31 | import net.named_data.jndn.encoding.EncodingException; |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Implementation of a {@link RepositoryServer} that segments packets stored in |
| 35 | * its repository. |
| 36 | * |
| 37 | * @author Andrew Brown <andrew.brown@intel.com> |
| 38 | */ |
| 39 | public class SegmentedServer extends ServerBaseImpl implements RepositoryServer { |
| 40 | |
andrewsbrown | a2a877d | 2015-04-14 14:20:45 -0700 | [diff] [blame] | 41 | private static final Logger logger = Logger.getLogger(SegmentedServer.class.getName()); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 42 | private final Repository repository = new ForLoopRepository(); |
| 43 | |
| 44 | /** |
| 45 | * {@inheritDoc} |
| 46 | */ |
| 47 | public SegmentedServer(Face face, Name prefix) { |
| 48 | super(face, prefix); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * {@inheritDoc} |
| 53 | */ |
| 54 | @Override |
| 55 | public void serve(Data data) throws IOException { |
| 56 | if (!isRegistered()) { |
| 57 | register(); |
| 58 | } |
| 59 | |
andrewsbrown | 6e2d6b7 | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 60 | if (data.getContent().size() >= SegmentedServerHelper.DEFAULT_SEGMENT_SIZE) { |
| 61 | InputStream stream = new ByteArrayInputStream(data.getContent().getImmutableArray()); |
| 62 | List<Data> segments = SegmentedServerHelper.segment(data, stream); |
| 63 | for (Data segment : segments) { |
| 64 | logger.fine("Adding segment: " + segment.getName().toUri()); |
| 65 | repository.put(segment); |
| 66 | } |
| 67 | } else { |
| 68 | logger.fine("Adding segment: " + data.getName().toUri()); |
| 69 | repository.put(data); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * {@inheritDoc} |
| 75 | */ |
| 76 | @Override |
andrewsbrown | 86088d7 | 2015-05-07 01:38:29 +0100 | [diff] [blame^] | 77 | public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) { |
andrewsbrown | 6e2d6b7 | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 78 | logger.finer("Serving packet for: " + interest.toUri()); |
| 79 | |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 80 | if (interest.getChildSelector() == -1) { |
| 81 | try { |
| 82 | interest.getName().get(-1).toSegment(); |
| 83 | } catch (EncodingException e) { |
| 84 | interest.setChildSelector(Interest.CHILD_SELECTOR_LEFT); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | try { |
| 89 | Data data = repository.get(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 find data satisfying: " + interest.toUri(), e); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
andrewsbrown | 6e2d6b7 | 2015-04-20 13:33:21 -0700 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * {@inheritDoc} |
| 99 | */ |
| 100 | @Override |
| 101 | public void cleanup() { |
| 102 | repository.cleanup(); |
| 103 | } |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 104 | } |