blob: ecd1d5a41932f8fb7e300bc1526235858db8d805 [file] [log] [blame]
Zhenkai Zhu8d935c82012-03-06 10:44:12 -08001/* -*- Mode: C++; 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: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080022
Chaoyi Bian11f294f2012-03-08 14:28:06 -080023#include "sync-logic.h"
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080024#include "sync-diff-leaf.h"
25#include "sync-full-leaf.h"
26#include <boost/make_shared.hpp>
27#include <boost/foreach.hpp>
28#include <sys/socket.h>
Zhenkai Zhua5d06d72012-03-09 15:16:24 -080029#include <vector>
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080030
31using namespace std;
32using namespace boost;
33
34namespace Sync
35{
36
Alexander Afanasyevc1030192012-03-08 22:21:28 -080037SyncLogic::SyncLogic (const string &syncPrefix,
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080038 LogicCallback fetch,
Alexander Afanasyevc1030192012-03-08 22:21:28 -080039 CcnxWrapperPtr ccnxHandle)
40 : m_syncPrefix (syncPrefix)
41 , m_fetch (fetch)
42 , m_ccnxHandle (ccnxHandle)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080043{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080044 srandom(time(NULL));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080045}
46
Alexander Afanasyevc1030192012-03-08 22:21:28 -080047SyncLogic::~SyncLogic ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080048{
49
50}
51
Alexander Afanasyevc1030192012-03-08 22:21:28 -080052void
53SyncLogic::processSyncData (const string &name, const string &dataBuffer)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080054{
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080055 string last = name.substr(name.find_last_of("/") + 1);
56 stringstream ss(dataBuffer);
Chaoyi Bian44fff0c2012-03-07 21:07:22 -080057
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -080058 const LeafContainer &fullLc = m_state.getLeaves();
59
60 if (last == "state")
61 {
62 FullState full;
63 ss >> full;
64 BOOST_FOREACH (LeafConstPtr leaf, full.getLeaves().get<ordered>())
65 {
66 shared_ptr<const FullLeaf> fullLeaf = dynamic_pointer_cast<const FullLeaf>(leaf);
67 const NameInfo &info = fullLeaf->getInfo();
68 LeafContainer::iterator it = fullLc.find(info);
69 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
70 SeqNo seq = fullLeaf->getSeq();
71
72 if (it == fullLc.end())
73 {
74 string prefix = info.toString();
75 prefix += "/";
76 prefix += seq.getSession();
77 m_fetch(prefix, 1, seq.getSeq());
78 m_state.update(pInfo, seq);
79 }
80 else
81 {
82 SeqNo currSeq = (*it)->getSeq();
83 if (currSeq < seq)
84 {
85 string prefix = info.toString();
86 prefix += "/";
87 prefix += seq.getSession();
88
89 if (currSeq.getSession() == seq.getSession())
90 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
91 else
92 m_fetch(prefix, 1, seq.getSeq());
93
94 m_state.update(pInfo, seq);
95 }
96 }
97 }
98 }
99 else
100 {
101 DiffState diff;
102 ss >> diff;
103 BOOST_FOREACH (LeafConstPtr leaf, diff.getLeaves().get<ordered>())
104 {
105 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf>(leaf);
106 const NameInfo &info = diffLeaf->getInfo();
107 LeafContainer::iterator it = fullLc.find(info);
108 SeqNo seq = diffLeaf->getSeq();
109
110 switch (diffLeaf->getOperation())
111 {
112 case UPDATE:
113 if (it == fullLc.end())
114 {
115 string prefix = info.toString();
116 prefix += "/";
117 prefix += seq.getSession();
118 m_fetch(prefix, 1, seq.getSeq());
119
120 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
121 m_state.update(pInfo, seq);
122 }
123 else
124 {
125 SeqNo currSeq = (*it)->getSeq();
126 if (currSeq < seq)
127 {
128 string prefix = info.toString();
129 prefix += "/";
130 prefix += seq.getSession();
131
132 if (currSeq.getSession() == seq.getSession())
133 m_fetch(prefix, currSeq.getSeq() + 1, seq.getSeq());
134 else
135 m_fetch(prefix, 1, seq.getSeq());
136
137 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
138 m_state.update(pInfo, seq);
139 }
140 }
141 break;
142
143 case REMOVE:
144 if (it != fullLc.end())
145 {
146 NameInfoConstPtr pInfo = StdNameInfo::FindOrCreate(info.toString());
147 m_state.remove(pInfo);
148 }
149 break;
150
151 default:
152 break;
153 }
154 }
155 }
156
157 sendSyncInterest();
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800158}
159
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800160void
161SyncLogic::addLocalNames (const string &prefix, uint32_t session, uint32_t seq)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800162{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800163 NameInfoConstPtr info = StdNameInfo::FindOrCreate(prefix);
164 SeqNo seqN(session, seq);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800165 DiffStatePtr diff = make_shared<DiffState>();
166 diff->update(info, seqN);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800167 m_state.update(info, seqN);
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800168 diff->setDigest(m_state.getDigest());
169
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800170 vector<string> pis = m_syncInterestTable.fetchAll();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800171 stringstream ss;
172 ss << *diff;
Zhenkai Zhua5d06d72012-03-09 15:16:24 -0800173 for (vector<string>::iterator ii = pis.begin(); ii != pis.end(); ++ii)
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800174 {
175 m_ccnxHandle->publishData(*ii, ss.str(), m_syncResponseFreshness);
176 }
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800177}
178
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800179void
180SyncLogic::respondSyncInterest (const string &interest)
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800181{
Chaoyi Bian4194b742012-03-08 17:21:35 -0800182 string hash = interest.substr(interest.find_last_of("/") + 1);
Chaoyi Bian4194b742012-03-08 17:21:35 -0800183 Digest digest;
Chaoyi Bian4194b742012-03-08 17:21:35 -0800184 digest << hash;
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800185
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800186 if (*m_state.getDigest() == digest)
187 {
188 m_syncInterestTable.insert(interest);
189 return;
190 }
Chaoyi Bian5badd712012-03-09 14:38:40 -0800191/*
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800192 DiffStateContainer::index<hashed>::type& idx = m_log.get<hashed> ();
193 DiffStateContainer::iterator ii = idx.find(digest);
194
195 if (ii != idx.end())
196 {
197 stringstream ss;
198 ss << *(*ii)->diff();
199 m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness);
200 }
201 else
202 {
203 int wait = rand() % 100 + 20;
204 sleep(wait/1000.0);
205 }
206
207 if (*m_state.getDigest() == digest)
208 {
209 m_syncInterestTable.insert(interest);
210 return;
211 }
212
213 ii = idx.find(digest);
214 if (ii != idx.end())
215 {
216 stringstream ss;
217 ss << *(*ii)->diff();
218 m_ccnxHandle->publishData(interest, ss.str(), m_syncResponseFreshness);
219 }
220 else
221 {
222 stringstream ss;
223 ss << m_state;
224 m_ccnxHandle->publishData(interest + "/state", ss.str(), m_syncResponseFreshness);
225 }
Chaoyi Bian5badd712012-03-09 14:38:40 -0800226 */
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800227}
228
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800229void
230SyncLogic::sendSyncInterest ()
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800231{
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800232 ostringstream os;
233 os << m_syncPrefix << "/" << m_state.getDigest();
Chaoyi Bian89ee2dc2012-03-09 14:06:01 -0800234
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800235 m_ccnxHandle->sendInterest (os.str (),
236 bind (&SyncLogic::processSyncData, this, _1, _2));
Chaoyi Bian44fff0c2012-03-07 21:07:22 -0800237}
238
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800239}