blob: cb00ba436fd3488b2622717d156a6a5d4f851d70 [file] [log] [blame]
Yingdi Yu43e71612013-10-30 22:19:31 -07001/* -*- 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"
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080022#include "sync-logging.h"
23
Yingdi Yu43e71612013-10-30 22:19:31 -070024using namespace std;
25using namespace ndn;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080026using namespace ndn::ptr_lib;
27
28INIT_LOGGER ("SyncSocket");
Yingdi Yu43e71612013-10-30 22:19:31 -070029
30namespace Sync {
31
32SyncSocket::SyncSocket (const string &syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080033 shared_ptr<SecPolicySync> policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080034 shared_ptr<Face> face,
Yingdi Yu43e71612013-10-30 22:19:31 -070035 NewDataCallback dataCallback,
36 RemoveCallback rmCallback )
37 : m_newDataCallback(dataCallback)
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080038 , m_policy(policy)
39 , m_verifier(new Verifier(policy))
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080040 , m_keyChain(new KeyChain())
Yingdi Yu6e235db2013-12-27 08:40:53 +080041 , m_face(face)
Yingdi Yu43e71612013-10-30 22:19:31 -070042 , m_syncLogic (syncPrefix,
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080043 policy,
Yingdi Yu6e235db2013-12-27 08:40:53 +080044 face,
Yingdi Yu43e71612013-10-30 22:19:31 -070045 bind(&SyncSocket::passCallback, this, _1),
46 rmCallback)
47{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080048 m_verifier->setFace(face);
Yingdi Yu43e71612013-10-30 22:19:31 -070049}
50
51SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080052{
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080053}
54
Yingdi Yu43e71612013-10-30 22:19:31 -070055bool
56SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness)
57{
58 uint32_t sequence = getNextSeq(prefix, session);
59 ostringstream contentNameWithSeqno;
60 contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
61
62 Name dataName(contentNameWithSeqno.str ());
Yingdi Yu5e0af3e2014-01-15 19:33:25 -080063 Name signingIdentity = m_policy->inferSigningIdentity(dataName);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080064
65 shared_ptr<Data> data = make_shared<Data>(dataName);
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080066 data->setContent(reinterpret_cast<const uint8_t*>(buf), len);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080067
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080068 Name certificateName = m_keyChain->getDefaultCertificateNameForIdentity(signingIdentity);
69 m_keyChain->sign(*data, certificateName);
Yingdi Yu43e71612013-10-30 22:19:31 -070070
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -080071 m_face->put(*data);
Yingdi Yu43e71612013-10-30 22:19:31 -070072
73 SeqNo s(session, sequence + 1);
74 m_sequenceLog[prefix] = s;
75 m_syncLogic.addLocalNames (prefix, session, sequence);
76 return true;
77}
78
79void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080080SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const OnVerified& onVerified, int retry)
Yingdi Yu43e71612013-10-30 22:19:31 -070081{
82 ostringstream interestName;
83 interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
84 //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl;
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080085
86 const OnVerifyFailed& onVerifyFailed = bind(&SyncSocket::onChatDataVerifyFailed, this, _1);
Yingdi Yu43e71612013-10-30 22:19:31 -070087
88
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080089 shared_ptr<ndn::Interest> interest = make_shared<ndn::Interest>(interestName.str());
90 m_face->expressInterest(*interest,
91 bind(&SyncSocket::onChatData, this, _1, _2, onVerified, onVerifyFailed),
92 bind(&SyncSocket::onChatDataTimeout, this, _1, retry, onVerified, onVerifyFailed));
93
Yingdi Yu43e71612013-10-30 22:19:31 -070094}
95
96void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +080097SyncSocket::onChatData(const shared_ptr<const ndn::Interest>& interest,
98 const shared_ptr<Data>& data,
99 const OnVerified& onVerified,
100 const OnVerifyFailed& onVerifyFailed)
101{
Yingdi Yu0cb0f2b2014-01-09 13:51:16 -0800102 m_verifier->verifyData(data, onVerified, onVerifyFailed);
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800103}
104
105void
106SyncSocket::onChatDataTimeout(const shared_ptr<const ndn::Interest>& interest,
107 int retry,
108 const OnVerified& onVerified,
109 const OnVerifyFailed& onVerifyFailed)
Yingdi Yu43e71612013-10-30 22:19:31 -0700110{
111 if(retry > 0)
112 {
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800113 m_face->expressInterest(*interest,
114 bind(&SyncSocket::onChatData,
115 this,
116 _1,
117 _2,
118 onVerified,
119 onVerifyFailed),
120 bind(&SyncSocket::onChatDataTimeout,
121 this,
122 _1,
123 retry - 1,
124 onVerified,
125 onVerifyFailed));
126
Yingdi Yu43e71612013-10-30 22:19:31 -0700127 }
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800128 else
129 _LOG_DEBUG("Chat interest eventually time out!");
Yingdi Yu43e71612013-10-30 22:19:31 -0700130}
131
132void
Yingdi Yu46c9f1a2013-12-18 15:15:46 +0800133SyncSocket::onChatDataVerifyFailed(const shared_ptr<Data>& data)
134{
135 _LOG_DEBUG("Chat data cannot be verified!");
136}
Yingdi Yu43e71612013-10-30 22:19:31 -0700137
138
139uint32_t
140SyncSocket::getNextSeq (const string &prefix, uint32_t session)
141{
142 SequenceLog::iterator i = m_sequenceLog.find (prefix);
143
144 if (i != m_sequenceLog.end ())
145 {
146 SeqNo s = i->second;
147 if (s.getSession() == session)
148 return s.getSeq();
149 }
150 return 0;
151}
152
153}//Sync