blob: 137e8b0e80d18d46961491dbb50fbeb48a98dfdf [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 Afanasyev44a5fbe2012-03-08 14:15:25 -080033#include <tinyxml.h>
34
35using namespace std;
36using namespace boost;
37
38typedef error_info<struct tag_errmsg, string> info_str;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080039
Alexander Afanasyevc1030192012-03-08 22:21:28 -080040using namespace Sync::Error;
41
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080042namespace Sync {
43
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070044/*
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080045std::ostream &
46operator << (std::ostream &os, const State &state)
47{
48 os << "<state>"; DEBUG_ENDL;
49
50 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
51 {
52 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
53 if (diffLeaf != 0)
54 {
55 os << "<item action=\"" << diffLeaf->getOperation () << "\">"; DEBUG_ENDL;
56 }
57 else
58 {
59 os << "<item>"; DEBUG_ENDL;
60 }
Alexander Afanasyeve4e2bf72012-03-12 12:44:54 -070061 os << "<name>" << *leaf->getInfo () << "</name>"; DEBUG_ENDL;
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080062 if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
63 {
64 os << "<seq>" << leaf->getSeq () << "</seq>"; DEBUG_ENDL;
65 }
66 os << "</item>"; DEBUG_ENDL;
67 }
68 os << "</state>";
69}
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070070*/
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -080071
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070072SyncStateMsg &
73operator << (SyncStateMsg &ossm, const State &state)
74{
75 BOOST_FOREACH (shared_ptr<const Leaf> leaf, state.getLeaves ().get<ordered> ())
76 {
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070077 SyncState *oss = ossm.add_ss();
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070078 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070079 if (diffLeaf != 0 && diffLeaf->getOperation() != UPDATE)
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070080 {
81 oss->set_type(SyncState::DELETE);
82 }
83 else
84 {
85 oss->set_type(SyncState::UPDATE);
86 }
87
88 std::ostringstream os;
89 os << *leaf->getInfo();
90 oss->set_name(os.str());
91
92 if (diffLeaf == 0 || (diffLeaf != 0 && diffLeaf->getOperation () == UPDATE))
93 {
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -070094 SyncState::SeqNo *seqNo = oss->mutable_seqno();
95 seqNo->set_session(leaf->getSeq().getSession());
96 seqNo->set_seq(leaf->getSeq().getSeq());
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -070097 }
98 }
99 return ossm;
100}
101
102/*
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800103std::istream &
104operator >> (std::istream &in, State &state)
105{
106 TiXmlDocument doc;
107 in >> doc;
108
109 if (doc.RootElement() == 0)
110 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML"));
111
112 for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item");
113 iterator != 0;
114 iterator = iterator->NextSiblingElement("item"))
115 {
116 TiXmlElement *name = iterator->FirstChildElement ("name");
117 if (name == 0 || name->GetText() == 0)
118 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing"));
119
120 NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText());
121
122 if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0)
123 {
124 TiXmlElement *seq = iterator->FirstChildElement ("seq");
125 if (seq == 0)
126 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing"));
127
128 TiXmlElement *session = seq->FirstChildElement ("session");
129 TiXmlElement *seqno = seq->FirstChildElement ("seqno");
130
131 if (session == 0 || session->GetText() == 0)
132 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing"));
133 if (seqno == 0 || seqno->GetText() == 0)
134 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing"));
135
136 state.update (info, SeqNo (
137 lexical_cast<uint32_t> (session->GetText()),
138 lexical_cast<uint32_t> (seqno->GetText())
139 ));
140 }
141 else
142 {
143 state.remove (info);
144 }
145 }
146
147 return in;
148}
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700149*/
150
151SyncStateMsg &
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700152operator >> (SyncStateMsg &issm, State &state)
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700153{
154 int n = issm.ss_size();
155 for (int i = 0; i < n; i++)
156 {
157 const SyncState &ss = issm.ss(i);
158 NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name());
159 if (ss.type() == SyncState::UPDATE)
160 {
Zhenkai Zhu3cfdcb92012-06-06 15:20:10 -0700161 uint32_t session = lexical_cast<uint32_t>(ss.seqno().session());
162 uint32_t seq = lexical_cast<uint32_t>(ss.seqno().seq());
163 SeqNo seqNo(session, seq);
164 state.update(info, seqNo);
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700165 }
166 else
167 {
168 state.remove(info);
169 }
170 }
171 return issm;
172}
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800173
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800174}