andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -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.mock.MockKeyChain; |
| 17 | import java.io.IOException; |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 18 | import java.util.logging.Logger; |
| 19 | import net.named_data.jndn.Data; |
| 20 | import net.named_data.jndn.Face; |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 21 | import net.named_data.jndn.Interest; |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 22 | import net.named_data.jndn.Name; |
| 23 | import net.named_data.jndn.encoding.EncodingException; |
| 24 | import net.named_data.jndn.security.KeyChain; |
| 25 | import net.named_data.jndn.security.SecurityException; |
| 26 | import net.named_data.jndn.util.Blob; |
| 27 | import static org.junit.Assert.*; |
| 28 | import org.junit.Test; |
| 29 | |
| 30 | /** |
| 31 | * Test the {@link SegmentedServer} on an actual NFD; to run this, pass the NFD |
| 32 | * IP/host name as a parameter like <code>-Dnfd.ip=10.1.1.1</code>. |
| 33 | * |
| 34 | * @author Andrew Brown <andrew.brown@intel.com> |
| 35 | */ |
| 36 | public class SegmentedServerTestIT { |
| 37 | |
| 38 | private static final Logger logger = Logger.getLogger(SegmentedServerTestIT.class.getName()); |
| 39 | Name prefix; |
| 40 | Face face; |
| 41 | SegmentedServer instance; |
| 42 | private String ip; |
| 43 | |
| 44 | public SegmentedServerTestIT() throws SecurityException { |
| 45 | this.ip = System.getProperty("nfd.ip"); |
| 46 | this.prefix = new Name("/test/for/segmented-server"); |
| 47 | this.face = new Face(ip); |
| 48 | this.instance = new SegmentedServer(face, prefix); |
| 49 | KeyChain mockKeyChain = MockKeyChain.configure(new Name("/test/server")); |
| 50 | face.setCommandSigningInfo(mockKeyChain, mockKeyChain.getDefaultCertificateName()); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testRegisterAndRetrieval() throws Exception { |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 55 | final Name name1 = new Name(prefix).append("1"); |
| 56 | final Name name2 = new Name(prefix).append("2"); |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 57 | |
| 58 | // why a new thread? The server will only operate if someone turns the crank, |
| 59 | // i.e. someone calls face.processEvents() every so often. |
| 60 | new Thread(new Runnable() { |
| 61 | @Override |
| 62 | public void run() { |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 63 | try { |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 64 | instance.serve(buildDataPacket(name1)); |
| 65 | instance.serve(buildDataPacket(name2)); |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 66 | } catch (IOException ex) { |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 67 | logger.info("Failed to serve data."); |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | while (true) { |
| 71 | try { |
| 72 | face.processEvents(); |
| 73 | } catch (IOException | EncodingException ex) { |
| 74 | logger.info("Failed while processing events."); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | }).start(); |
| 79 | |
| 80 | // why wait? we want to make sure the thread is running and that the prefix |
| 81 | // registration has succeeded on the NFD before we send interests |
| 82 | Thread.sleep(1000); |
| 83 | |
| 84 | // why a different face? because we don't want the abover face.processEvents() |
| 85 | // to interfere with the SimpleClient's processEvents(). |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 86 | logger.info("Retrieving data: " + prefix.toUri()); |
| 87 | Interest interest = new Interest(new Name(prefix)); |
| 88 | interest.setInterestLifetimeMilliseconds(2000); |
| 89 | interest.setMustBeFresh(true); |
| 90 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 91 | |
| 92 | Data retrieved = SegmentedClient.getDefault().getSync(new Face(ip), interest); |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 93 | assertNotNull(retrieved); |
| 94 | assertEquals("...", retrieved.getContent().toString()); |
andrewsbrown | 54cee48 | 2015-04-22 16:41:03 -0700 | [diff] [blame^] | 95 | logger.info("Retrieved data: " + retrieved.getName().toUri()); |
| 96 | } |
| 97 | |
| 98 | Data buildDataPacket(Name name) { |
| 99 | Data data = new Data(new Name(name).appendSegment(0)); |
| 100 | data.setContent(new Blob("...")); |
| 101 | data.getMetaInfo().setFreshnessPeriod(30000); |
| 102 | return data; |
andrewsbrown | 6f8f469 | 2015-04-20 13:39:49 -0700 | [diff] [blame] | 103 | } |
| 104 | } |