All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
channel-discovery.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
22 #ifndef NDN_CHANNEL_DISCOVERY_HPP
23 #define NDN_CHANNEL_DISCOVERY_HPP
24 
25 #include <map>
26 #include <ndn-cpp/sync/chrono-sync2013.hpp>
27 
28 namespace ndntools {
29 
38 public:
39  enum ErrorCode {
40  REGISTER_FAILED = 1,
41  INTEREST_TIMEOUT = 2
42  };
43 
44  typedef ndn::func_lib::function<void
45  (ChannelDiscovery& channelDiscovery,
46  const ndn::ptr_lib::shared_ptr<ndn::Name>& userPrefix)> OnReceivedChannelList;
47 
48  typedef ndn::func_lib::function<void
49  (ErrorCode errorCode, const std::string& message)> OnError;
50 
86  (const ndn::Name& applicationDataPrefix,
87  const std::string& channelListFilePath,
88  const ndn::Name& applicationBroadcastPrefix, ndn::Face& face,
89  ndn::KeyChain& keyChain, const ndn::Name& certificateName,
90  ndn::Milliseconds syncLifetime,
91  const OnReceivedChannelList& onReceivedChannelList, const OnError& onError)
92  : impl_(new Impl
93  (*this, applicationDataPrefix, channelListFilePath, face, keyChain,
94  certificateName, syncLifetime, onReceivedChannelList, onError))
95  {
96  impl_->initialize(applicationBroadcastPrefix);
97  }
98 
104  void
105  addChannel(const std::string& channel)
106  {
107  impl_->addChannel(channel);
108  }
109 
116  void
117  removeChannel(const std::string& channel)
118  {
119  impl_->removeChannel(channel);
120  }
121 
126  const ndn::Name&
127  getApplicationDataPrefix() { return impl_->getApplicationDataPrefix(); }
128 
135  ndn::ptr_lib::shared_ptr<std::vector<ndn::Name>>
137  {
138  return impl_->getOtherUserPrefixes();
139  }
140 
148  ndn::ptr_lib::shared_ptr<std::vector<std::string>>
149  getChannelList(const ndn::Name& userPrefix)
150  {
151  return impl_->getChannelList(userPrefix);
152  }
153 
154  // TODO: Implement shutdown.
155 
156 private:
162  class Impl : public ndn::ptr_lib::enable_shared_from_this<Impl> {
163  public:
169  Impl
170  (ChannelDiscovery& parent, const ndn::Name& applicationDataPrefix,
171  const std::string& channelListFilePath, ndn::Face& face,
172  ndn::KeyChain& keyChain, const ndn::Name& certificateName,
173  ndn::Milliseconds syncLifetime,
174  const OnReceivedChannelList& onReceivedChannelList, const OnError& onError);
175 
182  void
183  initialize(const ndn::Name& applicationBroadcastPrefix);
184 
185  void
186  addChannel(const std::string& channel);
187 
188  void
189  removeChannel(const std::string& channel);
190 
191  const ndn::Name&
192  getApplicationDataPrefix() { return applicationDataPrefix_; }
193 
194  ndn::ptr_lib::shared_ptr<std::vector<ndn::Name>>
196 
197  ndn::ptr_lib::shared_ptr<std::vector<std::string>>
198  getChannelList(const ndn::Name& userPrefix);
199 
208  void
209  onRegisterApplicationPrefixSuccess
210  (const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix,
211  uint64_t registeredPrefixId, const ndn::Name& applicationBroadcastPrefix,
212  int previousSequenceNumber);
213 
217  void
218  onInitialized();
219 
223  void
224  onReceivedSyncState
225  (const std::vector<ndn::ChronoSync2013::SyncState>& syncStates,
226  bool isRecovery);
227 
231  void
232  onData
233  (const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
234  const ndn::ptr_lib::shared_ptr<ndn::Data>& data);
235 
238  void
239  onApplicationInterest
240  (const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix,
241  const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest,
242  ndn::Face& face, uint64_t interestFilterId,
243  const ndn::ptr_lib::shared_ptr<const ndn::InterestFilter>& filter);
244 
250  void
251  onRegisterFailed(const ndn::ptr_lib::shared_ptr<const ndn::Name>& prefix);
252 
258  void
259  onTimeout(const ndn::ptr_lib::shared_ptr<const ndn::Interest>& interest);
260 
266  void
267  publishChannelListData();
268 
276  ndn::ptr_lib::shared_ptr<ndn::Data>
277  makeChannelListData(int sequenceNo);
278 
286  static void
287  parseChannelList
288  (const ndn::Blob& blob, std::vector<std::string>& channelList);
289 
295  static ndn::Blob
296  readBase64Blob(const std::string& filePath);
297 
303  static void
304  writeBase64Blob(const ndn::Blob& blob, const std::string& filePath);
305 
306  private:
307  ChannelDiscovery& parent_;
308  ndn::Name applicationDataPrefix_;
309  int sessionNo_;
310  std::string channelListFilePath_;
311  ndn::Face& face_;
312  ndn::KeyChain& keyChain_;
313  ndn::Name certificateName_;
314  ndn::Milliseconds syncLifetime_;
315  OnReceivedChannelList onReceivedChannelList_;
316  OnError onError_;
317  ndn::ptr_lib::shared_ptr<ndn::ChronoSync2013> sync_;
318  std::vector<std::string> myChannelList_;
319  // The key is the user's application prefix. The value is the list of channel names.
320  std::map<std::string, std::vector<std::string>> otherUserChannelList_;
321  ndn::ptr_lib::shared_ptr<ndn::Data> channelListData_;
322  bool enabled_;
323  bool isInitialized_;
324  bool needToPublish_;
325  };
326 
327  ndn::ptr_lib::shared_ptr<Impl> impl_;
328 };
329 
330 }
331 
332 #endif
double Milliseconds
A time interval represented as the number of milliseconds.
Definition: common.hpp:114
ndn::ptr_lib::shared_ptr< std::vector< ndn::Name > > getOtherUserPrefixes()
Get the list of prefixes for other users, which can be used in getChannelList(userPrefix).
Definition: channel-discovery.hpp:136
The Face class provides the main methods for NDN communication.
Definition: face.hpp:86
const ndn::Name & getApplicationDataPrefix()
Get the applicationDataPrefix given to the constructor.
Definition: channel-discovery.hpp:127
The ChannelDiscovery class uses ChronoSync to listen to the application's "discovery" channel for ann...
Definition: channel-discovery.hpp:37
KeyChain is the main class of the security library.
Definition: key-chain.hpp:53
A Name holds an array of Name::Component and represents an NDN name.
Definition: name.hpp:40
A Blob holds a pointer to an immutable byte array implemented as const std::vector<uint8_t>.
Definition: blob.hpp:42
ChannelDiscovery(const ndn::Name &applicationDataPrefix, const std::string &channelListFilePath, const ndn::Name &applicationBroadcastPrefix, ndn::Face &face, ndn::KeyChain &keyChain, const ndn::Name &certificateName, ndn::Milliseconds syncLifetime, const OnReceivedChannelList &onReceivedChannelList, const OnError &onError)
Create a ChannelDiscovery with the given parameters and begin listening for channel announcements...
Definition: channel-discovery.hpp:86
void removeChannel(const std::string &channel)
Remove the channel from this user's list of channels and publish the new list.
Definition: channel-discovery.hpp:117
ndn::ptr_lib::shared_ptr< std::vector< std::string > > getChannelList(const ndn::Name &userPrefix)
Get the channel list for the user.
Definition: channel-discovery.hpp:149
void addChannel(const std::string &channel)
Add the channel to this user's list of channels and publish the new list.
Definition: channel-discovery.hpp:105