blob: f70f6ba3e6d92548892737d094339c59e9299685 [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"
22
23using namespace std;
24using namespace ndn;
25
26namespace Sync {
27
28SyncSocket::SyncSocket (const string &syncPrefix,
29 Ptr<SyncPolicyManager> syncPolicyManager,
30 NewDataCallback dataCallback,
31 RemoveCallback rmCallback )
32 : m_newDataCallback(dataCallback)
33 , m_syncPolicyManager(syncPolicyManager)
Yingdi Yu8600a092013-11-01 16:12:31 -070034 , m_handler(Ptr<Wrapper>(new Wrapper(Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(),
35 m_syncPolicyManager,
36 NULL)))))
Yingdi Yu43e71612013-10-30 22:19:31 -070037 , m_syncLogic (syncPrefix,
38 syncPolicyManager,
Yingdi Yu8600a092013-11-01 16:12:31 -070039 m_handler,
Yingdi Yu43e71612013-10-30 22:19:31 -070040 bind(&SyncSocket::passCallback, this, _1),
41 rmCallback)
42{
Yingdi Yu8600a092013-11-01 16:12:31 -070043 // Ptr<security::Keychain> keychain = Ptr<security::Keychain>(new security::Keychain(Ptr<security::IdentityManager>::Create(), m_syncPolicyManager, NULL));
44 // m_handler = Ptr<Wrapper>(new Wrapper(keychain));
45 m_syncPolicyManager->setWrapper(m_handler);
Yingdi Yu43e71612013-10-30 22:19:31 -070046}
47
48SyncSocket::~SyncSocket()
Yingdi Yu479e1172013-11-06 16:36:19 -080049{
50 m_handler->shutdown();
51}
Yingdi Yu43e71612013-10-30 22:19:31 -070052
53bool
54SyncSocket::publishData(const std::string &prefix, uint32_t session, const char *buf, size_t len, int freshness)
55{
56 uint32_t sequence = getNextSeq(prefix, session);
57 ostringstream contentNameWithSeqno;
58 contentNameWithSeqno << prefix << "/" << session << "/" << sequence;
59
60 Name dataName(contentNameWithSeqno.str ());
61 Blob blob(buf, len);
62 Name signingIdentity = m_syncPolicyManager->inferSigningIdentity(dataName);
63
64 m_handler->publishDataByIdentity (dataName,
65 blob,
66 signingIdentity,
67 freshness);
68
69 SeqNo s(session, sequence + 1);
70 m_sequenceLog[prefix] = s;
71 m_syncLogic.addLocalNames (prefix, session, sequence);
72 return true;
73}
74
75void
76SyncSocket::fetchData(const string &prefix, const SeqNo &seq, const DataCallback& callback, int retry)
77{
78 ostringstream interestName;
79 interestName << prefix << "/" << seq.getSession() << "/" << seq.getSeq();
80 //std::cout << "Socket " << this << " Send Interest <" << interestName.str() << "> for raw data " << endl;
81
82
83 Ptr<ndn::Interest> interestPtr = Ptr<ndn::Interest>(new ndn::Interest(interestName.str()));
84 Ptr<Closure> closure = Ptr<Closure> (new Closure(callback,
85 boost::bind(&SyncSocket::onChatDataTimeout,
86 this,
87 _1,
88 _2,
89 retry),
90 boost::bind(&SyncSocket::onChatDataUnverified,
91 this,
92 _1)));
93 m_handler->sendInterest(interestPtr, closure);
94}
95
96void
97SyncSocket::onChatDataTimeout(Ptr<Closure> closure, Ptr<ndn::Interest> interest, int retry)
98{
99 if(retry > 0)
100 {
101 Ptr<Closure> newClosure = Ptr<Closure>(new Closure(closure->m_dataCallback,
102 boost::bind(&SyncSocket::onChatDataTimeout,
103 this,
104 _1,
105 _2,
106 retry - 1),
107 closure->m_unverifiedCallback,
108 closure->m_stepCount)
109 );
110 m_handler->sendInterest(interest, newClosure);
111 }
112}
113
114void
115SyncSocket::onChatDataUnverified(Ptr<Data> data)
116{}
117
118
119uint32_t
120SyncSocket::getNextSeq (const string &prefix, uint32_t session)
121{
122 SequenceLog::iterator i = m_sequenceLog.find (prefix);
123
124 if (i != m_sequenceLog.end ())
125 {
126 SeqNo s = i->second;
127 if (s.getSession() == session)
128 return s.getSeq();
129 }
130 return 0;
131}
132
133}//Sync