blob: 875ca87c31cc2e00910117a3bf020393dc27d8d3 [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.repository.ForLoopRepository;
17import com.intel.jndn.utils.repository.Repository;
18import com.intel.jndn.utils.server.SegmentedServerHelper;
19import com.intel.jndn.utils.server.ServerBaseImpl;
20import java.io.ByteArrayInputStream;
21import java.io.IOException;
22import java.io.InputStream;
andrewsbrown4dddd472015-04-01 14:28:46 -070023import java.util.List;
24import java.util.logging.Level;
25import java.util.logging.Logger;
26import net.named_data.jndn.Data;
27import net.named_data.jndn.Face;
28import net.named_data.jndn.Interest;
andrewsbrown86088d72015-05-07 01:38:29 +010029import net.named_data.jndn.InterestFilter;
andrewsbrown4dddd472015-04-01 14:28:46 -070030import net.named_data.jndn.Name;
31import net.named_data.jndn.encoding.EncodingException;
andrewsbrown4dddd472015-04-01 14:28:46 -070032
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 */
39public class SegmentedServer extends ServerBaseImpl implements RepositoryServer {
40
andrewsbrowna2a877d2015-04-14 14:20:45 -070041 private static final Logger logger = Logger.getLogger(SegmentedServer.class.getName());
andrewsbrown4dddd472015-04-01 14:28:46 -070042 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
andrewsbrown6e2d6b72015-04-20 13:33:21 -070060 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);
andrewsbrown4dddd472015-04-01 14:28:46 -070070 }
71 }
72
73 /**
74 * {@inheritDoc}
75 */
76 @Override
andrewsbrown86088d72015-05-07 01:38:29 +010077 public void onInterest(Name prefix, Interest interest, Face face, long interestFilterId, InterestFilter filter) {
andrewsbrown6e2d6b72015-04-20 13:33:21 -070078 logger.finer("Serving packet for: " + interest.toUri());
79
andrewsbrown4dddd472015-04-01 14:28:46 -070080 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);
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 find data satisfying: " + interest.toUri(), e);
andrewsbrown4dddd472015-04-01 14:28:46 -070094 }
95 }
andrewsbrown6e2d6b72015-04-20 13:33:21 -070096
97 /**
98 * {@inheritDoc}
99 */
100 @Override
101 public void cleanup() {
102 repository.cleanup();
103 }
andrewsbrown4dddd472015-04-01 14:28:46 -0700104}