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; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.util.List; |
| 25 | import java.util.logging.Level; |
| 26 | import java.util.logging.Logger; |
| 27 | import net.named_data.jndn.Data; |
| 28 | import net.named_data.jndn.Face; |
| 29 | import net.named_data.jndn.Interest; |
| 30 | import net.named_data.jndn.Name; |
| 31 | import net.named_data.jndn.encoding.EncodingException; |
| 32 | import net.named_data.jndn.transport.Transport; |
| 33 | |
| 34 | /** |
| 35 | * Implementation of a {@link RepositoryServer} that segments packets stored in |
| 36 | * its repository. |
| 37 | * |
| 38 | * @author Andrew Brown <andrew.brown@intel.com> |
| 39 | */ |
| 40 | public class SegmentedServer extends ServerBaseImpl implements RepositoryServer { |
| 41 | |
andrewsbrown | a2a877d | 2015-04-14 14:20:45 -0700 | [diff] [blame^] | 42 | private static final Logger logger = Logger.getLogger(SegmentedServer.class.getName()); |
andrewsbrown | 4dddd47 | 2015-04-01 14:28:46 -0700 | [diff] [blame] | 43 | private final Repository repository = new ForLoopRepository(); |
| 44 | |
| 45 | /** |
| 46 | * {@inheritDoc} |
| 47 | */ |
| 48 | public SegmentedServer(Face face, Name prefix) { |
| 49 | super(face, prefix); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * {@inheritDoc} |
| 54 | */ |
| 55 | @Override |
| 56 | public void serve(Data data) throws IOException { |
| 57 | if (!isRegistered()) { |
| 58 | register(); |
| 59 | } |
| 60 | |
| 61 | InputStream stream = new ByteArrayInputStream(data.getContent().getImmutableArray()); |
| 62 | List<Data> segments = SegmentedServerHelper.segment(data, stream); |
| 63 | for (Data segment : segments) { |
| 64 | logger.info("Added segment: " + segment.getName().toUri()); |
| 65 | repository.put(segment); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritDoc} |
| 71 | */ |
| 72 | @Override |
| 73 | public void onInterest(Name prefix, Interest interest, Transport transport, long registeredPrefixId) { |
| 74 | if (interest.getChildSelector() == -1) { |
| 75 | try { |
| 76 | interest.getName().get(-1).toSegment(); |
| 77 | } catch (EncodingException e) { |
| 78 | interest.setChildSelector(Interest.CHILD_SELECTOR_LEFT); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | Data data = repository.get(interest); |
| 84 | data = processPipeline(data); |
| 85 | ByteBuffer buffer = data.wireEncode().buf(); |
| 86 | transport.send(buffer); |
| 87 | } catch (Exception e) { |
| 88 | logger.log(Level.SEVERE, "Failed to send data for: " + interest.toUri(), e); |
| 89 | } |
| 90 | } |
| 91 | } |