blob: 21201aa359f15f3e89ef24d33688a38ea7d2b6a3 [file] [log] [blame]
Zhenkai Zhu8224bb62013-01-06 15:58:02 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 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 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "sync-core.h"
23
24SyncCore::SyncCore
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080025 : m_log(path, userName)
26 , m_localPrefix(localPrefix)
27 , m_syncPrefix(syncPrefix)
28 , m_stateMsgCallback(callback)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080029 , m_handle(handle)
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080030 , m_scheduler(scheduler)
Zhenkai Zhu8224bb62013-01-06 15:58:02 -080031{
Zhenkai Zhu74dd53c2013-01-10 23:39:57 -080032 m_rootHash = m_log.RememberStateInStateLog();
33 m_interestClosure = new Closure(0, boost::bind(&SyncCore::handleSyncData, this, _1, _2), boost::bind(&SyncCore::handleSyncInterestTimeout, this, _1));
34 m_scheduler->start();
35 sendSyncInterest();
36}
37
38SyncCore::~SyncCore()
39{
40 m_scheduler->stop();
41 delete m_interestClosure;
42 m_interestClosure = 0;
43}
44
45void
46SyncCore::updateLocalPrefix(const Name &localPrefix)
47{
48 m_localPrefix = localPrefix;
49 // optionally, we can have a sync action to announce the new prefix
50 // we are not doing this for now
51}
52
53void
54SyncCore::updateLocalState(seqno_t seqno)
55{
56 m_log.UpdateDeviceSeqNo(seqno);
57 HashPtr oldHash = m_rootHash;
58 m_rootHash = m_log.RememberStateInStateLog();
59
60 SyncStateMsgPtr msg = m_log.FindStateDifferences(oldHash, m_rootHash);
61
62 // reply sync Interest with oldHash as last component
63 Name syncName = constructSyncName(oldHash);
64 Bytes syncData;
65 msgToBytes(msg, syncData);
66 m_handle->publishData(syncName, syncData, FRESHNESS);
67
68 // no hurry in sending out new Sync Interest; if others send the new Sync Interest first, no problem, we know the new root hash already;
69 // this is trying to avoid the situation that the order of SyncData and new Sync Interest gets reversed at receivers
70 ostringstream ss;
71 ss << m_rootHash;
72 TaskPtr task(new OneTimeTask(boost::bind(&SyncCore::sendSyncInterest, this), ss.str(), m_scheduler, 0.05));
73 m_scheduler->addTask(task);
74}
75
76void
77SyncCore::handleSyncInterest(const Name &name)
78{
79}
80
81Closure::TimeoutCallbackReturnValue
82SyncCore::handleSyncInterestTimeout(const Name &name)
83{
84 // sendInterestInterest with the current root hash;
85 sendSyncInterest();
86 return Closure::OK;
87}
88
89void
90SyncCore::handleSyncData(const Name &name, const Bytes &content)
91{
92 SyncStateMsgPtr msg(new SyncStateMsg);
93 bool success = msg->ParseFromArray(head(content), content.size());
94 if(!success)
95 {
96 // ignore misformed SyncData
97 cerr << "Misformed SyncData with name: " << name << endl;
98 return;
99 }
100
101 int size = msg->state_size();
102 int index = 0;
103 while (index < size)
104 {
105 SyncState state = msg->state(index);
106 index++;
107 }
108
109}
110
111void
112SyncCore::sendSyncInterest()
113{
114 Name syncInterest = constructSyncName(m_rootHash);
115 sendInterest(syncInterest, m_interestClosure);
116}
117
118Name
119SyncCore::constructSyncName(const HashPtr &hash)
120{
121 Bytes bytes;
122 readRaw(bytes, (const unsigned char*)hash->GetHash(), hash->GetHashBytes);
123 Name syncName = m_syncPrefix;
124 syncName.append(bytes);
125 return syncName;
126}
127
128void
129SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes)
130{
131 int size = msg.ByteSize();
132 bytes.resize(size);
133 msg.SerializeToArray(head(bytes), size);
Zhenkai Zhu8224bb62013-01-06 15:58:02 -0800134}