blob: 68eaa7d6bdee07d47bb41526f3cc40c91ea222eb [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),
115 bind(&SyncSocket::onDataTimeout, this, _1, retry,
116 onValidated, onValidationFailed));
akmhoque66e66182014-02-21 17:56:03 -0600117
118}
119
120void
121SyncSocket::onData(const ndn::Interest& interest, Data& data,
122 const OnDataValidated& onValidated,
123 const OnDataValidationFailed& onValidationFailed)
124{
125 m_validator->validate(data, onValidated, onValidationFailed);
126}
127
128void
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700129SyncSocket::onDataTimeout(const ndn::Interest& interest,
akmhoque66e66182014-02-21 17:56:03 -0600130 int retry,
131 const OnDataValidated& onValidated,
132 const OnDataValidationFailed& onValidationFailed)
133{
134 if(retry > 0)
135 {
136 m_face->expressInterest(interest,
137 bind(&SyncSocket::onData,
138 this,
139 _1,
140 _2,
141 onValidated,
142 onValidationFailed),
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700143 bind(&SyncSocket::onDataTimeout,
akmhoque66e66182014-02-21 17:56:03 -0600144 this,
145 _1,
146 retry - 1,
147 onValidated,
148 onValidationFailed));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700149
akmhoque66e66182014-02-21 17:56:03 -0600150 }
151 else
152 _LOG_DEBUG("interest eventually time out!");
153}
154
155void
156SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
157{
158 _LOG_DEBUG("data cannot be verified!");
159}
160
161
162uint64_t
163SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
164{
165 SequenceLog::iterator i = m_sequenceLog.find (prefix);
166
167 if (i != m_sequenceLog.end ())
168 {
169 SeqNo s = i->second;
170 if (s.getSession() == session)
171 return s.getSeq();
172 }
173 return 0;
174}
175
176}//Sync