blob: 66d0a8eccc56cbbdc48b117a18548028093ad1e5 [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),
Alexander Afanasyev9c578182014-05-14 17:28:28 -070087 RegisterPrefixSuccessCallback(),
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070088 bind(&Producer::onRegisterFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070089 m_face.processEvents();
Wentao Shang38d79682014-01-15 15:55:52 -080090 }
91
92private:
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070093 Name m_name;
94 Face m_face;
95 KeyChain m_keychain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070096
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070097 std::vector< shared_ptr<Data> > m_store;
Wentao Shang38d79682014-01-15 15:55:52 -080098
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070099 bool m_isVerbose;
Wentao Shang38d79682014-01-15 15:55:52 -0800100};
101
102int
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700103main(int argc, char** argv)
Wentao Shang38d79682014-01-15 15:55:52 -0800104{
105 if (argc < 2)
106 {
107 std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
108 return -1;
109 }
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110
Wentao Shang38d79682014-01-15 15:55:52 -0800111 try
112 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700113 time::steady_clock::TimePoint startTime = time::steady_clock::now();
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114
Wentao Shang38d79682014-01-15 15:55:52 -0800115 std::cerr << "Preparing the input..." << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116 Producer producer(argv[1]);
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700117 std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700118
119 while (true)
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800120 {
121 try
122 {
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700123 // this will exit when daemon dies... so try to connect again if possible
124 producer.run();
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800125 }
126 catch (std::exception& e)
127 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128 std::cerr << "ERROR: " << e.what() << std::endl;
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800129 // and keep going
Alexander Afanasyev1dd95c52014-03-22 19:11:36 -0700130 sleep(1);
Alexander Afanasyev8995f542014-01-17 15:33:44 -0800131 }
132 }
Wentao Shang38d79682014-01-15 15:55:52 -0800133 }
134 catch (std::exception& e)
135 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700136 std::cerr << "ERROR: " << e.what() << std::endl;
Wentao Shang38d79682014-01-15 15:55:52 -0800137 }
138 return 0;
139}
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -0700140
141} // namespace ndn
142
143int
144main(int argc, char** argv)
145{
146 return ndn::main(argc, argv);
147}