Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 24 | SyncCore::SyncCore |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 25 | : m_log(path, userName) |
| 26 | , m_localPrefix(localPrefix) |
| 27 | , m_syncPrefix(syncPrefix) |
| 28 | , m_stateMsgCallback(callback) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 29 | , m_handle(handle) |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 30 | , m_scheduler(scheduler) |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 31 | { |
Zhenkai Zhu | 74dd53c | 2013-01-10 23:39:57 -0800 | [diff] [blame] | 32 | 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 | |
| 38 | SyncCore::~SyncCore() |
| 39 | { |
| 40 | m_scheduler->stop(); |
| 41 | delete m_interestClosure; |
| 42 | m_interestClosure = 0; |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | SyncCore::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 | |
| 53 | void |
| 54 | SyncCore::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 | |
| 76 | void |
| 77 | SyncCore::handleSyncInterest(const Name &name) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | Closure::TimeoutCallbackReturnValue |
| 82 | SyncCore::handleSyncInterestTimeout(const Name &name) |
| 83 | { |
| 84 | // sendInterestInterest with the current root hash; |
| 85 | sendSyncInterest(); |
| 86 | return Closure::OK; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | SyncCore::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 | |
| 111 | void |
| 112 | SyncCore::sendSyncInterest() |
| 113 | { |
| 114 | Name syncInterest = constructSyncName(m_rootHash); |
| 115 | sendInterest(syncInterest, m_interestClosure); |
| 116 | } |
| 117 | |
| 118 | Name |
| 119 | SyncCore::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 | |
| 128 | void |
| 129 | SyncCore::msgToBytes(const SyncStateMsgPtr &msg, Bytes &bytes) |
| 130 | { |
| 131 | int size = msg.ByteSize(); |
| 132 | bytes.resize(size); |
| 133 | msg.SerializeToArray(head(bytes), size); |
Zhenkai Zhu | 8224bb6 | 2013-01-06 15:58:02 -0800 | [diff] [blame] | 134 | } |