blob: bd4c0e493f3f04a1e369ad2ff980b9ca611342ed [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"
23
24using namespace std;
25using namespace ndn;
26
27INIT_LOGGER ("SyncSocket");
28
29namespace Sync {
30
31using ndn::shared_ptr;
32
33SyncSocket::SyncSocket (const Name &syncPrefix,
34 shared_ptr<Validator> validator,
35 shared_ptr<Face> face,
36 NewDataCallback dataCallback,
37 RemoveCallback rmCallback )
38 : m_newDataCallback(dataCallback)
39 , m_validator(validator)
40 , m_keyChain(new KeyChain())
41 , m_face(face)
42 , m_ioService(face->ioService())
43 , m_syncLogic (syncPrefix,
44 validator,
45 face,
46 bind(&SyncSocket::passCallback, this, _1),
47 rmCallback)
48{}
49
50SyncSocket::~SyncSocket()
51{
52}
53
54bool
55SyncSocket::publishData(const Name &prefix, uint64_t session, const char *buf, size_t len, int freshness,uint64_t seq)
56{
57 shared_ptr<Data> data = make_shared<Data>();
58 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
akmhoque05d5fcf2014-04-15 14:58:45 -050059 data->setFreshnessPeriod(time::seconds(freshness));
akmhoque66e66182014-02-21 17:56:03 -060060
61 m_ioService->post(bind(&SyncSocket::publishDataInternal, this, data, prefix, session,seq));
62
63 return true;
64}
65
66void
67SyncSocket::publishDataInternal(shared_ptr<Data> data, const Name &prefix, uint64_t session,uint64_t seq)
68{
69 uint64_t sequence = seq;
70 Name dataName = prefix;
71 dataName.append(boost::lexical_cast<string>(session)).append(boost::lexical_cast<string>(sequence));
72 data->setName(dataName);
73
74 m_keyChain->sign(*data);
75 m_face->put(*data);
76
77 SeqNo s(session, sequence + 1);
78
79 m_sequenceLog[prefix] = s;
80 m_syncLogic.addLocalNames (prefix, session, sequence);
81}
82
83void
84SyncSocket::fetchData(const Name &prefix, const SeqNo &seq, const OnDataValidated& onValidated, int retry)
85{
86 Name interestName = prefix;
87 interestName.append(boost::lexical_cast<string>(seq.getSession())).append(boost::lexical_cast<string>(seq.getSeq()));
88
89 const OnDataValidationFailed& onValidationFailed = bind(&SyncSocket::onDataValidationFailed, this, _1);
90
91 ndn::Interest interest(interestName);
92 interest.setMustBeFresh(true);
93 m_face->expressInterest(interest,
94 bind(&SyncSocket::onData, this, _1, _2, onValidated, onValidationFailed),
95 bind(&SyncSocket::onDataTimeout, this, _1, retry, onValidated, onValidationFailed));
96
97}
98
99void
100SyncSocket::onData(const ndn::Interest& interest, Data& data,
101 const OnDataValidated& onValidated,
102 const OnDataValidationFailed& onValidationFailed)
103{
104 m_validator->validate(data, onValidated, onValidationFailed);
105}
106
107void
108SyncSocket::onDataTimeout(const ndn::Interest& interest,
109 int retry,
110 const OnDataValidated& onValidated,
111 const OnDataValidationFailed& onValidationFailed)
112{
113 if(retry > 0)
114 {
115 m_face->expressInterest(interest,
116 bind(&SyncSocket::onData,
117 this,
118 _1,
119 _2,
120 onValidated,
121 onValidationFailed),
122 bind(&SyncSocket::onDataTimeout,
123 this,
124 _1,
125 retry - 1,
126 onValidated,
127 onValidationFailed));
128
129 }
130 else
131 _LOG_DEBUG("interest eventually time out!");
132}
133
134void
135SyncSocket::onDataValidationFailed(const shared_ptr<const Data>& data)
136{
137 _LOG_DEBUG("data cannot be verified!");
138}
139
140
141uint64_t
142SyncSocket::getNextSeq (const Name &prefix, uint64_t session)
143{
144 SequenceLog::iterator i = m_sequenceLog.find (prefix);
145
146 if (i != m_sequenceLog.end ())
147 {
148 SeqNo s = i->second;
149 if (s.getSession() == session)
150 return s.getSeq();
151 }
152 return 0;
153}
154
155}//Sync