blob: 45a7ab7c06c4c38cb3a13ddd325f8bf775c92227 [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 {
77 SyncState *oss = ossm->add_ss();
78 shared_ptr<const DiffLeaf> diffLeaf = dynamic_pointer_cast<const DiffLeaf> (leaf);
79 if (diffLeaf != 0 && diffLeaf->getOperation != UPDATE)
80 {
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 {
94 SyncState::SeqNo seqNo;
95 seqNo->set_session(leaf->getSeq()->getSession());
96 seqNo->set_seq(leaf->getSeq()->getSeq());
97 oss->set_seqNo(seqNo);
98 }
99 }
100 return ossm;
101}
102
103/*
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800104std::istream &
105operator >> (std::istream &in, State &state)
106{
107 TiXmlDocument doc;
108 in >> doc;
109
110 if (doc.RootElement() == 0)
111 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("Empty XML"));
112
113 for (TiXmlElement *iterator = doc.RootElement()->FirstChildElement ("item");
114 iterator != 0;
115 iterator = iterator->NextSiblingElement("item"))
116 {
117 TiXmlElement *name = iterator->FirstChildElement ("name");
118 if (name == 0 || name->GetText() == 0)
119 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<name> element is missing"));
120
121 NameInfoConstPtr info = StdNameInfo::FindOrCreate (name->GetText());
122
123 if (iterator->Attribute("action") == 0 || strcmp(iterator->Attribute("action"), "update") == 0)
124 {
125 TiXmlElement *seq = iterator->FirstChildElement ("seq");
126 if (seq == 0)
127 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seq> element is missing"));
128
129 TiXmlElement *session = seq->FirstChildElement ("session");
130 TiXmlElement *seqno = seq->FirstChildElement ("seqno");
131
132 if (session == 0 || session->GetText() == 0)
133 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<session> element is missing"));
134 if (seqno == 0 || seqno->GetText() == 0)
135 BOOST_THROW_EXCEPTION (SyncXmlDecodingFailure () << info_str ("<seqno> element is missing"));
136
137 state.update (info, SeqNo (
138 lexical_cast<uint32_t> (session->GetText()),
139 lexical_cast<uint32_t> (seqno->GetText())
140 ));
141 }
142 else
143 {
144 state.remove (info);
145 }
146 }
147
148 return in;
149}
Zhenkai Zhu97e36bd2012-06-06 13:55:03 -0700150*/
151
152SyncStateMsg &
153operator >> (SyncStateMsg &issm, const State &state)
154{
155 int n = issm.ss_size();
156 for (int i = 0; i < n; i++)
157 {
158 const SyncState &ss = issm.ss(i);
159 NameInfoConstPtr info = StdNameInfo::FindOrCreate (ss.name());
160 if (ss.type() == SyncState::UPDATE)
161 {
162 state.update(info, SeqNo(
163 lexical_cast<uint32_t> (ss.seqNo().session()),
164 lexical_cast<uint32_t> (ss.seqNo().seq()),
165 ));
166 }
167 else
168 {
169 state.remove(info);
170 }
171 }
172 return issm;
173}
Alexander Afanasyev44a5fbe2012-03-08 14:15:25 -0800174
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -0800175}