blob: c5cfec58138b262734ee8ba045cc7e780db48988 [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;
23import java.nio.ByteBuffer;
24import java.util.List;
25import java.util.logging.Level;
26import java.util.logging.Logger;
27import net.named_data.jndn.Data;
28import net.named_data.jndn.Face;
29import net.named_data.jndn.Interest;
30import net.named_data.jndn.Name;
31import net.named_data.jndn.encoding.EncodingException;
32import 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 */
40public class SegmentedServer extends ServerBaseImpl implements RepositoryServer {
41
andrewsbrowna2a877d2015-04-14 14:20:45 -070042 private static final Logger logger = Logger.getLogger(SegmentedServer.class.getName());
andrewsbrown4dddd472015-04-01 14:28:46 -070043 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}