Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 1 | /* -*- 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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-state.h" |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 24 | #include "sync-diff-leaf.h" |
| 25 | #include "sync-std-name-info.h" |
| 26 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 27 | #include <boost/assert.hpp> |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 28 | #include <boost/foreach.hpp> |
| 29 | #include <boost/shared_ptr.hpp> |
| 30 | #include <boost/throw_exception.hpp> |
| 31 | #include <boost/lexical_cast.hpp> |
| 32 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 33 | // using namespace std; |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 34 | using namespace boost; |
| 35 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 36 | typedef error_info<struct tag_errmsg, std::string> info_str; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 38 | using namespace Sync::Error; |
| 39 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 40 | namespace Sync { |
| 41 | |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 42 | /* |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 43 | std::ostream & |
| 44 | operator << (std::ostream &os, const State &state) |
| 45 | { |
| 46 | os << "<state>"; DEBUG_ENDL; |
| 47 | |
| 48 | BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ()) |
| 49 | { |
| 50 | shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
| 51 | if (diffLeaf != 0) |
| 52 | { |
| 53 | os << "<item action=\"" << diffLeaf->getOperation () << "\">"; DEBUG_ENDL; |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | os << "<item>"; DEBUG_ENDL; |
| 58 | } |
Alexander Afanasyev | e4e2bf7 | 2012-03-12 12:44:54 -0700 | [diff] [blame] | 59 | os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL; |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 60 | if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE)) |
| 61 | { |
| 62 | os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL; |
| 63 | } |
| 64 | os << "</item>"; DEBUG_ENDL; |
| 65 | } |
| 66 | os << "</state>"; |
| 67 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 68 | */ |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 69 | |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 70 | SyncStateMsg & |
| 71 | operator << (SyncStateMsg &ossm, const State &state) |
| 72 | { |
| 73 | BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ()) |
| 74 | { |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 75 | SyncState *oss = ossm.add_ss(); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 76 | shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 77 | if (diffLeaf != 0 && diffLeaf->getOperation() != UPDATE) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 78 | { |
| 79 | oss->set_type(SyncState::DELETE); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | oss->set_type(SyncState::UPDATE); |
| 84 | } |
| 85 | |
| 86 | std::ostringstream os; |
| 87 | os << *leaf->getInfo(); |
| 88 | oss->set_name(os.str()); |
| 89 | |
| 90 | if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE)) |
| 91 | { |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 92 | SyncState::SeqNo *seqNo = oss->mutable_seqno(); |
| 93 | seqNo->set_session(leaf->getSeq().getSession()); |
| 94 | seqNo->set_seq(leaf->getSeq().getSeq()); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | return ossm; |
| 98 | } |
| 99 | |
| 100 | /* |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 101 | std::istream & |
| 102 | operator >> (std::istream &in, State &state) |
| 103 | { |
| 104 | TiXmlDocument doc; |
| 105 | in >> doc; |
| 106 | |
| 107 | if (doc.RootElement() == 0) |
| 108 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML")); |
| 109 | |
| 110 | for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item"); |
| 111 | iterator != 0; |
| 112 | iterator = iterator->NextSiblingElement("item")) |
| 113 | { |
| 114 | TiXmlElement *name = iterator->FirstChildElement ("name"); |
| 115 | if (name == 0 || name->GetText() == 0) |
| 116 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing")); |
| 117 | |
| 118 | NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText()); |
| 119 | |
| 120 | if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0) |
| 121 | { |
| 122 | TiXmlElement *seq = iterator->FirstChildElement ("seq"); |
| 123 | if (seq == 0) |
| 124 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing")); |
| 125 | |
| 126 | TiXmlElement *session = seq->FirstChildElement ("session"); |
| 127 | TiXmlElement *seqno = seq->FirstChildElement ("seqno"); |
| 128 | |
| 129 | if (session == 0 || session->GetText() == 0) |
| 130 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing")); |
| 131 | if (seqno == 0 || seqno->GetText() == 0) |
| 132 | BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing")); |
| 133 | |
| 134 | state.update (info, SeqNo ( |
| 135 | lexical_cast<uint32_t> (session->GetText()), |
| 136 | lexical_cast<uint32_t> (seqno->GetText()) |
| 137 | )); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | state.remove (info); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return in; |
| 146 | } |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 147 | */ |
| 148 | |
| 149 | SyncStateMsg & |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 150 | operator >> (SyncStateMsg &issm, State &state) |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 151 | { |
| 152 | int n = issm.ss_size(); |
| 153 | for (int i = 0; i < n; i++) |
| 154 | { |
| 155 | const SyncState &ss = issm.ss(i); |
| 156 | NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name()); |
| 157 | if (ss.type() == SyncState::UPDATE) |
| 158 | { |
Yingdi Yu | 280bb96 | 2014-01-30 09:52:43 -0800 | [diff] [blame^] | 159 | uint64_t session = lexical_cast<uint64_t>(ss.seqno().session()); |
| 160 | uint64_t seq = lexical_cast<uint64_t>(ss.seqno().seq()); |
Zhenkai Zhu | 3cfdcb9 | 2012-06-06 15:20:10 -0700 | [diff] [blame] | 161 | SeqNo seqNo(session, seq); |
| 162 | state.update(info, seqNo); |
Zhenkai Zhu | 97e36bd | 2012-06-06 13:55:03 -0700 | [diff] [blame] | 163 | } |
| 164 | else |
| 165 | { |
| 166 | state.remove(info); |
| 167 | } |
| 168 | } |
| 169 | return issm; |
| 170 | } |
Alexander Afanasyev | 44a5fbe | 2012-03-08 14:15:25 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 172 | } |