Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.58 KB | None | 0 0
  1. /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
  2. /*
  3.  * Copyright (c) 2012-2014 University of California, Los Angeles
  4.  *
  5.  * This file is part of ChronoSync, synchronization library for distributed realtime
  6.  * applications for NDN.
  7.  *
  8.  * ChronoSync 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, either
  10.  * version 3 of the License, or (at your option) any later version.
  11.  *
  12.  * ChronoSync 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.  * ChronoSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
  20.  * @author Chaoyi Bian <bcy@pku.edu.cn>
  21.  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
  22.  * @author Yingdi Yu <yingdi@cs.ucla.edu>
  23.  */
  24.  
  25. #include "socket.hpp"
  26. #include "logger.hpp"
  27.  
  28. INIT_LOGGER("Socket");
  29.  
  30.  
  31. namespace chronosync {
  32.  
  33. const ndn::Name Socket::DEFAULT_NAME;
  34. const ndn::Name Socket::DEFAULT_PREFIX;
  35. const ndn::shared_ptr<ndn::Validator> Socket::DEFAULT_VALIDATOR;
  36.  
  37. Socket::Socket(const Name& syncPrefix,
  38.                const Name& userPrefix,
  39.                ndn::Face& face,
  40.                const UpdateCallback& updateCallback,
  41.                const Name& signingId,
  42.                ndn::shared_ptr<ndn::Validator> validator)
  43.   : m_userPrefix(userPrefix)
  44.   , m_face(face)
  45.   , m_logic(face, syncPrefix, userPrefix, updateCallback)
  46.   , m_signingId(signingId)
  47.   , m_keyChain(ns3::ndn::StackHelper::getKeyChain())
  48.   , m_validator(validator)
  49. {
  50.   if (m_userPrefix != DEFAULT_NAME)
  51.     m_registeredPrefixList[m_userPrefix] =
  52.       m_face.setInterestFilter(m_userPrefix,
  53.                                bind(&Socket::onInterest, this, _1, _2),
  54.                                [] (const Name& prefix, const std::string& msg) {});
  55. }
  56.  
  57. Socket::~Socket()
  58. {
  59.   for(const auto& itr : m_registeredPrefixList) {
  60.     if (static_cast<bool>(itr.second))
  61.       m_face.unsetInterestFilter(itr.second);
  62.   }
  63.   m_ims.erase("/");
  64. }
  65.  
  66. void
  67. Socket::addSyncNode(const Name& prefix, const Name& signingId)
  68. {
  69.   if (prefix == DEFAULT_NAME)
  70.     return;
  71.  
  72.   auto itr = m_registeredPrefixList.find(prefix);
  73.   if (itr != m_registeredPrefixList.end())
  74.     return;
  75.  
  76.   if (m_userPrefix == DEFAULT_NAME)
  77.     m_userPrefix = prefix;
  78.   m_logic.addUserNode(prefix, signingId);
  79.   m_registeredPrefixList[prefix] =
  80.     m_face.setInterestFilter(prefix,
  81.                              bind(&Socket::onInterest, this, _1, _2),
  82.                              [] (const Name& prefix, const std::string& msg) {});
  83. }
  84.  
  85. void
  86. Socket::removeSyncNode(const Name& prefix)
  87. {
  88.   if (prefix == DEFAULT_NAME)
  89.     return;
  90.  
  91.   auto itr = m_registeredPrefixList.find(prefix);
  92.   if (itr != m_registeredPrefixList.end()) {
  93.     if (static_cast<bool>(itr->second))
  94.       m_face.unsetInterestFilter(itr->second);
  95.     m_registeredPrefixList.erase(itr);
  96.   }
  97.  
  98.   m_ims.erase(prefix);
  99.   m_logic.removeUserNode(prefix);
  100.  
  101. }
  102.  
  103. void
  104. Socket::publishData(const uint8_t* buf, size_t len, const ndn::time::milliseconds& freshness,
  105.                     const Name& prefix)
  106. {
  107.   std::cout << "\033[1;31m" << prefix << " Publish Data: " << buf << "\033[0m\n\n";
  108.   publishData(ndn::dataBlock(ndn::tlv::Content, buf, len), freshness, prefix);
  109. }
  110.  
  111. void
  112. Socket::publishData(const Block& content, const ndn::time::milliseconds& freshness,
  113.                     const Name& prefix)
  114. {
  115.   shared_ptr<Data> data = make_shared<Data>();
  116.   data->setContent(content);
  117.   data->setFreshnessPeriod(freshness);
  118.  
  119.   Name dataName;
  120.   if(prefix == DEFAULT_PREFIX){
  121.     SeqNo newSeq = m_logic.getSeqNo(prefix) + 1;
  122.    
  123.     dataName.append(m_logic.getSessionName(prefix)).appendNumber(newSeq);
  124.     data->setName(dataName);
  125.  
  126.     if (m_signingId.empty())
  127.       m_keyChain.sign(*data);
  128.     else
  129.       m_keyChain.signByIdentity(*data, m_signingId);
  130.  
  131.     m_ims.insert(*data);
  132.  
  133.     m_logic.updateSeqNo(newSeq, prefix);
  134.   }
  135.   else{
  136.     data->setName(dataName);
  137.     if (m_signingId.empty())
  138.       m_keyChain.sign(*data);
  139.     else
  140.       m_keyChain.signByIdentity(*data, m_signingId);
  141.  
  142.     m_ims.insert(*data);
  143.  
  144.   }
  145. }
  146.  
  147. void
  148. Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
  149.                   const ndn::OnDataValidated& dataCallback,
  150.                   int nRetries)
  151. {
  152.   Name interestName;
  153.   if(seqNo != NULL)
  154.     interestName.append(sessionName).appendNumber(seqNo);
  155.   else
  156.     interestName.append(sessionName);
  157.  
  158.   std::cout << "\033[1;31m Fetch Data: " << interestName << "\033[0m\n\n";
  159.  
  160.   Interest interest(interestName);
  161.   interest.setMustBeFresh(true);
  162.  
  163.   ndn::OnDataValidationFailed failureCallback =
  164.     bind(&Socket::onDataValidationFailed, this, _1, _2);
  165.  
  166.   //std::cout << "Interest Name: " << interestName << "\n";
  167.  
  168.   std::cout << "\033[1;31m ExpressInterest: " << interest << "\033[0m\n\n";
  169.  
  170.   m_face.expressInterest(interest,
  171.                          bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
  172.                          bind(&Socket::onDataTimeout, this, _1, nRetries,
  173.                               dataCallback, failureCallback));
  174. }
  175.  
  176. void
  177. Socket::fetchData(const Name& sessionName, const SeqNo& seqNo,
  178.                   const ndn::OnDataValidated& dataCallback,
  179.                   const ndn::OnDataValidationFailed& failureCallback,
  180.                   const ndn::OnTimeout& onTimeout,
  181.                   int nRetries)
  182. {
  183.   _LOG_DEBUG(">> Socket::fetchData");
  184.   Name interestName;
  185.   interestName.append(sessionName).appendNumber(seqNo);
  186.  
  187.   Interest interest(interestName);
  188.   interest.setMustBeFresh(true);
  189.  
  190.   m_face.expressInterest(interest,
  191.                          bind(&Socket::onData, this, _1, _2, dataCallback, failureCallback),
  192.                          onTimeout);
  193.  
  194.   _LOG_DEBUG("<< Socket::fetchData");
  195. }
  196.  
  197. void
  198. Socket::onInterest(const Name& prefix, const Interest& interest)
  199. {
  200.   std::cout << "\033[1;35m Interest ricevuto: " << interest << "\033[0m\n\n";
  201.   shared_ptr<const Data>data = m_ims.find(interest);
  202.   if (static_cast<bool>(data)) {
  203.     m_face.put(*data);
  204.   }
  205. }
  206.  
  207. void
  208. Socket::onData(const Interest& interest, Data& data,
  209.                const ndn::OnDataValidated& onValidated,
  210.                const ndn::OnDataValidationFailed& onFailed)
  211. {
  212.   std::cout << "Socket::onData " << data <<"\n";
  213.  
  214.   if (static_cast<bool>(m_validator))
  215.     m_validator->validate(data, onValidated, onFailed);
  216.   else
  217.     onValidated(data.shared_from_this());
  218. }
  219.  
  220. void
  221. Socket::onDataTimeout(const Interest& interest, int nRetries,
  222.                       const ndn::OnDataValidated& onValidated,
  223.                       const ndn::OnDataValidationFailed& onFailed)
  224. {
  225.   std::cout << "Socket::onDataTimeout" << interest <<"\n";
  226.   if (nRetries <= 0)
  227.     return;
  228.  
  229.   m_face.expressInterest(interest,
  230.                          bind(&Socket::onData, this, _1, _2, onValidated, onFailed),
  231.                          bind(&Socket::onDataTimeout, this, _1, nRetries - 1,
  232.                               onValidated, onFailed));
  233. }
  234.  
  235. void
  236. Socket::onDataValidationFailed(const shared_ptr<const Data>& data,
  237.                                const std::string& failureInfo)
  238. {
  239.   std::cout << "\033[1;31m Pota\033[0m\n\n";
  240. }
  241.  
  242. ndn::ConstBufferPtr
  243. Socket::getRootDigest() const
  244. {
  245.   return m_logic.getRootDigest();
  246. }
  247.  
  248. } // namespace chronosync
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement