Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 22 | */ |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 23 | |
| 24 | #include "sync-state.h" |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 25 | #include "sync-diff-leaf.h" |
| 26 | #include "sync-std-name-info.h" |
| 27 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 28 | #include <boost/assert.hpp> |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 29 | #include <boost/foreach.hpp> |
| 30 | #include <boost/shared_ptr.hpp> |
| 31 | #include <boost/throw_exception.hpp> |
| 32 | #include <boost/lexical_cast.hpp> |
| 33 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 34 | // using namespace std; |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 35 | using namespace boost; |
| 36 | |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 37 | typedef error_info<struct tag_errmsg, std::string> info_str; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 38 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 39 | using namespace Sync::Error; |
| 40 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 41 | namespace Sync { |
| 42 | |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 43 | /* |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 44 | std::ostream & |
| 45 | operator << (std::ostream &os, const State &state) |
| 46 | { |
| 47 | os << "<state>"; DEBUG_ENDL; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 48 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 49 | BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ()) |
| 50 | { |
| 51 | shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 52 | if (diffLeaf != 0) |
| 53 | { |
| 54 | os << "<item action=\"" << diffLeaf->getOperation () << "\">"; DEBUG_ENDL; |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | os << "<item>"; DEBUG_ENDL; |
| 59 | } |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 60 | os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL; |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 61 | if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE)) |
| 62 | { |
| 63 | os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL; |
| 64 | } |
| 65 | os << "</item>"; DEBUG_ENDL; |
| 66 | } |
| 67 | os << "</state>"; |
| 68 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 69 | */ |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 70 | |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 71 | SyncStateMsg & |
| 72 | operator << (SyncStateMsg &ossm, const State &state) |
| 73 | { |
| 74 | BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ()) |
| 75 | { |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 76 | SyncState *oss = ossm.add_ss(); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 77 | shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 78 | if (diffLeaf != 0 && diffLeaf->getOperation() != UPDATE) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 79 | { |
| 80 | oss->set_type(SyncState::DELETE); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | oss->set_type(SyncState::UPDATE); |
| 85 | } |
| 86 | |
| 87 | std::ostringstream os; |
| 88 | os << *leaf->getInfo(); |
| 89 | oss->set_name(os.str()); |
| 90 | |
| 91 | if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE)) |
| 92 | { |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 93 | SyncState::SeqNo *seqNo = oss->mutable_seqno(); |
| 94 | seqNo->set_session(leaf->getSeq().getSession()); |
| 95 | seqNo->set_seq(leaf->getSeq().getSeq()); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | return ossm; |
| 99 | } |
| 100 | |
| 101 | /* |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 102 | std::istream & |
| 103 | operator >> (std::istream &in, State &state) |
| 104 | { |
| 105 | TiXmlDocument doc; |
| 106 | in >> doc; |
| 107 | |
| 108 | if (doc.RootElement() == 0) |
| 109 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML")); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 111 | for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item"); |
| 112 | iterator != 0; |
| 113 | iterator = iterator->NextSiblingElement("item")) |
| 114 | { |
| 115 | TiXmlElement *name = iterator->FirstChildElement ("name"); |
| 116 | if (name == 0 || name->GetText() == 0) |
| 117 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing")); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 118 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 119 | NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText()); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 121 | if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0) |
| 122 | { |
| 123 | TiXmlElement *seq = iterator->FirstChildElement ("seq"); |
| 124 | if (seq == 0) |
| 125 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing")); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 127 | TiXmlElement *session = seq->FirstChildElement ("session"); |
| 128 | TiXmlElement *seqno = seq->FirstChildElement ("seqno"); |
| 129 | |
| 130 | if (session == 0 || session->GetText() == 0) |
| 131 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing")); |
| 132 | if (seqno == 0 || seqno->GetText() == 0) |
| 133 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing")); |
| 134 | |
| 135 | state.update (info, SeqNo ( |
| 136 | lexical_cast<uint32_t> (session->GetText()), |
| 137 | lexical_cast<uint32_t> (seqno->GetText()) |
| 138 | )); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | state.remove (info); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return in; |
| 147 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 148 | */ |
| 149 | |
| 150 | SyncStateMsg & |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 151 | operator >> (SyncStateMsg &issm, State &state) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 152 | { |
| 153 | int n = issm.ss_size(); |
| 154 | for (int i = 0; i < n; i++) |
| 155 | { |
| 156 | const SyncState &ss = issm.ss(i); |
| 157 | NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name()); |
| 158 | if (ss.type() == SyncState::UPDATE) |
| 159 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame] | 160 | uint64_t session = lexical_cast<uint64_t>(ss.seqno().session()); |
| 161 | uint64_t seq = lexical_cast<uint64_t>(ss.seqno().seq()); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 162 | SeqNo seqNo(session, seq); |
| 163 | state.update(info, seqNo); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 164 | } |
| 165 | else |
| 166 | { |
| 167 | state.remove(info); |
| 168 | } |
| 169 | } |
| 170 | return issm; |
| 171 | } |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 173 | } |