blob: 00595ea20c05beb4fb90b8692a3e303bb43860a7 [file] [log] [blame]
shockjianga5ae48c2014-07-27 23:21:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
4 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef NDNS_NDN_APP_HPP
20#define NDNS_NDN_APP_HPP
21
22#include <ndn-cxx/face.hpp>
23#include <ndn-cxx/name.hpp>
24#include <ndn-cxx/security/key-chain.hpp>
25
26#include <boost/asio.hpp>
27#include <boost/bind.hpp>
28#include <boost/date_time/posix_time/posix_time.hpp>
29#include <boost/noncopyable.hpp>
30
31using namespace std;
32
33namespace ndn {
34namespace ndns{
35
36class NDNApp {
37public:
38 NDNApp(const char *programName, const char *prefix);
39 virtual ~NDNApp();
40
41 virtual void
42 onData(const ndn::Interest& interest, Data& data)
43 {
44 }
45 virtual void
46 onTimeout(const ndn::Interest& interest)
47 {
48 std::cout<<"!- Interest Timeout"<<interest.getName()<<std::endl;
49 }
50
51 virtual void
52 onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
53 {
54 m_error = "ERROR: Failed to register prefix in local hub's daemon";
55 m_error += " due to: ";
56 m_error += reason;
57 m_hasError = true;
58 this->stop();
59 }
60
61
62 virtual void
63 onInterest(const Name &name, const Interest& interest)
64 {
65
66 }
67
68 virtual void
69 signalHandler()
70 {
71 this->stop();
72 exit(1);
73 }
74
75
76 virtual void
77 stop()
78 {
79 std::cout<<m_programName<<" stops"<<std::endl;
80 m_ioService.stop();
81 m_face.shutdown();
82 if (hasError())
83 {
84 cout<<m_error<<endl;
85 }
86 }
87
88
89
90 bool hasError() const {
91 return m_hasError;
92 }
93
94 void setHasError(bool hasError) {
95 m_hasError = hasError;
96 }
97
98 const char* getPrefix() const {
99 return m_prefix;
100 }
101
102 void setPrefix(char* prefix) {
103 m_prefix = prefix;
104 }
105
106 const char* getProgramName() const {
107 return m_programName;
108 }
109
110 void setProgramName(char* programName) {
111 m_programName = programName;
112 }
113
114 const string& getErr() const {
115 return m_error;
116 }
117
118 void setErr(const string& err) {
119 m_error = err;
120 }
121
122 uint32_t getInterestTriedMax() const {
123 return m_interestTriedMax;
124 }
125
126 void setInterestTriedMax(uint32_t interestTriedMax) {
127 m_interestTriedMax = interestTriedMax;
128 }
129
130 time::milliseconds getContentFreshness() const {
131 return m_contentFreshness;
132 }
133
134 void setContentFreshness(time::milliseconds contentFreshness) {
135 m_contentFreshness = contentFreshness;
136 }
137
138 time::milliseconds getInterestLifetime() const {
139 return m_interestLifetime;
140 }
141
142 void setInterestLifetime(time::milliseconds interestLifetime) {
143 m_interestLifetime = interestLifetime;
144 }
145
146public:
147 const char* m_programName;
148 const char* m_prefix;
149 bool m_hasError;
150 string m_error;
151 time::milliseconds m_contentFreshness;
152 time::milliseconds m_interestLifetime;
153
154 uint32_t m_interestTriedMax;
155 uint32_t m_interestTriedNum;
156
157 //Name m_name;
158 boost::asio::io_service m_ioService;
159 //Zone m_zone;
160 //ZoneMgr m_zoneMgr;
161
162
163 Face m_face;
164 KeyChain m_keyChain;
165};
166
167} //namespace ndns
168} /* namespace ndn */
169
170#endif /* NDN_APP_HPP_ */