blob: 6c374a437da074195e38d47559e33f2a5ef786be [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080022
23#include "sync-state.h"
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080024#include "sync-diff-leaf.h"
25#include "sync-std-name-info.h"
26
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080027#include <boost/assert.hpp>
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080028#include <boost/foreach.hpp>
29#include <boost/shared_ptr.hpp>
30#include <boost/throw_exception.hpp>
31#include <boost/lexical_cast.hpp>
32
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080033// using namespace std;
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080034using namespace boost;
35
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080036typedef error_info<struct tag_errmsg, std::string> info_str;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080037
Alexander Afanasyevc1030192012-03-08 22:21:28 -080038using namespace Sync::Error;
39
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080040namespace Sync {
41
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070042/*
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080043std::ostream &
44operator << (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 Afanasyeve4e2bf72012-03-12 12:44:54 -070059 os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL;
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080060 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 Zhu97e36bd2012-06-06 13:55:03 -070068*/
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080069
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070070SyncStateMsg &
71operator << (SyncStateMsg &ossm, const State &state)
72{
73 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
74 {
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070075 SyncState *oss = ossm.add_ss();
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070076 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070077 if (diffLeaf != 0 && diffLeaf->getOperation() != UPDATE)
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070078 {
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 Zhu3cfdcb92012-06-06 15:20:10 -070092 SyncState::SeqNo *seqNo = oss->mutable_seqno();
93 seqNo->set_session(leaf->getSeq().getSession());
94 seqNo->set_seq(leaf->getSeq().getSeq());
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070095 }
96 }
97 return ossm;
98}
99
100/*
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800101std::istream &
102operator >> (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 Zhu97e36bd2012-06-06 13:55:03 -0700147*/
148
149SyncStateMsg &
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700150operator >> (SyncStateMsg &issm, State &state)
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700151{
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 Yu280bb962014-01-30 09:52:43 -0800159 uint64_t session = lexical_cast<uint64_t>(ss.seqno().session());
160 uint64_t seq = lexical_cast<uint64_t>(ss.seqno().seq());
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700161 SeqNo seqNo(session, seq);
162 state.update(info, seqNo);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700163 }
164 else
165 {
166 state.remove(info);
167 }
168 }
169 return issm;
170}
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800171
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800172}