blob: e6d74b2bbc3ec24a17df01c417ff302c92bf6b28 [file] [log] [blame]
Wentao Shang38d79682014-01-15 15:55:52 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07002/**
3 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
Wentao Shang38d79682014-01-15 15:55:52 -08005 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Wentao Shang38d79682014-01-15 15:55:52 -08008 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07009 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Wentao Shang38d79682014-01-15 15:55:52 -080011 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070012 * @author Wentao Shang <http://irl.cs.ucla.edu/~wentao/>
Wentao Shang38d79682014-01-15 15:55:52 -080013 */
14
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080015#include "face.hpp"
16#include "security/key-chain.hpp"
Wentao Shang38d79682014-01-15 15:55:52 -080017
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070018namespace ndn {
19
20const size_t MAX_SEG_SIZE = 4096;
Wentao Shang38d79682014-01-15 15:55:52 -080021
22class Producer
23{
24public:
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070025 Producer(const char* name)
26 : m_name(name)
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027 , m_isVerbose(false)
Wentao Shang38d79682014-01-15 15:55:52 -080028 {
29 int segnum = 0;
30 char* buf = new char[MAX_SEG_SIZE];
31 do
32 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033 std::cin.read(buf, MAX_SEG_SIZE);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070034 int got = std::cin.gcount();
Wentao Shang38d79682014-01-15 15:55:52 -080035
36 if (got > 0)
37 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070038 shared_ptr<Data> data =
39 make_shared<Data>(Name(m_name).appendSegment(segnum));
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070040
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070041 data->setFreshnessPeriod(time::milliseconds(10000)); // 10 sec
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070042 data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
43
Wentao Shang38d79682014-01-15 15:55:52 -080044 m_keychain.sign(*data);
45 m_store.push_back(data);
46 segnum++;
47 }
48 }
Alexander Afanasyev4b98e8c2014-03-22 19:10:19 -070049 while (static_cast<bool>(std::cin));
Wentao Shang38d79682014-01-15 15:55:52 -080050
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070051 if (m_isVerbose)
Wentao Shang38d79682014-01-15 15:55:52 -080052 std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
53 }
54
55 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070056 onInterest(const Name& name, const Interest& interest)
Wentao Shang38d79682014-01-15 15:55:52 -080057 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070058 if (m_isVerbose)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080059 std::cerr << "<< I: " << interest << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
61 size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
Wentao Shang38d79682014-01-15 15:55:52 -080062
63 if (segnum < m_store.size())
64 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065 m_face.put(*m_store[segnum]);
Wentao Shang38d79682014-01-15 15:55:52 -080066 }
67 }
68
69 void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070070 onRegisterFailed(const Name& prefix, const std::string& reason)
Wentao Shang38d79682014-01-15 15:55:52 -080071 {
Alexander Afanasyeve289b532014-02-09 22:14:44 -080072 std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070073 m_face.shutdown();
Wentao Shang38d79682014-01-15 15:55:52 -080074 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070075
Wentao Shang38d79682014-01-15 15:55:52 -080076 void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 run()
Wentao Shang38d79682014-01-15 15:55:52 -080078 {
79 if (m_store.empty())
80 {
81 std::cerr << "Nothing to serve. Exiting." << std::endl;
82 return;
83 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070084
85 m_face.setInterestFilter(m_name,
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070086 bind(&Producer::onInterest, this, _1, _2),
87 bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080089 }
90
91private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070092 Name m_name;
93 Face m_face;
94 KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070095
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070096 std::vector< shared_ptr<Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -080097
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070098 bool m_isVerbose;
Wentao Shang38d79682014-01-15 15:55:52 -080099};
100
101int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700102main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800103{
104 if (argc < 2)
105 {
106 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
107 return -1;
108 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700109
Wentao Shang38d79682014-01-15 15:55:52 -0800110 try
111 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700112 time::steady_clock::TimePoint startTime = time::steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113
Wentao Shang38d79682014-01-15 15:55:52 -0800114 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115 Producer producer(argv[1]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700116 std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
118 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800119 {
120 try
121 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700122 // this will exit when daemon dies... so try to connect again if possible
123 producer.run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800124 }
125 catch (std::exception& e)
126 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800128 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700129 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800130 }
131 }
Wentao Shang38d79682014-01-15 15:55:52 -0800132 }
133 catch (std::exception& e)
134 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800136 }
137 return 0;
138}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700139
140} // namespace ndn
141
142int
143main(int argc, char** argv)
144{
145 return ndn::main(argc, argv);
146}