blob: ef6b9fb4e9babfe7d1718071bbbbf41e1418e71d [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode: C32++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Yingdi Yu <yingdi@cs.ucla.edu>
19 */
20
21#include "sync-socket.h"
22#include "sync-logging.h"
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080023
Alexander Afanasyevdb49f4e2014-08-14 23:01:02 -070024#include <boost/lexical_cast.hpp>
Alexander Afanasyevf9f39102015-12-01 17:43:40 -080025#include <boost/thread.hpp>
akmhoque66e66182014-02-21 17:56:03 -060026
27using namespace std;
Vince Lehman0a7da612014-10-29 14:39:29 -050028
29using ndn::Data;
30using ndn::Face;
31using ndn::KeyChain;
32using ndn::Name;
33using ndn::OnDataValidated;
34using ndn::OnDataValidationFailed;
35using ndn::Validator;
akmhoque66e66182014-02-21 17:56:03 -060036
37INIT_LOGGER ("SyncSocket");
38
39namespace Sync {
40
41using ndn::shared_ptr;
Yingdi Yu40cd1c32014-04-17 15:02:17 -070042using ndn::make_shared;
akmhoque66e66182014-02-21 17:56:03 -060043
Yingdi Yu40cd1c32014-04-17 15:02:17 -070044SyncSocket::SyncSocket (const Name &syncPrefix,
akmhoque66e66182014-02-21 17:56:03 -060045 shared_ptr<Validator> validator,
46 shared_ptr<Face> face,
Yingdi Yu40cd1c32014-04-17 15:02:17 -070047 NewDataCallback dataCallback,
akmhoque66e66182014-02-21 17:56:03 -060048 RemoveCallback rmCallback )
49 : m_newDataCallback(dataCallback)
50 , m_validator(validator)
51 , m_keyChain(new KeyChain())
52 , m_face(face)
akmhoque66e66182014-02-21 17:56:03 -060053 , m_syncLogic (syncPrefix,
54 validator,
55 face,
56 bind(&SyncSocket::passCallback, this, _1),
57 rmCallback)
Alexander Afanasyevdb49f4e2014-08-14 23:01:02 -070058{
59}
akmhoque66e66182014-02-21 17:56:03 -060060
61SyncSocket::~SyncSocket()
62{
63}
64
Yingdi Yu40cd1c32014-04-17 15:02:17 -070065bool
akmhoque157b0a42014-05-13 00:26:37 -050066SyncSocket::publishData(const Name &prefix, uint64_t session,
67 const char *buf, size_t len,
68 int freshness,uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060069{
70 shared_ptr<Data> data = make_shared<Data>();
71 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
akmhoque157b0a42014-05-13 00:26:37 -050072 data->setFreshnessPeriod(ndn::time::seconds(freshness));
akmhoque66e66182014-02-21 17:56:03 -060073
akmhoque157b0a42014-05-13 00:26:37 -050074 m_face->getIoService().post(bind(&SyncSocket::publishDataInternal, this,
75 data, prefix, session,seq));
akmhoque66e66182014-02-21 17:56:03 -060076
Yingdi Yu40cd1c32014-04-17 15:02:17 -070077 return true;
akmhoque66e66182014-02-21 17:56:03 -060078}
79
80void
akmhoque157b0a42014-05-13 00:26:37 -050081SyncSocket::publishDataInternal(shared_ptr<Data> data,
82 const Name &prefix, uint64_t session, uint64_t seq)
akmhoque66e66182014-02-21 17:56:03 -060083{
84 uint64_t sequence = seq;
85 Name dataName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -050086 dataName.append(boost::lexical_cast<string>(session))
87 .append(boost::lexical_cast<string>(sequence));
akmhoque66e66182014-02-21 17:56:03 -060088 data->setName(dataName);
89
Yingdi Yu40cd1c32014-04-17 15:02:17 -070090 m_keyChain->sign(*data);
akmhoque66e66182014-02-21 17:56:03 -060091 m_face->put(*data);
92
93 SeqNo s(session, sequence + 1);
94
95 m_sequenceLog[prefix] = s;
96 m_syncLogic.addLocalNames (prefix, session, sequence);
97}
98
Yingdi Yu40cd1c32014-04-17 15:02:17 -070099void
akmhoque157b0a42014-05-13 00:26:37 -0500100SyncSocket::fetchData(const Name &prefix, const SeqNo &seq,
101 const OnDataValidated& onValidated, int retry)
akmhoque66e66182014-02-21 17:56:03 -0600102{
103 Name interestName = prefix;
akmhoque157b0a42014-05-13 00:26:37 -0500104 interestName.append(boost::lexical_cast<string>(seq.getSession()))
105 .append(boost::lexical_cast<string>(seq.getSeq()));
akmhoque66e66182014-02-21 17:56:03 -0600106
akmhoque157b0a42014-05-13 00:26:37 -0500107 const OnDataValidationFailed& onValidationFailed =
108 bind(&SyncSocket::onDataValidationFailed, this, _1);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700109
akmhoque66e66182014-02-21 17:56:03 -0600110 ndn::Interest interest(interestName);
111 interest.setMustBeFresh(true);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700112 m_face->expressInterest(interest,
akmhoque157b0a42014-05-13 00:26:37 -0500113 bind(&SyncSocket::onData, this, _1, _2,
114 onValidated, onValidationFailed),
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800115 bind(&SyncSocket::onDataTimeout, this, _1, retry, // Nack
116 onValidated, onValidationFailed),
akmhoque157b0a42014-05-13 00:26:37 -0500117 bind(&SyncSocket::onDataTimeout, this, _1, retry,
118 onValidated, onValidationFailed));
akmhoque66e66182014-02-21 17:56:03 -0600119
120}
121
122void
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800123SyncSocket::onData(const ndn::Interest& interest, const Data& data,
akmhoque66e66182014-02-21 17:56:03 -0600124 const OnDataValidated& onValidated,
125 const OnDataValidationFailed& onValidationFailed)
126{
127 m_validator->validate(data, onValidated, onValidationFailed);
128}
129
130void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700131SyncSocket::onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600132 int retry,
133 const OnDataValidated& onValidated,
134 const OnDataValidationFailed& onValidationFailed)
135{
136 if(retry > 0)
137 {
138 m_face->expressInterest(interest,
139 bind(&SyncSocket::onData,
140 this,
141 _1,
142 _2,
143 onValidated,
144 onValidationFailed),
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700145 bind(&SyncSocket::onDataTimeout,
akmhoque66e66182014-02-21 17:56:03 -0600146 this,
147 _1,
148 retry - 1,
149 onValidated,
Alexander Afanasyev1de901f2017-03-09 12:43:57 -0800150 onValidationFailed),
151 bind(&SyncSocket::onDataTimeout,
152 this,
153 _1,
154 retry - 1,
155 onValidated,
akmhoque66e66182014-02-21 17:56:03 -0600156 onValidationFailed));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700157
akmhoque66e66182014-02-21 17:56:03 -0600158 }
159 else
160 _LOG_DEBUG("interest eventually time out!");
161}
162
163void
164SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
165{
166 _LOG_DEBUG("data cannot be verified!");
167}
168
169
170uint64_t
171SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
172{
173 SequenceLog::iterator i = m_sequenceLog.find (prefix);
174
175 if (i != m_sequenceLog.end ())
176 {
177 SeqNo s = i->second;
178 if (s.getSession() == session)
179 return s.getSeq();
180 }
181 return 0;
182}
183
184}//Sync